代码改变世界

将字符串转为数字

2009-10-09 15:39  Iron  阅读(202)  评论(0编辑  收藏  举报
#include <cstdio>
#include <cassert>

int atoi(const char* str)
{
 assert(str!=NULL);
 const char* s = str;
 int value=0;
 while(*s!='\0')
 {
  value *= 10;
  value += *s - '0';
  s++;
 }
 return value;
}
int main()
{
 printf("%d\n",atoi("1234"));
}