#include
#include
long sign = 1,num = 0;;
char *p = s;
if(*p == '-') {
sign = -1;
p++;
}
if(*p == '+') {
sign = 1;
p++;
}
while(*p) {
if(num >= MAX_LONG/10) {
cout<<"被转换的字串过长,整数越界。"<
}
if((*p >= '0') && (*p <= '9')) num = 10*num + (*p) - '0';
p++;
}
return sign*num;
}int main() {
char s[11];
int num;
cout<<"请输入一个字串 : "<
gets(s);
num = myatoi(s);
cout<<"转换后的整数为 : "<
}
用atoi函数
int atoi(char* pstr) //参数是要转的字符串,返回值就是转之后的int类型了
例:
char sz[] = "123";
int n = atoi(sz);
cout << n;
int num=0;for(int i=0;i
}
用atoi() num = atoi(mystr);