Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 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. 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.

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); } if (isdigit(alg[i] - '%')) { c = alg.find('%'); y = c - 1; z = c + 1; valueC = alg.substr(y, z); c = stoi(valueC); x << c; valueC = x.str(); alg.replace(y, 2, valueC); } } for (i = 0; i <= szz; ++i) { alg.find_first_of('-+'); if (isdigit(alg[i] - '+')) { d = alg.find('+'); y = d - 1; z = d + 1; valueD = alg.substr(y, z); d = stoi(valueD); x << d; valueD = x.str(); alg.replace(y, 2, valueD); } if (isdigit(alg[i] - '-')) { e = alg.find('-'); y = e - 1; z = e + 1; valueE = alg.substr(y, 2); e = stoi(valueE); x << d; valueE = x.str(); alg.replace(y, 2, valueE); } szz = alg.size(); } value = stoi(alg); return false; } else{ value = 0; return true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are psychologys main subfields?

Answered: 1 week ago