-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproj5Base.cpp
More file actions
196 lines (169 loc) · 5.15 KB
/
proj5Base.cpp
File metadata and controls
196 lines (169 loc) · 5.15 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
/**************************************************************/
/* */
/* The Code below this point should NOT need to be modified */
/* for this program. If you feel you must modify the code */
/* below this point, you are probably trying to solve a */
/* more difficult problem that you are being asked to solve */
/* */
/**************************************************************/
#include <cstdio>
#include <cstring>
#include <cctype>
#include "calculator.h"
using namespace std;
void printCommands()
{
printf ("The commands for this program are:\n\n");
printf ("q - to quit the program\n");
printf ("? - to list the accepted commands\n");
printf ("or any infix mathematical expression using operators of (), *, /, +, -\n");
}
/**************************************************************/
/* */
/* The Code above this point should NOT need to be modified */
/* for this program. If you feel you must modify the code */
/* below this point, you are probably trying to solve a */
/* more difficult problem that you are being asked to solve */
/* */
/**************************************************************/
int eval(int x, int op, int y);
void popAndEval(MyStack& digit, MyStack& op);
void processExpression (Token inputToken, TokenReader *tr);
int main(int argc, char *argv[])
{
/***************************************************************/
Token inputToken;
TokenReader tr;
printf ("Starting Expression Evaluation Program\n\n");
printf ("Enter Expression:\n");
inputToken = tr.getNextToken ();
while (inputToken.equalsType(QUIT) == false)
{
/* check first Token on Line of input */
if(inputToken.equalsType(HELP))
{
printCommands();
tr.clearToEoln();
}
else if(inputToken.equalsType(ERROR))
{
printf ("Invalid Input - For a list of valid commands, type ?\n");
tr.clearToEoln();
}
else if(inputToken.equalsType(EOLN))
{
printf ("Blank Line - Do Nothing\n");
/* blank line - do nothing */
}
else
{
processExpression(inputToken, &tr);
}
printf ("Enter Expression:\n");
inputToken = tr.getNextToken ();
}
printf ("Quitting Program\n");
return 0;
}
void processExpression (Token inputToken, TokenReader *tr)
{
/**********************************************/
/* Declare both stack pointers here */
MyStack digitStack;
MyStack opStack;
/* Loop until the expression reaches its End */
while (!inputToken.equalsType(EOLN))
{
/* The expression contain a VALUE */
if (inputToken.equalsType(VALUE))
{
digitStack.push(inputToken.getValue());
}
/* The expression contains an OPERATOR */
else if (inputToken.equalsType(OPERATOR))
{
if (inputToken.getOperator() == '(') {
opStack.push(inputToken.getOperator());
}
else if (inputToken.getOperator() == '+' || inputToken.getOperator() == '-') {
while (!opStack.isEmpty() && (opStack.top() == '+'
|| opStack.top() == '-'
|| opStack.top() == '*'
|| opStack.top() == '/'
|| opStack.top() == '%')) {
popAndEval(digitStack, opStack);
}
opStack.push(inputToken.getOperator());
}
else if (inputToken.getOperator() == '*'
|| inputToken.getOperator() == '/'
|| inputToken.getOperator() == '%') {
while (!opStack.isEmpty() && (opStack.top() == '*'
|| opStack.top() == '/'
|| opStack.top() == '%')) {
popAndEval(digitStack, opStack);
}
opStack.push(inputToken.getOperator());
}
else if (inputToken.getOperator() == ')') {
while (!opStack.isEmpty() && opStack.top() != '(') {
popAndEval(digitStack, opStack);
}
if (opStack.isEmpty()) {
printf("missing '('\n");
}
else {
opStack.pop();
}
}
}
/* get next token from input */
inputToken = tr->getNextToken ();
}
/* The expression has reached its end */
// add code to perform this operation here
while (!opStack.isEmpty()) {
popAndEval(digitStack, opStack);
}
if (digitStack.isEmpty()) {
printf("Invalid expression.\n");
}
else {
// printf("Expression result: %d\n", digitStack.top());
int result = digitStack.top();
digitStack.pop();
if (!digitStack.isEmpty()) {
printf("invalid expression.\n");
}
else {
printf("Expression result: %d\n", result);
}
}
}
void popAndEval(MyStack& digitStack, MyStack& opStack) {
int oper = opStack.top();
opStack.pop();
int v2 = digitStack.top();
digitStack.pop();
int v = digitStack.top();
digitStack.pop();
int v3 = eval(v, oper, v2);
digitStack.push(v3);
}
int eval(int x, int op, int y) {
if (op == '+') {
return x + y;
}
else if (op == '-') {
return x - y;
}
else if (op == '*') {
return x * y;
}
else if (op == '/') {
return x / y;
}
else {
return x % y;
}
}