# 【探索】利用后缀表达式解方程 - Limour's Blog
这段Python代码实现了使用后缀表达式解方程的功能。它包括了一个栈类（Stack）和一个多项式类（Polynomial）。通过中缀表达式转后缀表达式的方式处理方程，最终实现了解一元一次方程的功能。代码包含了多个方法，如多项式的加法、减法、乘法、除法等操作。在解方程时，它先进行表达式的简化和转换，然后输出方程的解或者判断方程是否是一元一次方程。
Author: Limour

# 【探索】利用后缀表达式解方程

Last updated on March 19, 2024 pm

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145

    import re
    from fractions import Fraction

    class Stack(list):
        def isEmpty(self):
            return self == []
        def peek(self):
            if self == []: return None
            else: return self[-1]
        def size(self):
            return len(self)
        push = list.append
        def pop(self):
            if self == []: return None
            else: return super().pop()

    class Polynomial(list):
        def __init__(self, value):
            for item in value: self.append(Fraction(item))
        def add(self, value):
            if len(self) < len(value): self += [Fraction(0)]*(len(value)-len(self))
            for i in range(len(value)): self[i] += value[i]
        def sub(self, value):
            if len(self) < len(value): self += [Fraction(0)]*(len(value)-len(self))
            for i in range(len(value)): self[i] -= value[i]
        def mul(self, value):
            tmp = self.copy()
            size = len(self)
            self.clear()
            self += [Fraction(0)]*(size+len(value)-1)
            for i,item in enumerate(value):
                for j in range(size):
                    self[i+j] += tmp[j]*item
        def divn(self, n):
            if type(n) is Polynomial:
                for i in range(len(self)): self[i] /= n[0]
            else:
                _n = Fraction(n)
                for i in range(len(self)): self[i] /= _n
        def __str__(self):
            if self == []:
                return '0'
            elif len(self) == 1:
                return str(self[0])
            elif len(self) == 2:
                return f'({self[1]})x + {self[0]}'
            else: pass

    def get_Formula(equation):
        return equation.replace(' ','').split('=')

    _ep = re.compile(r'([\+\-\*/()][^\+\-\*/()]+)')
    _op = {
        '+': lambda x,y:x.add(y),
        '-': lambda x,y:x.sub(y),
        '*': lambda x,y:x.mul(y),
        '/': lambda x,y:x.divn(y)
    }
    def _middle2behind(Fma, res, s, e):
        sta = Stack()
        _s = s
        while s < e:
            if Fma[s] in '+-':
                if sta.isEmpty():
                    sta.push(Fma[s])
                    s += 1
                elif sta.peek() in '*/':
                    while sta: res.push(sta.pop())
                    sta.push(Fma[s])
                    s += 1
                elif sta.peek() in '+-':
                    res.push(sta.pop())
                    sta.push(Fma[s])
                    s += 1
            elif Fma[s] in '*/':
                if sta.isEmpty():
                    sta.push(Fma[s])
                    s += 1
                elif sta.peek() in '+-':
                    sta.push(Fma[s])
                    s += 1
                elif sta.peek() in '*/':
                    res.push(sta.pop())
                    sta.push(Fma[s])
                    s += 1
            elif Fma[s] == '(':
                s += 1
                d = _middle2behind(Fma, res, s, e)
                s += d
            elif Fma[s] == ')':
                s += 1
                break
            else:
                res.push(Fma[s])
                s += 1
        while sta: res.push(sta.pop())
        return s-_s
    def middle2behind(Formula):
        if Formula.startswith('-'): Formula = '0' + Formula
        expr = _ep.findall(Formula.replace('(-','(0-'))
        res = Stack()
        _middle2behind(expr, res, 0, len(expr))
        return res
    def str2polynomial(_str):
        if _str.endswith('x'):
            if _str == 'x': return Polynomial((0, 1))
            return Polynomial((0, _str.rstrip('x')))
        else: return Polynomial((_str,))
    def not_eval(Formula):
        expr = middle2behind(Formula)
        #print(expr)
        sta = Stack()
        for item in expr:
            if item in _op:
                y = sta.pop()
                x = sta.pop()
                _op[item](x, y)
                sta.push(x)
            else: sta.push(str2polynomial(item))
            #print(sta)
        return sta.pop()

    _x = re.compile(r'[a-zA-Z]+')
    def solve_eq(equation):
        print(f'equation is \t\t{equation}')
        xname = set(_x.findall(equation))
        if len(xname) != 1:
            #print(xname)
            print(f'别逗，{equation}是一元一次方程吗?')
            return
        xname = xname.pop()
        Fma = get_Formula(_x.sub('x', equation))
        if len(Fma) != 2:
            print('{equation}不是标准方程！')
            return
        Fma = [not_eval(expr) for expr in Fma]
        print(f'Simplification is \t{Fma[0]} = {Fma[1]}')
        tmp = Fma[0]
        tmp.sub(Fma[1])
        print(f'Transposition is \t{tmp} = 0')
        if len(tmp) < 2 or tmp[1] == 0:
            print(f'{xname} 无解')
        else:
            x = -tmp[0]/tmp[1]
            print(f'{xname} = x = {x}')

#探索
【探索】利用后缀表达式解方程
https://hexo.limour.top/Solving-equations-using-postfix-notation
Author
Limour
Posted on
December 12, 2020
Updated on
March 19, 2024
Licensed under
【迁移】二代测序数据处理之数据格式说明 Previous

#### Search

Keyword