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;
    }
  • 相关阅读:
    pycharm 安装第三方库报错:AttributeError: 'module' object has no attribute 'main'
    工作冥想
    对于测试工作与测试人员未来出路的思考
    测试计划再谈
    python 反转列表的3种方式
    关于最近练习PYTHON代码的一点心得
    python sum()函数的用法
    python count()函数
    SpringCloud和SpringBoot的详细版本说明
    使用 lntelliJ IDEA 创建 Maven 工程的springboot项目
  • 原文地址:https://www.cnblogs.com/super119/p/2011337.html
Copyright © 2011-2022 走看看