Infix到Postfix Converter Python 2.7

时间:2012-12-01 16:05:41

标签: python python-2.7 postfix-operator infix-operator

我试图在python中为homework分配创建一个infix到postfix转换器,我在网上发现了多个看起来很简单但没有一个符合我需要的要求。我必须使用以下类:

class Token(object):

    UNKNOWN     = 0             # unknown
    INT         = 4             # integer
    MINUS       = 5             # minus operator
    PLUS        = 6             # plus operator
    MUL         = 7             # multiply operator
    DIV         = 8             # divide operator

    FIRST_OP    = 5             # first operator code

    def getPrecedence(self):
        if self is '(':
            return 0

        elif self is '+'or '-':
            return 1

        elif self is '*' or '/':
            return 2

        else:
            return 3

    def _init_(self, value):
        if type(value) == int:
            self._type = Token.INT
        else:
        self._type = self._makeType(value)
        self._value = value

    def isOperator(self):
        return self._type >= Token.FIRST_OP

    def _str_(self):
        return str(self._value)

    def getType(self):
        return self._type

    def getValue(self):
        return self._value

    def _makeType(self, ch):
        if   ch == '*': return Token.MUL
        elif ch == '/': return Token.DIV
        elif ch == '+': return Token.PLUS
        elif ch == '-': return Token.MINUS
        else:           return Token.UNKNOWN;

我必须添加getPrecedence():方法,该方法返回一个表示运算符优先级的Integer。我还必须使用以下课程:

from token import Token

class Scanner(object):

    EOE = ';'        # end-of-expression
    TAB = '\t'       # tab

    def __init__(self, sourceStr):
        self._sourceStr = sourceStr
        self._getFirstToken()

    def hasNext(self):
        return self._currentToken != None

    def next(self):
        if not self.hasNext():
            raise Exception, "There are no more tokens"            
        temp = self._currentToken
        self._getNextToken()
        return temp

    def _getFirstToken(self):
        self._index = 0
        self._currentChar = self._sourceStr[0]
        self._getNextToken()

    def _getNextToken(self):
        self._skipWhiteSpace()
        if self._currentChar.isdigit():
            self._currentToken = Token(self._getInteger())
        elif self._currentChar == Scanner.EOE:
            self._currentToken = None
        else:
            self._currentToken = Token(self._currentChar)
            self._nextChar()

    def _nextChar(self):
        if self._index >= len(self._sourceStr) - 1:
            self._currentChar = Scanner.EOE
        else:
            self._index += 1
            self._currentChar = self._sourceStr[self._index]

    def _skipWhiteSpace(self):
        while self._currentChar in (' ', Scanner.TAB):
            self._nextChar()

    def _getInteger(self):
        num = 0
        while True:
            num = num * 10 + int(self._currentChar)
            self._nextChar()
            if not self._currentChar.isdigit():
                break
        return num

我必须编写一个将中缀表达式转换为后缀表达式的程序。该程序应该使用Token和Scanner类(我已经包含在上面)。该程序应包括一个预先形成输入和输出的主函数,以及一个名为IFToPFConverter的类。主函数接收输入字符串,并使用它创建扫描程序。然后将扫描器作为参数传递给转换器对象的构造函数。然后运行转换器对象convert方法来转换中缀表达式。此方法返回表示后缀字符串的标记列表。然后main函数显示该字符串。以下是我到目前为止的情况:

from arrayStack import ArrayStack
from token import Token
from scanner import Scanner

class IFToPFConverter(object):

     def convert(self):
        opStack = Stack()
        postFixList = []

        while self._scanner.hasNext():
            currentToken = self._scanner.next()
            if currentToken in '0123456789'
                postFixList.append(currentToken)
            elif currentToken == '(':
                opStack.push(currentToken)
            elif currentToken == '*':
                opStack.push(currentToken)
            elif currentToken == '/':
                opStack.push(currentToken)
            elif currentToken == '+':
                opStack.push(currentToken)
            elif currentToken == '-':
                opStack.push(currentToken)
            elif currentToken == ')':
                opStack.push(currentToken)

         while not opStack.isEmpty():                

def main():
    sourceStr = raw_input("Please enter an expression:")
    scanner = Scanner(sourceStr)
    conversion = IFToConverter.convert(scanner)
    return conversion

main()  

我不知道从哪里开始,我甚至不知道我在IFToPFConverter类中尝试做的是否可行。我已经看到了更简单的中缀到postfix转换器,我对编程很新,所以这似乎要理解很多。我可以使用任何可能的帮助,谢谢你提前。

0 个答案:

没有答案