Answered step by step
Verified Expert Solution
Link Copied!

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::getline(std::cin, expression);
}
void eval(){
expression = cleanExpression(expression);
double result = evaluate(expression);
outputResult = result;
}
void output(){
std::cout << "Result: "<< outputResult << std::endl;
}
private:
std::string expression;
double outputResult;
std::string cleanExpression(const std::string& expr){
std::string cleanExpr;
for (char c : expr){
if (!std::isspace(c)){
cleanExpr += c;
}
}
return cleanExpr;
}
bool isOperator(char c){
return (c =='+'|| c =='-'|| c =='*'|| c =='/'|| c =='^');
}
bool isFunction(const std::string& func){
return (func == "sin("|| func == "cos("|| func == "tan("||
func == "log("|| func == "sqrt(");
}
int getPriority(char c){
if (c =='+'|| c =='-') return 1;
else if (c =='*'|| c =='/') return 2;
else if (c =='^') return 3;
else return 0; // For functions
}
double applyOperator(double 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::pow(a, b);
default: return 0.0;
}
}
double evaluate(const std::string& expr){
std::stack operands;
std::stack operators;
// Map for functions
std::map> functions ={
{"sin(",[](double val){ return std::sin(val); }},
{"cos(",[](double val){ return std::cos(val); }},
{"tan(",[](double val){ return std::tan(val); }},
{"log(",[](double val){ return std::log(val); }},
{"sqrt(",[](double val){ return std::sqrt(val); }}
};
size_t i =0;
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.push(applyOperator(a, b, op));
}
return operands.top();
}
};
int main(){
Expression exp1;
exp1.input();
exp1.eval();
exp1.output();
return 0;
}" this is the code that i compile, please redo it agian to get the final and consider this "Evaluating Mathematical Epressions : due 2024/1/1 Mon (at 11:59pm)
A Mathematical epression is something like the following2+(5*(3.1^{3.3}+ sin(6.1)))/ log_{3}(15)*2, in which there is a mixture of arithmetic expression, trigonometric, and logarithmic functions. Here is a list of those 12 functions.
Basic arithmetic operators: +,-,*,/ along with the unary negation operator -, i.e.-5
Unary scientific functions: sin(), cos(), tan(), log(), sqrt()
Binary scientific functions: log_{b}(a) and 2^{3}. You should use the corresponding math functions in to implement them.
Remarks
Unary negation -: note that -5^{2} must be interpreted as -(5^{2}) instead of (-5)^{2}. The unary operator - acts on any double and turns its value negative.
log() stands for natural logarithm, i.e. with base e =2.718....
log_{b}(a) is ordanry logarithm with base b. Use the log() function in to implement log_{b}(a) as log(a)/log(b).
sqrt() is square root.
2^{3} stands for 2 raising to the third power, which is equal to 8
Note that, according to our syntax, the curly braces {} are only used in logarithm's subscript log_{b}(a) and power's superscript 5.1^{2}.
The operators' priorities are listed as follows.
You do not need to check for invalid expressions (like 2.5+=3)or invalid operations (such as sqrt(-5)).
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 getline(cin, 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions