以下是使用 Python 语言实现的代码: ```python import re def calculate_expression(expression): pattern = r'([\d.]+)\s*([+\-*/])\s*([\d.]+)' match = re.match(pattern, expression) if match: num1 = float(match.group(1)) operator = match.group(2) num2 = float(match.group(3)) if operator == '+': result = num1 + num2 elif operator == '-': result = num1 - num2 elif operator == '*': result = num1 * num2 elif operator == '/': if num2 == 0: return "除数不能为 0" else: result = num1 / num2 return "%.2f" % result else: return "输入的表达式不符合要求" expression = input("请输入实数四则运算表达式: ") print(calculate_expression(expression)) ``` 这个程序首先使用正则表达式来解析输入的表达式,提取操作数和运算符。然后根据运算符进行相应的计算,并将结果保留两位小数输出。如果除数为 0 或者表达式不符合要求,会输出相应的提示信息。 [2024-11-17 11:21:58 | AI写代码神器 | 286点数解答]