-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
261 lines (229 loc) · 5.33 KB
/
Main.cpp
File metadata and controls
261 lines (229 loc) · 5.33 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
// Created by Leni Halaapiapi on 10/23/18
// CS_480_Lab_3
#include <iostream>
#include <string>
#include <stack>
using namespace std;
// returns precedence of operators
int precedence(char c)
{
if (c == '^')
{
return 3;
}
else if (c == '*' || c == '/')
{
return 2;
}
else if (c == '+' || c == '-')
{
return 1;
}
else
{
return -1;
}
}
// main function to convert infix expression to postfix
string infixToPostfix(string s)
{
stack<char> workingStack;
workingStack.push('N');
int l = s.length();
string reversePolish;
for (int i = 0; i < l; i++)
{
// if the scanned character is an operand, add it to output string
if ((s[i] >= '0' && s[i] <= '9') || (s[i] == '.'))
{
reversePolish += s[i];
}
// if the scanned character is a '(', push it to the stack
else if (s[i] == '(')
{
workingStack.push('(');
}
// if the scanned character is a ')', pop to output string from the stack until '(' is encountered.
else if (s[i] == ')')
{
while (workingStack.top() != 'N' && workingStack.top() != '(')
{
char c = workingStack.top();
workingStack.pop();
reversePolish += c;
}
if (workingStack.top() == '(')
{
char c = workingStack.top();
workingStack.pop();
}
}
// if an operator is scanned
else
{
reversePolish += ",";
while (workingStack.top() != 'N' && precedence(s[i]) <= precedence(workingStack.top()))
{
char c = workingStack.top();
workingStack.pop();
reversePolish += c;
}
workingStack.push(s[i]);
}
}
// pop all remaining elements from stack
while (workingStack.top() != 'N')
{
char c = workingStack.top();
workingStack.pop();
reversePolish += c;
}
size_t find = reversePolish.find_last_of("0123456789");
reversePolish.insert(find + 1, ",");
return reversePolish;
}
float postfixToSolution(string rpn)
{
// values for stack operations
stack<double> finalStack;
double a, b, c;
string temp, perm;
while (!rpn.empty())
{
// finds first occurrence of operators for easy tokenizing
size_t found = rpn.find_first_of(",^*/+-()");
// this tokenizes all the operands and pushes them on the stack
if (rpn[found] == ',')
{
temp = rpn.substr(0, found);
perm = rpn.substr(found + 1, rpn.length() - found);
if (temp.empty())
{
cout << "Invalid character or space. Goodbye.\n";
_Exit(EXIT_FAILURE);
}
a = stod(temp);
finalStack.push(a);
a = 0;
rpn = perm;
}
// this does the exponentiation
else if (rpn[found] == '^')
{
perm = rpn.substr(found + 1, rpn.length() - found);
b = finalStack.top();
finalStack.pop();
a = finalStack.top();
finalStack.pop();
c = pow(a, b);
finalStack.push(c);
rpn = perm;
a, b, c = 0;
}
// multiplication
else if (rpn[found] == '*')
{
perm = rpn.substr(found + 1, rpn.length() - found);
b = finalStack.top();
finalStack.pop();
a = finalStack.top();
finalStack.pop();
c = (a * b);
finalStack.push(c);
rpn = perm;
a, b, c = 0;
}
// division
else if (rpn[found] == '/')
{
perm = rpn.substr(found + 1, rpn.length() - found);
b = finalStack.top();
finalStack.pop();
a = finalStack.top();
finalStack.pop();
if (b == 0) {
cout << "Invalid input. Division by 0 is forbidden. Goodbye.\n";
_Exit(EXIT_FAILURE);
}
c = (a / b);
finalStack.push(c);
rpn = perm;
a, b, c = 0;
}
// addition
else if (rpn[found] == '+')
{
perm = rpn.substr(found + 1, rpn.length() - found);
b = finalStack.top();
finalStack.pop();
a = finalStack.top();
finalStack.pop();
c = (a + b);
finalStack.push(c);
rpn = perm;
a, b, c = 0;
}
// subtraction
else if (rpn[found] == '-')
{
perm = rpn.substr(found + 1, rpn.length() - found);
b = finalStack.top();
finalStack.pop();
a = finalStack.top();
finalStack.pop();
c = (a - b);
finalStack.push(c);
rpn = perm;
a, b, c = 0;
}
// exits if there are other characters which should not be in the expression
else
{
_Exit(EXIT_FAILURE);
}
}
return finalStack.top();
}
int main()
{
// reads string from user
string str, rpn;
cout << "Please enter a mathematical expression.\n";
getline(cin, str);
// checks string for any values not specified
size_t found = str.find_first_not_of("0123456789.+-/*^()");
// if a space or invalid character is found, terminates program
if (found != string::npos)
{
cout << "Invalid character or space. Goodbye.\n";
return 0;
}
// tests for user trying to multiply without * operator
for (int i = 0; i < 10; i++)
{
string test, test1, test2;
test = to_string(i);
test1 = test + '(';
test2 = ')' + test;
if (str.find(")(") != string::npos || str.find(test1) != string::npos || str.find(test2) != string::npos)
{
cout << "If you wish to multiply, use the * operator. Goodbye.\n";
return 0;
}
}
// placeholder for the solution
double solution = 0;
// calls the infix to postfix function which returns the expression in reverse polish notation
rpn = infixToPostfix(str);
// checks if rpn is empty
if (rpn.empty())
{
cout << "Invalid character or space. Goodbye.\n";
return 0;
}
cout << "The Reverse Polish Notation for the expression: " << str << " is: " << rpn << "\n";
// solution is the rpn evaluated
solution = postfixToSolution(rpn);
cout << "The solution for the expression: " << str << " is: " << solution << "\n";
return 0;
}