判断是否是数字——IsNumberStr

#include <iostream>
#include <sstream>

using namespace std;

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

int main()
{
    const char* lpStr = "123";
    cout<<lpStr<<(IsNumbe(lpStr)==true?" is":" is not")<<" number"<<endl;
    return 0;

}





+++++++++++++++++++++++++++++++++++++++++++++++++++++++



#include <ctype.h>
#include <cstdio>
#include <cstring>

bool IsNumb(const char* lpStr)
{
    int cnt = strlen(lpStr);
    for(int i =0; i<cnt; i++)
    {
      if ( !isdigit(lpStr[i]) )
      {
         return false;
      }
    }
    return true;
}

int main()
{
    const char* lpStr = "1345d";
    printf("%s is %s number!!\n", lpStr, IsNumb(lpStr)==true?"":"not");
    return 0;
}


                            

猜你喜欢

转载自blog.csdn.net/fengdijiang/article/details/77840015
今日推荐