-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathin2post.cpp
More file actions
498 lines (392 loc) · 16.2 KB
/
Copy pathin2post.cpp
File metadata and controls
498 lines (392 loc) · 16.2 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <iostream>
#include <cstdlib>
#include <vector>
#include <math.h>
#include <string>
#include <sstream>
#include "stack.h"
#include "string.h"
#include "opnum.h"
using namespace std;
using namespace cop4530;
int main(int argc, char **argv)
{
// Returns the precedence of an input
int precedence(const char & inputChar);
// Calculates current operation given two operands and the operator
float postfixCalc(const float & topFloat, const float & nextFloat, const string & tempChar);
int retval = SYM_NULL;
set_input(argc, argv);
Stack<char> operators; // Stores the operators for infix to postfix conversion
Stack<float> operands; // Stack to push and pop the operands for postfix evaluation
string postfix; // Stores the postfix expression as a string to be evauluated
string tempInfix; // Stores the infix expression to be printed if there is an error
// These are for error checking
bool newLine; // Flag for a new line, meaning the next expression.
bool error; // Keeps track if an error occurs
bool typeError; // Tracks if the expression uses an invalid operand type
int matchingPar; // Tracker for finding a matching '(' for ')'
// These are for consecutive number tracking
string consecNum; // used to store and track consecutive numbers in the infix expression
int numOfConsecNum; // Used to know when to print the consecutive numbers
string consecOp; // used to store and track consecutive numbers in the infix expression
int numOfConsecOp; // Used to know when to print the consecutive operators
// Loop for converting each expression to postfix and evaluating if possible.
do{
char * opnum; // Stores the current opnum
postfix = ""; // Resets the postfix expression tracker
tempInfix = ""; // Resets the infix expression tracker
operators.clear(); // Clears the operator stack
operands.clear(); // Cleares the operand stack
newLine = false; // Flag to find the end of an expression
error = false; // Flag for errors
typeError = false; // Flag for invlaid operand types
matchingPar = 0; // Tracker for finding a matching '(' for ')'
consecNum = ""; // Resets consecutive number tracker
numOfConsecNum = 0;
consecOp = ""; // Resets consecutive operator tracker
numOfConsecOp = 0;
/* Converts a infix expression to a postfix expression and
stores it in a string. This also checks for errors, if there
is an error the error is printed and the conversion continues.
Once the conversion is complete it will either continue to the
postfix evaluation or if there was an error the initial infix
expression is printed.*/
while(newLine == false)
{
opnum = get_opnum(&retval);
// Exit statment
if(retval == SYM_NULL)
break;
// If not a new line, add to current infix expression.
if(retval != SYM_NEWLINE && retval != SYM_INVAL)
{
tempInfix = tempInfix + opnum + " ";
}
// Prints the consecutive operands
if(retval >= SYM_ADD && retval <= SYM_DIV || retval == SYM_NEWLINE)
{
// If this is an operator at the end of the expression
if(numOfConsecOp == 1 && retval == SYM_NEWLINE)
break;
// If this is the second operator in a row there is an error
if(consecOp != "")
{
error = true;
consecOp = consecOp + opnum;
}
// Adds to the consecutive operator count
numOfConsecOp++;
if(numOfConsecNum > 1)
cout << "Consecutive Numbers: " << consecNum << endl; // Prints the consecutive numbers
// Resets the consecutive number trackers
consecNum = "";
numOfConsecNum = 0;
}
// Prints consecutive operators
if(retval >= SYM_NAME && retval <= SYM_FLOAT || retval == SYM_NEWLINE)
{
// If this is the second number in a row there is an error
if(consecNum != "")
{
error = true;
consecNum = consecNum + opnum;
}
numOfConsecNum++;
if(numOfConsecOp > 1)
cout << "Consecutive Opcodes: " << consecOp << endl; // Prints the consecutive operators
// Resets the consecutive operator trackers
consecOp = "";
numOfConsecOp = 0;
}
if (retval) {
switch (retval)
{
case SYM_NAME:
typeError = true;
// Checks for consecustive operands (already caught)
if(consecNum != "")
break;
consecNum = consecNum + opnum + " "; // Adds new consecutive number to tracker
postfix = postfix + opnum + " "; // If operand pass it to string
break;
case SYM_INTEG:
// Checks for consecustive operands (already caught)
if(consecNum != "")
break;
consecNum = consecNum + opnum + " "; // Adds new consecutive number to tracker
postfix = postfix + opnum + " "; // If operand pass it to string
break;
case SYM_FLOAT:
// Checks for consecustive operands (already caught)
if(consecNum != "")
break;
consecNum = consecNum + opnum + " "; // Adds new consecutive number to tracker
postfix = postfix + opnum + " "; // If operand pass it to string
break;
case SYM_ADD:
// Error if consecutive operators
if(consecOp != "")
break;
// If stack is not empty check the precedence and push/pop
// Else the stack is empty and you should push to stack.
if(operators.empty() == false)
{
while(precedence(*opnum) <= precedence(operators.top()))
{
postfix = postfix + operators.top() + " ";
operators.pop();
}
}
operators.push(*opnum); // Pushes operator onto stack
consecOp = consecOp + opnum + " "; // Adds new consecutive operator to tracker
break;
case SYM_SUB:
// Error if consecutive operators
if(consecOp != "")
break;
// If stack is not empty check the precedence and push/pop
// Else the stack is empty and you should push to stack.
if(operators.empty() == false)
{
while(precedence(*opnum) <= precedence(operators.top()))
{
postfix = postfix + operators.top() + " ";
operators.pop();
}
}
operators.push(*opnum); // Pushes operator onto stack
consecOp = consecOp + opnum + " "; // Adds new consecutive operator to tracker
break;
case SYM_MUL:
// Error if consecutive operators
if(consecOp != "")
break;
// If stack is not empty check the precedence and push/pop
// Else the stack is empty and you should push to stack.
if(operators.empty() == false)
{
while(precedence(*opnum) <= precedence(operators.top()))
{
postfix = postfix + operators.top() + " ";
operators.pop();
}
}
operators.push(*opnum); // Pushes operator onto stack
consecOp = consecOp + opnum + " "; // Adds new consecutive operator to tracker
break;
case SYM_DIV:
if(consecOp != "")
break;
// If stack is not empty check the precedence and push/pop
// Else the stack is empty and you should push to stack.
if(operators.empty() == false)
{
while(precedence(*opnum) <= precedence(operators.top()))
{
postfix = postfix + operators.top() + " ";
operators.pop();
}
}
operators.push(*opnum); // Pushes operator onto stack
consecOp = consecOp + opnum + " "; // Adds new consecutive operator to tracker
break;
case SYM_OPEN:
matchingPar++; // Used to check for errors in case SYM_CLOSE
// If the last input was a operand, there is an error
if(consecNum != "")
{
// If there is aready an error ( edge case ")(" )
if(error == true)
{
consecNum = "";
numOfConsecNum = 0;
break;
}
// Sets error to true and prints the error
error = true;
cout << "Number (: " << consecNum << "(" << endl;
consecNum = "";
numOfConsecNum = 0;
break;
}
else
{
// Resets tracker
consecOp = "";
numOfConsecOp = 0;
}
operators.push(*opnum); // Pushes operator onto stack
break;
case SYM_CLOSE:
// If there is not a matching '('
if(matchingPar == 0)
{
error = true;
cout << "Error: cannot find a matching (." << endl;
break;
}
// If the parentheses are empty or has a operator as the last argument
if(consecNum == "")
{
error = true;
cout << "Error: cannot have an opcode or ( before )." << endl;
break;
}
// If stack is not empty check the precedence and push/pop
// Else the stack is empty and you should push to stack.
if(operators.empty() == false)
{
while(precedence(*opnum) <= precedence(operators.top()))
{
// Checks for the opening parentheses to break
if(operators.top() == '(')
{
operators.pop();
break;
}
// Puts the top of the stack into a string and pops it from the stack
postfix = postfix + operators.top() + " ";
operators.pop();
}
}
matchingPar--;
break;
case SYM_INVAL:
// No longer a consecutive operator
consecOp = "";
numOfConsecOp = 0;
// Adds to the consecutive number string
consecNum = consecNum + opnum;
// Saves the '.' into the temp infix string
tempInfix = tempInfix + opnum;
break;
case SYM_NEWLINE:
newLine = true; // Used to check for the end of the expression
break;
default: break;
}
}
}
// Used to exit the program
if(retval == SYM_NULL)
break;
// Checks if there is an opening parentheses still on the stack
if(operators.top() == '(')
{
error = true;
cout << "Error: failed conversion. Deck top=(" << endl;
}
// Prints the last operator on the stack if there is no error.
if(error == false)
{
while(!(operators.empty()))
{
postfix = postfix + operators.top() + " ";
operators.pop();
}
}
// If the expression has invalid operands
if(typeError == true && error == false)
{
cout << postfix << endl;
continue;
}
// If there was an error print the input else, prints the value of the expression
if(error == true)
{
cout << "Found in input:" << endl;
cout << tempInfix << '\n' << endl;
continue;
}
else
{
string tempChar; // Used to store each operand
// Creates a stream of the postfix expression
stringstream tempStream(postfix);
// Goes through each operator or operand seperated by a space
while(tempStream >> tempChar)
{
// Checks if the current element is a operand or operator
if(isdigit(tempChar[0]))
{
// If it is a digit, convert to float and store on operand stack
float tempFloat = stof(tempChar);
operands.push(tempFloat);
}
else
{
// Gets the first operand
float topFloat = operands.top();
operands.pop();
/* If there are more operators than operands
report error and print current total*/
if(operands.empty() == true)
{
error = true;
cout << "Error operator [" << tempChar << "] has only one operand: " << topFloat << endl;
cout << "Found in input:" << endl;
cout << tempInfix << endl;
cout << endl;
break;
}
// Gets the second operand
float nextFloat = operands.top();
operands.pop();
// Pushes the current calculation back on the stack.
operands.push(postfixCalc(topFloat, nextFloat, tempChar));
}
}
// If the postfix evaluation found no errors, print the top of the stack.(answer)
if(error == false)
cout << operands.top() << endl;
}
} while (retval > SYM_NULL);
}
int precedence(const char & inputChar)
{
int preced = 0; // Stores return value
// Sets a precedence for each value
switch(inputChar)
{
case ')': preced = 1; break;
case '(': preced = 2; break;
case '+': preced = 3; break;
case '-': preced = 3; break;
case '*': preced = 4; break;
case '/': preced = 4; break;
default:break;
}
return preced;
}
float postfixCalc(const float & topFloat, const float & nextFloat, const string & tempChar)
{
float combinedFloat = 0;
// If both operands are a float, convert to int and preform operation
// Else do the operation on the floats
if(roundf(topFloat) == topFloat && roundf(nextFloat) == nextFloat)
{
int tempTop = static_cast<int>(topFloat);
int tempNext = static_cast<int>(nextFloat);
if(tempChar == "-")
combinedFloat = tempNext - tempTop;
if(tempChar == "+")
combinedFloat = tempNext + tempTop;
if(tempChar == "*")
combinedFloat = tempNext * tempTop;
if(tempChar == "/")
combinedFloat = tempNext / tempTop;
}
else
{
if(tempChar == "-")
combinedFloat = nextFloat - topFloat;
if(tempChar == "+")
combinedFloat = nextFloat + topFloat;
if(tempChar == "*")
combinedFloat = nextFloat * topFloat;
if(tempChar == "/")
combinedFloat = nextFloat / topFloat;
}
return combinedFloat;
}