U E D R , A S I H C RSS

몸짱프로젝트/Infix To Prefix

Python version

  • 개발자 : 나휘동
ExpressionConverter.py
~cpp 
class ExpressionConverter:
    def __init__(self, aExpression = ''):
        self.expression = aExpression
        self.precedence = {'*':13, '+':12, '-':12, '/':13}
        self.list = []

    def getPrecedence(self, aToken):
        return self.precedence[aToken]

    def isOperator(self, aToken):
        if aToken in self.precedence:
            return True
        return False
    def getConvertedExpression(self):
        expression = ''
        for l in self.list:
            expression += l
        return expression

    def putToken(self, aToken):
        if self.isOperator(aToken):
            self.putOperator(aToken)
        else:
            self.list.append(aToken)

    def putOperator(self, aOperator):
        if self.isOperator(self.list[0]) and \
           self.precedence[self.list[0]] < self.precedence[aOperator]:
            self.list.insert(-1, aOperator)       
        else:
            self.list.insert(0, aOperator)

    def convertInfixToPrefix(self):
        for e in self.expression:
            self.putToken(e)

#########################################################################

import unittest

class ExpressionConverterTestCase(unittest.TestCase):
    def testGetPrecedence(self):
        e = ExpressionConverter()

        token = '*'
        self.assertEqual(e.getPrecedence(token), 13)
        token = '+'
        self.assertEqual(e.getPrecedence(token), 12)
    def testIsOperator(self):
        e = ExpressionConverter()

        token = 'a'
        self.assertEqual(e.isOperator(token), False)
        token = '+'
        self.assertEqual(e.isOperator(token), True)

    def testGetConvertedExpression(self):
        e = ExpressionConverter()
        e.list = '+a'
        self.assertEqual( e.getConvertedExpression(), '+a')
        
    def testPutToken(self):
        e = ExpressionConverter()

        token = 'a'
        e.putToken(token)
        token = '+'
        e.putToken(token)
        self.assertEqual( e.getConvertedExpression(), '+a')

        token = 'b'
        e.putToken(token)
        self.assertEqual( e.getConvertedExpression(), '+ab')

    def testPutOperator(self):
        e = ExpressionConverter()
        token = 'a'
        e.putToken(token)
        token = '+'
        e.putToken(token)
        token = 'b'
        e.putToken(token)

        token = '*'
        e.putOperator(token)
        self.assertEqual( e.getConvertedExpression(), '+a*b')

    def testConvertInfixToPrefix(self):
        expression = 'a+b*c'
        e = ExpressionConverter(expression)

        e.convertInfixToPrefix()
        self.assertEqual( e.getConvertedExpression(), '+a*bc')

        expression = 'a+b*c/x'
        e = ExpressionConverter(expression)
        
        e.convertInfixToPrefix()
        self.assertEqual( e.getConvertedExpression(), '+a*b/cx')
if __name__ == '__main__': 
    unittest.main()

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:29
Processing time 0.0189 sec