python图像化计算器

    技术2022-07-21  78

    用python实现简单计算器

    本文目录 1.功能目标 2.解题思路 3.关键函数说明 4.界面以及结果展示

    一.功能目标

    用户选择不同的功能实现不同的计算结果

    1.标准计算

    用户输入+, -, *, /,pow,sqrt等不同的按钮进行不同的计算

    2.解方程运算

    用户根据提示格式输入方程参数 a.解二元一次方程 b.解三元一次方程 c.解一元二次方程 d.解一元三次方程

    二.解题思路

    1.判断按下的数字按键还是功能按键,需要一个值进行判断 2.可以使用eval函数进行标准运算 3.使用sympy模块进行方程的解运算

    eval函数参考:

    https://www.runoob.com/python/python-func-eval.html

    sypmy模块的使用参考:

    https://blog.csdn.net/weixin_34352005/article/details/92949596#一求解多元一次方程-solve

    本文参考了此文章的实现:

    https://blog.csdn.net/cui_yonghua/article/details/104129520?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

    三.关键函数说明

    开方运算

    def f_sqrt(self): strnum = float(self.result.get()) endnum = math.sqrt(strnum) self.result.set(str(endnum)) if self.lists != 0: self.ispresssign = True self.lists.clear()

    幂运算

    def f_pow(self): num_one = float(self.lists.pop()) num_two = float(self.result.get()) self.ispresssign = True value = math.pow(num_one, num_two) self.result.set(str(format(value, '.6f'))) self.lists.clear()

    解方程的几个函数都使用float类型 解二元一次方程 用sympy模块的solve方法实现

    def t_count(self): aone = float(self.entry_a1.get()) bone = float(self.entry_b1.get()) cone = float(self.entry_c1.get()) atwo = float(self.entry_a2.get()) btwo = float(self.entry_b2.get()) ctwo = float(self.entry_c2.get()) x = sy.Symbol('x') y = sy.Symbol('y') t_result = sy.solve([aone*x + -1*cone + bone*y, atwo*x + -1*ctwo + btwo*y]) if x in t_result and y in t_result: self.result_x.set(t_result[x]) self.result_y.set(t_result[y]) elif x in t_result or y in t_result: self.result_x.set('无唯一解') self.result_y.set('无唯一解') else: self.result_x.set('无解') self.result_y.set('无解')

    解三元一次方程与二元一次方程类似

    def f_count_two(self): aone = float(self.tentry_a1.get()) bone = float(self.tentry_b1.get()) cone = float(self.tentry_c1.get()) done = float(self.tentry_d1.get()) atwo = float(self.tentry_a1.get()) btwo = float(self.tentry_b2.get()) ctwo = float(self.tentry_c2.get()) dtwo = float(self.tentry_d2.get()) athree = float(self.tentry_a3.get()) bthree = float(self.tentry_b3.get()) cthree = float(self.tentry_c3.get()) dthree = float(self.tentry_d3.get()) x = sy.Symbol('x') y = sy.Symbol('y') z = sy.Symbol('z') f1 = aone*x + bone*y + cone*z + -1*done f2 = atwo*x + btwo*y + ctwo*z + -1*dtwo f3 = athree*x + bthree*y + cthree*z + -1*dthree t_resultTwo = sy.solve([f1, f2, f3], [x, y, z]) if x in t_resultTwo and y in t_resultTwo and z in t_resultTwo: self.tresultX.set(t_resultTwo[x]) self.tresultY.set(t_resultTwo[y]) self.tresultZ.set(t_resultTwo[z]) elif x in t_resultTwo or y in t_resultTwo or z in t_resultTwo: self.tresultX.set('无唯一解') self.tresultY.set('无唯一解') self.tresultZ.set('无唯一解') else: self.tresultX.set('无解') self.tresultY.set('无解') self.tresultZ.set('无解')

    解一元二次方程

    a = float(self.entry_a.get()) b = float(self.entry_b.get()) c = float(self.entry_c.get()) x = sy.Symbol('x') tResult = sy.solve(a*x**2 + b*x + -1*c, x) if tResult: if len(tResult) == 2: self.resultX1.set(tResult[0]) self.resultX2.set(tResult[1]) if len(tResult) == 1: self.resultX1.set(tResult[0]) self.resultX2.set(tResult[0]) else: self.resultX1.set("无解") self.resultX2.set("无解")

    解三元一次方程

    def fCount(self): a = float(self.fentry_a.get()) b = float(self.fentry_b.get()) c = float(self.fentry_c.get()) d = float(self.fentry_d.get()) x = sy.Symbol('x') f = a*x**3 + b*x**2 + c*x + 1*d fResult = sy.solve(f, x) if fResult: if len(fResult) == 3: self.fresultX1.set(fResult[0]) self.fresultX2.set(fResult[1]) self.fresultX3.set(fResult[2]) elif len(fResult) == 1: self.fresultX1.set(fResult[0]) self.fresultX2.set(fResult[0]) self.fresultX3.set(fResult[0]) else: self.fresultX1.set("无解") self.fresultX2.set("无解") self.fresultX3.set("无解")

    四.界面以及结果展示

    1.标准模式界面 2.二元一次方程界面 3.三元一次方程的界面

    4.一元二次方程界面 5.一元三次方程的求解

    总结

    这份计算器使用了tkinter界面库进行显示,不过python的自带界面库和java一样都太简陋了,不过做个简单的界面问题不大。 使用python的eval函数方便直接进行计算,python具有丰富的库资源的确可以很大程度上帮助完成功能,不过也不能放弃自己的思考,所以这份计算器应该还能继续改进。 此计算器存在一定的问题,如果结果的位数超过label可以显示的范围,就无法看到头几位的结果,这个暂时没有想到如何解决。

    Processed: 0.008, SQL: 9