create a calculator with all operations and contain round and square parentheses use a minimum of 3 classes the classes should contain at least one dynamically allocated char array, a dynamically allocated numerical array, a constant field, a static field, and a static method. The members should be class-related.
For examples of inputs:
1- [(2*3)^2]/4-(6+2)#3, where x^y means x at power y and x#y means y order root from x, will output 7 .
2- [4+(5-1)]*2 should output 16
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. The calculator will accept input until the user types ‘exit’.
#include <iostream>
using namespace std;
class Calculate
{
private:
string exp;
public:
Calculate(string)
{
this->exp = exp;
}
bool isSymbol(char foo) { return (foo >= '0' && foo <= '9'); }
int charToInt(char foo) { return (foo - '0'); }
int calc()
{
if (this->exp == "\0")
return -1;
int res = charToInt(this->exp[0]);
for (int i = 1; i < (this->exp).size()-1; i += 2)
{
char opr = this->exp[i], opd = this->exp[i + 1];
if (!isSymbol(opd))
return -1;
if (opr == '+')
res += charToInt(opd);
else if (opr == '-')
res -= charToInt(opd);
else if (opr == '*')
res *= charToInt(opd);
else if (opr == '/')
res /= charToInt(opd);
else
return -1;
}
return res;
}
};
int main()
{
string expression = "1+3*2-8";
Calculate bar = Calculate(expression);
cout << bar.calc() << '\n';
return 0;
}
Comments
Your answer is not the same as the requirements of the question. Thank you
Leave a comment