将字符串转换为整数 Convert the string to integer

来源:互联网 发布:linux 编写图形界面 编辑:程序博客网 时间:2024/06/02 04:25
int atoi(char* ch){int i = 0;char* c = ch;while(*c){if(*c >= '0' && *c <= '9')i = i * 10 + *c - '0';elsereturn -1;c++;}return i;}


int atoi1(char* ch){if(NULL == ch)return 0;bool posi = true;bool begin = false;int i = 0;char* c = ch;while(*c){if(!begin){if(*c == '-'){posi = false;begin = true;}else if(*c <= '9'&& *c >= '0'){begin = true;i = i * 10 + *c - '0';}}else{if(*c <= '9'&& *c >= '0')i = i * 10 + *c - '0';elsereturn posi? i : -i;}c++;}return posi? i : -i;}


0 0