Python Forum

Full Version: prefix to infix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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 ()
How would you determine by hand if parentheses are needed or not? Give a set of rules for this before writing the code.
Like in normal math.. so for example output in normal math will be 7+7+7+7 .. This code makes outputs like (7+7)+7)+7)
rontoto Wrote:Like in normal math..
This doesn't make a set of rules. I know that mathematically, the result of 7+7+7 is the same as ((7+7)+7) but for your program it is very different because if the program outputs the latter, you'll say that there are useless parentheses and you want to output the former. So you need to define precisely in which cases the program should output parentheses and in which cases it should not. That's what I mean by a set of rules. If you cannot describe it with words, you cannot describe it with code.
Yeah. I understand. But u know, problem is, when u are unsure about prefix to infix in real life because I do that for the first time. I changed my code, but dont know, if I coded all of the options. And second problem is, that input comes like this

/-*+*++* *85 27 39 87 65 65 37 63 91
or this
+*-73+54 64 52/*97 22 36

and i cannot remember how to change the input in code (I did gaps for each symbol for testing)
a = list(map(str, input().split()))
class Calculator:

    def __init__ (self):
        self.stack = []

    def push (self, p):
        if p in ['+', '-', '*', '/', "**"]:
            op1 = self.stack.pop ()
            op2 = self.stack.pop ()

            if p in['+', '-',"**"]:
                self.stack.append ('%s%s%s' % (op1, p, op2))
            if p in ["*", "/", ]:
                if  "+" or "-" not in self.stack:
                    self.stack.append('(%s)%s%s' % (op1, p, op2))
                else:
                    self.stack.append('%s%s(%s)' % (op1, p, op2))

        else:
            self.stack.append (p)

    def convert (self, l):
        l.reverse ()
        for e in l:
            self.push (e)
        return self.stack.pop ()

c = Calculator ()

print (c.convert ( a ))