Write a C program to simulate a simple calculator based on u

Write a C++ program to simulate a simple calculator based on user input involving branch statements and loops!?
In the program, you can just use these statements if, if-else, do-while, while, and switch.

Solution


/**

Simulation of simple c++ calculator
*/

//calculator.cpp
#include<iostream>
#include<cmath>
using namespace std;

//function prototypes
char getOperation();
double readFirstOperand();
double readSecondOperand(char);
double getresult(char opcode,double num1,double num2);

//main method
int main()
{

   char opcode,choice;
   double num1;
   double num2;
   bool repeat=true;

   do
   {

       //calling methods
       opcode=getOperation();
       num1=readFirstOperand();
       num2=readSecondOperand(opcode);

       cout<<\"Result : \"<<getresult(opcode,num1,num2)<<endl;

       cout<<\"Do you want to continue [y or n] : \";
       cin>>choice;

       if(choice==\'n\' || choice ==\'N\')
           repeat=false;

   }while(repeat);

   system(\"pause\");
   return 0;
}


//Method takes character, two double values as input and
//performs the operation and returns the result
double getresult(char opcode,double num1,double num2)
{

   double res=0;

   switch(opcode)
   {
   case \'+\':
           res=num1+num2;
           break;
   case \'-\':
           res=num1-num2;
           break;
   case \'/\':
           res=num1/num2;
           break;
   case \'*\':
           res=num1*num2;
           break;
   case \'^\':
           res=pow(num1,num2);
           break;
   }

   return res;
}


/**
Method that takes operation code and
prompts user to enter value if operation code
is division and value is 0 then reprompt until user enters
value not zero.
*/
double readSecondOperand(char opcode)
{
   double num2;
   bool repeat=true;
   do
   {

       cout<<\"Enter second operand : \";
       cin>>num2;

       if(opcode==\'/\' && num2==0)
       {
           cout<<\"Cannot be zero for division .\"<<endl;
           repeat=true;
       }
       else if(opcode==\'^\' && (num2>8 || num2<-8))
       {
           if(num2>8)
               num2=8;
           else if(num2<-8)
               num2=-8;

           repeat=false;
       }
       else
       {
           repeat=false;
       }

   }while(repeat);


   return num2;
}

/**
The method that prompts value and reprompt for
value if value is less than zero and retrns the
value
*/
double readFirstOperand()
{

   double num1;
   do
   {

       cout<<\"Enter first operand : \";
       cin>>num1;

       if(num1<0)
           cout<<\"Must be positive number ,>0.\"<<endl;

   }while(num1<0);


   return num1;
}


/**
Method that prompts the operation code
and re-prompts operation code if operation
code is not +,-, / opr % otherwise return operation code.
*/
char getOperation()
{
   bool repeat=true;
   char opcode;
   do
   {

       cout<<\"Enter operation code : +,-,*,/ and ^ : \";
       cin>>opcode;

       if(opcode==\'+\'||opcode==\'-\'||opcode==\'*\'||opcode==\'/\'||opcode==\'^\')
           repeat=false;
       else
       {
           cout<<\"Invalid operation code .\"<<endl;
           repeat=true;
       }          

   }while(repeat);
   return opcode;
}

sample output:

Enter operation code : +,-,*,/ and ^ : +
Enter first operand : 1
Enter second operand : 2
Result : 3
Do you want to continue [y or n] : y
Enter operation code : +,-,*,/ and ^ : -
Enter first operand : 1
Enter second operand : 2
Result : -1
Do you want to continue [y or n] : y
Enter operation code : +,-,*,/ and ^ : *
Enter first operand : 1
Enter second operand : 2
Result : 2
Do you want to continue [y or n] : y
Enter operation code : +,-,*,/ and ^ : /
Enter first operand : 1
Enter second operand : 0
Cannot be zero for division .
Enter second operand : 2
Result : 0.5
Do you want to continue [y or n] : y
Enter operation code : +,-,*,/ and ^ : ^
Enter first operand : 2
Enter second operand : 8
Result : 256
Do you want to continue [y or n] :n

Write a C++ program to simulate a simple calculator based on user input involving branch statements and loops!? In the program, you can just use these statement
Write a C++ program to simulate a simple calculator based on user input involving branch statements and loops!? In the program, you can just use these statement
Write a C++ program to simulate a simple calculator based on user input involving branch statements and loops!? In the program, you can just use these statement
Write a C++ program to simulate a simple calculator based on user input involving branch statements and loops!? In the program, you can just use these statement

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site