今天教大家用python模拟有理数计算,首先展示功能页面:
啊这?怎么用这个东西呀?我们来看看~
<iframe allowfullscreen="true" data-mediaembed="csdn" id="bAFoh5Ob-1643416044953" src="https://live.csdn.net/v/embed/185070"></iframe>有理数计算模拟器
哇,功能这么强大?那实现起来一定很难吧???
其实也没那么难。。。因为python早就整合好了这部分功能,我们来看下代码~
前端功能页面:
from tkinter import * from tkinter import messagebox import tkinter as tk import numpy as np class basedesk():#底板 def __init__(self, master): self.master = master self.master.title("有理数计算模拟器") self.master.configure(bg='#B1FFF9') self.master.geometry("1000x600") mainwindow(self.master) class mainwindow():#主界面 def __init__(self, master): self.master = master self.window = tk.Frame(self.master, bg='#e5ffe5') self.window.place(x=0,y=0,width=1000,height=600) self.window.showname_label=tk.Label(self.window,text="有理数计算模拟器",fg='#26734d', bg='#ffe5ff',font=("Helvetic",60,"bold"),relief=RAISED).place(x=0, y=10,width=1000, height=150) self.window.enter_btn=tk.Button(self.window,text="开始",bg='#ffffe5',fg='#333399',font=("Helvetic", 60, "bold"),command=self.changetofunction).place(x=360, y=300,width=250, height=150) def changetofunction(self,): self.window.destroy() functionwindow(self.master) class functionwindow(): def __init__(self, master): self.master = master self.window = tk.Frame(self.master, bg='#e5f9ff') self.window.place(x=0, y=0, width=1000, height=600) self.window.show_label = tk.Label(self.window, text="请输入要计算的式子(只可输入有理数以及+-*/符号):", font=("Helvetic", 20, "bold"), bg='#e5f9ff').place(x=20, y=10) self.window.showlistbox=tk.Entry(self.window, font=("Helvetica", 20)) self.window.showlistbox.place(x=50, y=50, width=900, height=40) self.window.enter_btn=tk.Button(self.window,text="计算",bg='#ffffe5',fg='#333399',font=("Helvetic", 60, "bold"),command=self.calculate).place(x=360, y=300,width=250, height=150)
哇咔咔,前端页面完成了,那后端算法应该也不少吧?
后端算法:
def calculate(self): lister=self.window.showlistbox.get() try: a=eval(lister) except: messagebox.showerror("错误","请输入计算式") return messagebox.showinfo("计算结果",eval(lister))
what???怎么才这么少?原来,python有理数计算功能早已整合成了eval函数,直接用就可以啦~
最后展示完整代码:
from tkinter import * from tkinter import messagebox import tkinter as tk import numpy as np class basedesk():#底板 def __init__(self, master): self.master = master self.master.title("有理数计算模拟器") self.master.configure(bg='#B1FFF9') self.master.geometry("1000x600") mainwindow(self.master) class mainwindow():#主界面 def __init__(self, master): self.master = master self.window = tk.Frame(self.master, bg='#e5ffe5') self.window.place(x=0,y=0,width=1000,height=600) self.window.showname_label=tk.Label(self.window,text="有理数计算模拟器",fg='#26734d', bg='#ffe5ff',font=("Helvetic",60,"bold"),relief=RAISED).place(x=0, y=10,width=1000, height=150) self.window.enter_btn=tk.Button(self.window,text="开始",bg='#ffffe5',fg='#333399',font=("Helvetic", 60, "bold"),command=self.changetofunction).place(x=360, y=300,width=250, height=150) def changetofunction(self,): self.window.destroy() functionwindow(self.master) class functionwindow(): def __init__(self, master): self.master = master self.window = tk.Frame(self.master, bg='#e5f9ff') self.window.place(x=0, y=0, width=1000, height=600) self.window.show_label = tk.Label(self.window, text="请输入要计算的式子(只可输入有理数以及+-*/符号):", font=("Helvetic", 20, "bold"), bg='#e5f9ff').place(x=20, y=10) self.window.showlistbox=tk.Entry(self.window, font=("Helvetica", 20)) self.window.showlistbox.place(x=50, y=50, width=900, height=40) self.window.enter_btn=tk.Button(self.window,text="计算",bg='#ffffe5',fg='#333399',font=("Helvetic", 60, "bold"),command=self.calculate).place(x=360, y=300,width=250, height=150) def calculate(self): lister=self.window.showlistbox.get() try: a=eval(lister) except: messagebox.showerror("错误","请输入计算式") return messagebox.showinfo("计算结果",eval(lister)) if __name__ == '__main__':#主函数 root = tk.Tk() root.resizable(False, False) basedesk(root) root.mainloop()