The following piece of code gets an infix string and will convert it to postfix and output that new expression as a string. However it does not support negative numbers or floats. The following code only allows for single digit values:
Such as (0-9) nothing like 10 or 11. Otherwise it throws a "key error". Also if I add a negative sign it also throws a key error.
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 isNumber(self, txt):
if not isinstance(txt,str) or len(txt.strip())==0:
print("Argument error in isNumber")
return False
# YOUR CODE STARTS HERE
try:
float(txt)
return True
except ValueError:
return False
#########################################################################################################
def infixToPostfix(infixexpr):
prec = {}
prec["^"] = 4
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()
for token in tokenList:
if token in "0123456789":
postfixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.isEmpty()) and \
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)
while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)
So here is my fix to allow floats as well:
I added this function:
def isNumber(x):
try:
float(x)
return True
except ValueError:
return False
And changed this line: if token in "0123456789":
into this: if Stack.isNumber(token):
And now the code allows floats.
So what is the other problem? Well the other problem is that my code assumes the input string will have exactly one space in between each of the characters, hence I string.split() to put all the characters in the list. Except the input string can have an arbitrary amount of spaces in between the characters, and if there are no spaces, my code will compare something like "((" to my list of characters and not find it and throw a Key error. So since I have to allow a negative numbers (noted by a minus sign). How can I modify my code so that it will no longer throw the keyerror
and allow me to have negative numbers?
When I do this:
print(Stack.infixToPostfix("( ( 1 + 3 ) ) * 4 - ( 9.2 - 0 ) * ( 5 + 8 )"))
My code outputs this:
1 3 + 4 * 9.2 0 - 5 8 + * -
Which works perfectly, however if I remove one space:
"(( 1 + 3 ) ) * 4 - ( 9.2 - 0 ) * ( 5 + 8 )"
My code no longer works. Key error '((' I know why it throws this error (explanation above) but I'm not sure how to fix it.
So final question TL:DR
How to modify my infixtopostfix code to allow for an arbitrary amount of spaces between the characters and allow for negative numbers?