class Solution {
public:
int StrToInt(string str) {
int sum = 0;
int data;
if(str.empty())
return 0;
int i = 0;
if(str[i] == '+'||str[i] == '-')
{
if(str[2]==NULL)
return 0;
else
{
if(str[2]>'9'||str[2]<'1')
return 0;
else
{
int i = 3;
while(str[i]!=NULL)
{
if(str[i]>'9'||str[i]<'0')
return 0;
//break;
i++;
}
}
}
}
else
{
if(str[i] == '0')
return 0;
else
{
while(str[i]!=NULL)
{
if(str[i]>'9'||str[i]<'0')
return 0;
//break;
i++;
}
}
}
i = 0;
if(str[0]=='-')
{
data = -1;
i++;
}
else
{
if(str[0]=='+')
{
data = 1;
i++;
}
}
while(str[i] != NULL)
{
sum = sum*10 + (str[i]-'0');
i++;
}
return sum*data;
}
};
剑指offer-将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串
猜你喜欢
转载自blog.csdn.net/m0_37756557/article/details/83818890
今日推荐
周排行