Answered step by step
Verified Expert Solution
Question
1 Approved Answer
class Expression { public: Expression ( ) { } void input ( ) { std::cout < < Enter the mathematical expression: ; std::getline (
"class Expression
public:
Expression
void input
std::cout "Enter the mathematical expression: ;
std::getlinestd::cin, expression;
void eval
expression cleanExpressionexpression;
double result evaluateexpression;
outputResult result;
void output
std::cout "Result: outputResult std::endl;
private:
std::string expression;
double outputResult;
std::string cleanExpressionconst std::string& expr
std::string cleanExpr;
for char c : expr
if std::isspacec
cleanExpr c;
return cleanExpr;
bool isOperatorchar c
return c c c c c ;
bool isFunctionconst std::string& func
return func "sin func "cos func "tan
func "log func "sqrt;
int getPrioritychar c
if c c return ;
else if c c return ;
else if c return ;
else return ; For functions
double applyOperatordouble a double b char op
switch op
case : return a b;
case : return a b;
case : return a b;
case : return a b;
case : return std::powa b;
default: return ;
double evaluateconst std::string& expr
std::stack operands;
std::stack operators;
Map for functions
std::map functions
sindouble val return std::sinval;
cosdouble val return std::cosval;
tandouble val return std::tanval;
logdouble val return std::logval;
sqrtdouble val return std::sqrtval;
;
sizet i ;
while i expr.length
Logic for expression evaluation...
while operators.empty
double b operands.top;
operands.pop;
double a operands.top;
operands.pop;
char op operators.top;
operators.pop;
operands.pushapplyOperatora b op;
return operands.top;
;
int main
Expression exp;
expinput;
expeval;
expoutput;
return ;
this is the code that i compile, please redo it agian to get the final and consider this "Evaluating Mathematical Epressions : due Mon at :pm
A Mathematical epression is something like the following sin log in which there is a mixture of arithmetic expression, trigonometric, and logarithmic functions. Here is a list of those functions.
Basic arithmetic operators: along with the unary negation operator ie
Unary scientific functions: sin cos tan log sqrt
Binary scientific functions: logba and You should use the corresponding math functions in to implement them.
Remarks
Unary negation : note that must be interpreted as instead of The unary operator acts on any double and turns its value negative.
log stands for natural logarithm, ie with base e
logba is ordanry logarithm with base b Use the log function in to implement logba as logalogb
sqrt is square root.
stands for raising to the third power, which is equal to
Note that, according to our syntax, the curly braces are only used in logarithms subscript logba and power's superscript
The operators' priorities are listed as follows.
You do not need to check for invalid expressions like or invalid operations such as sqrt
priority operators
lowest
medium unary
high
highest sin cos tan log log sqrt
If two operators have the same priority, they should be evaluated in from left to right.
Write a class Expression that holds the value of a Mathematical epression and it has at least two member functions input and eval in which the latter evaluates the expression. You may add members you like, as long as you keep all data members private or protected. We strongly suggest you use the getline function to read the whole expression as a stringobject remember to include Do not use cin str; as it will stop on whitespaces. Use getlinecin str; instead to read the whole line in which str is a string object The main function is given as follows. Do not change main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started