Objectives: Learn how to define and use C++ functions. Also learn how to pass ar
ID: 3907606 • Letter: O
Question
Objectives: Learn how to define and use C++ functions. Also learn how to
pass arguments by reference and use return values from functions.
Instructions: This assignment is to write a program that simulates a
simple hand-held calculator. Your program should execute with the
following behavior.
• Step 1: Display a prompt and wait for the user to enter an operator.
o ’+’ for addition (x+y)
o ’-’ for subtraction (x-y)
o ’*’for multiplication (x*y)
o ’/’ for division (x/y)
o ’ˆ’ for power (i.e. x**y)
o ’@’ for power of 10 (i.e. 10**x)
o ’#’ for power of 2 (i.e. 2**x)
o ’x’ for exit
• Step 2 (if needed): Display a prompt and wait for the user to enter a
floating-point value.
• Step 3 (if needed): Display a prompt and wait for the user to
enter another floating-point value.
• Step 4 (if needed): Display the result of performing the
operation.
• Step 5: Go back to step 1 if the operator was invalid, not ’x’, or end
of file is encountered.
You should write separate functions for each of the following tasks. You
should provide the corresponding prototypes after the #include
statements so the function definitions can be arranged in any order.
(1) Prompting and getting the operator. This function should return a
boolean value indicating if the operator was valid. The function
should also return by the use of reference parameters the operator
entered by the user and the required number of operands for that
operator.
(2) Prompting and getting a floating-point value. This function should
return a double.
(3) Calculating the result of an operation. This function should be
passed a character operator and two double operands. It should
return a double.
(4) Printing the result. This function should be passed a double and
should return no value.
(5) Calculate the power of x**y. This function should be passed two
doubles, a base and an exponent. It should return a double.
You should also check for error conditions. If the operator entered is
invalid, then the user should be informed. Your program should also
check for invalid operands. For instance, you need to check for a
divide by zero. In this case, just print a warning message and set the
result to zero. You also only have to handle integer exponents. If the
exponent for a power is not an integer value, then you should print a
warning message and truncate the exponent.
Explanation / Answer
#include <iostream>
#include <math.h>
using namespace std;
// function prototypes
void displayMenu();
bool getOperator (char&, int&);
double getNumber(double&);
double getNumber(double&);
double calculateResult(char, double, double);
void print(double);
void exit();
void invalidOption();
// function to display menu
void displayMenu ()
{
cout<<" '+' for addition (x+y) "<<endl;
cout<<" '-' for subtraction (x-y) " <<endl;
cout<<" '*' for multiplication (x*y) " <<endl;
cout<<" '/' for division (x/y) " <<endl;
cout<<" '^' for power (i.e. x**y) " <<endl;
cout<<" '@' for power of 10 (i.e. 10**x) " <<endl;
cout<<" '#' for power of 2 (i.e. 2**x) " <<endl;
cout<<" 'x' for exit " <<endl;
}
// function to get user option for operator
bool getOperator (char& selection, int& numOfOperands)
{
cout<<"Enter option: ";
cin>>selection;
// valid option
if(selection == '+' || selection == '-' || selection == '*' || selection == '/' || selection == '^' ){ // two operands calculation
numOfOperands = 2;
return true;
}
if(selection == '@' || selection == '#'){ // Single operand calculation
numOfOperands = 1;
return true;
}
if(selection == 'x'){ // exit operation
numOfOperands = 0;
return true;
}
// invalid option
return false;
}
// function to get operand
double getNumber(double& num)
{
cout<<"Enter number: ";
cin>>num;
}
// function to calculate result
double calculateResult(char op, double num1, double num2){
if(op == '+'){
return num1+num2; // Addition
}
if(op == '-'){
return num1-num2; // Subtraction
}
if(op == '*'){
return num1 * num2; // Multiplication
}
if(op == '/'){
return num1 / num2; // Division
}
if(op == '^'){
return pow(num1, num2); // Power of (base ** exponent)
}
if(op == '@'){
return pow(10, num1); // Power of 10 (10 ** num)
}
if(op == '#'){
return pow(2, num1); // Power of 2 (2 ** num)
}
}
// function to print result
void print(double output){
cout<< "Output:"<<output<<" ";
}
// function to print exit
void exit(){
cout<< "Exit ";
}
// function to display invalid operation
void invalidOption(){
cout<< "Invalid Option... Try again ";
}
int main ()
{
char op = ' '; // user option initialization
do{
// decalring required varailbles for every calculation
int numOfOperands = 0;
double num1, num2, result;
bool isOperatorValid;
displayMenu(); // displays menu
isOperatorValid = getOperator(op, numOfOperands); // get operator from user
if(isOperatorValid){ // validating user option
if(numOfOperands == 2){ // two operands calculation
getNumber(num1);
getNumber(num2);
result = calculateResult(op, num1, num2 );
print(result);
}
else if(numOfOperands == 1){ // single operand calculation
getNumber(num1);
result = calculateResult(op, num1, num2 );
print(result);
}
else if(numOfOperands == 0){ // no operand: exit
cout<<"Exiting...";
}
}
else{ // invalid user option
cout<<"Invalid option... try again ";
}
}
while(op!='x');
//system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.