今天學校 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