-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatoi.c
More file actions
41 lines (39 loc) · 847 Bytes
/
atoi.c
File metadata and controls
41 lines (39 loc) · 847 Bytes
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
#include<stdio.h>
int myAtoi(char* str) {
int i=0;
long long n=0;
char firstChar;
while(*str==' '||*str=='\t'||*str=='\n')
str++;
firstChar=*str;
if(firstChar=='-'||firstChar=='+'){
str++;
}
while(*str){
if((*str<='9')&& (*str>='0')){
n=n*10+(*str - '0');
printf("%lld\n",n);
if(firstChar!='-'&&(n>2147483647)){
n=2147483647;
break;
}
else if(firstChar!='+'&&n>2147483648){
n=2147483648;
break;
}
}
else
break;
str++;
}
n=n*((firstChar=='-')?-1:1);
printf("%lld\n",n);
return n;
}
int main(){
char s[1000];
while(1){
scanf("%s",s);
printf("%d %d\n",myAtoi(s),atoi(s));
}
}