我有一个可以执行指数计算的计算器程序,但它不喜欢(无法处理)10 ^ 10000000000000之类的东西。我看了以前的一篇文章,说使用信号超时,但这只是unix。关于我的代码的另一点是:当按下equals时,它将通过try块运行输入,但将ERROR打印到计算器的显示屏除外。有什么方法可以检测到潜在的溢出并在经过一段时间(引发错误)之后引发错误或超时?注意:文件需要传输到另一个系统,因此解决方案必须具有通用性。谢谢!
编辑:我正在使用python运行tkinter来制作下面的GUI。我需要将文件传输到运行Jessie的树莓派。
from tkinter import *
import math
class Calc:
def __init__(self,master):
self.master = master
self.lastbutton = ''
self.varlist = dict()
self.varlist = {"A":"0","B":"0","C":"0","D":"0","E":"0","F":"0","G":"0","H":"0","I":"0","J":"0"}
self.dlist = dict()
self.dlist = {'^':'**','sqrt(':'math.sqrt(',')cubrt':'**(1/3)',
'sin(':'math.sin(','cos(':'math.cos(','tan(':'math.tan(',
'arcsin(':'math.asin(','arccos(':'math.acos(','arctan(':'math.atan(',"τ":"(math.tau)",
'π':'(math.pi)','e':'(math.e)',"e^":"math.exp(",'(π/2)':'((math.pi)/2)','(π/3)':'((math.pi)/3)','(π/4)':'((math.pi)/4)',
"A":"self.varlist['A']","B":"self.varlist['B']","C":"self.varlist['C']","D":"self.varlist['D']",
"E":"self.varlist['E']","F":"self.varlist['F']","G":"self.varlist['G']","H":"self.varlist['H']",
"I":"self.varlist['I']","J":"self.varlist['J']",
"mod(":"math.fmod(","e^(":"math.exp(","ln(":"math.log(","log(":"math.log10(","log b (":"math.log(",
"sec(":"(1/math.cos(","csc(":"(1/math.sin(","cot(":"(1/math.tan(","arcsec(":"(1/math.acos(","arccsc(":"(1/math.asin(","arccot(":"(1/math.atan(",
"toRadians(":"math.radians(","toDegrees":"math.degrees(","hypot(":"math.hypot("}
self.text1 = StringVar(value = '1')
self.text2 = StringVar(value = '2')
self.text3 = StringVar(value = '3')
self.text4 = StringVar(value = '4')
self.text5 = StringVar(value = '5')
self.text6 = StringVar(value = '6')
self.text7 = StringVar(value = '7')
self.text8 = StringVar(value = '8')
self.text9 = StringVar(value = '9')
self.text0 = StringVar(value = '0')
self.acl = StringVar(value = 'Del')
self.period = StringVar(value = '.')
self.shft = StringVar(value = 'shft1')
self.var1 = StringVar()
self.var2 = StringVar()
self.var3 = StringVar()
self.option1 = ['^2','^3','sqrt(',')cubrt','^','mod(','ln(','log(','log b (']
self.option2 = ['τ','π','e','e^','(π/2)','(π/3)','(π/4)','toRadians(','toDegrees(']
self.option3 = ['sin(','cos(','tan(','arcsin(','arccos(','arctan(', 'sec(', 'csc(','cot(','arcsec(','arccsc(','arccot(','hypot(']
self.master.title('My Calculator')
self.displaya = Entry(master)
self.displaya.grid(row = 0, columnspan = 6)
self.displayb = Entry(master)
self.displayb.grid(row = 1, columnspan = 6)
self.displayc = Entry(master)
self.displayc.grid(row = 2, columnspan = 6)
self.one = Button(master, textvariable = self.text1,width = 10, command = lambda: self.display(self.text1.get()))
self.one.grid(row = 4, column = 0)
self.two = Button(master, textvariable = self.text2,width = 10, command = lambda: self.display(self.text2.get()))
self.two.grid(row = 4, column = 1)
self.three = Button(master, textvariable = self.text3, width = 10,command = lambda: self.display(self.text3.get()))
self.three.grid(row = 4, column = 2)
self.four = Button(master, textvariable = self.text4, width = 10,command = lambda: self.display(self.text4.get()))
self.four.grid(row = 5, column = 0)
self.five = Button(master, textvariable = self.text5,width = 10, command = lambda: self.display(self.text5.get()))
self.five.grid(row = 5, column = 1)
self.six = Button(master, textvariable = self.text6,width = 10, command = lambda: self.display(self.text6.get()))
self.six.grid(row = 5, column = 2)
self.seven = Button(master, textvariable = self.text7,width = 10, command = lambda: self.display(self.text7.get()))
self.seven.grid(row = 6, column = 0)
self.eight = Button(master, textvariable = self.text8, width = 10,command = lambda: self.display(self.text8.get()))
self.eight.grid(row = 6, column = 1)
self.nine = Button(master, textvariable = self.text9, width = 10,command = lambda: self.display(self.text9.get()))
self.nine.grid(row = 6, column = 2)
self.zero = Button(master, textvariable = self.text0, width = 10,command = lambda: self.display(self.text0.get()))
self.zero.grid(row = 7, column = 1)
self.result = Button(master, text = '=', width = 10, height = 1, command = lambda : self.equals())
self.result.grid(row = 7, column = 0)
self.ac = Button(master, textvariable = self.acl, width = 10, height = 1, command = lambda : self.clear(self.acl.get()))
self.ac.grid(row = 7, column = 2)
self.plus = Button(master, text = '+', width = 10, height = 1, command = lambda : self.display('+'))
self.plus.grid(row = 4, column = 3)
self.minus = Button(master, text = '-', width = 10, height = 1, command = lambda : self.display('-'))
self.minus.grid(row = 5, column = 3)
self.times = Button(master, text = '*', width = 10, height = 1, command = lambda : self.display('*'))
self.times.grid(row = 6, column = 3)
self.div = Button(master, text = '/', width = 10, height = 1, command = lambda : self.display('/'))
self.div.grid(row = 7, column = 3)
self.per = Button(master, textvariable = self.period, width = 4, command = lambda : self.display(self.period.get()))
self.per.grid(row = 1, column = 0)
self.opp = Button(master, text = '(', width = 1, height = 1, command = lambda : self.display('('))
self.opp.grid(row = 0, column = 1)
self.clp = Button(master, text = ')', width = 1, height = 1, command = lambda : self.display(')'))
self.clp.grid(row = 1, column = 1)
self.update_button = Button(master, textvariable = self.shft, width = 4, command = lambda: self.update(int(self.shft.get()[4:5])))
self.update_button.grid(row = 0, column = 0)
self.store = Button(master, text = 'sto', command = lambda : self.sto(self.displayc.get()))
self.store.grid(row = 2, column = 1)
self.menu1 = OptionMenu(master, self.var1, *self.option1)
self.menu1.grid(row = 4, column = 4, ipadx = 20)
self.menu2 = OptionMenu(master, self.var2, *self.option2)
self.menu2.grid(row = 5, column = 4, ipadx = 20)
self.menu3 = OptionMenu(master, self.var3, *self.option3)
self.menu3.grid(row = 6, column = 4, ipadx = 20)
self.place1 = Button(root, text = 'Place', width = 10, height = 1, command = lambda : self.display(self.var1.get()))
self.place1.grid(row = 4, column = 5)
self.place2 = Button(root, text = 'Place', width = 10, height = 1, command = lambda : self.display(self.var2.get()))
self.place2.grid(row = 5, column = 5)
self.place3 = Button(root, text = 'Place', width = 10, height = 1, command = lambda : self.display(self.var3.get()))
self.place3.grid(row = 6, column = 5)
self.hbox = Text(master, height = 5, width = 35)
self.hbox.grid(row = 1, rowspan = 3, column = 4, columnspan = 2)
self.help = Button(master, text = 'Help', command = lambda : self.hlp())
self.help.grid(row = 2, column = 0)
def update(self, mode):
if mode == 1:
self.text1.set('A')
self.text2.set('B')
self.text3.set('C')
self.text4.set('D')
self.text5.set('E')
self.text6.set('F')
self.text7.set('G')
self.text8.set('H')
self.text9.set('I')
self.text0.set('J')
self.acl.set('AC')
self.period.set(',')
self.shft.set('shft2')
else:
self.text1.set('1')
self.text2.set('2')
self.text3.set('3')
self.text4.set('4')
self.text5.set('5')
self.text6.set('6')
self.text7.set('7')
self.text8.set('8')
self.text9.set('9')
self.text0.set('0')
self.acl.set('Del')
self.period.set('.')
self.shft.set('shft1')
def sto(self, dis):
disl = dis.split(',')
var = disl[0]
val = disl[1]
self.varlist[var] = val
self.displaya.delete(0,END)
self.displaya.insert(0, self.displayb.get())
self.displayb.delete(0,END)
txt = val + '-->' + var
self.displayb.insert(0, txt)
self.displayc.delete(0,END)
self.displayc.insert(0, round(float(val), 7))
self.lastbutton = 'sto('
def hlp(self):
self.hbox.delete(1.0, END)
sme = dict()
sme = {"-":"+","*":"+","/":"+","B":"A","C":"A","D":"A","E":"A","F":"A","G":"A","H":"A","I":"A","J":"A",
"cos(":"sin(","tan(":"sin(","arcsin(":"sin(","arccos(":"sin(","arctan(":"sin(",
"sec(":"sin(","csc(":"sin(","cot(":"sin(","arcsec(":"sin(","arccsc(":"sin(","arccot(":"sin("}
bh = dict()
bh = {"+":"+, -, *, / \n Arithmetic operators (op)\n format : x op y",
"sin(":"sin(), cos(), etc\n Trigonometric functions (funct()) \nformat : funct(x)",
"A":"Variables can hold 1 value\nat a time with STO\nuse like number",
"^2":"Square (exponent = 2)\n format : x^2",
"^":"Carrot (x ^ y)\n format : x^y",
")cubrt":"Cube root\n format : (x)cubrt",
"sqrt(":"Square root\n format : sqrt(x)",
"mod(":"Modulo (remainder x/y)\nformat : mod(x,y)",
"ln(":"Natural log\n format : ln(x)",
"log(":"Log base 10\n format : log(x)",
"log b (":"Logarithm of base b\n format : log(x,b)",
"sto(":"Store value to variable\n format : sto(variable,value)",
"hypot(":"Hypotenuse (Pythagorean theorem)\n Find hypotenuse given two sides\n format : hypot(s1,s2)"}
helpfunct = sme[self.lastbutton] if self.lastbutton in sme.keys() else self.lastbutton
new = bh[helpfunct] if helpfunct in bh.keys() else "No help available for this item"
add = 'Help:' + '\n' + new
self.hbox.insert(1.0, add)
def display(self,e):
self.displayc.insert(END,e)
self.lastbutton = e
def equals(self):
self.displaya.delete(0,END)
b = self.displayb.get()
self.displaya.insert(0, b)
self.displayb.delete(0,END)
c = self.displayc.get()
for key in self.dlist:
c = c.replace(key, self.dlist[key])
try:
equals = round(float(eval(c)),7)
c = c + '=' + str(equals)
self.displayb.insert(0, c)
self.displayc.delete(0,END)
self.displayc.insert(0, equals)
except ZeroDivisionError:
self.displayb.insert(0,c)
c = 'Error: Div by Zero: '
self.displayc.insert(0,c)
except SyntaxError:
self.displayb.insert(0,c)
c = 'Error: Syntax'
self.displayc.delete(0,END)
self.displayc.insert(0,c)
except IndexError:
self.displayb.insert(0,c)
c = 'Error: Syntax'
self.displayc.delete(0,END)
self.displayc.insert(0,c)
except TypeError:
self.displayb.insert(0,c)
c = 'Error: Syntax'
self.displayc.delete(0,END)
self.displayc.insert(0,c)
return
def clear(self, com):
if com == 'AC':
if len(self.displayc.get()) == 0:
self.displayb.delete(0, END)
self.displaya.delete(0, END)
else:
self.displayc.delete(0, END)
else:
self.displayc.delete(len(self.displayc.get())-1,END)
return
root = Tk()
calc = Calc(root)
root.mainloop()
最佳答案
您的特殊问题是10^10000000000000
表达式(您将其翻译为10**10000000000000
仅包含整数,而Python支持任意精度整数,因此它正试图为您提供答案...
但是从代码中可以很明显地看到您正在编写一个浮点计算器。如果将表达式更改为10.0**10000000000000.0
,您将立即得到溢出异常,添加try:except:
即可捕获该异常并显示合适的结果。
您将需要弄清楚如何在代码中添加.0
。您还应该考虑如果用户输入3+*4
或其他垃圾会发生什么。问问自己,使用eval
是否真的是实现计算器的最佳方法。
高温超导