2931 - EE2310_Assg3 Scoreboard

Time

2023/12/18 21:57:00 2024/01/01 23:59:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
14185 EE2310_Assg3

14185 - EE2310_Assg3   

Description

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.

  1. Basic arithmetic operators: +, -, *, / along with the unary negation operator -, i.e. -5
  2. Unary scientific functions: sin(), cos(), tan(), log(), sqrt()
  3. Binary scientific functions: log_{b}(a) and 2^{3}. You should use the corresponding math functions in <cmath> to implement them.

Remarks

  1. 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.
  2. log() stands for natural logarithm, i.e. with base e = 2.718....
  3. log_{b}(a) is ordanry logarithm with base b. Use the log() function in <cmath> to implement log_{b}(a) as log(a)/log(b).
  4. sqrt() is square root.
  5. 2^{3} stands for 2 raising to the third power, which is equal to 8
  6. 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}.
  7. The operators' priorities are listed as follows.
  8. 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 <string>). 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() otherwise you may lose penalty points.

int main () { // DO NOT CHANGE MAIN!!
    Expression exp1;
    exp1.input();
    exp1.eval();
    exp1.output();
    return 0;
}

Try to explore the power of C++ and make your program as flexible as possible, i.e. if new functions like sinh or tanh are added in the future, you do not need to modify too much of your work to support them.

Hint

You may need to use two stacks to do this assignment. One for storing the operands, and the other for storing the operators. You can use the standard library templates to do that. Include <stack> to use their templates. You also need to include <string> and <cmath> libraries.

Grading & Submission

Same as Assignment #2. 70% correctness and 30% style. Submit your sourcecode (with detailed comments) on both OJ and eLearn.

Input 1

2 + (5 *(3.1^{3.3}+ sin(6.1)))/ log_{3}(15)*2

Output 1

170.961

Input 2

-87.38 + sqrt(.23^{1.4} + 91.13)*3.456+tan(98.23) 

Output 2

-53.2482

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss