简体   繁体   中英

Python Prefix / Infix / Postfix expression evaluation

Little brief about the code: I have to do a class that will evaluate prefix, postfix or infix expression. It has to determine whether it is pre/post/infix and convert them to postfix, for example prefixTOpostfix() (others are deleted as not needed now) method in the code which converts from '/x7' to 'x7/', the expression is edited in method edit() from 'x7/' to 'x 7 /'. Both methods work fine, tested on multiple examples (not posting here the whole code as it is and assignment, but also they are not needed. The question being asked is just an error I am getting, don't worry I am not asking for solution to my assignment). There is also assign method, because there can be variables, for example 'a = 3', and it can be somewhere in the expression. Problem: When i run print(v.evaluate('x 7/')) (what is already in postfix), where x = 14 , it returns 2 as it should. However when I run print(v.evaluate('/x 7')) (what is in prefix) it returns None . Both expressions look exactly the same after they ran through their methods 'x 7 /' (I had testing prints in the code), Both stacks are the same. First there is '14' , then '14 7' , and lastly '2' . When I change return(s.pop()) to return(s.top()) , both expressions are evaluated fine as '2' . So why return(s.pop()) doesn't work with the second one ? If there are more questions about the code or something isn't clear enough, tell me I will try to explain it differently.

class MyClass:

    class Stack:
        ...

    def __init__(self):
        self.table = {}

    def __repr__(self):
        ...

    def assign(self, variable, exp):

        if '+-*/%' in exp: # temporary solution
            exp = self.evaluate(exp)

        self.table[variable] = exp

    def evaluate(self, exp):

        if exp[0] in '+-*/%': # Prefix
            new_exp = self.prefixTOpostfix(exp)
            self.evaluate(new_exp)

        elif exp[len(exp)-1] in '+-*/%': # Postfix
            s = self.Stack()
            exp = self.edit(exp) # from 'x7/' to 'x 7 /'

            for item in exp.split():

                if item == '+':
                    s.push(s.pop() + s.pop())

                ... # other operations

                elif prvok == '/':
                    temp = s.pop()
                    if temp == 0:
                        return None
                    s.push(s.pop() // temp) # it has to be // !

                else: # if it is number / variable
                    if item in self.table:
                        s.push(int(self.table[item]))
                    else:
                        s.push(int(item))

                s.printOUT()

            return(s.pop())

        else: # Infix
            ...


    def prefixTOpostfix(self, exp):

        ...

    def edit(self, exp):

        ...
    if exp[0] in '+-*/%': # Prefix
        new_exp = self.prefixTOpostfix(exp)
        self.evaluate(new_exp)

You need to return the result of your recursive call.

    if exp[0] in '+-*/%': # Prefix
        new_exp = self.prefixTOpostfix(exp)
        return self.evaluate(new_exp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM