-
Notifications
You must be signed in to change notification settings - Fork 1
/
strtoint.cpp
178 lines (153 loc) · 4.55 KB
/
strtoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// {"category": "String", "notes": "Convert string into an integer"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <Windows.h>
using namespace std;
//------------------------------------------------------------------------------
//
// How would you convert a string into an integer?
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
int StrToInt(const char* pszString)
{
int number = 0;
int sign = 1;
int length = 0;
if (nullptr == pszString)
{
throw exception();
}
length = strlen(pszString);
if (0 == length)
{
throw exception();
}
for (int i = 0; i < length; i++)
{
switch (pszString[i])
{
case '+':
if (i != 0 || 1 == length)
{
throw exception();
}
break;
case '-':
if (i != 0 || 1 == length)
{
throw exception();
}
sign = -1;
break;
default:
if (pszString[i] >= '0' && pszString[i] <= '9')
{
int toAdd = static_cast<int>(pszString[i] - '0');
if (number > MAXINT32 / 10)
{
throw exception();
}
number *= 10;
if (sign > 0)
{
if (number > MAXINT32 - toAdd)
{
throw exception();
}
}
else
{
if (-number < -MAXINT32 - 1 + toAdd)
{
throw exception();
}
}
number += toAdd;
}
else
{
throw exception();
}
break;
}
}
return sign * number;
}
//------------------------------------------------------------------------------
//
// Unit tests
//
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
struct TestCase
{
char* pszString;
int numberExpected;
bool exceptionExpected;
};
const TestCase tests[] =
{
{ nullptr, 0, true },
{ "", 0, true },
{ "12345", 12345, false },
{ "+12345", 12345, false },
{ "3.14", 0, true },
{ "0x1234", 0, true },
{ "!@#$%", 0, true },
{ "-67890", -67890, false },
{ "000", 0, false },
{ "+", 0, true },
{ "-", 0, true },
{ "1+1", 0, true },
{ "2-", 0, true },
{ " ", 0, true },
{ " 1", 0, true },
{ "1 ", 0, true },
{ "2147483646", MAXINT32-1, false },
{ "2147483647", MAXINT32, false },
{ "2147483648", 0, true },
{ "2147483650", 0, true },
{ "-2147483647", -MAXINT32, false },
{ "-2147483648", -MAXINT32-1, false },
{ "-2147483649", 0, true },
{ "-2147483650", 0, true },
};
int countFailed = 0;
for (int i = 0; i < ARRAYSIZE(tests); i++)
{
bool failed = false;
try
{
if (StrToInt(tests[i].pszString) != tests[i].numberExpected ||
tests[i].exceptionExpected)
{
failed = true;
}
}
catch (exception)
{
if (!tests[i].exceptionExpected)
{
failed = true;
}
}
if (failed)
{
cout << "Failed test case { \"" << tests[i].pszString << "\", ";
cout << tests[i].numberExpected << ", ";
cout << tests[i].exceptionExpected << " }" << endl;
countFailed++;
}
}
cout << ARRAYSIZE(tests) - countFailed << " passed, ";
cout << countFailed << " failed." << endl;
return 0;
}