Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the following code by a Chegg Expert below, Can you further fix, in addition to further implement the following features: - When user is

Given the following code by a Chegg Expert below, Can you further fix, in addition to further implement the following features:

- When user is prompted for an expression, they should end their expression with a semicolon, otherwise output a "Syntax Error"

- if the user decides to add a string ID of "myVar" or "myVar2" instead of "X" or "Y" that it would be counted as an expression (examples down below)

- instead of terminating the program abruptly and "throwing an instance of 'std::out_of_range' ", output to the user that there's a "Syntax Error"

#include

#include

#include

#include

#include

using namespace std;

class token

{

public:

double num1,num2;

char op;

token(double a, char b, double c) : num1(a),op(b),num2(c){};

token() : num1(0),op(0),num2(0){};

void initialize(double a, char b, double c) {

num1 = a;

op = b;

num2 = c;

return;

}

double solve()

{

switch(op)

{

case '+' : return (num1+num2);

case '-' : return (num1-num2);

case '*' : return (num1*num2);

case '/' : return (num1/num2);

case '^' : return (pow(num1,num2));

case 'r' : return (pow(num2,1/num1));

}

}

};

string expression;

deque numbers;

deque operators;

void express()

{

cout << "'+' to add '-' to subtract '*' to multiply '/' to divide '^' for powers 'r' for roots ";

cout << "Enter your expression: ";

cin >> expression;

return;

}

void extract() //Turns an expression into a list of numbers and operators.

{

vector digits;

unsigned int i,k; int temp=0;

for (i=0; i<=expression.size(); i++)

{

if (expression[i]>='0' && expression[i] <='9')

digits.push_back(expression[i]-'0');

else {

if(((expression[i]>='a' && expression[i]<='z')||(expression[i]>='A' && expression[i]<='Z'))&&expression[i]!='r'){

cout<<"What is "<

double val;

cin>>val;

digits.push_back(val);

}

else{

operators.push_back(expression[i]);

for (k=0; k

temp+=digits.at(k)*pow(10,(digits.size()-1-k));

numbers.push_back(temp);

digits.clear();

temp=0;

}

}

}

operators.erase(operators.end());

return;

}

void addsub() //Does adding and subtracting.

{

token tempp;

double temp = numbers.at(0);

while (true)

{

if (operators.at(0) == '+' || operators.at(0) == '-')

{

numbers.erase(numbers.begin());

tempp.initialize(temp ,operators.at(0), numbers.at(0));

temp = tempp.solve();

if (operators.size() == 1)

{

operators.clear();

numbers.at(0) = temp;

return;

}

else

operators.erase(operators.begin());

}

}

}

void multidiv() //Does multiplication and division.

{

token tempp;

double temp;

unsigned int i;

for (i=0; i

{

if (operators.at(i) == '*' || operators.at(i) == '/')

{

tempp.initialize(numbers.at(i) ,operators.at(i), numbers.at(i+1));

temp = tempp.solve();

if (operators.size() == 1)

{

operators.clear();

numbers.at(0) = temp;

return;

}

else

{

operators.erase(operators.begin()+i);

numbers.erase(numbers.begin()+i+1);

numbers.at(i) = temp;

}

--i;

}

}

addsub();

}

void calculate() //Does powers and roots.

{

token tempp;

double temp;

unsigned int i;

for (i=0; i

{

if (operators.at(i) == '^' || operators.at(i) == 'r')

{

tempp.initialize(numbers.at(i) ,operators.at(i), numbers.at(i+1));

temp = tempp.solve();

if (operators.size() == 1)

{

operators.clear();

numbers.at(0) = temp;

return;

}

else

{

operators.erase(operators.begin()+i);

numbers.erase(numbers.begin()+i+1);

numbers.at(i) = temp;

}

--i;

}

}

multidiv();

}

int main ()

{

express();

extract();

calculate();

cout << "Final answer: " << numbers.at(0) << endl << endl;

return 0;

}

EXAMPLE 1:

Enter your expression:

myVar + myVar2 * 11;

What is myVar= ?

1234

What is myVar2=?

2

Final answer: 1256

EXAMPLE 2:

Enter your expression:

Y^2 + Z^3 + 7 * 8 * X + 8 * 8 + 8 * X + 7 * Y;

What is Y= ?

12

What is Z= ?

5

What is X= ?

8

Final Answer: 929

EXAMPLE 3:

Enter your expression:

X^2 + + 7;

Syntax Error

EXAMPLE 4:

Enter your expression:

X^4 * 8 + 9 + 7

Syntax Error

For some reason, examples 1 and 2 aren't working as they're intended by the code given above and states that they're out of range which I'm very confused, can you fulfill the requirements as stated above and fulfill in completing the following examples? Please show your work, and Thank you very much for your time.

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions