the user to enter an equation in the form of 10-5, or 10-5, or 1 5, or 13/4, or
ID: 3734332 • Letter: T
Question
the user to enter an equation in the form of 10-5, or 10-5, or 1 5, or 13/4, or 3. The program should reod in the equation by reading in three values: a double and a or not 4. This is an example of how you would declare the three items to be read in. numt, num2 5· char choper; 7. This is an example of how you would read in the three items o The program should do addition, subtraction, multiplication or division. o Make sure and test division by zero!!! o Make sure and CHANGE the numbers to INTEGER before attempting the After showing your instructor, copy and paste the code for all of your programs into Canvas for the(04CH4) Gaddis Assignment 8. Other than the data inputted, your output should look EXACTLY as follows: Enter your equation (e.g. 10+5):9*6 9 5 45 Welcomet Enter your equation (e.g. 10+ 5):1+0 1 0-1 Welcome! Enter your equation (e.g. 10 5): 25/2 25 / 2 12.5 Welcome! Enter your equation (e.g. 10+5): 0-10 0-10-10 (e-g. 10+ 5): 1/0 Welcome! Enter your equation (e.g.10+5):1 0 Welcome! Enter your equation (e.g. 10 + 5): 1.0 Welcomet Enter your equotion (e.g. 10+5): 1%0 ! Enter your equation (e-g. 10 5): 0/1 0/1 o Remember, the items shown ofter the colon are whot YOU type; those items may vary. The spaces don't matter when you input the equotion. The above shows six diffterent somple runs 2 0Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
double num1,num2;
char choper;
cout<<"Welcome! Enter your equation (e.g.- 10 + 5): ";
cin>>num1>>choper>>num2;
switch(choper)
{
case '+':
cout<<num1<<" "<<choper<<" "<<num2<<" = "<<num1+num2<<endl;
break;
case '-':
cout<<num1<<" "<<choper<<" "<<num2<<" = "<<num1-num2<<endl;
break;
case '*':
cout<<num1<<" "<<choper<<" "<<num2<<" = "<<num1*num2<<endl;
break;
case '/':
if(num2==0)
cout<<"ERROR: "<<num1<<" "<<choper<<" "<<num2<<" => CANNOT DIVIDE BY ZERO"<<endl;
else
cout<<num1<<" "<<choper<<" "<<num2<<" = "<<num1/num2<<endl;
break;
case '%':
if(num2==0)
cout<<"ERROR: "<<num1<<" "<<choper<<" "<<num2<<" => CANNOT DIVIDE BY ZERO"<<endl;
else
cout<<num1<<" "<<choper<<" "<<num2<<" = "<<(int)num1%(int)num2<<endl;
break;
default:
cout<<"ERROR: "<<num1<<" "<<choper<<" "<<num2<<" => INVALID OPERATOR: "<<choper<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.