Objective: To implement functions in the C++ programing language and to decompos
ID: 3809093 • Letter: O
Question
Objective: To implement functions in the C++ programing language and to decompose a program into logica function blocks. To further our understanding of loops, their logic and the correct use of the whilel, do while() and forcontrol structures in the C++ programming language. Specifically to show an understanding of the logically appropriate use of nested loops and embedded conditional statements. To reinforce our understanding of data types and variables. Namely, how to create, initialize, display and perform basic arithmetic operations on those variables. Assignment: Write a program that simulates a simple calculator to perform basic arithmetic computations. Th program should support the following operations: Addition Subtraction Multiplication Division The program should continue to prompt the user to enter an equation in the form of a b a-b a*b where a represents the first operand, l+, represents the operation addition, subtraction, multiplication and division respectively and b represents the second operand. Each time a valid equation is entered, the equation should be computed and the results of the computation should be displayed also in the form of an equation (i.e. 5*4 20). The program should continue requesting equations to compute until the user enters some sentinal value or sentinal equation. Example: -999 as the first operand or txx as the operation, etc.Explanation / Answer
#include <iostream>
using namespace std;
int op1,op2,num1,num2; //op1 & op2 stores the operand
char op,sig; //op stores the operand
class calc
{
public:
bool parseInput(int a,char b,int c) //Accept input from the user
{
op1=a;
op=c;
op2=c;
cout<<"Enter an equation or 0x0 to exit:";
cin>>op1>>op>>op2;
if (op1>0 && op2>0)
return(true);
else if (op1>0 && op=='/' && op2==0)
{
cout<<"Error...cannot divide by 0 ";
return false;
}
else
return(false);
}
double calculator(int a1,char b2,int c2)
{
int tot;
num1=a1;
sig=b2;
num2=c2;
if(sig=='+') //if operand is + it calls addition function
{
tot=addition(num1,num2);
return tot;
}
else if(sig=='-') //if operand is - it calls subtraction function
{
tot=subtraction(num1,num2);
return tot;
}
else if(sig=='*') //if operand is * it calls multiplication function
{
tot=multiplication(num1,num2);
return tot;
}
else //if operand is / it calls division function
{
tot=division(num1,num2);
return tot;
}
}
int addition(int x,int y) //addition function
{
int s;
s=x+y;
return (s);
}
int subtraction(int x,int y) //subtraction function
{
int s;
s=x-y;
return (s);
}
int multiplication(int x,int y) //multiplication function
{
int s;
s=x*y;
return (s);
}
int division(int x,int y) //division function
{
int s;
s=0;
if(x==0 || y==0)
cout<<"Error.....Cannot divide by 0";
else
{
s=x/y;
return (s);
}
}
};
int main()
{
calc obj;
double p;
bool ch;
do
{
ch=obj.parseInput(0,' ',0); //calling the parseInput function
if(ch==true)
{
p=obj.calculator(op1,op,op2); //calling calculator
cout<<op1<<op<<op2<<"="<<p<<" ";
}
else
cout<<"ok,It was a pleasure doing math of you... ";
}while(op1>0 && op2>0);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.