-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53.h
More file actions
55 lines (51 loc) · 1.33 KB
/
53.h
File metadata and controls
55 lines (51 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @Author: FreedomLy
* @Date: 2018-05-03 20:32:04
* @Last Modified by: FreedomLy
* @Last Modified time: 2018-05-03 20:40:01
* 题目
* 请实现一个函数用来判断字符串是否表示数值(包括整数和小数以及
* 科学计数)
*/
// 思路
// 采用json解析数字的方法
// https://github.com/Felon03/json-tutorial/blob/master/2-Number/leptjson.c
#pragma once
#define ISDIGIT1TO9(ch) ((ch) >= '1' && (ch) <= '9')
#define ISDIGIT(ch) ((ch) >= '0' && (ch) <= '9')
class Solution53 {
public:
bool is_numeric(char* str)
{
if (*str == '+' || *str == '-')
++str;
if (*str == '0')
++str;
else
{
if (!ISDIGIT1TO9(*str))
return false;
while (ISDIGIT(*str))
++str;
}
if (*str == '.')
{
++str;
if (!ISDIGIT(*str)) // 小数点后面必须是数字
return false;
while (ISDIGIT(*str))
++str;
}
if (*str == 'e' || *str == 'E') // 科学计数
{
++str;
if (*str == '+' || *str == '-')
++str;
if (!ISDIGIT(*str))
return false;
while (ISDIGIT(*str))
++str;
}
return *str == '\0';
}
};