-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
257 lines (235 loc) · 6.32 KB
/
main.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
// Matthew Lacey
// CSCI 61 HW01
#include <iostream>
#include <stack> // Provides the stack template class
#include <string>
using namespace std;
// Function Prototypes
string prefixToInfix(string prefix);
string infixToPrefix(string infixS);
string reverseStr(string input);
int pemdas(string input);
bool isOperator(string symbol);
string message(int flag);
void menu();
void printStack(stack<string> s); // for debugging
// Define Main Function
int main() {
menu();
return 0;
}
// Convert Infix Notation to Prefix
string infixToPrefix(string infixS) {
stack<string> infix;
infixS = reverseStr(infixS);
for (char &c: infixS){
if(c != ' ')
infix.push(string(1, c));
}
string prefix; // string to contain result (input equation in infix notation)
stack<string> ops; // stack to hold operators
stack<string> buffer; // stack to assist in the formation of the prefix equation
//Loops for as many characters are in the input infix equation
while(!infix.empty()){
// if the current character is not an operator, assume it is
// an operand and add to buffer stack
if(!isOperator(infix.top())){
string num = infix.top();
infix.pop();
while(!infix.empty()){
if(!isOperator(infix.top())){
num = infix.top() + num;
infix.pop();
}
else
break;
}
buffer.push(num);
}
else if ( infix.top() == "(" || ops.empty() || pemdas(infix.top()) > pemdas(ops.top()) ){
ops.push(infix.top());
infix.pop();
}
else if ( infix.top() == ")"){
while(ops.top() != "("){
/* the following six lines of code is repeated throughout
this function. (because nested functions arent allowed
for some reason) They take the last two "blocks" being
operated on by the operator from the buffer stack and
flip them around. This ensures that something like
C / D translates to /CD instead of /DC */
string operand1 = buffer.top();
buffer.pop();
string operand2 = buffer.top();
buffer.pop();
buffer.push(operand1 + " " + operand2 + " " + ops.top());
ops.pop();
}
ops.pop(); // remove the '(' from the stack
infix.pop();
}
else if ( pemdas(infix.top()) <= pemdas(ops.top()) ){
while(!ops.empty() && pemdas(infix.top()) <= pemdas(ops.top())){
string operand1 = buffer.top();
buffer.pop();
string operand2 = buffer.top();
buffer.pop();
buffer.push(operand1 + " " + operand2 + " " + ops.top());
ops.pop();
}
ops.push(infix.top()); // push current char onto ops stack
infix.pop();
}
}
while(!ops.empty()){
string operand1 = buffer.top();
buffer.pop();
string operand2 = buffer.top();
buffer.pop();
buffer.push(operand1 + " " + operand2 + " " + ops.top());
ops.pop();
}
prefix = reverseStr(buffer.top());
return prefix;
}
// Convert Prefix Notation to Infix
string prefixToInfix(string prefixS) {
// reverse input so operators come after operands
stack<string> prefix;
for (char &c: prefixS){
prefix.push(string(1, c));
}
string infix; // final infix notation string returned by function
stack<string> buffer; // stack to help in formation of final equation
while(!prefix.empty()){
// if the current character is not an operator, assume it is
// an operand and add to buffer stack
if(!isOperator(prefix.top())){
string num;
while(!prefix.empty()){
if(prefix.top().compare(" ") == 0){
prefix.pop();
break;
}
else if(!isOperator(prefix.top())){
num = prefix.top() + num;
prefix.pop();
}
else
break;
}
buffer.push(num);
} else {
auto operand1 = buffer.top();
buffer.pop();
auto operand2 = buffer.top();
buffer.pop();
string result = operand1 + prefix.top() + operand2;
prefix.pop();
if (!prefix.empty()){
buffer.push("(" + result + ")");
while(!prefix.empty()){
if(prefix.top().compare(" ") == 0){
prefix.pop();
}
else
break;
}
}
else
buffer.push(result);
}
}
infix = buffer.top();
return infix;
}
// Reverses content of a String
string reverseStr(string input) {
string output = input;
for(int i = 0; i < input.length()/2; i++)
swap(output[i], output[input.length()-i-1]);
return output;
}
// Assigns Order of Operations (Not including parenthesis)
int pemdas(string input) {
switch(input[0]) {
case '+':
case '-':
return 1;
break;
case '*':
case '/':
return 2;
break;
case '^':
return 3;
break;
default:
return -1;
break;
}
}
// Checks to see if a character is a mathematical operator
bool isOperator(string symbol) {
switch(symbol[0]) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return true;
default:
return false;
// returns false if the symbol is other than given above
}
}
string message(int flag){
switch(flag){
case 1:
return "********************************************\n Welcome to the Infix/Prefix Converter!\n\n i: input equation in infix notation\n and get equation in prefix notation\n p: input equation in prefix notation\n and get equation in infix notation\n h: reprint this message\n q: exit program\n********************************************\n\n>> ";
case 2:
return "Enter Infix Equation >> ";
case 3:
return "Enter Prefix Equation >> ";
default:
return "ERROR";
}
}
void menu(){
char input = 'h';
string inputS;
while(input != 'q'){
switch(input){
case 'i':
cout << message(2);
cin.ignore();
getline(cin, inputS);
cout << infixToPrefix(inputS) << "\n\n>> ";
break;
case 'p':
cout << message(3);
cin.ignore();
getline(cin, inputS);
cout << prefixToInfix(inputS) << "\n\n>> ";
break;
case 'h':
cout << message(1);
break;
case 'q':
break;
default:
cout << "Invalid input. type 'h' for help.\n>> ";
}
cin >> input;
}
}
void printStack(stack<string> s){
cout << "---------\n";
while(!s.empty()){
cout << s.top() << endl;
s.pop();
}
cout << "---------\n";
}