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
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) *...
(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...
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...
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??
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app). It...
Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app). It needs to have two classes. One class name is CalculatorBrain.swift for model and another is ViewController.swift for view. Also please show the screenshot of storyboard. Requirements: It needs to be able to handle multiple operations in a sequence. It should ONLY use the last operand if multiple operands are entered consecutively Example: “5++-1=”is equivalent to “5-1=” Proper error handling and self-correction. “+-5” is equivalent...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app) Requirements:...
Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app) Requirements: It needs to be able to handle multiple operations in a sequence. It should ONLY use the last operand if multiple operands are entered consecutively Example: “5++-1=”is equivalent to “5-1=” Proper error handling and self-correction. “+-5” is equivalent to “negative 5” “0004+32 ++ 0101 -- 04=” is equivalent to “4+32+101-4=” Implement 3 more buttons: ¼ ½ and ¾ at the bottom of the keyboard....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT