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):
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.
#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;
}
Comments
Leave a comment