-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculatorMain.cpp
192 lines (152 loc) · 3.32 KB
/
CalculatorMain.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
#include "Stack.h"
#include "Queue.h"
bool checkIfOp(string value);
bool checkIfOp(char value);
int checkPrecedence(string value);
double doMath(double first, double second, string theOperator);
int main()
{
Stack theStack;
Queue theQueue;
string mathProblem;
int problemLength;
string currentValue;
cout << "Enter math problem: ";
getline(cin, mathProblem);
problemLength = mathProblem.length();
char current;
char next;
string value = "";
string enqueueValue;
// ----------------- Converting Infix to Postfix -----------------------
for (int i = 0; i < problemLength; i++)
{
current = mathProblem[i];
next = (i < problemLength - 1) ? mathProblem[i + 1] : ' ';
if (current != ' ')
value += current;
else
continue;
if (checkIfOp(value))
{
if (value == ")")
{
string pTemp = theStack.Pop();
while (pTemp != "(")
{
theQueue.Enqueue(pTemp, true);
pTemp = theStack.Pop();
}
}
else if (value == "(")
theStack.Push(value, true);
else if (theStack.topPrecedence() >= checkPrecedence(value))
{
enqueueValue = theStack.Pop();
theQueue.Enqueue(enqueueValue, true);
theStack.Push(value, true);
}
else
theStack.Push(value, true);
value = "";
}
else if (next == ' ' || checkIfOp(next))
{
theQueue.Enqueue(value, false);
value = "";
}
}
if (!theStack.isEmpty())
{
while (!theStack.isEmpty())
{
enqueueValue = theStack.Pop();
theQueue.Enqueue(enqueueValue, true);
}
}
cout << "Infix converted to Postfix: ";
theQueue.Print();
// -------------- Evaluating postfix notation to find answer ---------------------
qNodePtr qNode;
string op;
string second;
string first;
double secondConverted;
double firstConverted;
double pushValue;
string stringPushValue;
string answer;
while (!theQueue.isEmpty())
{
qNode = theQueue.DequeueNode();
if (qNode->isOp)
{
op = qNode->value;
second = theStack.Pop();
first = theStack.Pop();
secondConverted = stod(second);
firstConverted = stod(first);
pushValue = doMath(firstConverted, secondConverted, op);
stringPushValue = to_string(pushValue);
theStack.Push(stringPushValue, false);
}
else
{
theStack.Push(qNode->value, false);
}
}
answer = theStack.Pop();
cout << "The answer is: " << answer << endl;
system("PAUSE");
return 0;
}
bool checkIfOp(string value)
{
if (value.length() > 1)
return false;
else if (value == "+" || value == "-" || value == "*" || value == "/"
|| value == "(" || value == ")")
return true;
else
return false;
}
bool checkIfOp(char value)
{
if (value == '+' || value == '-' || value == '*' || value == '/'
|| value == '(' || value == ')')
return true;
else
return false;
}
int checkPrecedence(string value)
{
char symbol = value[0];
switch (symbol) {
case '(': return 2;
break;
case ')': return 2;
break;
case '*': return 4;
break;
case '/': return 4;
break;
case '+': return 3;
break;
case '-': return 3;
break;
default: cout << "Error, operator not found." << endl;
}
}
double doMath(double first, double second, string theOperator)
{
double answer;
if (theOperator == "*")
answer = first * second;
else if (theOperator == "/")
answer = first / second;
else if (theOperator == "+")
answer = first + second;
else
answer = first - second;
return answer;
}