forked from Aloshi/EmulationStation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathExp.cpp
More file actions
163 lines (133 loc) · 2.82 KB
/
MathExp.cpp
File metadata and controls
163 lines (133 loc) · 2.82 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
#include "MathExp.h"
#include <iostream>
#include <sstream>
bool MathExp::isOperator(const char c)
{
if(c == *"+" || c == *"-" || c == *"*" || c == *"/" || c == *"(")
return true;
else
return false;
}
bool MathExp::isRParen(const char c)
{
if(c == *")")
return true;
else
return false;
}
int MathExp::getPrecedence(const char c)
{
if(c == *"(")
return -5;
if(c == *"+" || c == *"-")
return 0;
if(c == *"*" || c == *"/")
return 1;
std::cout << "Error - getPrecedence(): unknown character '" << c << "'\n";
return -1;
}
float MathExp::eval()
{
unsigned int start = 0;
for(unsigned int i = 0; i < mExpression.length(); i++)
{
if(isOperator(mExpression.at(i)))
{
//the string from start to i is an operand, and i is an operator
if(start != i) //if we actually do have an operand
mOperands.push(strToVal(mExpression.substr(start, i - start)));
else
std::cout << "skipping operand, start == i\n";
//now we must decide what to do with the operator
const char op = mExpression.at(i);
if(op != *"(")
{
while(mOperators.size() && getPrecedence(mOperators.top()) >= getPrecedence(op))
{
doNextOperation();
}
}
mOperators.push(op);
start = i + 1;
}else{
if(isRParen(mExpression.at(i)))
{
while(mOperators.top() != *"(")
{
doNextOperation();
}
mOperators.pop();
}
}
}
mOperands.push(strToVal(mExpression.substr(start, mExpression.length() - start)));
while(mOperators.size() > 0)
doNextOperation();
if(mOperands.size() != 1)
{
std::cout << "Error - mOperands.size() = " << mOperands.size() << " at the end of evaluation!\n";
return 0;
}
float final = mOperands.top();
mOperands.pop();
return final;
}
void MathExp::doNextOperation()
{
//pop operator off and apply it, then push the value onto the operand stack
const char top = mOperators.top();
float val = 0;
if(top == *"+")
{
val = mOperands.top();
mOperands.pop();
val += mOperands.top();
mOperands.pop();
}
if(top == *"-")
{
val = mOperands.top();
mOperands.pop();
val = mOperands.top() - val;
mOperands.pop();
}
if(top == *"*")
{
val = mOperands.top();
mOperands.pop();
val *= mOperands.top();
mOperands.pop();
}
if(top == *"/")
{
val = mOperands.top();
mOperands.pop();
val = mOperands.top() / val;
mOperands.pop();
}
mOperands.push(val);
mOperators.pop();
}
void MathExp::setExpression(std::string str)
{
mExpression = str;
}
void MathExp::setVariable(std::string name, float val)
{
mVariables[name] = val;
}
float MathExp::getVariable(std::string name)
{
return mVariables[name];
}
float MathExp::strToVal(std::string str)
{
if(str[0] == *"$")
return getVariable(str.substr(1, str.length() - 1)); //it's a variable!
//it's a value!
std::stringstream stream;
stream << str;
float value;
stream >> value;
return value;
}