zoukankan      html  css  js  c++  java
  • 判断字符串是否是整数或浮点数

    判断一个字符串是否是一个整型数或浮点数

    两个函数:isNum-判断是否是一个整型数 isFloat-判断是否是一个浮点数

    代码:
    Code: Select all
    bool CUtility::isNum(CString csInput)
    {
    //将字符串一个一个字符拆出来判断是否是数字
    char cTemp;
    int nStringLength = csInput.GetLength();
    bool bReturnValue = true;

    for (int nLoopindex = 0; nLoopindex < nStringLength; nLoopindex++)
    {
      //将字符一个一个取出来
      cTemp = csInput.GetAt(nLoopindex);
      if (cTemp >= 0x30 && cTemp <= 0x39)   //在0-9范围之内
      {
       //是数字,什么都不做
      }
      else
      {
       bReturnValue = false;
       break;
      }
    }

    return bReturnValue;
    }

    bool CUtility::isFloat(CString csInput)
    {
    //将字符一个一个取出来
    char cTemp;
    int nStringLength = csInput.GetLength();
    bool bReturnValue = true;  //返回值

    int nZhengshu = 0;  //整数有几位
    int nXiaoshu = 0;   //小数有几位

    bool bReachDot = false;  //是否已经读到小数点了


    for (int nLoopindex = 0; nLoopindex < nStringLength; nLoopindex++)
    {
      cTemp = csInput.GetAt(nLoopindex);

      if (cTemp >= 0x30 && cTemp <= 0x39)
      {
       if (bReachDot == true)
       {
        nXiaoshu++;
       }
       else
       {
        nZhengshu++;
       }
      }
      else
      {
       //非数字,看看是不是小数点
       if (cTemp == '.')
       {
        if (bReachDot == true)
        {
         //已经碰到过小数点了,又碰到,所以错误
         bReturnValue = false;
         break;
        }
        else
        {
         //碰到小数点了,看看整数部分是不是有了
         bReachDot = true;
         if (nZhengshu > 0)
         {
          //整数部分正确
         }
         else
         {
          //无整数部分,错误
          bReturnValue = false;
          break;
         }
        }
       }
       else
       {
        bReturnValue = false;
        break;
       }
      }
    }

    return bReturnValue;
    }
  • 相关阅读:
    Module build failed: TypeError: this.getResolve is not a function 安装node-sass运行报错
    RMAN Catalog 和 Nocatalog 的区别
    oracle 闪回区故障
    mysql windows 安装 错误
    git rejected
    oracle 计算机改名后监听无法启动
    mysql GTID
    java程序员技术范围
    activiti
    spring mybatis mysql 事务不起作用
  • 原文地址:https://www.cnblogs.com/super119/p/2011337.html
Copyright © 2011-2022 走看看