-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.cpp
313 lines (305 loc) · 10.4 KB
/
validator.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
void _assert(bool expr, const std::string t)
{
if (!expr)
{
std::cerr << "Assert failed!\n";
std::cerr << t << '\n';
exit(1);
//abort();
}
}
void _assert(bool expr, int line, int pos, const std::string t)
{
if (!expr)
{
std::cerr << "Assert failed at line " << line << " pos " << pos << "!\n";
std::cerr << t << '\n';
exit(1);
//abort();
}
}
std::string to_string(long long int num)
{
bool is_neg = false;
std::string str = "";
if (num == 0)
return "0";
else if (num < 0)
{
is_neg = true;
num *= -1;
}
while (num > 0)
{
str += '0' + (num % 10);
num /= 10;
}
if (is_neg)
str += '-';
std::reverse(str.begin(), str.end());
return str;
}
class stream
{
private:
int line_num;
int char_num;
int rd;
bool fail;
std::string str;
std::string gulp(std::istream &in)
{
std::string ret;
char buffer[4096];
while (in.read(buffer, sizeof(buffer)))
ret.append(buffer, sizeof(buffer));
ret.append(buffer, in.gcount());
return ret;
}
bool stream_empty()
{
return (rd == (int)str.length());
}
bool is_digit(char c)
{
return ('0' <= c && c <= '9');
}
bool is_lowercase(char c)
{
return ('a' <= c && c <= 'z');
}
bool is_uppercase(char c)
{
return ('A' <= c && c <= 'Z');
}
bool is_space(char c)
{
return (c == ' ');
}
bool is_newline(char c)
{
return (c == '\n');
}
std::string printable(char c)
{
std::string tmp;
if (c == '\n')
tmp = "\\n";
else
tmp = c;
return tmp;
}
public:
stream(std::istream &in)
{
rd = 0;
line_num = 1;
char_num = 1;
fail = false;
str = gulp(in);
}
bool done()
{
_assert(!fail && stream_empty(), line_num, char_num, "Expected EOF, but stream is not empty.");
return !fail && stream_empty();
}
void read_char(char c)
{
std::string expected = printable(c);
if (stream_empty() || fail) //never read from an invalid stream
{
fail = true;
_assert(false, line_num, char_num, "Expected '" + expected + "', but stream is empty.");
}
else if (str.at(rd) != c)
{
std::string found = printable(str.at(rd));
fail = true;
_assert(false, line_num, char_num, "Expected '" + expected + "', but found '" + found + "'.");
}
else
rd++;
if (c == '\n')
{
line_num++;
char_num = 1;
}
else
char_num++;
}
void read_space()
{
read_char(' ');
}
void read_newline()
{
read_char('\n');
}
std::string read_word()
{
std::string pr = "";
if (stream_empty() || fail) //never read from an invalid stream
{
fail = true;
_assert(false, line_num, char_num, "Expected word, but stream is empty.");
}
else
{
for (int t_rd = rd; t_rd < (int)str.length(); t_rd++)
{
if (is_space(str.at(t_rd)) || is_newline(str.at(t_rd)))
break;
else
pr += str.at(t_rd);
}
if (pr.empty())
{
fail = true;
if (is_space(str.at(rd)))
_assert(false, line_num, char_num, "Expected word, but found ' '.");
else
_assert(false, line_num, char_num, "Expected word, but found '\\n'.");
}
}
rd += pr.length();
char_num += pr.length();
return pr;
}
long long int read_int(long long int l, long long int r)
{
long long int val = 0;
std::string num = "";
std::string pr = "";
if (stream_empty() || fail) //never read from an invalid stream
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but stream is empty.");
}
else
{
for (int t_rd = rd; t_rd < (int)str.length(); t_rd++)
{
if (is_space(str.at(t_rd)) || is_newline(str.at(t_rd)))
break;
else
pr += str.at(t_rd);
}
if (pr.empty())
{
fail = true;
if (is_space(str.at(rd)))
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found ' '.");
else
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found '\\n'.");
}
bool is_neg = false;
for (; rd < (int)str.length(); rd++)
{
if (str.at(rd) == '-')
{
//there's only 1 place this is okay, which is at the start
if (num == "" && !is_neg)
is_neg = true;
else
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found non-integer word \"" + pr + "\".");
}
}
else if (is_digit(str.at(rd)))
num += str.at(rd);
else if (is_space(str.at(rd)) || is_newline(str.at(rd)))
break;
else
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found non-integer word \"" + pr + "\".");
}
}
if (!fail)
{
if (num == "")
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found non-integer word \"" + pr + "\".");
}
else
{
if (num.at(0) == '0' && num.length() > 1)
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found non-integer word \"" + pr + "\".");
}
else if (num.at(0) == '0' && is_neg)
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found non-integer word \"" + pr + "\".");
}
else
{
std::string limit = "9223372036854775807";
if (num.length() > limit.length())
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found \"" + pr + "\".");
}
else if (num.length() == limit.length())
{
for (int i = 0; i < (int)num.length(); i++)
{
if (num.at(i) < limit.at(i))
break;
else if (num.at(i) > limit.at(i))
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found \"" + pr + "\".");
}
}
}
if (!fail)
{
long long int num_int;
std::stringstream ss;
ss << num;
ss >> num_int;
ss.clear();
ss.str("");
num_int *= (is_neg ? -1 : 1);
if (l <= num_int && num_int <= r)
{
val = num_int;
}
else
{
fail = true;
_assert(false, line_num, char_num, "Expected integer in range [" + to_string(l) + ", " + to_string(r) + "], but found \"" + pr + "\".");
}
}
}
}
}
}
char_num += pr.length();
return val;
}
};
/*
Example:
The input consists of two integers, a and b, separated by a space.
(1 <= a, b <= 100; a + b <= 100)
*/
int main()
{
stream st = stream(std::cin);
int a = st.read_int(1, 100);
st.read_space();
int b = st.read_int(1, 100);
_assert(a + b <= 100, "a + b must be <= 100.");
st.read_newline();
st.done();
exit(42);
}