-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.cpp
107 lines (95 loc) · 2.46 KB
/
atoi.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
/*
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
*/
#include <string>
#include <iostream>
#include <stdlib.h>
using std::string;
using std::cout;
using std::cin;
using std::endl;
class Solution {
public:
int myAtoi(string str) {
int size = str.size();
int result = 0;
int multi = 1;
int start = 0;
bool ifStart = false;
bool ifMinus = false;
bool ifOverflow = false;
//purify
if(size == 0){
return 0;
} else{
for(int i = 0; i != size; i ++){
if(!ifStart){
if(str[i] == ' '){
continue;
} else if((str[i] >= '0' && str[i] <= '9') || str[i] == '-' || str[i] == '+'){
ifStart = true;
start = i;
} else{
return 0;
}
} else{
if(str[i] >= '0' && str[i] <= '9'){
continue;
} else{
size = i;
break;
}
}
}
}
for(int i = size - 1; i != start; i --){
if(str[i] >= '0' && str[i] <= '9'){
result += (str[i] - '0') * multi;
multi = multi * 10;
}
if(result < 0){
ifOverflow = true;
}
}
if(str[start] == '-'){
result = 0 - result;
ifMinus = true;
start ++;
} else if(str[start] == '+'){
start ++;
} else{
result += (str[start] - '0') * multi;
if(result < 0){
ifOverflow = true;
}
}
//if overflow
if(start <= size - 11){
ifOverflow = true;
} else if(start == size - 10 && str[start] > '2' && str[start] <= '9'){
ifOverflow = true;
}
if(!ifMinus){
if(ifOverflow){
return 2147483647;
}
} else{
if(ifOverflow){
return -2147483648;
}
}
return result;
}
};
int main(int argc, char const *argv[])
{
Solution test;
string str;
cout << "input the string to change" << endl;
cin >> str;
int result = test.myAtoi(str);
cout << result << endl;
return 0;
}