欢迎来到IT嘟嘟的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

判断输入字符串是不是都是数字

//判断输入字符串是不是都是数字
//解题思路:
//C++中有三种字符串流,分别是istringstream, ostringstream, stringstream, 
//分别处理字符串流的输入,输出,和输入输出。istringstream sin(s); 定义一个字符串输入流的对象sin,
//并调用sin的复制构造函数,将s中所包含的字符串放入sin 对象中!
#include <iostream>
#include <sstream>  
using namespace std;
bool isNum(string str)
{
	stringstream sin(str);
	double d;
	char c;
	if (!(sin >> d))
	{
		return false;
	}
	if (sin >> c)
	{
		return false;
	}
	return true;
}
int main(void)
{	
	string str1,str2;
	cin >> str1;
	if (isNum(str1))
	{
		cout << "str1 is a num" << endl;
	}
	else
	{
		cout << "str1 is not a num" << endl;
	}
	system("pause");
	return 0;
}

  

posted on 2019-08-18 09:42  IT嘟嘟  阅读(736)  评论(0编辑  收藏  举报

导航