首页 > 其他 > 详细

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

时间:2019-08-18 11:12:10      阅读:111      评论:0      收藏:0      [点我收藏+]
//判断输入字符串是不是都是数字
//解题思路:
//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;
}

  

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

原文:https://www.cnblogs.com/277223178dudu/p/11371478.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!