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

Analysis: create a calculator that adds, subtracts, multiplies, divides and take

ID: 3931304 • Letter: A

Question

Analysis: create a calculator that adds, subtracts, multiplies, divides and takes the modulus of a number. Use functions. Design: output a prompt - void prompt() get character answer from user (what function are they doing?) a = add, s = subtract, m = multiply, d = divide, r = remainder, q = quit double getValue() - prompt for x, prompt for y function to add - double add(double x, double y) similar functions for the other operations use a switch statement in main to choose which operation based on answer calculate solution output solution Testing: input x: 1 input y: 2 add: 3, subtract:-1, mult: 2, divide: .5, mod: 1 input x: 1 input y: 0 add: 1, sub: 1, mult: 0, divide: VALIDATE!!! cant div by zero mod: VALIDATE!! can't div by zero

Explanation / Answer

//Tested on Ubuntu,Linux

#include <iostream>
#include <math.h>
using namespace std;
/*Variable declaration*/
double x,y;
/*add function implementation*/
double add(double x,double y) {
return (x+y);
}
/*subtract function implementation*/
double subtract(double x,double y) {
return (x-y);
}
/*multiply function implementation*/
double multiply(double x,double y) {
return (x*y);
}
/*divide function implementation*/
double divide(double x,double y) {  
return (x/y);
}
/*remind function implementation*/
double remind(double x,double y) {
return remainder(x,y);
}
/*getValue function implementation
* prompt for value of x and y*/
double getValue() {

/*Prompt for x and y value*/
std::cout<<"Please Enter x value"<<std::endl;
std::cin>>x;
std::cout<<"Please Enter y value"<<std::endl;
std::cin>>y;

return y;
}
/*Main function start*/
int main(){

char ch;


/*calling getvalue function*/
getValue();

/*Getting choice from user*/
std::cout<<"Please Enter your choice"<<std::endl;
std::cout<<"a.Add s.Subtract m.Multiply d.Divide r.remainder q.quit ";
std::cin>>ch;

/*Based on the choice it will call that function*/
switch(ch){
case 'a':         
std::cout<<"add:"<<add(x,y)<<std::endl;
break;
case 's':
std::cout<<"subtract:"<<subtract(x,y)<<std::endl;
break;
case 'm':
std::cout<<"mult:"<<multiply(x,y)<<std::endl;
break;
case 'd':
if(y==0) {
std::cout<<"VALIDATE!!!cant div by zero"<<std::endl;
}else {
std::cout<<"divide:"<<divide(x,y)<<std::endl;
}
break;
case 'r':
if(y==0) {
std::cout<<"VALIDATE!!!cant div by zero"<<std::endl;
}else {
std::cout<<"mod:"<<remind(x,y)<<std::endl;
}
break;
case 'q':
break;
}

return 0;
}
/*End main Function*/

/***************output***************/
anshu@anshu:~/Desktop/chegg$ g++ calculator.cpp -lm
anshu@anshu:~/Desktop/chegg$ ./a.out
Please Enter x value
1
Please Enter y value
2
Please Enter your choice
a.Add
s.Subtract
m.Multiply
d.Divide
r.remainder
q.quit
r
mod:1
anshu@anshu:~/Desktop/chegg$ ./a.out
Please Enter x value
1
Please Enter y value
0
Please Enter your choice
a.Add
s.Subtract
m.Multiply
d.Divide
r.remainder
q.quit
d
VALIDATE!!!cant div by zero
anshu@anshu:~/Desktop/chegg$ ./a.out
Please Enter x value
1
Please Enter y value
0
Please Enter your choice
a.Add
s.Subtract
m.Multiply
d.Divide
r.remainder
q.quit
r
VALIDATE!!!cant div by zero
anshu@anshu:~/Desktop/chegg$ ./a.out
Please Enter x value
1
Please Enter y value
2
Please Enter your choice
a.Add
s.Subtract
m.Multiply
d.Divide
r.remainder
q.quit
a
add:3
anshu@anshu:~/Desktop/chegg$ ./a.out
Please Enter x value
1
Please Enter y value
2
Please Enter your choice
a.Add
s.Subtract
m.Multiply
d.Divide
r.remainder
q.quit
s
subtract:-1
anshu@anshu:~/Desktop/chegg$ ./a.out
Please Enter x value
1
Please Enter y value
2
Please Enter your choice
a.Add
s.Subtract
m.Multiply
d.Divide
r.remainder
q.quit
m
mult:2
anshu@anshu:~/Desktop/chegg$ ./a.out
Please Enter x value
1
Please Enter y value
2
Please Enter your choice
a.Add
s.Subtract
m.Multiply
d.Divide
r.remainder
q.quit
d
divide:0.5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote