示例#1
0
def blockToStr(block, string):
    if not (Expression.IsBlock(block)
            ) and not type(block) == Expression.Expression:
        return string + str(block)
    if type(block) == Expression.PowerBlock:
        pars = type(
            block.expressions[1]
        ) == Expression.ExpressionBlock  #Expression.IsBlock(block.expressions[1]) # wenn der nenner ein block ist, klammern setzen
        if pars:
            return blockToStr(block.expressions[0],
                              string) + "^(" + blockToStr(
                                  block.expressions[1], "") + ")"
        else:
            return blockToStr(block.expressions[0], string) + "^" + blockToStr(
                block.expressions[1], "")
    elif type(block) == Expression.QuotientBlock:
        pars = type(
            block.expressions[1]
        ) == Expression.ExpressionBlock  #Expression.IsBlock(block.expressions[1]) # wenn der nenner ein block ist, klammern setzen
        if pars:
            return blockToStr(block.expressions[0],
                              string) + "/(" + blockToStr(
                                  block.expressions[1], "") + ")"
        else:
            return blockToStr(block.expressions[0], string) + "/" + blockToStr(
                block.expressions[1], "")
    for el in block.expressions:
        if Expression.IsBlock(el):
            string += "("
            string = blockToStr(el, string)
            string += ")"
        else:
            string += str(el)
    return string
示例#2
0
def printBlock(block, indents):
    for el in block.expressions:
        if Expression.IsBlock(el):
            #print(type(el))
            printBlock(el,indents+2)
            continue
        print(indents*'-'+str(el))
示例#3
0
def PackFunctions(expression):
    '''
        Wandelt Teile der Art <FunctionElement> <ExpressionBlock> in einen FunctionBlock um
    '''
    new_expressions = []
    i = 0
    while i < len(expression.expressions):
        #for el in expression.expressions:
        el = expression.expressions[i]
        #if type(el) == Expression.ExpressionBlock:
        if Expression.IsBlock(el):
            block = (type(el))()
            block.expressions = PackFunctions(el)
            new_expressions.append(block)
        elif type(el) == Expression.FunctionElement:
            new_block = Expression.FunctionBlock()
            new_block.append(el)
            if (i == len(expression.expressions) - 1):
                raise Exception("KEIN FUNC ARGUMENT")
            new_block.append(expression.expressions[i + 1])
            new_expressions.append(new_block)
            i += 1
        else:
            new_expressions.append(el)
        i += 1
    return new_expressions
示例#4
0
def AddMultiplyOperators(expression):
    i = 0
    new_expressions = []
    while i < len(expression.expressions):

        el = expression.expressions[i]
        if not isinstance(el, Expression.OperatorElement) and (
                type(expression) == Expression.ExpressionBlock
                or type(expression) == Expression.Expression):
            before = None
            if (i != 0):
                before = new_expressions[len(new_expressions) - 1]
            if before != None:
                if not isinstance(
                        before, Expression.OperatorElement) and not isinstance(
                            before, Expression.FunctionElement):
                    new_expressions.append(Expression.multiplyOperator)

        if (Expression.IsBlock(el)):
            block = (type(el))()
            block.expressions = AddMultiplyOperators(el)

            new_expressions.append(block)
        else:
            new_expressions.append(el)

        if not isinstance(el, Expression.OperatorElement) and (
                type(expression) == Expression.ExpressionBlock
                or type(expression) == Expression.Expression):
            after = None
            if (i < len(expression.expressions) - 1):
                after = expression.expressions[i + 1]
            if after != None:
                if not isinstance(
                        after, Expression.OperatorElement) and not isinstance(
                            el, Expression.FunctionElement):
                    new_expressions.append(Expression.multiplyOperator)

        i += 1
    return new_expressions
示例#5
0
def PackQuotientBlocks(expression):
    i = 0
    new_expressions = []
    while i < len(expression.expressions):

        el = expression.expressions[i]
        if Expression.IsBlock(el):

            block = (type(el))()
            block.expressions = PackQuotientBlocks(el)
            new_expressions.append(block)
        elif el == Expression.divideOperator:
            if (i == 0):
                raise Exception("Error: No numerator before divide operator")
            elif (i == len(expression.expressions) - 1):
                raise Exception("Error: No denominator after divide operator")
            quotientBlock = Expression.QuotientBlock()
            # before_el = expression.expressions[i-1]
            before_el = new_expressions[len(new_expressions) - 1]
            after_el = expression.expressions[i + 1]
            denom = Expression.ExpressionBlock(after_el)

            if isinstance(after_el, Expression.OperatorElement):
                denom = Expression.ExpressionBlock(
                    after_el, expression.expressions[i + 2])
                i += 1

            new_expressions.pop(len(new_expressions) - 1)

            quotientBlock.append(before_el)
            quotientBlock.append(denom)
            new_expressions.append(quotientBlock)
            i += 1
        else:
            new_expressions.append(el)
        i += 1
    return new_expressions
示例#6
0
def PackPowerBlocks(expression):
    new_expressions = []
    i = 0
    while i < len(expression.expressions):
        el = expression.expressions[i]
        if Expression.IsBlock(el):
            block = (type(el))()
            block.expressions = PackPowerBlocks(el)
            new_expressions.append(block)
        elif el == Expression.powerOperator:
            if (i == 0):
                raise Exception("Error: No base before power operator")
            elif (i == len(expression.expressions) - 1):
                raise Exception("Error: No exponent after power operator")
            powerBlock = Expression.PowerBlock()

            #      before_el = expression.expressions[i-1]
            before_el = new_expressions[len(new_expressions) - 1]
            after_el = expression.expressions[i + 1]

            exp = after_el

            if isinstance(after_el, Expression.OperatorElement):
                exp = Expression.ExpressionBlock(after_el,
                                                 expression.expressions[i + 2])
                i += 1

            new_expressions.pop(len(new_expressions) - 1)

            powerBlock.append(before_el)
            powerBlock.append(exp)
            new_expressions.append(powerBlock)
            i += 1
        else:
            new_expressions.append(el)
        i += 1
    return new_expressions