-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (66 loc) · 1.03 KB
/
Copy pathmain.cpp
File metadata and controls
77 lines (66 loc) · 1.03 KB
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
#include <string>
class Solution {
public:
Solution() :m_status(kValid)
{}
int StrToIntCore(std::string::iterator &it, const std::string::iterator &end, bool minus)
{
long long num = 0;
int flag = minus ? -1 : 1;
while (it != end)
{
if (*it >= '0' && *it <= '9')
{
num = num * 10 + flag * (*it - '0');
if ((!minus && num > 0x7fffffff) || (minus && num < (int)0x80000000))
{
num = 0;
break;
}
++it;
}
else
{
num = 0;
break;
}
}
if (it == end)
m_status = kValid;
return (int)num;
}
int StrToInt(std::string str) {
m_status = kInvalid;
int num = 0;
if (str.size() > 0)
{
bool minus = false;
std::string::iterator it = str.begin();
if (*it == '+')
++it;
else if (*it == '-')
{
++it;
minus = true;
}
if (it != str.end())
{
num = StrToIntCore(it, str.end(), minus);
}
}
return num;
}
private:
enum Status
{
kValid,
kInvalid
};
int m_status;
};
int main()
{
Solution so;
so.StrToInt("-123");
return 0;
}