forked from 2unaa/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
183 lines (171 loc) · 4.61 KB
/
calculator.cpp
File metadata and controls
183 lines (171 loc) · 4.61 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
#include <iostream>
#include "stack_L_T.h"
using namespace std;
int priority(char ch);//function to get an operator and return its priority number
bool isOperand(char ch);//function to check if the token is an operand
bool isOperator(char ch);//function to check if the token is an operator
int charToInt(char ch); //convert char to int
string infixToPost(const string infix);//function to convert from infix to postfix and returns postfix
double evaluation(const string postfix);//function to evaluate postfix and returns the answer
int charToInt(char ch); //function to convert char to int
double doMath(double operand1, char opt, double operand2);//to evaluate the postfix expression and return the number
int main()
{
string infix;
// cout << "Enter infix : ";
cin >> infix;
try
{
string postfix = infixToPost(infix); // convert infix to postfix
cout << postfix << endl;
cout << evaluation(postfix) << endl; //does the math of the postfix
}
catch(Stack<char>::Underflow) //if pop is underflow
{
cout << "The expression is in a wrong format" << endl;
}
catch(Stack<double>::Underflow) //if pop is underflow
{
cout << "The expression is in a wrong format" << endl;
}
catch(const char* msg) //msg in evaluation function
{
cout << msg << endl;
}
}
//checks if it is an operand
bool isOperand(char ch)
{
if(ch >= '0' && ch <= '9')
return true;
else
return false;
}
//checks if it is an operator
bool isOperator(char ch)
{
if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
return true;
else
return false;
}
//sets the prioriy order
//* and / higher than + and -
int priority(char ch)
{
switch(ch)
{
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
}
}
//makes char to int
int charToInt(char ch)
{
return ch - 48;
}
//change the infix to postfix
string infixToPost(const string expr)
{
Stack <char> s;
string postfix;
for(int i = 0; i < expr.length(); i++) //goes through the expression
{
if(isOperand(expr[i])) //if operand
{
postfix+=expr[i];
}
else if(isOperator(expr[i])) //if operator
{
while(!s.empty() && isOperator(s.getTop()) && priority(s.getTop()) >= priority(expr[i])) //need to make sure its not empty, priority is set and its an operator at the top
{
postfix += s.getTop(); //add it to the postfix
s.pop();// pop top
}
s.push(expr[i]); //push it to the stack
}
else if(expr[i] == '(') //if its a (
{
s.push(expr[i]); //push it to the stack
}
else if(expr[i] == ')') //if its a )
{
while (s.getTop() != '(') //get the top til it hits (
{
postfix += s.getTop(); //add it to the postfix
s.pop(); //pop top
}
s.pop(); //pops the ( aswell
}
else
throw "The expression is in a wrong format";
}
while(!s.empty()) //gets the last one
{
postfix += s.getTop(); //add it to the postfix
s.pop(); //pop top
}
return postfix; //return the postfix expression
}
//evaluating the postfix expression and getting the answer
double evaluation(const string postfix)
{
Stack<double> s;
double answer, n1, n2, current;
for(int i = 0; i < postfix.length(); i++) //go through entire postfix expression
{
if(isOperand(postfix[i])) //if it is an operand
s.push(charToInt(postfix[i])); //push to stack and convert to int
else if(isOperator(postfix[i])) //if operator
{
if(!s.empty()) //check if stack is empty
{
n1 = s.getTop();//get top value
s.pop();//pop top
if(!s.empty()) //if its still not empty
{
n2 = s.getTop(); //get the top value
s.pop(); //pop top
current = doMath(n2, postfix[i], n1); //get the answer of the last two popped and do the math and put that into current
s.push(current); //push current into stack
}
else //if empty throw
throw "The expression is in a wrong format";
}
else //if empty throw
throw "The expression is in a wrong format";
}
else //if empty throw
throw "The expression is in a wrong format";
}
if(!s.empty())// if not empty
{
answer = s.getTop(); //whatever is left is the answer
s.pop(); //pop it
if(!s.empty()) //if its empty, throw
throw "The expression is in a wrong format";
else
return answer; //return answer
}
}
//does the math
double doMath(double operand1, char opt, double operand2)
{
switch(opt)
{
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
return operand1 / operand2;
}
}