#include <iostream>
#include <string>
using namespace std;
//实现string到unsigned int的转换
unsigned int string_to_unsigned_int(string str)
{
unsigned int result(0);//最大可表示值为4294967296(=2‘32-1)
//从字符串首位读取到末位(下标由0到str.size() - 1)
for (int i = str.size()-1;i >= 0;i--)
{
unsigned int temp(0),k = str.size() - i - 1;
//判断是否为数字
if (isdigit(str[i]))
{
//求出数字与零相对位置
temp = str[i] - '0';
while (k--)
temp *= 10;
result += temp;
}
else
//exit(-1);
break;
}
return result;
}
int main()
{
string str;
while (cin >> str)
{
cout << "the result is: " << string_to_unsigned_int(str) << endl;
}
return 0;
}
实现string到unsigned int的转换
最新推荐文章于 2025-02-14 08:00:00 发布