-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
8.c
78 lines (73 loc) · 1.45 KB
/
8.c
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
int myAtoi(char *str)
{
int minusFlag = 0;
int length = strlen(str);
long int result = 0;
char numberBuffer[11];
int counter = 0;
while (str[counter] == ' ')
{
counter++;
}
str = &str[counter];
counter = 0;
for (int i = 0; i < length; i++)
{
if (i == 0)
{
if (str[0] == '-')
{
minusFlag = 1;
i++;
}
else if (str[0] == '+')
{
i++;
}
}
if (counter > 10)
{
if (minusFlag)
{
return __INT_MAX__ * -1 - 1;
}
else
{
return __INT_MAX__;
}
}
if (str[i] < '0' || str[i] > '9')
{
break;
}
if (counter == 0 && str[i] == '0')
{
continue;
}
numberBuffer[counter] = str[i];
counter++;
}
int i = 0;
while (counter > 0)
{
if (minusFlag)
{
result -= (numberBuffer[i] - '0') * pow(10.0, counter - 1);
}
else
{
result += (numberBuffer[i] - '0') * pow(10.0, counter - 1);
}
i++;
counter--;
}
if (result > __INT_MAX__)
{
return __INT_MAX__;
}
else if (result < __INT_MAX__ * -1 - 1)
{
return __INT_MAX__ * -1 - 1;
}
return result;
}