[cpp]
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
int str_2_int(string str)
{
assert(str.size()>0);
int pos = 0;
int sym = 1;
if(str[pos] == '+')
pos++;
else if(str[pos] == '-')
{
pos++;
sym=-1;
}
int num =0;
while(pos<str.length())
{
assert(str[pos]>='0');
assert(str[pos]<='9');
num = num*10+(str[pos]-'0');
assert(num>=0);
pos++;
}
num*=sym;
return num;
}
int main()
{
string str = "-1024";
int num = str_2_int(str);
cout << num << endl;
return 0;
}
作者:zhangerqing