Dec-03-2019, 11:05 AM
Hello, i have got a rpbolem with prefix to infix code.
I need to change this code, so it can work with input like this: +-4 2 3 or *4-2 3
and the output must be without useless parenthesis ............ 4-2+3 or 4*(2-3)
Can u help me please? Here is the code
I need to change this code, so it can work with input like this: +-4 2 3 or *4-2 3
and the output must be without useless parenthesis ............ 4-2+3 or 4*(2-3)
Can u help me please? Here is the code
class Calculator:
def __init__ (self):
self.stack = []
def push (self, p):
if p in ['+', '-', '*', '/']:
op1 = self.stack.pop ()
op2 = self.stack.pop ()
self.stack.append ('(%s %s %s)' % (op1, p, op2) )
elif p == '!':
op = self.stack.pop ()
self.stack.append ('%s!' % (op) )
elif p in ['sin', 'cos', 'tan']:
op = self.stack.pop ()
self.stack.append ('%s(%s)' % (p, op) )
else:
self.stack.append (p)
def convert (self, l):
l.reverse ()
for e in l:
self.push (e)
return self.stack.pop ()
c = Calculator ()
