-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpresionEvaluation.cpp
151 lines (134 loc) · 2.76 KB
/
ExpresionEvaluation.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
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
class Exp {
public:
void addExp(const string& exp) {
expresions.push_back(exp);
}
void addExp(float exp) {
expresions.push_back(to_string(exp));
}
void ConvertToPostfix() {
Exp temp;
stack<string> exp_stack;
for (const auto& exp:expresions) {
if (isOperator(exp)) {
if (exp == ")") {
while (exp_stack.top() != "(") {
temp.addExp(exp_stack.top());
exp_stack.pop();
}
exp_stack.pop();
}
else {
if (!exp_stack.empty()) {
while (!exp_stack.empty() && compare(exp[0], exp_stack.top()[0]) == 1) {
string exp = exp_stack.top();
temp.addExp(exp);
exp_stack.pop();
}
}
exp_stack.push(exp);
}
}
else {
temp.addExp(exp);
}
}
while (!exp_stack.empty()) {
temp.addExp(exp_stack.top());
exp_stack.pop();
}
*this=temp;
}
float executeOperation(float n1, float n2, string op) {
if (op == "^")
return pow(n1, n2);
else if (op == "*")
return n1 * n2;
else if (op == "/")
return n1 / n2;
else if (op == "+")
return n1 + n2;
else if (op == "-")
return n1 - n2;
}
float eval() {
stack<float> result;
for (const auto& exp :expresions) {
if (isOperator(exp)) {
float op1 = result.top();
result.pop();
float op2 = result.top();
result.pop();
result.push(executeOperation(op2, op1, exp));
}
else {
result.push(convertToNumber(exp));
}
}
return result.top();
}
void print() {
for (const auto& exp:expresions) {
cout << exp<<' ';
}
cout << endl;
}
private:
vector<string> expresions;
int index = 0;
const char* operators = "-= */ ^";
string next() { this->index++; return expresions[this->index]; }
int reachedEnd() { return (this->index == expresions.size() - 1); }
int refresh() { this->index = 0; }
bool isOperator(string s) {
if (s.size() > 1 || (s != "/" && s != "*" && s != "^" && s != "+" && s != "-" && s!="("&& s!=")")) {
return false;
}
return true;
}
float convertToNumber(string s) {
return stof(s.c_str());
}
int compare(char c1,char c2) {
int index1 = -1, index2 = -1;
for (int i = 0; i < strlen(operators); i++) {
if (c1 == operators[i]) {
index1 = i;
}
if (c2 ==operators[i])
index2 = i;
}
if (index1 > index2 && abs(index1 - index2) != 1)
return -1;
else if (index2 > index1 && abs(index1 - index2) != 1)
return 1;
else
return 0;
}
};
int main()
{
string s;
string temp;
getline(cin, s);
Exp exps;
for (const auto& c : s) {
if (c == ' ') {
exps.addExp(temp);
temp.clear();
}
else {
temp.push_back(c);
}
}
exps.addExp(temp);
temp.clear();
exps.ConvertToPostfix();
cout << exps.eval();
}