剑指Offer20 表示数值的字符串

不多赘述,可以看书本。

最重要的是可以通过比较两个指针的值(即他们的地址),判断其中一个指针有没有向前移动。

  1 /*******************************************************************
  2 Copyright(c) 2016, Harry He
  3 All rights reserved.
  4 
  5 Distributed under the BSD license.
  6 (See accompanying file LICENSE.txt at
  7 https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
  8 *******************************************************************/
  9 
 10 //==================================================================
 11 // 《剑指Offer——名企面试官精讲典型编程题》代码
 12 // 作者:何海涛
 13 //==================================================================
 14 
 15 // 面试题20:表示数值的字符串
 16 // 题目:请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,
 17 // 字符串“+100”、“5e2”、“-123”、“3.1416”及“-1E-16”都表示数值,但“12e”、
 18 // “1a3.14”、“1.2.3”、“+-5”及“12e+5.4”都不是
 19 
 20 #include <stdio.h>
 21 
 22 bool scanUnsignedInteger(const char **str);
 23 bool scanInteger(const char **str);
 24 
 25 // 数字的格式可以用A[.[B]][e|EC]或者.B[e|EC]表示,其中A和C都是
 26 // 整数(可以有正负号,也可以没有),而B是一个无符号整数
 27 bool isNumeric(const char *str)
 28 {
 29     if (str == nullptr)
 30         return false;
 31 
 32     bool numeric = scanInteger(&str);
 33 
 34     // 如果出现'.',接下来是数字的小数部分
 35     if (*str == '.')
 36     {
 37         ++str;
 38 
 39         // 下面一行代码用||的原因:
 40         // 1. 小数可以没有整数部分,例如.123等于0.123;
 41         // 2. 小数点后面可以没有数字,例如233.等于233.0;
 42         // 3. 当然小数点前面和后面可以有数字,例如233.666
 43         numeric = scanUnsignedInteger(&str) || numeric;
 44     }
 45 
 46     // 如果出现'e'或者'E',接下来跟着的是数字的指数部分
 47     if (*str == 'e' || *str == 'E')
 48     {
 49         ++str;
 50 
 51         // 下面一行代码用&&的原因:
 52         // 1. 当e或E前面没有数字时,整个字符串不能表示数字,例如.e1、e1;
 53         // 2. 当e或E后面没有整数时,整个字符串不能表示数字,例如12e、12e+5.4
 54         numeric = numeric && scanInteger(&str);
 55     }
 56 
 57     return numeric && *str == '';
 58 }
 59 
 60 bool scanUnsignedInteger(const char **str)
 61 {
 62     const char *before = *str;
 63     while (**str != '' && **str >= '0' && **str <= '9')
 64         ++(*str);
 65 
 66     // 当str中存在若干0-9的数字时,返回true
 67     return *str > before;
 68 }
 69 
 70 // 整数的格式可以用[+|-]B表示, 其中B为无符号整数
 71 bool scanInteger(const char **str)
 72 {
 73     if (**str == '+' || **str == '-')
 74         ++(*str);
 75     return scanUnsignedInteger(str);
 76 }
 77 
 78 // ====================测试代码====================
 79 void Test(const char *testName, const char *str, bool expected)
 80 {
 81     if (testName != nullptr)
 82         printf("%s begins: ", testName);
 83 
 84     if (isNumeric(str) == expected)
 85         printf("Passed.
");
 86     else
 87         printf("FAILED.
");
 88 }
 89 
 90 int main(int argc, char *argv[])
 91 {
 92     Test("Test1", "100", true);
 93     Test("Test2", "123.45e+6", true);
 94     Test("Test3", "+500", true);
 95     Test("Test4", "5e2", true);
 96     Test("Test5", "3.1416", true);
 97     Test("Test6", "600.", true);
 98     Test("Test7", "-.123", true);
 99     Test("Test8", "-1E-16", true);
100     Test("Test9", "1.79769313486232E+308", true);
101 
102     printf("

");
103 
104     Test("Test10", "12e", false);
105     Test("Test11", "1a3.14", false);
106     Test("Test12", "1+23", false);
107     Test("Test13", "1.2.3", false);
108     Test("Test14", "+-5", false);
109     Test("Test15", "12e+5.4", false);
110     Test("Test16", ".", false);
111     Test("Test17", ".e1", false);
112     Test("Test18", "e1", false);
113     Test("Test19", "+.", false);
114     Test("Test20", "", false);
115     Test("Test21", nullptr, false);
116 
117     return 0;
118 }
原文地址:https://www.cnblogs.com/rookiez/p/13232541.html