Question

In: Computer Science

Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined...

Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if “(exp1)op(exp2)” is a normal, fully parenthesized expression whose operation is op, the postfix version of this is “pexp1 pexp2 op”, where pexp1 is the postfix version of exp1 and pexp2 is the postfix version of exp2. The postfix version of a sin- gle number or variable is just that number or variable. For example, the postfix version of "((5+2) * (8-3))/4" is "5 2 + 8 3 - * 4 /". Write a Python program that evaluate a postfix expression non-recursively if consider only binary operators "+", "*", "/", "-".

Solutions

Expert Solution

Solution:

# Python program to evaluate value of a postfix expression

# Class to convert the expression

class Evaluate:

# Constructor to initialize the class variables

def __init__(self, capacity):

self.top = -1

self.capacity = capacity

# This array is used a stack

self.array = []

# check if the stack is empty

def isEmpty(self):

return True if self.top == -1 else False

# Return the value of the top of the stack

def peek(self):

return self.array[-1]

# Pop the element from the stack

def pop(self):

if not self.isEmpty():

self.top -= 1

return self.array.pop()

else:

return "$"

# Push the element to the stack

def push(self, op):

self.top += 1

self.array.append(op)

# The main function that converts given infix expression to postfix expression

def evaluatePostfix(self, exp):

# Iterate over the expression for conversion

for i in exp:

# If the scanned character is an operand

# (number here) push it to the stack

if i.isdigit():

self.push(i)

# If the scanned character is an operator,

# pop two elements from stack and apply it.

else:

val1 = self.pop()

val2 = self.pop()

self.push(str(eval(val2 + i + val1)))

return (self.pop())

# Driver program to test above function

exp = "52+83-*4/"

obj = Evaluate(len(exp))

print("Value of %s is %s" %(exp, obj.evaluatePostfix(exp)))

Output:

Please give thumbsup, if you like it. Thanks.


Related Solutions

(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
Write in Java Postfix notation is a way of writing expressions without using parentheses. For example,...
Write in Java Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two operands with the...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
Your assignment for this program is to evaluate a numeric expression in postfix notation using a...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a dynamic (pointer based) stack. As stated in the handout, in order to evaluate a numeric expression, a compiler converts an infix numeric expression to postfix notation and then it uses an algorithm and a stack to evaluate the expression. Your program should implement the pseudocode algorithm described in the attached handout. Your program will read and evaluate expressions stored in an input file (infile.txt)....
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 -...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
the postfix notation for (7-4)+2 is
the postfix notation for (7-4)+2 is
Postfix Arithmetic Operation (C++) Friday, 27 September 2019, 04:21 PM Use stack to implement postfix arithmetic...
Postfix Arithmetic Operation (C++) Friday, 27 September 2019, 04:21 PM Use stack to implement postfix arithmetic operation. Input Many test cases until EOF. Each line is a postfix arithmetic operation formula. There is no blank between the numbers, and the number only is 0-9. Output The result of each line. But if the postfix arithmetic operation formula is wrong (it means you can't get the correct answer), please output "Input Error". Sample Input 35+ 3+5 63/14-*3+8- Sample Output 8 Input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT