Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will implement an infix arithmetic expression evaluator, as described in the lecture slides, using a stack template that you implement in Java. You must

You will implement an infix arithmetic expression evaluator, as described in the lecture slides, using a stack template that you implement in Java. You must implement your own stack template and not use an existing template.

Stack Write the methods in the template provided that are needed to compile and pass the tests. The input is infix arithmetic expressions, one per line, so think about how you want to manage the input. Expression Evaluator The following operators, and no others, will appear in the infix expressions. Note that parentheses, in various forms, are appropriately categorized as operators. More about that below. + Addition - Subtraction * Multiplication / Division ^ Exponentiation (power) sin and cos Trigonometric functions that take arguments in radians, not degrees log Natural logarithm (base e) ( ), { }, [ ] Parentheses in three forms

The space character, , is used as a delimiter to separate tokens in the input. This means that operators and operands are going to be separated from each other by a space. ( 3 + 5 ) is a valid expression ( 3 + 5) is not a valid expression because there is no space between the operand, 5, and the right parenthesis. The input type is float (double) so a number such as 3.5 is a valid operand input. So that you know to expect this, there may be cases where the expression includes a divide operator with a denominator operand equal to zero. Java and computer hardware follow the IEEE 754 standard for computer arithmetic on floating point values. Thus, in cases of divide-by-zero, the double type output will be a positive or a negative infinity. A correctly formatted infix expression will include one or more parenthesis pair(s) that directly enclose each operation and its operands or its operand (for the log operation). Individual operands may also be enclosed in one or more parenthesis pairs. For example, 3 + 5 is not valid because there is no enclosing parenthesis pair ( { 3 } + 5 ) is a valid expression because an operand may be enclosed or not enclosed by one parenthesis pair and there is one pair enclosing the + operator and its operands ( { 3 } + 5 } is not valid because the outer parenthesis pair does not match as to symbol used ( log 3 ) is a valid expression, ( log ( 3 ) ) is a valid expression and both evaluate to 1.0986122886681098 { ( cos ( ( 90 ) + [ [ [ 5 ] ] ] ) ) } is a valid expression that evaluates to 0.7301735609948197 In addition to correctly evaluating correctly formatted infix expressions, your program should reject incorrectly formatted infix expressions. Incorrect formatting includes the following situations only. An empty pair of parentheses, e.g., { }, which does not enclose either an operand alone or a pair of operands with an infix-positioned operator Mismatched parenthesis pair type, e.g., ( paired with } as in ( 3 + 5 } Mismatched parenthesis pair orientation, e.g., ( paired with ( as in ( 3 + 5 ( An incorrectly formatted operand, e.g., 7.8k7o Your program should print Invalid expression in response to an input line that contains any of these errors. It is acceptable to end effort for evaluating an expression as soon as a formatting error is detected. To detect invalid formatting you should treat parenthesis symbols as you do other operators with respect to storing them in a stack. Error detection will also require care in matching parenthesis symbols and in determining what token, if any, is contained within a parenthesis pair. Detecting malformed operands can be accomplished by checking for a NumberFormatException

No inverse Trignometric function used

Input : ( 3 + [ 4 + 5 ] ) Output :12

Input : { 2 / ( 5 - 5 ) } Output :Infinity

Input : { 42 + [ 87 / ( { 3 - 9 } * 56 ) ] } Output : 41.74107142857143

Input : ( 3 + 5.8k2o ) Ouput : Invalid

Input : ( sin ( log ( 3.1 ^ 3 ) ) ) Ouput : -0.24993553919734932

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago