//判断输入字符串是不是都是数字
//解题思路:
//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