-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.cpp
316 lines (284 loc) · 17 KB
/
Calculator.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
314
315
316
/*
This program implements a advanced expression calculator.
Edited by Zhu Wenjie in 9/28/2018
Input from input.txt; output to output.txt.
The grammar for input is:
Statement:
Expression
Print
Print:
;
Expression:
Term
Expression + Term
Expression – Term
Term:
HighOrder_Term
Term * HighOrder_Term
Term / HighOrder_Term
Term % HighOrder_Term
HighOrder_Term:
TopOrder
-TopOrder
+TopOrder
TopOrder_Term:
Primary
Primary!
Primary:
ANS
Number
( Expression )
Number:
floating-point-literal
Input comes from fin through the Token_stream called ts.
*/
#include<iostream>
#include<cmath>
#include<string>
#include<fstream>
#include<stdexcept>
#include<memory>
using namespace std;
inline bool isInt(double a)
{
return int(a) == a;
}
inline void error(const string& s)
{
throw runtime_error(s);
}
//------------------------------------------------------------------------------
// Token holds operators, operands and ANS for last result
struct Token {
char kind;
double value;
string name;
Token(char ch) :kind(ch), value(0) { } // Operator
Token(char ch, double val) :kind(ch), value(val) { } // Operand
Token(char ch, string n) :kind(ch), name(n) { } // Signal
};
// Stream of tokens
ifstream fin("input.txt");
ofstream fout("output.txt");
class Token_stream {
bool full;
Token buffer; // Holds a symbol that wasn't used by specific function
public:
Token_stream() :full(false), buffer(0) { }
Token get();
void unget(Token t) { buffer = t; full = true; } // Returns symbol to buffer. If 'full' = true, get() will use symbol from buffer
void ignore(char); // Ignores all characters before specific symbol
};
double last_res = 0;//Restore the last result
const char ans = 'A'; // ANS is used for restore the last result
const char print = ';'; // Prints the result of calculation
const char number = '8'; // Indicates that this is a number
int leftnum = 0;//The number of ( which has no ) to match it
Token Token_stream::get() // Get characters from fin
{
if (full) { full = false; return buffer; } // If buffer is not empty, return value from it
char ch; // Otherwise start looking for characters in input stream
fin>>ch;
switch (ch) {
case '(':
leftnum++;
return Token(ch);
case ')':
if (leftnum == 0)error("error");
leftnum--;
case '+':
case '-':
case '*':
case '/':
case '%':
case ';':
case '!':
return Token(ch); // If it is an operator or comma then return it
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
fin.unget(); // If it is a number then return it to the stream
double val;
fin >> val; // Read the complete value
return Token(number, val);
}
default:
if (fin&&isalpha(ch)) { // If it is a letter
string s;
s += ch; // Put the letter in 's' because it has been already read
while (fin.get(ch) && isalpha(ch) )s += ch; // Continue to read in 's'
fin.unget(); // Return the character into the stream
if (s == "ANS")
return Token(ans); // Evaluate the former result
}
error("error");
}
}
void Token_stream::ignore(char c) // Ignores all characters before specific symbol
{
if (full && c == buffer.kind) { // If 'c' is already in buffer then just discard it
full = false;
return;
}
full = false; // Otherwise discard value in buffer and look for it further
char ch;
while (fin >> ch) // Read all values before 'c'
if (ch == c) return; // Return when 'c' is found
}
Token_stream ts; // Holds all Tokens
double expression();
double primary() // Processes semicolons, numbers and returns variables
{
Token t = ts.get();
switch (t.kind) {
case ans: // Calculate the last result
{
return last_res;
}
case '(':
{ double d = expression(); // Perform calculations in semicolons
t = ts.get(); // Get a ')' closing character
if (t.kind != ')') { ts.unget(t); error("error"); } // If there wasn't any ')' return an error and recover the stream
return d;
}
case number: // If Token is a number
return t.value; // Return the number
default:
ts.unget(t);
error("error"); // Return an error if an inappropriate character was provided
}
}
double toporder_term() // Performs '!'
{
double left = primary(); // Get a number
while (true) {
Token t = ts.get(); // Get a new character
switch (t.kind) {
case '!': // Factorial
{
if (left == 0)return 1;
if (left < 0 ||!isInt(left))error("error");
double x = left;
for (int i = 1; i < left; i++) { // Get a multiplication of all numbers before x (including x)
x *= i;
}
if (x == 0) left = 1;
else left = x;
break;
}
default:
ts.unget(t); // If nothing was done return character to the stream
return left; // Return new or unchanged value of 'left'
}
}
}
double highorder_term() // Performs '+', '-'
{
double k = 1;
while(true)
{
Token t = ts.get(); // Get a new character
switch (t.kind) {
case '+':
{
break;
}
case '-': // For negative digits
{
k *= -1;
break;
}
default:
ts.unget(t); // If nothing was done return character to the stream
return k*toporder_term(); // Return new or unchanged value of 'left'
}
}
}
double term() // Performs '*', '/', '%'
{
double left = highorder_term(); // Get a number
while (true) {
Token t = ts.get(); // Get a new character
switch (t.kind) {
case '*': // Multiplication
left *= highorder_term();
break;
case '/': // Division
{
double d = highorder_term();
if (d == 0) error("error"); // Division by zero is prohibited
left /= d;
break;
}
case '%': // Modulo
{
double d = highorder_term();
if (d == 0) error("error");
else if(isInt(left)&&isInt(d))//Each operand should be integer
left = fmod(left, d); // Use fmod to divide floating-point numbers with remainder
else error("error");
break;
}
default:
ts.unget(t); // If nothing was done return character to the stream
if (left == -0) return 0; // Change -0 to 0 when it was multiplied or divided by negative digit
return left; // Return new or unchanged value of 'left'
}
}
}
double expression() // Performs '+' and '-' operations
{
double left = term(); // Get the number or the result of calculations in term
while (true) {
Token t = ts.get();
switch (t.kind) {
case '+':
left += term(); // Addition
break;
case '-':
left -= term(); // Subtraction
t.kind = 'n';
break;
default:
ts.unget(t); // If nothing was done return character to the stream
return left; // Return the new or unchanged value of 'left'
}
}
}
void clean_up_mess() // Ignores all characters before ';' if an exception was thrown
{
ts.ignore(print);
}
void calculate() // Performs calculations
{
while (fin) try {//Iterating until EOF
leftnum = 0;//reset the condition
Token t = ts.get(); // Get a new character
while (t.kind == print) t = ts.get(); // Read all ';'
ts.unget(t); // Return a character into the stream
if(fin){
last_res = expression();
fout << last_res << endl; // Output the result
}
}
catch (runtime_error& e) {
if (fin) {//Forbid output when it has been EOF
fout << e.what() << endl; // fout the error if the exception was thrown
clean_up_mess(); // Ignores all characters till the ';'
}
}
}
int main()
{
calculate(); // Performs calculations
return 0;
}