Answer to Question #283164 in C++ for GIGIG

Question #283164

create a calculator using C++ (OOP) with all operations and contain round and square parentheses and it supports the following operations: addition, subtraction, multiplication, division, power, nth root use a minimum of 4 classes, should contain at for invalid expressions the calculator will display an error message - All the attributes will have getters and setters; the setters will contain validations. Each class will have at least two operators overloaded from the following ones (the same operator will not be overloaded for two different classes):

  • index operator []
  • an implicit or explicit cast
  • a conditional operator (<.>,=<,>=,==)

For examples of inputs:

1- [4+(5-1)]*2 should output 16

2- 5 / 0 will display an error because division to 0 does not make sense


The calculator will take the input from the console. The user will have the ability to write equations and entering the app will display the result.

1
Expert's answer
2021-12-28T01:31:23-0500
#include <iostream>

using namespace std;

  

// Main program

main()

{

    char op;

    float num1, num2;

  

    // It allows user to enter operator i.e. +, -, *, /

    cin >> op;

  

    // It allow user to enter the operands

    cin >> num1 >> num2;

  

    // Switch statement begins

    switch (op) {

          

        // If user enter + 

        case '+':

            cout << num1 + num2;

            break;

          

        // If user enter - 

        case '-':

            cout << num1 - num2;

            break;

          

        // If user enter *

        case '*':

            cout << num1 * num2;

            break;

          

        // If user enter /

        case '/':

            cout << num1 / num2;

            break;

          

        // If the operator is other than +, -, * or /, 

        // error message will display

        default:

            cout << "Error! operator is not correct";

            break;

    } // switch statement ends

  

    return 0; 

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment