机试前两题不难,第三题坑好多,最后也没AK
做了个比较简洁的实现,用python的locals()+eval(),其实可以很好处理加减乘除。
import sys d = locals() for line in sys.stdin: line = line.strip().replace("/", "//") if line.startswith("let "): t = line[4:].split(" = ") if len(t) != 2 or not t[0].isidentifier(): print("<syntax-error>") else: name, expr = t try: # d[name] = eval(expr) l = expr.split() while len(l) > 1: x = eval(" ".join(l[:3])) l = [str(x)] + l[3:] d[name] = int(l[0]) except NameError: d[name] = "UNDEF" except: print("<syntax-error>") elif line.startswith("out(") and line.endswith(")"): name = line[4:-1] if not name.isidentifier(): print("<syntax-error>") elif name not in d or d[name] == "UNDEF": print("<undefined>") elif d[name] < -2147483648: print("<underflow>") elif d[name] > 2147483647: print("<overflow>") else: print(d[name]) else: print("<syntax-error>")#华为#