Reputation: 11
I'm doing this code for a project to evaluate an algorithmic equation, if it doesn't have a "a" in the string of the algorithmic equation I'm given, I'm supposed to return the value and return false. If it does have "a" I'm supposed to return value = 0 and return true. This is all being done a in a bool function.
#include <string>
#include <sstream>
bool Evaluate(const string& expression, int& value){
int a;
int b;
int c;
int d;
int e;
string valueA;
string valueB;
string valueC;
string valueD;
string valueE;
int y;
int z;
unsigned int i;
stringstream x;
int szz;
string alg;
if(!expression.find('a')) {
szz = expression.size();
alg = expression.copy(0, szz);
for (i = 0; i <= szz; ++i) {
alg.find_first_of('*/%');
if (isdigit(alg[i] - '*')) {
a = alg.find('*');
y = a - 1;
z = a + 1;
valueA = alg.substr(y, z);
a = stoi(valueA);
x << a;
valueA = x.str();
alg.replace(y, 2, valueA);
}
if (isdigit(alg[i] - '/')) {
b = alg.find('/');
y = b - 1;
z = b + 1;
valueB = alg.substr(y, z);
b = stoi(valueB);
x << b;
valueB = x.str();
alg.replace(y, 2, valueB);
}
value = stoi(alg);
return false;
}
else{
value = 0;
return true;
}
}
I'm expecting to get the correct values and the correct true/false. Currently if there's an a in the equation I get the result correct as 0, but not the evaluate result (true/false). The opposite happens if there's no a in the equation, I get the evaluate result correct as false but not the actual correct value.
Upvotes: 0
Views: 83
Reputation: 1714
Your algorithm does a lot more than what you write. The goal, apparently, is not to look for a
but rather to evaluate entire math expressions.
Furthermore, your algorithm is flawed in many ways, I just point out some here:
The if
in
string alg;
if(!alg.find('a')) {
is always true since there never is an 'a' in alg
since alg
is created empty just the line before.
This does just copy an entire string
szz = expression.size();
alg = expression.copy(0, szz);
and is 100% equivalent to string alg = expression;
You don't need a for loop in
for (i = 0; i <= szz; ++i) {
alg.find_first_of('*/%');
since the find_first_of
does what it advertises and already loops over the string itself.
You just discard the return value of alg.find_first_of('*/%');
. You want to write int operator_position = alg.find_first_of('*/%');
, then you know that at alg[operator_position]
there is one of */%
.
You have several lines of this type
isdigit(alg[i] - '*')
but it appears to be obvious to me that this is very much not what you want. From the logic of your code, I understand you need to know if a sub-string in alg is a digit (or still an expression containing +-*/%). Your statement only acts on a single char
Other potential problems.
You actual problem you describe can be solved easily:
bool Evaluate(const string& expression, int& value){
if (expression.find('a')!=string::npos) {
// whatever it means to "contain the value" ????
return false;
}
value = 0;
return true;
}
But this will serve no reasonable purpose and I am sure you are really just mis-representing the actual problem posed....
Here is what I would infer as an actual related problem and algorithm: Q: evaluate math expressions from a string and return the answer. Thus, "2*6+4-6/2" should return 13. If you want to achieve this, you need iterative function calling of Evaluate
and I would propose double Evaluate(const string& expression)
:
+-
if yes, you iteratively evaluate the two independent parts (using expression.substr(start,length)
) proceeding as well as preceeding the operator_position
(see above for a hint). You then either add or subtract the two results depending on +
or -
. You return the result.*/%
and if yes, you iteratively evaluate the two independent sub-strings proceeding and preceeding the operator_position
. You either multiply, divide or modulo the two values and return the result.number
and you convert the string into a double (std::stod(expression)
) and return it.This iterative algorithm will magically correctly evaluate any math expression.
Upvotes: 2