def calculate(expression): """ 计算给定算术表达式的值 :param expression: 字符串描述的算术表达式 :return: 计算结果 """ def parse_number(): nonlocal i j = i while j < len(expression) and expression[j].isdigit(): j += 1 number = int(expression[i:j]) i = j return number def parse_term(): nonlocal i if i >= len(expression): return 0 if expression[i] == "(": i += 1 result = parse_expr() i += 1 return result elif expression[i].isdigit(): return parse_number() elif expression[i] == "-": i += 1 return -parse_term() else: raise ValueError("Invalid expression") def parse_factor(): nonlocal i left = parse_term() while i < len(expression) and expression[i] in "*/": operator = expression[i] i += 1 right = parse_term() if operator == "*": left *= right else: left //= right return left def parse_expr(): nonlocal i left = parse_factor() while i < len(expression) and expression[i] in "+-": operator = expression[i] i += 1 right = parse_factor() if operator == "+": left += right else: left -= right return left # 解析表达式并计算结果 i = 0 return parse_expr() # 示例用法 expression = input() result = calculate(expression) print(result) # 输出 465