-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
67 lines (65 loc) · 1.54 KB
/
program.cpp
File metadata and controls
67 lines (65 loc) · 1.54 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
#include "../include/prevector.h"
#define CALC_INT_LIMITS
#ifdef CALC_INT_LIMITS
const int INT_MAX = std::numeric_limits<int>::max();
const int INT_MIN = std::numeric_limits<int>::min();
#endif
int myAtoi(string str)
{
int rst = 0, j = 0;
bool negetive = false;
while(str[j] == ' ') j++;
if(str[j] == '+' || str[j] == '-') {
negetive = (str[j] == '-');
j++;
}
for(int i = j; i < str.size(); i++) {
if(str[i] < '0' || str[i] > '9') break;
int digit = (str[i] - '0');
if (!negetive) {
if(rst > INT_MAX / 10 || rst * 10 > INT_MAX - digit) {
if(negetive) return INT_MIN;
return INT_MAX;
}
rst = rst * 10 + digit;
}
else{
if(rst < INT_MIN / 10 || rst * 10 < INT_MIN + digit) {
if(negetive) return INT_MIN;
return INT_MAX;
}
rst = rst * 10 - digit;
}
}
return rst;
}
int Mymain()
{
cout << (int)'+' << (int)'-' << endl;
vector<std::string> tests = {
" -12kjhsdg8972",
" +010",
"+-4646",
"45fds5",
"+12",
"1",
"+2147483648",
"2147483648",
"-2147483649",
"2147483647",
"-2147483648",
"",
"-",
"0",
"009",
"-9898",
"4665446465464541",
"-45243325543436",
"123567",
"-19283"
};
for (auto& str : tests)
{
cout << str << "\t:\t" << myAtoi(str) << endl;
}
}