-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.c
executable file
·58 lines (55 loc) · 1.48 KB
/
atoi.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
#include <stdio.h>
/*
complete atoi
*/
int core(const char* str, int minus){
int ret = 0;
int flag = (minus == 0)?-1:1;
while(*str != '\0'){
if(*str >= '0' && *str <='9'){
ret = ret*10 + flag*(*str - '0');
if( (minus == 0 && ret < 0x80000000) ||
(minus == -1 && ret > 0x7FFFFFFF) ){
ret = -1;
break;
}
++str;
}else{
ret = -1;
break;
}
}
if(*str != '\0')
return -1;
return ret;
}
int my_atoi(const char* str){
int ret = 0;
int minus = -1;
if(str == NULL || *str != '\0'){
if(*str == '+'){
++str;
}
if(*str == '-'){
++str;
minus = 0;
}
if(*str != '\0'){
ret = core(str,minus);
}
return ret;
}
}
int main(){
char test[3] = {'a','b','c'};
char* test1 = "-100";
int ret;
int a =1;
int b =-1;
printf("%d %d\n",!a, !b);
printf("%d\n",sizeof(long long));
printf("%s\n", test);
ret = my_atoi((const char*)test1);
printf("%d\n",ret);
return 0;
}