- 论坛徽章:
- 2
|
不知如下atoi的是否满足要求
- #include <stdio.h>
- int atoi(const char *s, int *ret){
- const char *p;
- if(s==NULL||ret==NULL)return-1;
- for(; *s!='\0'; s++)if(*s==' ')continue;else if(*s>='0'&&*s<='9'||*s=='-')break;else return-1;
- for(p=*s=='-'?s+1:s,*ret=0; *p!='\0'; p++)if(*p>='0'&&*p<='9')*ret=*ret*10+*p-'0';else if(*p==' ')break;else return-1;
- if(*s=='-')*ret=-*ret;
- return 0;
- }
- int main(void){
- char str[]=" -100 "; /* 前后可加空格 */
- int num;
- if(atoi(str, &num)==0){
- printf("%s == %d\n", str, num);
- }
- return 0;
- }
复制代码 |
|