0
点赞
收藏
分享

微信扫一扫

C++判断字符串是否为数字


机器学习以及人工智能的学习需要扎实的数学功底才能走的更远,爬的更高,所以打好数学基础是关键,但无论工作学习都没有充足的时间去拿着书本一个字一个字的去学习了,这里我建议大家找几个比较靠谱入门的机器学习或者人工智能学习平台,一定要系统全面的去学习才能有效果,不要半途而废,

思路1:挨个字符判断其ASCII码是否属于数字范围 48--57

 

转为ASC码 :int i=(int)a; //a是字符

string s;

int tmp = (int)s[i];

 

 

思路二

C++实例:

 

#include <iostream>
#include <sstream>
using namespace std;

bool isNum(string str);
int main( )
{

string ss1="2y5r";
string ss2="2558";
if(isNum(ss1))
{
cout<<"ss1 is a num"<<endl;
}
else{
cout<<"ss1 is not a num"<<endl;

}
if(isNum(ss2))
{
cout<<"ss2 is a num"<<endl;
}
else{
cout<<"ss2 is not a num"<<endl;

}
return 0;
}

bool isNum(string str)
{
stringstream sin(str);
double d;
char c;
if(!(sin >> d))
return false;
if (sin >> c)
return false;
return true;
}

输出结果:

 

ss1 is not a num

ss2 is a num

举报

相关推荐

0 条评论