Objective: To implement functions in the C++ programing language and to decompos
ID: 3809944 • 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
Assignment code :-
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
class calculation{
public:
bool parseInt(int* op1,int* op2,char* op){
int f1,f2;
char operand;
cout << "Please enter the equation";
try{
cin >> f1 >> operand >> f2;
if(operand == '+' || operand == '*' || operand == '/' || operand == '-'){
*op=operand;
}
else
throw 'a';
}
catch(int e){
cout<< "Operands should be number ..Please try again"<<endl;
return false;
}
catch(char c){
cout << "Please provide valid arithmatic operation"<<endl;
return false;
}
*op1=f1;
*op2=f2;
return true;
}
double calculator(int op1,char op,int op2){
double res=0.0;
switch(op){
case '+':
res=(double)addition(op1,op2);
return res;
break;
case '-':
res=(double)subtraction(op1,op2);
return res;
break;
case '*':
res=(double)multiplication(op1,op2);
return res;
break;
case '/':
res=division(op1,op2);
return res;
break;
default:
cout<<"enter proper operation....please try again"<<endl;
return -0.0001;
}
}
int addition(int first,int second){
return (first+second);
}
int subtraction(int first,int second){
return first-second;
}
int multiplication(int first,int second){
return first*second;
}
double division(int first,int second){
return (double)first/(double)second;
}
};
int main() {
int op1,op2;
char option;
char op;
double result;
calculation obj1;
int check=1;
while(check){
if(!(obj1.parseInt(&op1,&op2,&op))){
cout << "Please enter proper order or valid variables";
continue;
}
result=obj1.calculator(op1,op,op2);
if(result!=-0.0001)
cout<<"The result of the expression is : "<<result<<endl;
else{
cout<<"SOMETHING WRONG HAPPENED PLEASE TRY AGAIN";
continue;
}
cout <<"Want to try for other numbers then press 1";
cin>>option;
if(option != '1'){
check=0;
}
}
cout<<"Thank you"<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.