-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
53 lines (51 loc) · 1.18 KB
/
program.cpp
File metadata and controls
53 lines (51 loc) · 1.18 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
#include "../include/pre.h"
bool isNumber(string s)
{
auto is_digit = [](char ch)->bool{ return ch >= '0' && ch <= '9'; };
auto is_flag = [](char ch)->bool{ return ch == '+' || ch == '-'; };
size_t i = 0;
auto skip_digit = [&is_digit, &s, &i](){
int count = i;
for (; i != s.size(); i++)
if (!is_digit(s[i])) break;
return i - count;
};
auto skip_space = [&s, &i](){
for (; i != s.size(); i++)
if (s[i] != ' ') break;
};
skip_space();
if (is_flag(s[i])) i++;
int count = skip_digit();
if (s[i] == '.') {
i++;
count += skip_digit();
}
if (s[i] == 'e') {
i++;
if (is_flag(s[i])) i++;
if (skip_digit() == 0) return false;
}
skip_space();
if (count == 0 || i != s.size()) return false; else return true;
}
int main()
{
vector<string> tests {
" ",
".e12",
".09e",
".12 ", //treat as true
" +1.02 ",
"-1.9e4",
"1e",
".",
"sdf4325",
"0.2",
"1 a"
};
for (auto& x : tests) {
cout << x << "\t:\t" << isNumber(x) << "\n";
}
return 0;
}