forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validNumber.cpp
118 lines (103 loc) · 2.53 KB
/
validNumber.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Source : https://oj.leetcode.com/problems/valid-number/
// Author : Hao Chen
// Date : 2014-08-26
/**********************************************************************************
*
* Validate if a given string is numeric.
*
* Some examples:
* "0" => true
* " 0.1 " => true
* "abc" => false
* "1 a" => false
* "2e10" => true
*
* Note: It is intended for the problem statement to be ambiguous.
* You should gather all requirements up front before implementing one.
*
*
**********************************************************************************/
#include <iostream>
using namespace std;
bool isdigit(const char c){
return (c>='0' && c<='9');
}
bool isspace(const char c) {
return (c==' ' || c =='\t' || c=='\n' || c=='\r' || c=='\f' || c=='\v');
}
bool isNumber(const char *s) {
bool point = false;
bool hasE = false;
//trim the space
while(isspace(*s)) s++;
//check empty
if (*s == '\0' ) return false;
//check sign
if (*s=='+' || *s=='-') s++;
const char *head = s;
for(; *s!='\0'; s++){
// if meet point
if ( *s == '.' ){
if ( hasE == true || point == true){
return false;
}
if ( s == head && !isdigit(*(s+1)) ){
return false;
}
point = true;
continue;
}
//if meet "e"
if ( *s == 'e' ){
if ( hasE == true || s == head) {
return false;
}
s++;
if ( *s=='+' || *s=='-' ) s++;
if ( !isdigit(*s) ) return false;
hasE = true;
continue;
}
//if meet space, check the rest chars are space or not
if (isspace(*s)){
for (; *s != '\0'; s++){
if (!isspace(*s)) return false;
}
return true;
}
if ( !isdigit(*s) ) {
return false;
}
}
return true;
}
#define TEST(s) cout << "\"" << s << "\"" << " : " << isNumber(s) << endl
int main(int argc, char** argv)
{
const char* s="123";
if(argc>1){
s = argv[1];
}
TEST(s);
TEST("1.044");
TEST(" 1.044 ");
TEST("1.a");
TEST("abc");
TEST("e");
TEST("1e");
TEST("1e2");
TEST("");
TEST(" ");
TEST("1.");
TEST(".2");
TEST(" . ");
TEST(".");
TEST("1.2.3");
TEST("1e2e3");
TEST("1..");
TEST("+1.");
TEST(" -1.");
TEST("6e6.5");
TEST("005047e+6");
return 0;
}