Python語言實現(xiàn)科學計算器
更新時間:2022年01月29日 10:05:15 作者:Jld-Tx
這篇文章主要為大家詳細介紹了Python語言實現(xiàn)科學計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Python語言實現(xiàn)科學計算器的具體代碼,供大家參考,具體內(nèi)容如下
自學Python語言一個月,還是小白,發(fā)一個科學計算器的代碼,希望大家批評指正,共勉嘛。
calculator.py
from tkinter import * from functools import partial from calculate import * # 生成計算器主界面 def buju(root): ? ? menu = Menu(root) ?# 菜單 ? ? submenu1 = Menu(menu, tearoff=0) ?# 分窗,0為在原窗,1為點擊分為兩個窗口 ? ? menu.add_cascade(label='編輯', menu=submenu1) ?# 添加子選項(label參數(shù)為顯示內(nèi)容) ? ? submenu1.add_command(label='復制', command=lambda: bianji(entry, 'copy')) ?# 添加命令 ? ? submenu1.add_command(label='剪切', command=lambda: bianji(entry, 'cut')) ? ? submenu1.add_command(label='粘貼', command=lambda: bianji(entry, 'paste')) ? ? submenu2 = Menu(menu, tearoff=0) ? ? menu.add_cascade(label='查看', menu=submenu2) ? ? submenu2.add_command(label='幫助', command=lambda: chakan(entry, 'help')) ? ? submenu2.add_command(label='作者', command=lambda: chakan(entry, 'author')) ? ? root.config(menu=menu) ?# 重新配置,添加菜單 ? ? label = Label(root, width=29, height=1, bd=5, bg='#FFFACD', anchor='se', ? ? ? ? ? ? ? ? ? textvariable=label_text) ?# 標簽,可以顯示文字或圖片 ? ? label.grid(row=0, columnspan=5) ?# 布局器,向窗口注冊并顯示控件; rowspan:設置單元格縱向跨越的列數(shù) ? ? entry = Entry(root, width=23, bd=5, bg='#FFFACD', justify="right", font=('微軟雅黑', 12)) ?# 文本框(單行) ? ? entry.grid(row=1, column=0, columnspan=5, sticky=N + W + S + E, padx=5, pady=5) ?# 設置控件周圍x、y方向空白區(qū)域保留大小 ? ? myButton = partial(Button, root, width=5, cursor='hand2', activebackground='#90EE90') ?# 偏函數(shù):帶有固定參數(shù)的函數(shù) ? ? button_sin = myButton(text='sin', command=lambda: get_input(entry, 'sin(')) ?# 按鈕 ? ? button_arcsin = myButton(text='arcsin', command=lambda: get_input(entry, 'arcsin(')) ? ? button_exp = myButton(text='e', command=lambda: get_input(entry, 'e')) ? ? button_ln = myButton(text='ln', command=lambda: get_input(entry, 'ln(')) ? ? button_xy = myButton(text='x^y', command=lambda: get_input(entry, '^')) ? ? button_sin.grid(row=2, column=0) ? ? button_arcsin.grid(row=2, column=1) ? ? button_exp.grid(row=2, column=2) ? ? button_ln.grid(row=2, column=3) ? ? button_xy.grid(row=2, column=4) ? ? button_shanyige = myButton(text='←', command=lambda: backspace(entry)) ?# command指定按鈕消息的回調(diào)函數(shù) ? ? button_shanquanbu = myButton(text=' C ', command=lambda: clear(entry)) ? ? button_zuokuohao = myButton(text='(', command=lambda: get_input(entry, '(')) ? ? button_youkuohao = myButton(text=')', command=lambda: get_input(entry, ')')) ? ? button_genhao = myButton(text='√x', command=lambda: get_input(entry, '√(')) ? ? button_shanyige.grid(row=3, column=0) ? ? button_shanquanbu.grid(row=3, column=1) ? ? button_zuokuohao.grid(row=3, column=2) ? ? button_youkuohao.grid(row=3, column=3) ? ? button_genhao.grid(row=3, column=4) ? ? button_7 = myButton(text=' 7 ', command=lambda: get_input(entry, '7')) ? ? button_8 = myButton(text=' 8 ', command=lambda: get_input(entry, '8')) ? ? button_9 = myButton(text=' 9 ', command=lambda: get_input(entry, '9')) ? ? button_chu = myButton(text=' / ', command=lambda: get_input(entry, '/')) ? ? button_yu = myButton(text='%', command=lambda: get_input(entry, '%')) ? ? button_7.grid(row=4, column=0) ? ? button_8.grid(row=4, column=1) ? ? button_9.grid(row=4, column=2) ? ? button_chu.grid(row=4, column=3) ? ? button_yu.grid(row=4, column=4) ? ? button_4 = myButton(text=' 4 ', command=lambda: get_input(entry, '4')) ? ? button_5 = myButton(text=' 5 ', command=lambda: get_input(entry, '5')) ? ? button_6 = myButton(text=' 6 ', command=lambda: get_input(entry, '6')) ? ? button_cheng = myButton(text=' * ', command=lambda: get_input(entry, '*')) ? ? button_jiecheng = myButton(text='二進制', command=lambda: jinzhi(entry)) ? ? button_4.grid(row=5, column=0) ? ? button_5.grid(row=5, column=1) ? ? button_6.grid(row=5, column=2) ? ? button_cheng.grid(row=5, column=3) ? ? button_jiecheng.grid(row=5, column=4) ? ? button_1 = myButton(text=' 1 ', command=lambda: get_input(entry, '1')) ? ? button_2 = myButton(text=' 2 ', command=lambda: get_input(entry, '2')) ? ? button_3 = myButton(text=' 3 ', command=lambda: get_input(entry, '3')) ? ? button_jian = myButton(text=' - ', command=lambda: get_input(entry, '-')) ? ? button_dengyu = myButton(text=' \n = \n ', command=lambda: calculator(entry)) ? ? button_1.grid(row=6, column=0) ? ? button_2.grid(row=6, column=1) ? ? button_3.grid(row=6, column=2) ? ? button_jian.grid(row=6, column=3) ? ? button_dengyu.grid(row=6, column=4, rowspan=2) ?# rowspan:設置單元格橫向跨越的行數(shù) ? ? button_pai = myButton(text=' π ', command=lambda: get_input(entry, 'π')) ? ? button_0 = myButton(text=' 0 ', command=lambda: get_input(entry, '0')) ? ? button_xiaoshudian = myButton(text=' . ', command=lambda: get_input(entry, '.')) ? ? button_jia = myButton(text=' + ', command=lambda: get_input(entry, '+')) ? ? button_pai.grid(row=7, column=0) ? ? button_0.grid(row=7, column=1) ? ? button_xiaoshudian.grid(row=7, column=2) ? ? button_jia.grid(row=7, column=3) # 對文本框中的算式或答案進行復制、剪切或粘貼 def bianji(entry, argu): ? ? """ ? ? :param entry: 文本框 ? ? :param argu: 按鈕對應的值 ? ? """ ? ? if argu == 'copy': ? ? ? ? entry.event_generate("<<Copy>>") ? ? elif argu == 'cut': ? ? ? ? entry.event_generate("<<Cut>>") ? ? ? ? clear(entry) ? ? elif argu == 'paste': ? ? ? ? entry.event_generate("<<Paste>>") # 查看使用幫助和作者信息 def chakan(entry, argu): ? ? root = Tk() ? ? root.resizable(0, 0) ? ? text = Text(root, width=20, height=2, bd=5, bg='#FFFACD', font=('微軟雅黑', 12)) ? ? text.grid(padx=5, pady=5) ? ? if argu == 'help': ? ? ? ? root.title('幫助') ? ? ? ? text.insert(INSERT, '這個計算器多簡單!\n') ? ? ? ? text.insert(INSERT, '就別跟我要幫助了!') ? ? elif argu == 'author': ? ? ? ? root.title('作者') ? ? ? ? text.insert(INSERT, 'Author:冀梁棟\n') ? ? ? ? text.insert(INSERT, 'Time:2019-07-08') # 刪除最后一次輸入內(nèi)容 def backspace(entry): ? ? entry.delete(len(entry.get()) - 1) ?# 刪除文本框的最后一個輸入值 # 刪除所有輸入內(nèi)容和顯示內(nèi)容 def clear(entry): ? ? entry.delete(0, END) ?# 刪除文本框的所有內(nèi)容 ? ? label_text.set('') # 點擊計算器輸入按鈕后向文本框中添加內(nèi)容 def get_input(entry, argu): ? ? formula = entry.get() ? ? for char in formula: ? ? ? ? if '\u4e00' <= char <= '\u9fa5': ? ? ? ? ? ? clear(entry) ?# 刪除文本框中的漢字顯示,減少手動刪除操作 ? ? entry.insert(INSERT, argu) ?# 使用END時,鍵盤敲入和按鍵輸入組合操作會出錯 # 十進制整數(shù)轉(zhuǎn)換為二進制整數(shù) def jinzhi(entry): ? ? try: ? ? ? ? formula = entry.get() ? ? ? ? if re.match('\d+$', formula): ? ? ? ? ? ? number = int(formula) ? ? ? ? ? ? cunchu = [] ?# 放置每次除以2后的余數(shù) ? ? ? ? ? ? result = '' ? ? ? ? ? ? while number: ? ? ? ? ? ? ? ? cunchu.append(number % 2) ? ? ? ? ? ? ? ? number //= 2 ?# 整數(shù)除法,返回商 ? ? ? ? ? ? while cunchu: ? ? ? ? ? ? ? ? result += str(cunchu.pop()) ?# 將所有余數(shù)倒置得到結(jié)果 ? ? ? ? ? ? clear(entry) ? ? ? ? ? ? entry.insert(END, result) ? ? ? ? ? ? label_text.set(''.join(formula + '=')) ? ? ? ? else: ? ? ? ? ? ? clear(entry) ? ? ? ? ? ? entry.insert(END, '請輸入十進制整數(shù)') ? ? except: ? ? ? ? clear(entry) ? ? ? ? entry.insert(END, '出錯') # 點擊“=”后進行計算 def calculator(entry): ? ? try: ? ? ? ? formula = entry.get() ? ? ? ? # 輸入內(nèi)容只是數(shù)字或π或e時,仍顯示該內(nèi)容 ? ? ? ? if re.match('-?[\d+,π,e]\.?\d*$', formula): ? ? ? ? ? ? label_text.set(''.join(formula + '=')) ? ? ? ? ? ? return ? ? ? ? # 輸入內(nèi)容是算式時,顯示其計算結(jié)果 ? ? ? ? result = final_calc(formula_format(formula)) ? ? ? ? clear(entry) ? ? ? ? entry.insert(END, result) ?# 將結(jié)果輸出到文本框中 ? ? ? ? label_text.set(''.join(formula + '=')) ? ? except: ? ? ? ? clear(entry) ? ? ? ? entry.insert(END, '出錯') if __name__ == '__main__': ? ? root = Tk() ?# 生成窗口 ? ? root.title('理正計算器') ?# 窗口的名字 ? ? root.resizable(0, 0) ?# 窗口大小可調(diào)性,分別表示x,y方向的可變性 ? ? global label_text ?# 定義全局變量 ? ? label_text = StringVar() ? ? buju(root) ? ? root.mainloop() ?# 進入消息循環(huán)(必需組件),否則生成的窗口一閃而過
calculate.py
import re from math import * # 將算式從字符串處理成列表,解決橫杠是負號還是減號的問題 def formula_format(formula): ? ? """ ? ? :param formula: str ? ? """ ? ? formula = re.sub(' ', '', formula) ?# 去掉算式中的空格s ? ? # 以 '橫杠數(shù)字' 分割, 其中正則表達式:(\-\d+\.?\d*) 括號內(nèi): ? ? # \- 表示匹配橫杠開頭;\d+ 表示匹配數(shù)字1次或多次;\.?表示匹配小數(shù)點0次或1次;\d*表示匹配數(shù)字0次或多次。 ? ? formula_list = [i for i in re.split('(-[\d+,π,e]\.?\d*)', formula) if i] ? ? final_formula = [] ?# 最終的算式列表 ? ? for item in formula_list: ? ? ? ? # 算式以橫杠開頭,則第一個數(shù)字為負數(shù),橫杠為負號 ? ? ? ? if len(final_formula) == 0 and re.match('-[\d+,π,e]\.?\d*$', item): ? ? ? ? ? ? final_formula.append(item) ? ? ? ? ? ? continue ? ? ? ? # 如果當前的算式列表最后一個元素是運算符['+', '-', '*', '/', '(', '%', '^'], 則橫杠為減號 ? ? ? ? if len(final_formula) > 0: ? ? ? ? ? ? if re.match('[\+\-\*\/\(\%\^]$', final_formula[-1]): ? ? ? ? ? ? ? ? final_formula.append(item) ? ? ? ? ? ? ? ? continue ? ? ? ? # 按照運算符分割開 ? ? ? ? item_split = [i for i in re.split('([\+\-\*\/\(\)\%\^\√])', item) if i] ? ? ? ? final_formula += item_split ? ? return final_formula # 判斷是否是運算符,如果是返回True def is_operator(e): ? ? """ ? ? :param e: str ? ? :return: bool ? ? """ ? ? opers = ['+', '-', '*', '/', '(', ')', '%', '^', '√', 'sin', 'arcsin', 'ln'] ? ? return True if e in opers else False ?# 在for循環(huán)中嵌套使用if和else語句 # 比較連續(xù)兩個運算符來判斷是壓棧還是彈棧 def decision(tail_op, now_op): ? ? """ ? ? :param tail_op: 運算符棧的最后一個運算符 ? ? :param now_op: 從算式列表取出的當前運算符 ? ? :return: 1代表彈棧運算,0代表彈出運算符棧最后一個元素'(',-1表示壓棧 ? ? """ ? ? # 定義4種運算符級別 ? ? rate1 = ['+', '-'] ? ? rate2 = ['*', '/', '%'] ? ? rate3 = ['^', '√', 'sin', 'arcsin', 'ln'] ? ? rate4 = ['('] ? ? rate5 = [')'] ? ? if tail_op in rate1: ? ? ? ? if now_op in rate2 or now_op in rate3 or now_op in rate4: ? ? ? ? ? ? return -1 ?# 說明當前運算符優(yōu)先級高于運算符棧的最后一個運算符,需要壓棧 ? ? ? ? else: ? ? ? ? ? ? return 1 ?# 說明當前運算符優(yōu)先級等于運算符棧的最后一個運算符,需要彈棧運算 ? ? elif tail_op in rate2: ? ? ? ? if now_op in rate3 or now_op in rate4: ? ? ? ? ? ? return -1 ? ? ? ? else: ? ? ? ? ? ? return 1 ? ? elif tail_op in rate3: ? ? ? ? if now_op in rate4: ? ? ? ? ? ? return -1 ? ? ? ? else: ? ? ? ? ? ? return 1 ? ? elif tail_op in rate4: ? ? ? ? if now_op in rate5: ? ? ? ? ? ? return 0 ?# '('遇上')',需要彈出'('并丟掉')',表明該括號內(nèi)的算式已計算完成并將結(jié)果壓入數(shù)字棧中 ? ? ? ? else: ? ? ? ? ? ? return -1 ?# 只要棧頂元素為'('且當前元素不是')',都應壓入棧中 # 傳入兩個數(shù)字,一個運算符,根據(jù)運算符不同返回相應結(jié)果 def calculate(n1, n2, operator): ? ? """ ? ? :param n1: float ? ? :param n2: float ? ? :param operator: + - * / % ^ ? ? :return: float ? ? """ ? ? result = 0 ? ? if operator == '+': ? ? ? ? result = n1 + n2 ? ? if operator == '-': ? ? ? ? result = n1 - n2 ? ? if operator == '*': ? ? ? ? result = n1 * n2 ? ? if operator == '/': ? ? ? ? result = n1 / n2 ? ? if operator == '%': ? ? ? ? result = n1 % n2 ? ? if operator == '^': ? ? ? ? result = n1 ** n2 ? ? return result # 括號內(nèi)的算式求出計算結(jié)果后,計算√()、sin()或arcsin() def gaojie(op_stack, num_stack): ? ? if op_stack[-1] == '√': ? ? ? ? op = op_stack.pop() ? ? ? ? num2 = num_stack.pop() ? ? ? ? num_stack.append(sqrt(num2)) ? ? elif op_stack[-1] == 'sin': ? ? ? ? op = op_stack.pop() ? ? ? ? num2 = num_stack.pop() ? ? ? ? num_stack.append(sin(num2)) ? ? elif op_stack[-1] == 'arcsin': ? ? ? ? op = op_stack.pop() ? ? ? ? num2 = num_stack.pop() ? ? ? ? num_stack.append(asin(num2)) ? ? elif op_stack[-1] == 'ln': ? ? ? ? op = op_stack.pop() ? ? ? ? num2 = num_stack.pop() ? ? ? ? num_stack.append(log(num2)) # 負責遍歷算式列表中的字符,決定壓入數(shù)字棧中或壓入運算符棧中或彈棧運算 def final_calc(formula_list): ? ? """ ? ? :param formula_list: 算式列表 ? ? :return: 計算結(jié)果 ? ? """ ? ? num_stack = [] ?# 數(shù)字棧 ? ? op_stack = [] ?# 運算符棧 ? ? for item in formula_list: ? ? ? ? operator = is_operator(item) ? ? ? ? # 壓入數(shù)字棧 ? ? ? ? if not operator: ? ? ? ? ? ? # π和e轉(zhuǎn)換成可用于計算的值 ? ? ? ? ? ? if item == 'π': ? ? ? ? ? ? ? ? num_stack.append(pi) ? ? ? ? ? ? elif item == '-π': ? ? ? ? ? ? ? ? num_stack.append(-pi) ? ? ? ? ? ? elif item == 'e': ? ? ? ? ? ? ? ? num_stack.append(e) ? ? ? ? ? ? elif item == '-e': ? ? ? ? ? ? ? ? num_stack.append(-e) ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? num_stack.append(float(item)) ?# 字符串轉(zhuǎn)換為浮點數(shù) ? ? ? ? # 如果是運算符 ? ? ? ? else: ? ? ? ? ? ? while True: ? ? ? ? ? ? ? ? # 如果運算符棧為空,則無條件入棧 ? ? ? ? ? ? ? ? if len(op_stack) == 0: ? ? ? ? ? ? ? ? ? ? op_stack.append(item) ? ? ? ? ? ? ? ? ? ? break ? ? ? ? ? ? ? ? # 決定壓?;驈棗? ? ? ? ? ? ? ? ? tag = decision(op_stack[-1], item) ? ? ? ? ? ? ? ? # 如果是-1,則壓入運算符棧并進入下一次循環(huán) ? ? ? ? ? ? ? ? if tag == -1: ? ? ? ? ? ? ? ? ? ? op_stack.append(item) ? ? ? ? ? ? ? ? ? ? break ? ? ? ? ? ? ? ? # 如果是0,則彈出運算符棧內(nèi)最后一個'('并丟掉當前')',進入下一次循環(huán) ? ? ? ? ? ? ? ? elif tag == 0: ? ? ? ? ? ? ? ? ? ? op_stack.pop() ? ? ? ? ? ? ? ? ? ? gaojie(op_stack, num_stack) ?# '('前是'√'、'sin'或'arcsin'時,對括號內(nèi)算式的計算結(jié)果作相應的運算 ? ? ? ? ? ? ? ? ? ? break ? ? ? ? ? ? ? ? # 如果是1,則彈出運算符棧內(nèi)最后一個元素和數(shù)字棧內(nèi)最后兩個元素 ? ? ? ? ? ? ? ? elif tag == 1: ? ? ? ? ? ? ? ? ? ? if item in ['√', 'sin', 'arcsin']: ? ? ? ? ? ? ? ? ? ? ? ? op_stack.append(item) ? ? ? ? ? ? ? ? ? ? ? ? break ? ? ? ? ? ? ? ? ? ? op = op_stack.pop() ? ? ? ? ? ? ? ? ? ? num2 = num_stack.pop() ? ? ? ? ? ? ? ? ? ? num1 = num_stack.pop() ? ? ? ? ? ? ? ? ? ? # 將計算結(jié)果壓入數(shù)字棧并接著循環(huán),直到遇到break跳出循環(huán) ? ? ? ? ? ? ? ? ? ? num_stack.append(calculate(num1, num2, op)) ? ? # 大循環(huán)結(jié)束后,數(shù)字棧和運算符棧中可能還有元素的情況 ? ? while len(op_stack) != 0: ? ? ? ? op = op_stack.pop() ? ? ? ? num2 = num_stack.pop() ? ? ? ? num1 = num_stack.pop() ? ? ? ? num_stack.append(calculate(num1, num2, op)) ? ? result = str(num_stack[0]) ? ? # 去掉無效的0和小數(shù)點,例:1.0轉(zhuǎn)換為1 ? ? if result[len(result) - 1] == '0' and result[len(result) - 2] == '.': ? ? ? ? result = result[0:-2] ? ? return result if __name__ == '__main__': ? ? # formula = "2 * ( 3 - 5 * ( - 6 + 3 * 2 / 2 ) )" ? ? formula = "arcsin ( 0 )" ? ? formula_list = formula_format(formula) ? ? result = final_calc(formula_list) ? ? print("算式:", formula) ? ? print("計算結(jié)果:", result)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)圖片灰度化以及圖片顯示的兩種方法
這篇文章給大家介紹了Python實現(xiàn)圖片,灰度化以及圖片顯示的兩種方法并通過代碼示例和圖文結(jié)合的方式給大家講解的非常詳細,需要的朋友可以參考下2024-02-02使用python接受tgam的腦波數(shù)據(jù)實例
這篇文章主要介紹了使用python接受tgam的腦波數(shù)據(jù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python基于Pymssql模塊實現(xiàn)連接SQL Server數(shù)據(jù)庫的方法詳解
這篇文章主要介紹了Python基于Pymssql模塊實現(xiàn)連接SQL Server數(shù)據(jù)庫的方法,較為詳細的分析了pymssql模塊的下載、安裝及連接、操作SQL Server數(shù)據(jù)庫的相關實現(xiàn)技巧,需要的朋友可以參考下2017-07-07