#include<iostream>
using namespace std;
int str2int(const char *str)
{
int temp = 0;
const char *ptr = str;
if (*str == '-' || *str == '+')
str++;
while (*str != 0)
{
if ((*str) < '0' || (*str) > '9')
break;
temp = temp * 10 + (*str - '0');
str++;
}
if (*ptr == '-')
temp = -temp;
return temp;
}
int main()
{
int n = 0;
char p[10] = "";
cout << "please enter a number" << endl;
cin.getline(p, 20);
n = str2int(p);
cout << n << endl;
return 0;
}
思想,减去’0’就是数字,数字逐渐乘10,组成实际数字。