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