Question

Implement the Calculator code, this's C++ /* Implement a calculator take the input for a character...

Implement the Calculator code, this's C++

/* Implement a calculator
take the input for a character
if character is "+", then it's going to take the input of two numbers and show the sum
if character is "-", then it's going to take the input of two numbers and show the minus result
if character is "*", then it's going to take the input of two numbers and show the product
if character is "/", then it's going to take the input of two numbers and show the divide result
if character is "%", then it's going to take the input of two numbers and show the modulus result
if character is "^", then it's going to take the input of two numbers and show the number1^number2

*/

#include <iostream>
#include <cmath>
using namespace std;
int main(){
char operation;
int number1, number2;
cout << "Enter +, -, *, /, %, ^";
cin >> operation;
if ( operation == '+' ){
//take the input of two numbers and display the sum
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Sum is = "<< (number1+number2)<<endl;
}
else if ( operation == '-' ){
//take the input of two numbers and display the minus result
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Number 2 subtracted from number 1 is = "<< (number1-number2)<<endl;
}
else if ( operation == '*' ){
//take the input of two numbers and display the sum
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Product is = "<< (number1*number2)<<endl;
}
else if ( operation == '/' ){
//take the input of two numbers and display the divide result
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Number1 divided by number 2 is = "<< (number1/number2)<<endl;
}
else if ( operation == '%' ){
//take the input of two numbers and display the modulus
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Modulus is = "<< (number1%number2)<<endl;
}
else if ( operation == '^' ){
//take the input of two numbers and display the pow
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Number1 raised to number2 is = "<< (pow(number1,number2))<<endl;
}
else
cout << "Invalid operator";



}

Homework Answers

Answer #1

*******************Summary********************

The program is given below with changes and comments and output

Now the program asks repeatedly for the next operation......with exiting message..And i have done some changes to tje existing code to make it easier...

The explanation for changes is provided in comments..

I hope it works for you :) !!!!!!!!!!!

*******************Program***********************

#include <iostream>
#include <cmath>    //required forusing pow() function 
using namespace std;

int main()
{
  char operation;    //varable for storing type of operation
  int number1, number2;    //variables for storinf numbers on which
  // operation is going to be performed
  
  while(1)    //loop for asking for operation again
  {
    cout << "\nEnter +, -, *, /, %, ^ (0 for exit) : ";
    cin >> operation;    //take input for type of operation

    switch(operation)    //match operation value with cases 
    //and execute respective case
    {
      case '+':    //if operation is + then do follwing
        //take the input of two numbers and display the sum
          cout << "Enter two numbers\n";
          cin >> number1 >> number2;
          cout << "Sum is = "<< (number1+number2)<<endl;
        break;      //break out of switch case
        
      case '-':    //if operation is -
        //take the input of two numbers and display the minus result
          cout << "Enter two numbers \n";
          cin >> number1 >> number2;
          cout << "Number 2 subtracted from number 1 is = "<< (number1-number2)<<endl;
        break;      //break out of switch case
        
      case '*':    //if operation is *
         //take the input of two numbers and display the sum
          cout << "Enter two numbers \n";
          cin >> number1 >> number2;
          cout << "Product is = "<< (number1*number2)<<endl;
         break;      //break out of switch case
         
      case '/':    //if operation is /
          //take the input of two numbers and display the divide result
            cout << "Enter two numbers \n";
            cin >> number1 >> number2;
            cout << "Number1 divided by number 2 is = "<< (number1/number2)<<endl;
          break;      //break out of switch case
          
      case '%':    //if operation is %
          //take the input of two numbers and display the modulus
            cout << "Enter two numbers \n";
            cin >> number1 >> number2;
            cout << "Modulus is = "<< (number1%number2)<<endl;
          break;      //break out of switch case
          
      case '^':    //if operation is ^
          //take the input of two numbers and display the pow
            cout << "Enter two numbers\n";
            cin >> number1 >> number2;
            cout << "Number1 raised to number2 is = "<< (pow(number1,number2))<<endl;
          break;      //break out of switch case
          
      case '0':
            cout<<"Goodbye :)";    //if user wants to exit
          exit(0);      //exit the program with return 0
          
      default:    //if operation does not match any cases then
            cout << "Invalid operator";
          break;      //break out of switch case
    }//end of switch case
    
  }//end of while loop
  
}

*******************Output*************************

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
1. Create a new project. Type in the following program. Add a comment at the top...
1. Create a new project. Type in the following program. Add a comment at the top to include your name and the date. 2. Compile and Run with different values. What data values should you choose? Think about what we discussed in class. 3. Add code to compute the modulus. Hint: you will also have to declare a new variable. //this program demonstrates the use of various operators #include <iostream > using namespace std; int main() { int num1; int...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include <cmath> using namespace std; int getWeight(); float deliveryCharge(int weight); void displayCharge(int weight); int main () {    displayCharge(getWeight());    return 0; }   int getWeight() {    int weight;    cout << "Enter package weight (oz): ";    cin >> weight;    return weight; } float deliveryCharge(int weight) {    if (weight > 16)    {        return (((weight - 16) / 4) *...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
Write a program that mimics a calculator. The program should take as input two integer and...
Write a program that mimics a calculator. The program should take as input two integer and an arithmetic operation (+, -, *, /, %) to be performed. It should then output the numbers the operator and the result. Division by zero??
(C++ program) Use the code segment below to answer the two questions that follow. … int...
(C++ program) Use the code segment below to answer the two questions that follow. … int size = 0; cout << “Enter size: “; cin >> size; int sizeCode = size / 10; if (sizeCode == 0)    cout << “extra small”; else if(sizeCode == 1 )    cout << “small”; else if (sizeCode == 2)    cout << “medium”; else if (sizeCode == 3)    cout << “large”; else    cout << “extra large”; //end if … What would...
c++ Language Fix this code to have it concatenate two strings st1 and st2 and put...
c++ Language Fix this code to have it concatenate two strings st1 and st2 and put the result in st3 with a space separator. it has to be done using array representation of strings #include <iostream> using namespace std; #include <string> int main() { string st1,st2, st3; int c = 0, i =0;    cout << "Enter a string: "; cin >> st1; cout << "Enter another string: "; cin >> st2;    while (st1[c] != '\0'){ st3[c] = st1[c];...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor and inserts it as the nth element of the list; test your implementation by turning the flag LAB3_TEST2 from 0 to 1 in config.h; - Programming Exercise 3: implement the ListArray member function find(...) that searches for the element given as a parameter; the search starts at the cursor and stops when it finds the element or at the end of the list; the...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
In C++ ------------------------------------------ All functions take no parameters as input and return nothing. Create a function...
In C++ ------------------------------------------ All functions take no parameters as input and return nothing. Create a function for each of the following: A function that reads all text up to a space character (' ') while outputting everything up to that space A function that reads two numbers (int) ending in a ';' and adds them together and outputs the result. Note: You only have to read the first two numbers.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT