Question

In: Computer Science

Question 1: Given the infix arithmetic expression as follows: Exp = Z * ((B + C...

Question 1:

Given the infix arithmetic expression as follows:

Exp = Z * ((B + C * D) / S + F)

            Where the values of the variables are:

                        Z = 3, B = 6, C = 2, D = 5, S = 4 , F = 21

            Evaluate the expression using a stack. Shows the logical representation of the evaluation process.

..............................................................................

Solutions

Expert Solution

The infix expression: 3 * ((6 + 2 * 5) / 4 + 21)
For this, we need to maintain two stacks, Operator and operand stacks respectively.

Steps:

  • If the character is an operand, we will push it to the operand stack.
  • If the character is an operator and the operator stack is empty, we will push it to the operator stack.
  • If the operator stack isn't empty, we will check:
    1. if input character's precedence is greater than the top of the operand stack, then we will simply push it.
    2. In the other case, we will pop two elements from the operand stack and one operator from the operator stack, perform the required operation and then push the result back to the operand stack.
  • If the input character is "(" then we will simply push it to the operator stack.
  • If it is ")", then we will pop two operand from operand stack, one operator from the operator stack, perform the operation and store the result to the operand stack. We will keep on doing this until we encounter "(" on top of the operator stack.

For your reference, the order of precedence from the highest to the lowest is shown here:

  1. ^
  2. * /
  3. + -
Character Action Operand stack Operator stack A little explanation
3 Push it to the operand stack 3
* Push it to the operator stack 3 *
( Push it to the operator stack 3 ( *
( Push it to the operator stack 3 ( ( *
6 Push it to the operand stack 6 3 ( ( *
+ Push it to the operator stack 6 3 + ( ( *
2 Push it to the operand stack 2 6 3 + ( ( *
* Push it to the operator stack 2 6 3 * + ( ( * As the top of the operator stack is containing the operator with lower precedence, we will just push it to this stack.
5 Push it to the operand stack 5 2 6 3 * + ( ( *
) Pop two operands from the top of the operand stack and pop one operator from the operator stack and perform the operation until "(" is encountered. 6 3 + ( ( * 5 and 2 will be popped out and * operation will be performed.
same as above Do 5 * 2 = 10 6 3 + ( ( *
same as above Push 10 to the operand stack 10 6 3 + ( ( *
same as above We haven't encountered "(" yet. So we will pop 10 and 6 from operand stack and pop + from operator stack. 3 ( ( *
same as above Do 6 + 10 = 16 3 ( ( *
same as above Push result 16 to the operand stack 16 3 ( ( *
same as above Now that we have encountered "(", top of the operator stack will be popped and then we will scan the next input. 16 3 ( *
/ Push it to the operator stack 16 3 / ( *
4 Push it to the operand stack 4 16 3 / ( *
+ Pop two elements from the top of operator stack and pop operator from the operator stack 3 ( * Top of operator stack is containing operator with higher precedence so we will perform the mentioned action
same as above Do 16 / 4 = 4 3 ( *
same as above Push 4 to the operand stack 4 3 ( *
same as above Now push + to the operator stack 4 3 + ( * As there is no operator on top of the operator stack, we don't to do any comparison
21 Push it to the operand stack 21 4 3 + ( *
) Pop 2 operands from operand stack and one operator from the operator stack 3 ( *
same as above Do 21 + 4 = 25 3 ( *
same as above Push 25 to the operator stack 25 3 ( *
) Pop "(" from the operator stack 25 3 * Top of the stack is not containing any operator. So we will simply pop "(".
Pop 2 operands from operand stack and one operator from the operator stack These are the only things left inside both the stacks
Do 25 * 3 = 75
Push 75 to the operand stack And this is the required answer.

Related Solutions

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.
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the infix notation, the operator comes between two operands. In the postfix notation, the operator comes after its two operands. For example, an expression a/b can be transformed to ab/. The following are some examples of the postfix expressions: 2+6*4 Its postfix notation is 2 6 4 * + 2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -...
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...
1. Consider an arithmetic expression of the form a#b=c. Check whether it is possible to replace...
1. Consider an arithmetic expression of the form a#b=c. Check whether it is possible to replace with one of the four signs: +, -, * or / to obtain a correct expression. Test Sample a b c Expected Output 1 2 3 5 True 2 8 2 4 True 3 8 3 2 False 4 6 3 3 True 5 5 2 0 False 6 10 2 2 False Make a MATLAB program
Bezout’s Theorem and the Fundamental Theorem of Arithmetic 1. Let a, b, c ∈ Z. Prove...
Bezout’s Theorem and the Fundamental Theorem of Arithmetic 1. Let a, b, c ∈ Z. Prove that c = ma + nb for some m, n ∈ Z if and only if gcd(a, b)|c. 2. Prove that if c|ab and gcd(a, c) = 1, then c|b. 3. Prove that for all a, b ∈ Z not both zero, gcd(a, b) = 1 if and only if a and b have no prime factors in common.
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.).
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token in the expression is an operator, If the operator stack is empty or the priority of the operator is greater...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
write a program that evaluates the following arithmetic expression: ((A+B)/C)*((D-A)+E). Assign test values to the variables...
write a program that evaluates the following arithmetic expression: ((A+B)/C)*((D-A)+E). Assign test values to the variables and display the resulting value.
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a postfix form, and then computes its value (by using stack-based algorithms). Assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT