编写程序,实现一个简单的计算器,实现加、减、乘、除以及整除、幂运算和平方根运算。单击clear按钮时清除文本框中的表达式,单击=按钮时计算文本框中表达式的值。要求进行必要的错误检查,例如,一个数据中不能包含多于一个的小数点,表达式中不能包括连续的运算符。
时间: 2024-02-25 16:58:48 浏览: 54
Java计算器源码 JavaJisuanqi.rar
好的,以下是一个简单的计算器程序的示例代码:
```python
from math import sqrt
def calculate(expression):
# 检查表达式是否合法
if '..' in expression or '+-' in expression or '-+' in expression or '//' in expression:
return 'Error'
try:
result = eval(expression)
except ZeroDivisionError:
return 'Error'
except:
return 'Error'
return result
def clear():
# 清空文本框中的内容
text.delete(0, END)
def button_click(button_text):
# 将按钮文本添加到文本框中
text.insert(END, button_text)
def button_clear():
# 清空文本框中的内容
clear()
def button_equal():
# 计算表达式的值并显示结果
expression = text.get()
result = calculate(expression)
clear()
text.insert(END, result)
def button_sqrt():
# 计算平方根并显示结果
expression = text.get()
try:
result = sqrt(float(expression))
except:
result = 'Error'
clear()
text.insert(END, result)
# 创建一个窗口
window = Tk()
window.title('计算器')
# 创建一个文本框
text = Entry(window, width=30)
text.grid(row=0, column=0, columnspan=4)
# 创建数字和运算符按钮
button_1 = Button(window, text='1', command=lambda: button_click('1'))
button_1.grid(row=1, column=0)
button_2 = Button(window, text='2', command=lambda: button_click('2'))
button_2.grid(row=1, column=1)
button_3 = Button(window, text='3', command=lambda: button_click('3'))
button_3.grid(row=1, column=2)
button_4 = Button(window, text='4', command=lambda: button_click('4'))
button_4.grid(row=2, column=0)
button_5 = Button(window, text='5', command=lambda: button_click('5'))
button_5.grid(row=2, column=1)
button_6 = Button(window, text='6', command=lambda: button_click('6'))
button_6.grid(row=2, column=2)
button_7 = Button(window, text='7', command=lambda: button_click('7'))
button_7.grid(row=3, column=0)
button_8 = Button(window, text='8', command=lambda: button_click('8'))
button_8.grid(row=3, column=1)
button_9 = Button(window, text='9', command=lambda: button_click('9'))
button_9.grid(row=3, column=2)
button_0 = Button(window, text='0', command=lambda: button_click('0'))
button_0.grid(row=4, column=1)
button_decimal = Button(window, text='.', command=lambda: button_click('.'))
button_decimal.grid(row=4, column=2)
button_add = Button(window, text='+', command=lambda: button_click('+'))
button_add.grid(row=1, column=3)
button_minus = Button(window, text='-', command=lambda: button_click('-'))
button_minus.grid(row=2, column=3)
button_multiply = Button(window, text='*', command=lambda: button_click('*'))
button_multiply.grid(row=3, column=3)
button_divide = Button(window, text='/', command=lambda: button_click('/'))
button_divide.grid(row=4, column=3)
button_floor_divide = Button(window, text='//', command=lambda: button_click('//'))
button_floor_divide.grid(row=5, column=0)
button_power = Button(window, text='^', command=lambda: button_click('**'))
button_power.grid(row=5, column=1)
button_sqrt = Button(window, text='√', command=button_sqrt)
button_sqrt.grid(row=5, column=2)
# 创建清空和等于按钮
button_clear = Button(window, text='Clear', command=button_clear)
button_clear.grid(row=4, column=0)
button_equal = Button(window, text='=', command=button_equal)
button_equal.grid(row=5, column=3)
# 运行窗口
window.mainloop()
```
该程序使用了Python的tkinter库创建了一个简单的图形用户界面(GUI)。用户可以通过点击数字和运算符按钮来输入表达式,还可以点击清空和等于按钮进行操作。程序还包含了错误检查,例如不能包含多余的小数点或连续的运算符。
阅读全文