- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个可以执行指数计算的计算器程序,但它不喜欢(无法处理)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
是否真的是实现计算器的最佳方法。
高温超导
关于macos - 溢出预期/超时Mac OS X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022740/
wait() 和 wait(timeout) 之间有什么区别。无论如何 wait() 需要等待通知调用,但为什么我们有 wait(timeout)? 那么 sleep(timeout) 和 wait(
如何向以下脚本添加超时?我希望它将文本显示为“超时”。 var bustcachevar = 1 //bust potential caching of external pages after in
我正在使用 Firebase once() 方法来检索 React Native 移动应用中的值。问题是,如果手机离线,once() 永远不会返回。文档说 ref.off() 方法应该取消回调,但这似
我在一个表中有一个大型数据集(超过 200 万行,每行超过 100 列),存储在 cassandra 中,几个月前(也许是 2 个月?)我能够执行一个简单的命令来跟踪该表中的记录数量: SELECT
我使用 jquery 开发移动应用程序,下面是我的代码,当我向包含的页面添加 5 或 6 行时,一切正常。但如果我添加多行显示错误消息:Javascript 执行超时。 function succes
我正在使用一个 javascript 确认,它将在 15 分钟后重复调用。如果用户未选择确认框中的任何选项我会在等待 1 分钟后重定向他。如何实现这一目标?我的代码是这样的 var timeo
每次我在沙箱环境中运行这段代码时,我都会超时并最终崩溃。我已经通过多个 IDE 运行它,但仍然找不到任何语法错误。如果有人看到了我没有看到的东西,我将非常感谢您的意见。 //assign variab
更新联系人后我会显示一条消息,1500 毫秒后我会转到另一个页面。我是这样做的: onSubmit() { if (this.form.valid) {
从昨天开始,我拼命尝试使用最新版本的 PHPMailer 运行一个非常简单的电子邮件脚本。 最荒谬的是,同一个脚本在两台服务器上不起作用,但在另一台服务器上却起作用。 这是我的尝试(来自 PHPMai
我已阅读以下 2 篇文章并尝试实现相同的文章。 我的代码是这样的,超时发生在这里 HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
我正在尝试连接到 wsdl 服务, 但收到此错误: wsdl 错误:获取 http://api.didww.com/api/?wsdl - HTTP 错误: header 的套接字读取超时 本地没有问
我在使用 Ansible 的 CentOs7 实例上从 Artifactory 下载 jar 文件时遇到问题。这是我第一次在 Linux 实例上这样做。 我在每个 Windows 实例上都使用了 wi
在过去的两天里,我一直在寻找原因,我在互联网上和堆栈上尝试了很多解决方案。 我有一个带有 ubuntu 16.04 和 apache2 的专用 VM -> 服务器版本:Apache/2.4.18 (U
我正处于构建 PHP 应用程序的早期阶段,其中一部分涉及使用 file_get_contents()从远程服务器获取大文件并将它们传输给用户。例如,要获取的目标文件是 200 mB。 如果下载到服务器
我正在尝试连接到本地网络内的路由器。到目前为止,我已经使用了 TcpClient。 检查我的代码: public static void RouterConnect() {
我正在尝试构建一段代码来搜索使用 Mechanize 和 Ruby 超时的页面。我的测试台包括一个专门写入超时的页面,以及 3 个正常运行的页面。这是代码: urls = ['http://examp
我是 python 的新手,也是语义网查询领域的新手。我正在使用 SPARQLWrapper 库查询 dbpedia,我搜索了库文档但未能找到从 sparqlWrapper 触发到 dbpedia 的
我正在从 GenServer 中的句柄信息功能调用 elixir genserver 以添加电话号码获取表单客户端。但是一旦调用了handle_call,所有者进程就会崩溃[超时]。请帮忙。 全局创建
假设我的 WCF 服务中有以下执行链: ServiceMethod 调用并等待 Method1,然后调用并等待 Method2,后者调用并等待 Method3。最后 ServiceMethod 在返回
目前我正在开发一个从远程服务器发送和接收文件的应用程序。为了进行网络操作,我正在使用 QNetworkAccessManager。 要上传文件,我使用 QNetworkAccessManager::p
我是一名优秀的程序员,十分优秀!