zoukankan      html  css  js  c++  java
  • 判断输入字符串是不是都是数字

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

      

  • 相关阅读:
    I2C总线之(三)---以C语言理解IIC
    /sys/class/gpio 文件接口操作IO端口(s3c2440)
    CSP-201503
    CSP-201409
    CSP-201412
    Linux命令行学习笔记
    sstream
    VECTOR
    CSP-201403
    CSP-201312
  • 原文地址:https://www.cnblogs.com/277223178dudu/p/11371478.html
Copyright © 2011-2022 走看看