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

*** USING C++ *** Topics Methods Description Write a program that will implement

ID: 3838451 • Letter: #

Question

*** USING C++ ***

Topics

Methods

Description

Write a program that will implement a simple calculator. The program will prompt the user to input an operation and two decimal numbers. It will perform the specified operation on the two decimal numbers and will display the result. The program will allow the following values for operation:

+ (means add)

- (means subtract)      

* (means multiply)

/    (means divide)

The program will do the above repeatedly so that a user may perform different operations on a different pair of numbers.

/* C++ users

The program will end when the user enters x for operation.

*/

Testing

Test Run

Enter an operation

+

Enter the first number

2.2

Enter the second number

1.1

The result is 3.3

Enter an operation

-

Enter the first number

2.2

Enter the second number

1.1

The result is 1.1

Enter an operation

*

Enter the first number

2.2

Enter the second number

1.1

The result is 2.42

Enter an operation

/

Enter the first number

2.2

Enter the second number

1.1

The result is 2

Enter an operation

press cancel (java users)

x (c++ users)

Implementation

The program will provide two methods: main and calculate.

Method calculate

The method calculate will receive three parameters and will return a double value.

It will receive the following three parameters.

       A char parameter op for receiving the operation value. The possible values for this parameter are: +, -, *, /.

       A double parameter n1 for receiving the first operand.

         A double parameter n2 for receiving the second operand.

It will return a double value.

Below is the sample method header with an empty body.

C++ Users

*/

double calculate (String op, double n1, double n2)

{

}

Method main ( )

The method main will do the following repeatedly:

·       It will prompt the user to enter the desired operation.

·       It will prompt the user to enter the first operand.

·       It will prompt the user to enter the second operand.

·       It will call the method calculate and pass it the operation and the two operands as parameters.

·       On return from calculate, the method main will receive the result in a variable using an assignment statement.

·       It will display the result.

Explanation / Answer

#include <iostream>

using namespace std;
double calculate (string op, double n1, double n2)
{
if(op=="+"){
return n1+n2;
}
else if(op=="-"){
return n1-n2;
}
else if(op=="*"){
return n1* n2;
}
else if(op=="/") {
return n1/n2;
}
return -1;
}
int main()
{
string op;
double n1,n2,result;
while(true) {
cout << "Enter an operation: " ;
cin >> op;
if(op=="x"){
break;
}
cout <<"Enter the first number: " ;
cin >> n1;
cout << "Enter the second number: " ;
cin >> n2;
result = calculate(op,n1,n2);
if(result!=-1)
cout<<"The result is "<<result<<endl;
else
cout<<"The operation is invalid."<<endl;
}
cout<<"Bye"<<endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter an operation: +                                                                                                                                                                                                                                                  

Enter the first number: 2.2                                                                                                                                                                                                                                            

Enter the second number: 1.1                                                                                                                                                                                                                                           

The result is 3.3                                                                                                                                                                                                                                                      

Enter an operation: -                                                                                                                                                                                                                                                  

Enter the first number: 2.2                                                                                                                                                                                                                                            

Enter the second number: 1.1                                                                                                                                                                                                                                           

The result is 1.1                                                                                                                                                                                                                                                      

Enter an operation: x                                                                                                                                                                                                                                                  

Bye