Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

just for part 1 Due: Thu, Mar 8, 2:30 p.m. 1. (28%) Create a program that evalua

ID: 3725834 • Letter: J

Question

just for part 1 Due: Thu, Mar 8, 2:30 p.m. 1. (28%) Create a program that evaluates a simple arithmetic expression. More precisely, the program reads from the user a line of text that's sup- posed to contain two numbers separated by an arithmetic operator. The program then prints back the expression and its value, as in 35.6 + 12 = 47.6 operator and nothing else. There can be whitespace before the first num- ber, after the second number and between each number and the opera The operator can be either ,-, or /. In case of an error, the should print Invalid expression and quit. Use string streams. (48%) Add some error-checking to the class song you created in the pre vious assignment by doing the following. tor progranm 2.

Explanation / Answer

#include<iostream>

#include<sstream>

using namespace std;

//declare functions

double add(double op1, double op2);

double subtract(double op1, double op2);

double multiply(double op1, double op2);

double divide(double op1, double op2);

int main()

{

stringstream ss;

string input,op;

char Operator;

//declare double variables to store operand

double D1, D2,D3;

do

{

//take input from user

cout << endl<<"Enter the arithmetic expression,eg (4.5 + 6.7 ): ";

getline(cin, input);

ss << input;

ss >> D1 >> op >> D2;

//assign Operator value from the string op

Operator = op[0];

//now switch on operator

switch (Operator)

{

case '+':

//call add function

D3 = add(D1, D2);

cout << D1 << " + " << D2 << " = " << D3;

break;

case '-':

//call subtract function

D3 = subtract(D1, D2);

cout << D1 << " - " << D2 << " = " << D3;

break;

case '*':

//call multiply function

D3 = multiply(D1, D2);

cout << D1 << " * " << D2 << " = " << D3;

break;

case '/':

if (D2 == 0)

{

cout << "You can't devide by zero" << endl;

}

else

{

//call devide function

D3 = divide(D1, D2);

cout << D1 << " / " << D2 << " = " << D3;

}

break;

default:

cout << "Invalid response " << Operator << " - Please enter a valid operation " << endl;

break;

}

ss.clear();

cout << endl<<"Do you want to continue(y/n)? ";

cin >> op;

if (op == "n" || op == "N")

break;

cin.ignore();

cin.clear();

} while (1);

}

double add(double op1, double op2)

{

return op1 + op2;

}

double subtract(double op1, double op2)

{

return op1 - op2;

}

double multiply(double op1, double op2)

{

return op1 * op2;

}

double divide(double op1, double op2)

{

return op1 / op2;

}

/*

Enter the arithmetic expression,eg (4.5 + 6.7 ): 789.56 + 9876.89

789.56 + 9876.89 = 10666.4

Do you want to continue(y/n)? y

Enter the arithmetic expression,eg (4.5 + 6.7 ): 678.34 - 567.98

678.34 - 567.98 = 110.36

Do you want to continue(y/n)? y

Enter the arithmetic expression,eg (4.5 + 6.7 ): 78.98 * 679.89

78.98 * 679.89 = 53697.7

Do you want to continue(y/n)? y

Enter the arithmetic expression,eg (4.5 + 6.7 ): 78 / 0

You can't devide by zero

Do you want to continue(y/n)? y

Enter the arithmetic expression,eg (4.5 + 6.7 ): 678 / 78

678 / 78 = 8.69231

Do you want to continue(y/n)? y

Enter the arithmetic expression,eg (4.5 + 6.7 ): 678 % 78

Invalid response % - Please enter a valid operation

Do you want to continue(y/n)? n

*/