Question

In: Computer Science

Use the algorithm below to evaluate the following infix expression: a) a + 3 * 4...

Use the algorithm below to evaluate the following infix expression: a) a + 3 * 4 – 9 b) ( 2 + 6 ) / ( 3 – 5 ) . Algorithm

WRITE STEP BY STEP

Scan the characters in the infix expression. While there are characters left in the infix expression: Check the next character in the expression.

1. If the next character is operand, remove it from the expression and push it onto the operand stack.

2. If the next character is the operator ‘^’, remove it from the expression and push it onto the operator stack.

3. If the next character is one of the operators ‘+’, ‘-‘, ‘*’ and ‘/’: check its precedence.

3.1. While the operator stack is not empty and the operator in the expression has a lower or same precedence than that at the top of the operator stack: - Pop an item from the operator stack. If it is ‘(‘, discard it, otherwise place the operator in the ‘Calculator’. - Pop two operands from the operand stack and place them in the ‘Calculator’. Note that the first popped value will be the second operand in the ‘Calculator’. - Push the result from the ‘Calculator’ onto the operand stack.

3.2. Remove the operator from the expression and push it onto the operator stack.

4. If the next character is ‘(‘, remove it from the expression and push it onto the operator stack. Note: when comparing the precedence of operators in the operator stack, a ‘(‘ has a lower precedence than any operator.

5. If the next character is ‘)‘, remove x it from the expression and discard it, and repeat the steps in 3.1 until ‘(‘ pops. Discard ‘(‘. When there are no more characters in the expression string, check the operator stack. While the operator stack is not empty: - Pop an item from the operator stack. - If it is ‘(‘, discard it, - Otherwise: - Place the operator in the ‘Calculator’. - Pop two operands from the operand stack and place them in the ‘Calculator’. Note that the first popped value will be the second operand in the ‘Calculator’. - Push the result from the ‘Calculator’ onto the operand stack. Upon completion, there should be only one value left in the stack and this is the value of the expression

Solutions

Expert Solution

a + 3 * 4 – 9

# indicate to empty

Symbol

Operand stack

Operator Stack

Pop operator

Operand 1

Operand 2

Calculate

a

a

#

+

a

#, +

3

a,3

#,+

*

a,3

#,+,*

4

a,3,4

#,+,*

-

a,

#,+

*

3

4

34*

a, 34*

#

+

a

34*

a34*+

a34*+

#, -

9

a34*+,9

#, -

-

a34*+

9

a34*+9-

a34*+9-

#

( 2 + 6 ) / ( 3 – 5 )

# indicate to empty

Symbol

Operand stack

Operator Stack

Pop operator

Operand 1

Operand 2

Calculate

(

#,(

2

2

#, (

+

2

#,( ,+

6

2,6

#,(, +

)

#,(,+

+

2

6

26+

26+

#

/

26+

#,/

(

26+

#,/,(

3

26+, 3

#, / , (

-

26+, 3

#, / , (, -

5

26+, 3,5

#, / , (, -

)

26+

#, /

-

3

5

35-

26+,35-

#, /

#

/

26+

34-

26+34-/

26+34-/

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot.


Related Solutions

Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Following is an infix expression.                                       &n
Following is an infix expression.                                           ((A ^ B) ^ C ^ M * W / X ) ^ Y ^ Z Convert it into postfix and prefix using stack and verify through binary tree. Evaluate infix, postfix and prefix with the following values. A = 2, B = 2, C = 3, M = 1, W = 4, X = 8, Y = 1, Z = 3
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expres
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expression                                                 Stack                             Postfix Expression ---------------------------------------------------------------------------------------------------------------------------- 8 - 9*(2 + 1/4) + 3*7                                                     (empty)                            (blank) Evaluate the following Postfix expression. Once again, show all work.            Postfix                                               Stack                     Result           ---------------------------------------    --------------------    --------------            2 7 + 12 4 / * 8 5 + -
2. Convert the following infix form expression into the postfix form expression by using a stack:...
2. Convert the following infix form expression into the postfix form expression by using a stack: A+(B*(C-D)+E)/F-G*H Show the stack after each push/pop.
Write a program that takes an infix expression as input and produces a postfix expression. The...
Write a program that takes an infix expression as input and produces a postfix expression. The infix and postfix expressions are in the form of vectors of strings. We will test with a main method that takes a file name as input from the command line. We will test your implementation using printPostFix() method.   There are sample input and output files in this folder. You may edit the header file. Example 1 infix expression = apple + banana * cat...
Implement an infix expression to postfix expression convertor in C++. Note: - Support +, -, *,...
Implement an infix expression to postfix expression convertor in C++. Note: - Support +, -, *, /, ( and ) in infix expressions. - Operands will be nonnegative only. - Assume infix expressions are valid and well formatted (one blank space to separate operand/operator) For example, - 3 * 4 + 5 ➔ 3 4 * 5 + - 3 + 4 * 5 ➔ 3 4 5 * + - (3 + 4) * (5 + 6) ➔ 3...
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix...
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and Queue classes (my code is a...
Convert an infix expression to its postfix representation in JAVA
Convert an infix expression to its postfix representation in JAVA
Arithmetic Expression Evaluation Write a program that reads an infix expression (that contains only A, B...
Arithmetic Expression Evaluation Write a program that reads an infix expression (that contains only A, B and/or C as operands) from the user and prints out the value of the expression as an output. Implement the following methods: o String infixToPostfix(String expr) Converts the given infix expression expr and returns the corresponding postfix notation of expr. o Integer evaluatePostfixExpr(String expr) Evaluates and returns the value of the given postfix expression expr. Sample Input/Output #1 Enter your arithmetic expression: A+B-C*A Enter...
Show step-by-step how Java will evaluate the following expression: 8 % 3 + 3 % 2...
Show step-by-step how Java will evaluate the following expression: 8 % 3 + 3 % 2 – 9 % 2.0 * 5 15/15 % 7 *2   – 11. /4 * 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT