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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
| ///////////////////////////////////////////////////////////////////////////////
// Калькулятор делает то же, что и калькулятор в книжке Страуструпа, т.е.
// вычисляет последовательность арифметических выражений, в которых используются
// круглые скобки. Арифметические выражения должны быть записаны в одной строке,
// и разделены точкой с запятой. Арифметическим выражением также считается
// инициализация переменной. Имеются встроенные константы pi и e.
// Пример корректной строки ввода:
// r = 1.5; a = 2 * pi * r; b = a * a
///////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
///////////////////////////////////////////////////////////////////////////////
typedef std::string T_str;
typedef double T_num;
typedef std::map<T_str, T_num> T_names_values;
///////////////////////////////////////////////////////////////////////////////
class T_calculator
{
//-------------------------------------------------------------------------
enum
{
LEFT_PARENTHESIS = '(',
RIGHT_PARENTHESIS = ')',
PLUS_SIGN = '+',
MINUS_SIGN = '-',
ASTERISK = '*',
SLASH = '/',
EQUALS_SIGN = '=',
SEMICOLON = ';',
FINISH_SYMB = '$'
};
//-------------------------------------------------------------------------
T_names_values names_values;
//-------------------------------------------------------------------------
public:
T_calculator();
//-------------------------------------------------------------------------
private:
//-------------------------------------------------------------------------
bool calc_expr_list (const T_str& s_inp, T_str& s_out);
bool print_expr (const T_str& s_inp, T_str& s_out);
void remove_spaces (T_str& s);
bool calc_expr (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_semicolon (const T_str& s_inp, T_str& s_out);
bool calc_summand (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_line_of_summand (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_factor (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_line_of_factor (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_add_summand (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_subtract_summand (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_minus_factor (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_expr_in_brackets (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_name_and_name_init (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_num (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_mult_to_factor (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_div_into_factor (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_plus (const T_str& s_inp, T_str& s_out);
bool calc_minus (const T_str& s_inp, T_str& s_out);
bool calc_left_parenthesis (const T_str& s_inp, T_str& s_out);
bool calc_right_parenthesis (const T_str& s_inp, T_str& s_out);
void calc_name (const T_str& s_inp, T_str& s_out, T_str& name);
bool calc_name_init (const T_str& s_inp, T_str& s_out, T_num& res);
bool calc_asterisk (const T_str& s_inp, T_str& s_out);
bool calc_slash (const T_str& s_inp, T_str& s_out);
bool calc_equals_sign (const T_str& s_inp, T_str& s_out);
bool calc_symb (const char c, const T_str& s_inp, T_str& s_out);
};
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_name_init(const T_str& s_inp, T_str& s_out, T_num& res)
{
calc_equals_sign (s_inp, s_out);
return calc_expr (s_out, s_out, res);
}
///////////////////////////////////////////////////////////////////////////////
void T_calculator::calc_name(const T_str& s_inp, T_str& s_out, T_str& name)
{
T_str::const_iterator str_it = std::find_if(s_inp.begin(), s_inp.end(),
std::not1(std::ptr_fun(isalpha)));
name.assign(s_inp.begin(), str_it);
s_out.assign(str_it, s_inp.end());
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_name_and_name_init(const T_str& s_inp, T_str& s_out, T_num& res)
{
T_str name;
calc_name(s_inp, s_out, name);
bool bool_res;
if(s_out[0] == EQUALS_SIGN)
{
bool_res = calc_name_init(s_out, s_out, res);
if(bool_res)
{
names_values[name] = res;
}
return bool_res;
}
else//После имени нет инициализатора.
{
bool_res = (names_values.count(name) != 0);
//Если имя уже было инициализировано:
if(bool_res)
{
res = names_values[name];
}
else
{
std::cout << "The variable \""
<< name
<< "\" is not initialized."
<< std::endl;
}
return bool_res;
}
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_equals_sign(const T_str& s_inp, T_str& s_out)
{
return calc_symb(EQUALS_SIGN, s_inp, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_semicolon(const T_str& s_inp, T_str& s_out)
{
return calc_symb(SEMICOLON, s_inp, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::print_expr(const T_str& s_inp, T_str& s_out)
{
T_num num_res;
bool bool_res = calc_expr(s_inp, s_out, num_res);
if(bool_res)
{
std::cout << num_res
<< std::endl;
}
return bool_res;
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_expr_list(const T_str& s_inp, T_str& s_out)
{
if(!print_expr(s_inp, s_out))
{
return false;
}
switch(s_out[0])
{
case FINISH_SYMB:
return true;
case SEMICOLON:
calc_semicolon(s_out, s_out);
if(s_out[0] == FINISH_SYMB)
{
return true;
}
else
{
return calc_expr_list(s_out, s_out);
}
default:
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
T_calculator::T_calculator()
{
names_values["pi"] = 3.1415926535897932385;
names_values["e"] = 2.7182818284590452354;
std::cout << "-> ";
T_str s_inp;
std::getline(std::cin, s_inp);
if(s_inp.empty()) exit(0);
remove_spaces(s_inp);
T_str s_out;
if(!calc_expr_list(s_inp += FINISH_SYMB, s_out))
{
std::cout << "Not correct expr.";
}
std::cout << std::endl
<< std::endl
<< std::endl;
}
///////////////////////////////////////////////////////////////////////////////
void T_calculator::remove_spaces(T_str& s)
{
for(;;)
{
T_str::size_type pos = s.find_first_of(" \t");
if(pos == T_str::npos)
{
break;
}
s = s.erase(pos, 1);
}
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_expr(const T_str& s_inp, T_str& s_out, T_num& res)
{
return calc_summand (s_inp, s_out, res)
&& calc_line_of_summand (s_out, s_out, res);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_summand(const T_str& s_inp, T_str& s_out, T_num& res)
{
return calc_factor (s_inp, s_out, res)
&& calc_line_of_factor (s_out, s_out, res);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_line_of_summand(const T_str& s_inp, T_str& s_out, T_num& res)
{
switch(s_inp[0])
{
case PLUS_SIGN:
return calc_add_summand (s_inp, s_out, res)
&& calc_line_of_summand (s_out, s_out, res);
case MINUS_SIGN:
return calc_subtract_summand (s_inp, s_out, res)
&& calc_line_of_summand (s_out, s_out, res);
default:
return true;
}
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_factor(const T_str& s_inp, T_str& s_out, T_num& res)
{
switch(s_inp[0])
{
case MINUS_SIGN:
return calc_minus_factor (s_inp, s_out, res);
case LEFT_PARENTHESIS:
return calc_expr_in_brackets (s_inp, s_out, res);
default:
if(isalpha(s_inp[0]))
{
return calc_name_and_name_init (s_inp, s_out, res);
}
else
{
return calc_num (s_inp, s_out, res);
}
}
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_minus_factor(const T_str& s_inp, T_str& s_out, T_num& res)
{
bool bool_res = calc_minus (s_inp, s_out)
&& calc_factor (s_out, s_out, res);
res *= -1;
return bool_res;
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_line_of_factor(const T_str& s_inp, T_str& s_out, T_num& res)
{
switch(s_inp[0])
{
case ASTERISK:
return calc_mult_to_factor (s_inp, s_out, res)
&& calc_line_of_factor (s_out, s_out, res);
case SLASH:
return calc_div_into_factor (s_inp, s_out, res)
&& calc_line_of_factor (s_out, s_out, res);
default:
return true;
}
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_add_summand(const T_str& s_inp, T_str& s_out, T_num& res)
{
T_num res_summand;
bool bool_res = calc_plus (s_inp, s_out)
&& calc_summand (s_out, s_out, res_summand);
res += res_summand;
return bool_res;
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_subtract_summand(const T_str& s_inp, T_str& s_out, T_num& res)
{
T_num res_summand;
bool bool_res = calc_minus (s_inp, s_out)
&& calc_summand (s_out, s_out, res_summand);
res -= res_summand;
return bool_res;
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_mult_to_factor(const T_str& s_inp, T_str& s_out, T_num& res)
{
T_num res_factor;
bool bool_res = calc_asterisk (s_inp, s_out)
&& calc_factor (s_out, s_out, res_factor);
res *= res_factor;
return bool_res;
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_div_into_factor(const T_str& s_inp, T_str& s_out, T_num& res)
{
T_num res_factor;
bool bool_res = calc_slash (s_inp, s_out)
&& calc_factor (s_out, s_out, res_factor);
res /= res_factor;
return bool_res;
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_plus(const T_str& s_inp, T_str& s_out)
{
return calc_symb(PLUS_SIGN, s_inp, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_asterisk(const T_str& s_inp, T_str& s_out)
{
return calc_symb(ASTERISK, s_inp, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_slash(const T_str& s_inp, T_str& s_out)
{
return calc_symb(SLASH, s_inp, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_minus(const T_str& s_inp, T_str& s_out)
{
return calc_symb(MINUS_SIGN, s_inp, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_expr_in_brackets(const T_str& s_inp, T_str& s_out, T_num& res)
{
return calc_left_parenthesis (s_inp, s_out)
&& calc_expr (s_out, s_out, res)
&& calc_right_parenthesis (s_out, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_num(const T_str& s_inp, T_str& s_out, T_num& res)
{
std::istringstream sin(s_inp);
bool bool_res = ((sin >> res) != 0);
sin >> s_out;
return bool_res;
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_left_parenthesis(const T_str& s_inp, T_str& s_out)
{
return calc_symb(LEFT_PARENTHESIS, s_inp, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_right_parenthesis(const T_str& s_inp, T_str& s_out)
{
return calc_symb(RIGHT_PARENTHESIS, s_inp, s_out);
}
///////////////////////////////////////////////////////////////////////////////
bool T_calculator::calc_symb(const char c, const T_str& s_inp, T_str& s_out)
{
bool res = (s_inp[0] == c);
if(res)
{
s_out = s_inp.substr(1);
}
return res;
}
///////////////////////////////////////////////////////////////////////////////
int main()
{
for(;;)
{
T_calculator calculator;
}
return 0;
} |