题目:
设计函数 int atoi(char *s)
题解:
- #include <stdio.h>
- #include <ctype.h>
- int main(void){
- char str[]="-100";
- char str1[]="567";
- int c;
- c=atoi(str)+atoi(str1);
- printf("c content is %d/n",c);
- return 0;
- }
- int atoi(const char *s){
- int len;
- char symbol='+';
- unsigned char *str;
- int v=0;
- str=s;
- while(*str==' ')str++;
- if(*str=='+'||*str=='-')
- symbol=*str++;
- while(isdigit(*str)){
- v=v*10+*str-'0';
- str++;
- }
- return symbol=='-'?(-1)*v:v;
- }
编译环境:
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
运行过程:
[explover@Explover 20081101]$ gcc -g atoi.c -o atoi
atoi.c: In function `atoi':
atoi.c:18: warning: assignment discards qualifiers from pointer target type
[explover@Explover 20081101]$ ./atoi
c content is 467
(本人水平有限,不足之处,请大家多多指正,谢谢!)