//注意!待解决问题:1.如果字符串以非数字结束,则会出错(已解决!)
// 2.如果字符串中出现中文,则会出错
#include<iostream>
#include<string>
using namespace std;
int main()
{
string x;
getline(cin, x);
unsigned int i = 0;
size_t pos=0;
while (pos!=x.size())
{
for (i;i<x.size(); i++)
{
if (isdigit(x[i]))
{
x=x.substr(i);
break;//如果有数字,提取该字符串从数字之后的子串(包括数字串),并跳出for循环
}
try
{
int j;
for ( j= i; j < x.size(); j++)
{
if(isdigit(x[j]))
break;
}
if(!isdigit(x[j]))
throw string("\n检索完毕!\n");
}
catch (string x)
{
cout << x;
exit(1);
}
}
double y=0;
y=stod(x,&pos);//将数字字符串转换为double型变量,pos用于返回字符串s无法被转换的第一个字符的索引(即下标)
i = pos;
cout << y<<endl;
}
return 0;
}
C++ 提取字符串中的数字(英文状态下的字符串)
猜你喜欢
转载自blog.csdn.net/qq_44713855/article/details/103347663
今日推荐
周排行