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
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...
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...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b)...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U 2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
Write a C++ program that converts an infix expression, which includes (, ), +, -, *,...
Write a C++ program that converts an infix expression, which includes (, ), +, -, *, and / operations to postfix notation. The program should allow the user to enter an infix expression using lower case characters, then it will display the result of conversion on the screen. (Note: Use the STL stack class to accomplish the solution.).
Given ?(?) = sqrt(3 + ? ) + 4. Use the definition of 4.4.3 to evaluate...
Given ?(?) = sqrt(3 + ? ) + 4. Use the definition of 4.4.3 to evaluate each of the area in the range [0, 6] if ? = 4. a) Find the area under the curve when ?? ∗ is the left endpoint of the subinterval. b) Find the area under the curve when ?? ∗ is the midpoints of the subinterval. c) Find the area under the curve when ?? ∗ is the right endpoint of the subinterval.
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression...
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6 2 + 5 * 8 4 / - The program should read the expression into StringBuilder or String infix and use the Stack and Stack Interface class...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW STACK CONFIGURATION DETAILS 20 3 18 8 - * 10 + +10 12 - /
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT