def parse(s: str): if s.find('+') > 0: pos = s.find('+') s1 = s[0:pos] s2 = s[pos+1:] return Add(parse(s1),parse(s2)) if s.rfind('-') > 0: pos = s.find('-') s1 = s[0:pos] s2 = s[pos+1:] return Sub(parse(s1),parse(s2)) if s.find('*') > 0: pos = s.find('*') s1 = s[0:pos] s2 = s[pos+1:] return Mul(parse(s1),parse(s2)) if s.find('/') > 0: pos = s.find('/') s1 = s[0:pos] s2 = s[pos+1:] return Div(parse(s1),parse(s2)) return Val(int(s))
def parse(s: str): if s.rfind('+') > 0: #足し算 pos = s.rfind('+') s1 = s[:pos] s2 = s[pos + 1:] return Add(parse(s1), parse(s2)) elif s.rfind('-') > 0: #引き算 pos = s.rfind('-') s1 = s[:pos] s2 = s[pos + 1:] return Sub(parse(s1), parse(s2)) elif s.rfind('*') > 0: #かけ算 pos = s.rfind('*') s1 = s[:pos] s2 = s[pos + 1:] return Mul(parse(s1), parse(s2)) elif s.rfind('/') > 0: #割り算 pos = s.rfind('/') s1 = s[:pos] s2 = s[pos + 1:] return Div(parse(s1), parse(s2)) else: num = int(s) return Val(num)
def parse(s: str): pos = s.rfind("+") # +文字の位置を見つける if pos > 0: s1 = s[0:pos] s2 = s[pos+1:] return Add(parse(s1), parse(s2)) pos = s.rfind("-") # -文字の位置を見つける if pos > 0: s1 = s[0:pos] s2 = s[pos+1:] return Sub(parse(s1), parse(s2)) pos = s.rfind("*") # *文字の位置を見つける if pos > 0: s1 = s[0:pos] s2 = s[pos+1:] return Mul(parse(s1), parse(s2)) pos = s.rfind("/") # /文字の位置を見つける if pos > 0: s1 = s[0:pos] s2 = s[pos+1:] return Div(parse(s1), parse(s2)) num = int(s) return Val(num)
def parse(s: str): if s.find('+') > 0: pos = s.find('+') s1 = s[0:pos] s2 = s[pos + 1:] num = Add(parse(s1), parse(s2)) return num if s.find('-') > 0: pos = s.find('-') s1 = s[0:pos] s2 = s[pos + 1:] num = Sub(parse(s1), parse(s2)) return num if s.find('*') > 0: pos = s.find('*') s1 = s[0:pos] s2 = s[pos + 1:] num = Mul(parse(s1), parse(s2)) return num if s.find('/') > 0: pos = s.find('/') s1 = s[0:pos] s2 = s[pos + 1:] num = Div(parse(s1), parse(s2)) return num return Val(int(s))
def parse(s: str): if s.find('+') > 0: pos = s.find('+') s1 = s[0:pos] s2 = s[pos + 1:] return Add(parse(s1), parse(s2)) if s.find('-') > 0: pos = s.find('-') s1 = s[0:pos] s2 = s[pos + 1:] return Sub(parse(s1), parse(s2)) if s.find('*') > 0: pos = s.find('*') s1 = s[0:pos] s2 = s[pos + 1:] return Mul(parse(s1), parse(s2))
def parse(s: str): if s.find("+") > 0: pos = s.find("+") s1 = s[0:pos] s2 = s[pos + 1:] return Add(parse(s1), parse(s2)) if s.rfind("-") > 0: pos = s.rfind("-") s1 = s[0:pos] s2 = s[pos + 1:] return Sub(parse(s1), parse(s2)) if s.find("*") > 0: pos = s.find("*") s1 = s[0:pos] s2 = s[pos + 1:] return Mul(parse(s1), parse(s2)) if s.rfind("/") > 0: pos = s.rfind("/") s1 = s[0:pos] s2 = s[pos + 1:] return Div(parse(s1), parse(s2)) return Val(int(s))
def parse(r: str): if r.find('-') > 0: pos = r.find('-') s1 = r[0:pos] s2 = r[pos + 1:] return Sub(parse(s1), parse(s2))
pos = s.find('+') s1 = s[0:pos] s2 = s[pos + 1:] num = Add(parse(s1), parse(s2)) return num if s.find('-') > 0: pos = s.find('-') s1 = s[0:pos] s2 = s[pos + 1:] num = Sub(parse(s1), parse(s2)) return num if s.find('*') > 0: pos = s.find('*') s1 = s[0:pos] s2 = s[pos + 1:] num = Mul(parse(s1), parse(s2)) return num if s.find('/') > 0: pos = s.find('/') s1 = s[0:pos] s2 = s[pos + 1:] num = Div(parse(s1), parse(s2)) return num return Val(int(s)) a = Sub(1, 2) print(a.eval()) e = parse("1-2-3") print(e, e.eval())