-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1537.cc
79 lines (68 loc) · 1.71 KB
/
1537.cc
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
//Name: Identifying Legal Pascal Real Constants
//Level: 2
//Category: 構文解析
//Note:
/*
* 問題文の通りに構文解析するだけ.
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
typedef string::iterator Iterator;
void trim(string &str) {
Iterator it = str.begin();
while(it != str.end() && *it == ' ') ++it;
str.erase(str.begin(), it);
if(str.size() > 0) {
Iterator it = str.end()-1;
while(it != str.begin() && *it == ' ') --it;
if(*it != ' ') ++it;
str.erase(it, str.end());
}
}
void skip(Iterator &it, char c, const Iterator &end) {
if(it == end || tolower(*it) != c) {
throw "Unexpected char";
}
++it;
}
void integer(Iterator &it, const Iterator &end) {
if(it == end) throw "End of line";
if(*it == '-' || *it == '+') skip(it, *it, end);
if(it == end || !isdigit(*it)) throw "Not an integer";
while(it != end && isdigit(*it)) ++it;
}
bool valid(Iterator &it, const Iterator &end) {
integer(it, end);
if(it == end || *it == '.') {
skip(it, '.', end);
integer(it, end);
}
if(it != end) {
skip(it, 'e', end);
integer(it, end);
}
return it == end;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
while(true) {
string line;
getline(cin, line);
if(line == "*") break;
trim(line);
Iterator it = line.begin();
bool res = true;
try {
res = valid(it, line.end());
} catch(const char *str) {
res = false;
}
cout << line << " is " << (res ? "" : "il") << "legal." << endl;
}
return 0;
}