I am creating a postfix calculator that accepts arithmetic expressions and first pushes the operators to the stacl.
./pythonfilename 3 4 1 + - was used as my input. However, since no output is displayed, I tried to debug my program to see why my program is not taking in my argument. does not result in printing any output. I hit Ctrl+C to display the Traceback Call and it points out x = sys.stdin.readlines.
#!/usr/bin/python
import sys
import fileinput
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self,item):
self.items.append(item)
def pop(self):
return self.items(pop)
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
def is_number(line):
try:
float(line)
except ValueError:
return False
def infixtoPostfix():
initStack=Stack()
x = sys.stdin.readlines() #read user input
for lines in x:#for lines in fileinput.input():
for line in lines.strip().split(" "):
if is_number(line):
initStack.push(line)
line = float(line)
elif line =='+':
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum + secNum
initStack.push(result)
print initStack.peek()
elif line == '-':
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum - secNum
initStack.push(result)
print initStack.peek()
elif line == '*':
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum * secNum
initStack.push(result)
print initStack.peek()
elif line == "/":
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum / secNum
initStack.push(result)
print initStack.peek()
elif line == "%":
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum % secNum
initStack.push(result)
print initStack.peek()
infixtoPostfix()