今天学校 C 语言老师教到字串转数字,我本来写先判别正负号的程式,
可是我同学没有判别正负结果结果也正确,真的很奇怪。以下是程式码
#include <stdio.h>
#include <stdlib.h>
int atoi(char *s){
int n = 0;//, t = 0;
//if(*s == '-'){t = 1;s++;}
while(*s)n = 10*n + (*s++) - '0';
return n;
//return (t ? n : -n);
}
void main(){
printf("%d\n", atoi("-123456789"));
system("pause");
}
作者:
kwpn (ITSST)
2014-05-09 13:25:00为什么很奇怪, 你拿atoi查到的应该是convert string tointeger而不是positive integer或unsigned integer
作者:
BlazarArc (Midnight Sun)
2014-05-09 13:29:00你用到了 <stdlib.h> 里面的 atoi 了
int atoi (const char * s);
作者:
uranusjr (â†é€™äººæ˜¯è¶…級笨蛋)
2014-05-09 16:51:00VC++ 本来就可以编译 C 啊, IDE 的名字只是名字而已
作者:
s25g5d4 (function(){})()
2014-05-09 18:31:00C++ 不是 C 的 superset 吗...
作者:
uranusjr (â†é€™äººæ˜¯è¶…級笨蛋)
2014-05-09 20:44:00↑ 不是
作者:
s25g5d4 (function(){})()
2014-05-09 22:02:00作者:
Rondou (东东)
2014-05-10 02:50:00你举的那个例子在 C99 就可以用了喔
作者: virve (std::vie) 2014-05-15 21:29:00
放心在VC写.c吧 标准很旧想写新标准也没办法XD