python计算器实现运算符不能重复
时间: 2023-04-01 07:03:42 浏览: 105
可以通过编写代码来限制运算符重复的情况,例如使用 if 语句判断输入的运算符是否已经被使用过,如果已经使用过则提示用户重新输入。具体实现可以参考以下代码:
```
num1 = float(input("请输入第一个数字:"))
num2 = float(input("请输入第二个数字:"))
operator = input("请输入运算符:")
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
else:
print("输入的运算符不合法,请重新输入!")
operator = input("请输入运算符:")
print("运算结果为:", result)
```
以上代码中,如果用户输入的运算符不合法,则会提示用户重新输入,直到输入合法的运算符为止。
相关问题
计算器实现运算符优先级
计算器可以通过使用栈来实现运算符优先级的计算。具体实现步骤如下:
1. 创建两个栈,一个栈用来存放数字,一个栈用来存放运算符。
2. 从左到右遍历表达式,遇到数字时将其压入数字栈中。
3. 遇到运算符时,将其与运算符栈栈顶的运算符进行比较。
4. 如果当前运算符的优先级小于等于栈顶运算符的优先级,则将数字栈中的两个数字弹出,将栈顶运算符弹出并进行运算,将运算结果压入数字栈中。
5. 如果当前运算符的优先级大于栈顶运算符的优先级,则将当前运算符入栈。
6. 遍历完表达式后,依次弹出运算符栈中的运算符并进行运算,将运算结果压入数字栈中,直到运算符栈为空。
7. 最终数字栈中的唯一元素即为表达式的运算结果。
以下是一个示例代码实现:
```python
def calculate(expression):
num_stack = []
operator_stack = []
operators = {'+': 1, '-': 1, '*': 2, '/': 2, '(': 0}
for char in expression:
if char.isdigit():
num_stack.append(int(char))
elif char in operators:
while operator_stack and operators[char] <= operators[operator_stack[-1]]:
num2 = num_stack.pop()
num1 = num_stack.pop()
operator = operator_stack.pop()
if operator == '+':
num_stack.append(num1 + num2)
elif operator == '-':
num_stack.append(num1 - num2)
elif operator == '*':
num_stack.append(num1 * num2)
elif operator == '/':
num_stack.append(num1 / num2)
if char != ')':
operator_stack.append(char)
elif char == ')':
while operator_stack[-1] != '(':
num2 = num_stack.pop()
num1 = num_stack.pop()
operator = operator_stack.pop()
if operator == '+':
num_stack.append(num1 + num2)
elif operator == '-':
num_stack.append(num1 - num2)
elif operator == '*':
num_stack.append(num1 * num2)
elif operator == '/':
num_stack.append(num1 / num2)
operator_stack.pop()
while operator_stack:
num2 = num_stack.pop()
num1 = num_stack.pop()
operator = operator_stack.pop()
if operator == '+':
num_stack.append(num1 + num2)
elif operator == '-':
num_stack.append(num1 - num2)
elif operator == '*':
num_stack.append(num1 * num2)
elif operator == '/':
num_stack.append(num1 / num2)
return num_stack.pop()
```
这个函数接受一个表达式作为参数,并返回表达式的运算结果。例如,calculate("3+4*5-(6/2)")的返回值为17。
python 计算器实现,包含标准、科学、程序员
以下是一个简单的 Python 计算器实现,包括标准、科学和程序员模式。其中标准模式支持加、减、乘、除、取模等基本运算;科学模式支持三角函数、指数、对数等高级运算;程序员模式支持二进制、八进制、十六进制等进制转换。
```python
import math
def standard():
print("标准计算器")
num1 = float(input("请输入第一个数字:"))
operator = input("请输入运算符(+、-、*、/、%、^):")
num2 = float(input("请输入第二个数字:"))
if operator == '+':
print("结果为:", num1 + num2)
elif operator == '-':
print("结果为:", num1 - num2)
elif operator == '*':
print("结果为:", num1 * num2)
elif operator == '/':
if num2 == 0:
print("错误:除数不能为0")
else:
print("结果为:", num1 / num2)
elif operator == '%':
print("结果为:", num1 % num2)
elif operator == '^':
print("结果为:", num1 ** num2)
else:
print("错误:无效的运算符")
def scientific():
print("科学计算器")
num = float(input("请输入数字:"))
operator = input("请输入运算符(sin、cos、tan、sqrt、exp、log):")
if operator == 'sin':
print("结果为:", math.sin(num))
elif operator == 'cos':
print("结果为:", math.cos(num))
elif operator == 'tan':
print("结果为:", math.tan(num))
elif operator == 'sqrt':
print("结果为:", math.sqrt(num))
elif operator == 'exp':
print("结果为:", math.exp(num))
elif operator == 'log':
print("结果为:", math.log(num))
else:
print("错误:无效的运算符")
def programmer():
print("程序员计算器")
num = int(input("请输入数字:"))
print("二进制:", bin(num))
print("八进制:", oct(num))
print("十六进制:", hex(num))
mode = input("请选择计算器模式(standard、scientific、programmer):")
if mode == 'standard':
standard()
elif mode == 'scientific':
scientific()
elif mode == 'programmer':
programmer()
else:
print("错误:无效的模式")
```
在运行程序时,用户需要输入所需的模式,例如输入 `standard` 后,程序进入标准模式,用户可以输入两个数字和一个运算符,程序会输出计算结果。其他模式也是类似的操作。