Untitled

 avatar
unknown
plain_text
a year ago
824 B
11
Indexable
MAX = 10
stack = []
top = -1

def push(x):
    global top
    if top == MAX - 1:
        print("Stack is full")
    else:
        stack.append(x)
        top += 1

def pop():
    global top
    if top == -1:
        print("Stack is empty")
        return -9999
    else:
        top -= 1
        return stack.pop()

# Main Execution
expr = input("Enter postfix expression: ")

for ch in expr:
    if ch.isdigit():
        push(int(ch))
    elif ch in "+-*/":
        op2 = pop()
        op1 = pop()
        if ch == '+':
            push(op1 + op2)
        elif ch == '-':
            push(op1 - op2)
        elif ch == '*':
            push(op1 * op2)
        elif ch == '/':
            push(op1 // op2)  # integer division
    elif ch == ' ':
        continue  # skip spaces

result = pop()
print("The result is", result)
Editor is loading...
Leave a Comment