- 未知の想像です
- 実数の定数として、
- 最後に解決しました。
import re
class Item:
def __init__(self,imag=0,real=0): self.imag = imag self.real = real def __str__(self): return format("(%.6f : %.6fX)")%(self.real,self .imag) def __repr__(self): return self.__str__() def _calc(a,b,op): if op == '+': a.imag += b.imag a.real += b.real return a elif op == '-': a.imag -= b.imag a.real -= b.real elif op == '*': if b.imag == 0: a.imag *= b.real a.real *= b.real else: a.imag,a.real,b.imag,b.real= b.imag,b.real,a.imag,a.real a.imag *= b.real a.real *= b.real elif op == '/': if b.real== 0: raise Exception a.imag /= b.real a.real /= b.real return a def calculate(list): def _ca(oplist,nulist,i,stop,opers): op = oplist[-1] while op in opers: first,second = nulist[-2:] _calc(first,second,op) del nulist[-1] del oplist[-1] if len(oplist): op = oplist[-1] else: op = stop else: oplist.append(i) oplist = [] nulist = [] for i in list: if isinstance(i,str): if i == '(': oplist.append(i) elif i in '+-': if len(oplist): _ca(oplist,nulist,i,"(","+-*/") else: oplist.append(i) elif i in "*/": if len(oplist): _ca(oplist,nulist,i,"(","*/") else: oplist.append(i) else: if len(oplist): _ca(oplist,nulist,i,"stop","+-*/") del oplist[-1] del oplist[-1] else: nulist.append(i) _ca(oplist,nulist,i,"stop","+-*/") return nulist[0] if __name__ == "__main__":
出典:http://www.1994july.club/seo/?p=1703