-
Notifications
You must be signed in to change notification settings - Fork 0
/
μcalc.cpp
395 lines (389 loc) · 14.6 KB
/
μcalc.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// C++11
#include <iostream>
#include <string>
#include <list>
#include <stdexcept>
using namespace std;
struct internalExpressionFormat {
list <char> operators;
list <long long> numbers;
char flag; // for internal information
internalExpressionFormat () {
operators.clear();
operators.clear();
flag = 'n'; // n - normal
}
};
bool higherPriority (char a, char b) { // Compare sign priority
char aPriority, bPriority; // true, if first sign (a) has a higher priority than second sign (b);
switch (a) {
case 1: // Stop-operator; very low priority. It need for easily parsing
aPriority = 0;
break;
case '+':
case '-':
aPriority = 1;
break;
case '*':
case '/':
case '%':
aPriority = 2;
break;
}
switch (b) {
case 1:
bPriority = 0;
break;
case '+':
case '-':
bPriority = 1;
break;
case '*':
case '/':
case '%':
bPriority = 2;
break;
}
if (aPriority > bPriority) {
return true;
}
else {
return false;
}
};
internalExpressionFormat readExp () { // read expression from stdin and translate to postfix form
bool endOfStream = false; // if true, exp. parsed
bool isLastOp = true; // true, if last significant symbol was operator
bool minus = false; // true, if next number must be negativ
bool isNum = false; // true, if last significant symbol was number
char nextChar; // next char from cin
long bracketsBalance = 0; // obvious
long long tempNum; // number, which reading now
list <char> sideStack; // stack for operators
sideStack.clear();
sideStack.push_back(1); // stop-operator
internalExpressionFormat res;
while (!endOfStream) {
nextChar = cin.get();
switch (nextChar) {
case '0': // next char is part of number
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (isNum) {
tempNum *= 10;
tempNum += nextChar-'0';
}
else {
isNum = true;
tempNum = nextChar - '0';
if (minus) {
tempNum *= -1;
minus = false;
}
}
isLastOp = false;
break;
case '-': // next char is '-'. Maybe it is part of negative number?
if (isLastOp) {
minus = ~minus;
break;
}
case '+': // next char is math operator (including '-')
case '*':
case '/':
case '%':
if (isNum) { // Before it, we read number. This is end og number, so add number to parced exp.
res.numbers.push_back(tempNum);
res.operators.push_back('n');
isNum = false;
} // Operator after operator. It is not good.
if (isLastOp) {
cerr<<"Parsing error - too many operators"<<endl;
res.flag = 'e';
return res;
break;
} // translate operation
if (!higherPriority(nextChar,sideStack.back())) {
res.operators.push_back(sideStack.back());
sideStack.pop_back();
}
sideStack.push_back(nextChar);
isLastOp = true;
break;
case '(':
bracketsBalance++;
if (isNum) { // Before it, we read number. This is end og number, so add number to parced exp.
res.numbers.push_back(tempNum);
res.operators.push_back('n');
isNum = false;
}
sideStack.push_back('('); // translate operation
break;
case ')':
bracketsBalance--;
if (isNum) { // Before it, we read number. This is end og number, so add number to parced exp.
res.numbers.push_back(tempNum);
res.operators.push_back('n');
isNum = false;
}
while (sideStack.back() != '(') { // translate operation
if (sideStack.size() == 1) { // '(' wasn't found =(
cerr<<"Parsing error - lost left bracket"<<endl;
res.flag = 'e';
return res;
break;
}
res.operators.push_back(sideStack.back());
sideStack.pop_back();
}
sideStack.pop_back();
break;
case 'q': // q for quit
res.flag = 'q';
case '.': // Just cruth for test. Don't need now
case '\n': // End of input
if (isNum) {
res.numbers.push_back(tempNum);
res.operators.push_back('n');
isNum = false;
}
endOfStream = true;
break;
case ' ': // Ignoring all spaces
break;
default: // Unexcepted symbol
cerr<<"Parsing error - wrong symbol ('"<<nextChar<<"')"<<endl;
}
}
while (!sideStack.empty()) { // End of translation
res.operators.push_back(sideStack.back());
sideStack.pop_back();
}
if (bracketsBalance != 0) {
cerr<<"Parsing error - brackets are not balanced."<<endl;
res.flag = 'e';
}
return res;
};
long long calculate (internalExpressionFormat exp) { // calculating exp. and gives an answer
list <long long>::iterator itNum = exp.numbers.begin();
list <char>::iterator itOp = exp.operators.begin();
itOp++;
long long a,b;
char oper;
if (exp.flag != 'n') {
cerr<<"Something went wrong, calculation aborting"<<endl;
return 0;
}
if (exp.operators.size() == 1) { // There is only one operator in expression. It is stop.
cout<<"Nothing to do";
return 0;
}
while (exp.operators.size() != 2) {
while (*itOp == 'n') { // Find first sign
itOp++;
itNum++;
}
oper = *itOp; // Remember this sign
if (oper == 1) { // End while more then 2 (n1) operators? So bad.
cerr<<"Calculating error - too many operands"<<endl;
return 0;
}
itOp--;
if ((itOp == exp.operators.begin())||(itNum == exp.numbers.begin())) {
cerr<<"Calculating error - not enough operands"<<endl;
return 0;
}
itOp--;
itOp = exp.operators.erase(itOp);
itOp++;
itOp = exp.operators.erase(itOp); // replasing nn+ in exp with n
itNum--;
a = *itNum; // remember operands
itNum = exp.numbers.erase(itNum); // and replasing one of them with result
b = *itNum;
switch (oper) { // calculating
case '+':
*itNum = a+b;
break;
case '-':
*itNum = a-b;
break;
case '*':
*itNum = a*b;
break;
case '/':
if (b == 0) {
cerr<<"Calculating error - division by zero"<<endl;
return 0;
}
else {
*itNum = a/b;
}
break;
case '%':
if (b == 0) {
cerr<<"Calculating error - division by zero"<<endl;
return 0;
}
else {
*itNum = a%b;
}
break;
}
}
return exp.numbers.front();
}
int main (int argc, const char* argv[]) {
// Don't panic, it is just text 'How to'
string help = "This is calculator. It can calculate expression with decimal numbers, operations + - * / % and brackets.\nUsage: μcalc <command> [expression]\n\nCommands:\n\to - One expression\n\tm - More than one expressions\n\t (enter q for quit)\n\tc - Get expression from arguments of command line // sadly, but don't working now\n\t (for correct work you must separate operands and operators (including brackets) with whitespaces)\n\th - print help",
operators = "Operators:\n\t+ addition\n\t- subtraction\n\t* multiplication\n\t/ integer division (round down)\n\t% modulo\n\t( and ) change priority\n\n* / % have higher priority than + -, but you can use brackets.",
wrong = "For help type \"μcalc h\"",
hello = "Print expression and press Enter";
switch (argc) {
case 1: { // No command - print help and exit
cout<<help<<endl;
break;
}
case 2: { // It is a 'h', 'o' or 'm'
switch (argv[1][0]) {
case 'h': { // h - print full help and exit
cout<<help<<endl<<'\n'<<operators<<endl;
break;
}
case 'o': { // o - get expression from stdin and give a answer throgh stdout
cout<<hello<<endl<<"> ";
cout<<"\t= "<<calculate(readExp())<<endl;
break;
}
case 'm': { // m - not one exp.
cout<<hello<<endl;
bool quitFlag = false;
do {
cout<<"> "; // get next input
internalExpressionFormat tempExpForm = readExp(); // from stdit and
switch (tempExpForm.flag) { // parce it
case 'q': { // q for quit
quitFlag = true;
break;
}
case 'e': { // catch a error
cout<<"You give wrong expression! Try again, if you want. "<<wrong<<endl;
break;
}
case 'n': { // It is right exp., so calculate it
cout<<"\t= "<<calculate(tempExpForm)<<endl;
break;
}
default: {
cerr<<"Unknown error.";
cout<<"Something went wrong..."<<endl;
break;
}
}
} while (!quitFlag);
break;
}
default: { // any other input is wrong
cout<<"Wrong argument ("<<argv[1]<<")! "<<wrong<<endl;
break;
}
}
break;
}
default: { // 3+ args; It is must be "c <expression>" (like "calc c ( 5 + 17 ) % 7"
if (argv[1][0] != 'c') {
cout<<"Wrong arguments ('"<<argv[1]<<"')! "<<wrong<<endl;
}
else {
// Getting expression
internalExpressionFormat expFromCL;
list <char> sideStack;
sideStack.clear();
sideStack.push_back(1);
for (int i = 2; i < argc; i++) { // Translate to postfix form
string tempArg = argv[i];
if (tempArg.size() == 1) {
switch (argv[i][0]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
expFromCL.numbers.push_back(tempArg[0]-'0');
expFromCL.operators.push_back('n');
break;
case '(':
sideStack.push_back('(');
break;
case ')':
while (sideStack.back() != '(') {
if (sideStack.size() == 1) {
expFromCL.flag = 'e';
break;
}
expFromCL.operators.push_back(sideStack.back());
sideStack.pop_back();
}
sideStack.pop_back();
break;
case '+':
case '-':
case '*':
case '/':
case '%':
if (!higherPriority(tempArg[0],sideStack.back())) {
expFromCL.operators.push_back(sideStack.back());
sideStack.pop_back();
}
sideStack.push_back(tempArg[0]);
break;
default:
cerr<<"Wrong input - '"<<tempArg[0]<<"'! It must was + - * / % or 0..9."<<endl;
cout<<"I can't parse "<<tempArg[0]<<endl;
}
}
else {
try {
long long t = stoll(argv[i]);
expFromCL.numbers.push_back(t);
expFromCL.operators.push_back('n');
}
catch (const out_of_range& e) {
cerr<<"Out of range: "<<e.what()<<endl;
cout<<"You gave too big number. I can't correct handle this, so programm now suicide."<<endl;
exit(0);
}
catch (const invalid_argument& e) {
cerr<<"Not a valid number: "<<e.what()<<endl;
exit(0);
}
}
}
while (!sideStack.empty()) {
expFromCL.operators.push_back(sideStack.back());
sideStack.pop_back();
}
if (expFromCL.flag == 'n') {
cout<<"\t= "<<calculate(expFromCL)<<endl;
}
else {
cout<<"Input wasn't parsed."<<endl;
}
}
}
}
return 0;
}