用Python编程实现一个简易4则计算器。
时间: 2023-03-31 21:04:46 浏览: 436
好的,我可以回答这个问题。以下是一个简易的Python 4则计算器代码:
```python
# 定义四则运算函数
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
# 用户输入
print("选择运算:")
print("1、相加")
print("2、相减")
print("3、相乘")
print("4、相除")
choice = input("输入你的选择(1/2/3/4):")
num1 = float(input("输入第一个数字: "))
num2 = float(input("输入第二个数字: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("非法输入")
```
希望对你有帮助!
相关问题
编程实现一个简易四则计算器
好的,以下是 Python 语言的实现示例:
```python
while True:
expression = input("请输入要计算的表达式:")
# 判断是否退出
if expression == "exit":
break
# 将表达式进行分割
parts = expression.split()
# 判断输入是否合法
if len(parts) != 3:
print("输入错误,请重新输入!")
continue
# 获取操作数和运算符
num1, operator, num2 = parts
# 将操作数转换为浮点数
num1 = float(num1)
num2 = float(num2)
# 根据运算符进行计算
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
if num2 == 0:
print("除数不能为0,请重新输入!")
continue
result = num1 / num2
else:
print("输入错误,请重新输入!")
continue
# 输出结果
print("结果为:", result)
```
该程序会循环进行计算,直到输入 exit 命令退出。每次输入一个表达式,程序会将其分割为操作数和运算符,根据运算符进行计算并输出结果。需要注意的是,除法运算时要判断除数是否为0。
编程实现一个简易四则计算器,如:输入3+2,输出5
好的,以下是 Python 语言的实现示例:
```python
# 定义四则运算函数
def calculator(s):
# 去除空格
s = s.replace(" ", "")
# 定义数字和符号堆栈
nums, ops = [], []
# 定义优先级字典
priority = {"+": 1, "-": 1, "*": 2, "/": 2}
# 定义数字字符集合
digits = set("0123456789")
# 定义当前数字
num = ""
for i in range(len(s)):
char = s[i]
if char in digits:
num += char
# 如果已经是最后一个数字,或者下一个字符不是数字,就将当前数字入栈
if i == len(s) - 1 or s[i + 1] not in digits:
nums.append(int(num))
num = ""
elif char in priority:
# 如果当前符号优先级低于栈顶符号,就先进行计算
while ops and priority[char] <= priority[ops[-1]]:
b, a = nums.pop(), nums.pop()
nums.append(eval(str(a) + ops.pop() + str(b)))
ops.append(char)
elif char == "(":
ops.append(char)
elif char == ")":
# 如果是右括号,就一直进行计算,直到遇到左括号
while ops[-1] != "(":
b, a = nums.pop(), nums.pop()
nums.append(eval(str(a) + ops.pop() + str(b)))
ops.pop()
# 计算剩余的表达式
while ops:
b, a = nums.pop(), nums.pop()
nums.append(eval(str(a) + ops.pop() + str(b)))
# 返回计算结果
return nums[0]
# 测试
print(calculator("3+2")) # 输出 5
```
需要注意的是,以上实现只支持整数四则运算,不支持小数和负数。如果需要支持这些功能,需要对代码进行相应的修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)