-
Notifications
You must be signed in to change notification settings - Fork 0
/
EquationSolver.cpp
227 lines (220 loc) · 6.39 KB
/
EquationSolver.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
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
/**********************************************************/
//name : EquationSolver.cpp
//function : ¼ÆË㹫ʽÀà
//copyright :
//author : WSS
//date : 2015-05-06
/**********************************************************/
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include "EquationSolver.h"
#include "Numerics.h"
using namespace EquationHelper;
using namespace Numerics;
using std::size_t;
using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::ios;
typedef EquationSolver ES;
/**
* Private constructor - does nothing.
*/
ES::EquationSolver(){}
/**
* Performs the specified operation against the
* argument strings. The operation is dependant on
* the value of op.
*/
string ES::doOperation(const string& lhs, char op, const string& rhs){
Double bdLhs = lhs;
Double bdRhs = rhs;
Double temp;
switch(op){
case '^':
temp( pow( bdLhs, bdRhs ) );
break;
case '*':
temp( bdLhs * bdRhs );
break;
case '/':
temp( bdLhs / bdRhs );
break;
case '+':
temp( bdLhs + bdRhs );
break;
case '%':
temp( fmod(bdLhs, bdRhs) );
break;
}
return temp.getString();
}
/**
* Returns the string with its enclosing paranthesis
* stripped from it.
*/
void ES::correctedString(string& arg){
size_t pos1 = arg.find_first_of("(");
size_t pos2 = arg.find_last_of(")");
if(pos1 >= 0 && pos1 < arg.length() && pos2 >= 0 && pos2 <= arg.length())
arg[pos1] = arg[pos2] = ' ';
}
/**
* Remove spaces from the argument string.
*/
void ES::removeSpaces(string& argu){
string temp = "";
for(size_t i = 0; i < argu.length(); i++)
if(argu[i] != ' ')
temp += argu[i];
argu = temp;
}
/**
* The brains of the program.
* Solves expressions by using recursion for complex expressions.
*/
string ES::parse(const string& param){
string expression = param;
correctedString(expression);
removeSpaces(expression);
string finalExpression = "";
bool operatorEncountered = true;
for(size_t i = 0; i < expression.length(); i++){
if(expression[i] == '('){
string placeHolder = "(";
int valuesCounted = 1;
operatorEncountered = false;
for(size_t j = i + 1; valuesCounted != 0; j++){
if(expression[j] == '(')
valuesCounted++;
else if(expression[j] == ')')
valuesCounted--;
placeHolder += expression[j];
}
string evaluatedString = parse(placeHolder);
finalExpression += evaluatedString;
i += (placeHolder.length() - 1);
}else{
if(expression[i] == '-' && operatorEncountered == false)
finalExpression += '+';
finalExpression += expression[i];
if((expression[i] == '+'
|| expression[i] == '/'
|| expression[i] == '^'
|| expression[i] == '*'
|| expression[i] == '%'
|| expression[i] == '-'))
operatorEncountered = true;
else if(expression[i] != ' ')
operatorEncountered = false;
}
}
removeSpaces(finalExpression);
string perfectExpression = "";
for(size_t i = 0; i < finalExpression.length(); i++){
if((i + 1) < finalExpression.length())
if(finalExpression[i] == '-' && finalExpression[i + 1] == '-')
i += 2;
perfectExpression += finalExpression[i];
}
finalExpression = perfectExpression;
vector<string> totalNumbers;
vector<char> totalOperations;
//cout << finalExpression << endl;
for(size_t i = 0; i < finalExpression.length(); i++){
if(finalExpression[i] >= '0' && finalExpression[i] <= '9'
|| finalExpression[i] == '-' || finalExpression[i] == '.'){
string temp = ""; //
for(size_t j = i; j < finalExpression.length(); j++){
if(finalExpression[j] >= '0' && finalExpression[j] <= '9'
|| finalExpression[j] == '-' || finalExpression[j] == '.'){
temp += finalExpression[j];
}else break;
}
totalNumbers.push_back(temp);
i += temp.length() == 0 ? 0 : (temp.length() - 1);
}else if(finalExpression[i] == '*'
|| finalExpression[i] == '/'
|| finalExpression[i] == '^'
|| finalExpression[i] == '+'
|| finalExpression[i] == '%'
){
totalOperations.push_back(finalExpression[i]);
}
}
ES::calculate(totalNumbers, totalOperations, "^");
ES::calculate(totalNumbers, totalOperations, "*/%");
ES::calculate(totalNumbers, totalOperations, "+");
return totalNumbers[0];
}
/**
* Calculates the numbers in the first vector using the operands in the 2nd vector,
* based on the expressions allowed which are determined by the string argument.
*/
void ES::calculate(vector<string>& totalNumbers, vector<char>& totalOperations,
const string& arg){
for(int i = 0; i < static_cast<int>(totalOperations.size()); i++){
if( arg.find(totalOperations[i]) != arg.npos){
totalNumbers[i] = doOperation(totalNumbers[i], totalOperations[i], totalNumbers[i + 1]);
size_t oldNumberLength = totalNumbers.size();
size_t oldOperatorLength = totalOperations.size();
size_t nextNumberLength = oldNumberLength - 1;
size_t nextOperatorLength = oldOperatorLength - 1;
size_t sCount = 0;
size_t oCount = 0;
vector<string> temp1 ( nextNumberLength );
vector<char> temp2 ( nextOperatorLength );
for(size_t j = 0; j < oldNumberLength; j++){
if(j != static_cast<int>(i + 1)){
temp1[sCount++] = totalNumbers[j];
}
if(j != i && j < oldOperatorLength){
temp2[oCount++] = totalOperations[j];
}
}
totalNumbers = temp1;
totalOperations = temp2;
i--;
}
}
}
/**
* Returns true if the equation is solvable (not really),
* returns false otherwise.
*
* This function is truly a misnomer, because more restrictions
* should be put in place.
*/
bool ES::isSolvable(const string& eq){
int paranthesisCount = 0;
for(size_t i = 0; i < eq.length(); i++){
if(eq[i] == '(')
paranthesisCount++;
else if(eq[i] == ')')
paranthesisCount--;
if(paranthesisCount < 0)
return false;
}
return paranthesisCount == 0;
}
/**
* An attempt to solve a string-expression, given
* a precision value.
*/
string ES::solve(const string& eq, int prec){
if(isSolvable(eq)){
stringstream ss (stringstream::in | stringstream::out);
//cout << eq << endl;
string value;
value += '(';
value += eq;
value += ')';
ss.setf(0, ios::floatfield);
ss.precision(prec);
ss << parse(value);
return ss.str();
}else return "";
}