前几天看到某小学毕业模拟考试有一道编程题,编写一个简单的计算器程序,要求支持小数和负数运算,不能调用math库,还不能使用eval等表达式求值函数,这还不算完,居然还要求要能够提示输入错误,对于小学生来讲,这样的题目确实还是蛮麻烦的,所以有小学生找到我,让我帮帮忙,我没办法,只能尝试着写一写。

我写的这个程序虽然不是很完美,但是也算是符合要求了。完全没有调用math库,实现了常用的数学函数,如sin,log,pow等,而且还实现了和python中eval函数类似的功能,同时还支持括号匹配检测,包括括号的优先级(即小括号在内,中括号在外),而且对一些常见的错误都可以给出提示。

有需要的同学可以拿去参考参考,里面的括号匹配和表达式求值的过程还是很值得一看的。

下面给一个连接,里面包含了源程序、使用说明书和讲解视频,大家可以下载,我的建议是,大家在使用前一定先看一下使用说明书!一定先看一下使用说明书!一定先看一下使用说明书!

链接:https://pan.baidu.com/s/1vLbVC8seXCpWjcJNXEVFLA 
提取码:e2jd

下面展示一下效果吧!

程序如下:

# -*- coding: utf-8 -*-
"""
Created on Fri May  6 01:14:57 2022
@author: 2540817538
环境:Windows 10     Spyder 4    Python 3.8
使用前请先查看使用说明书!!!!
"""import re#导入re,用于正则表达式匹配
from tkinter import *#导入thinter用于用户图形界面制作
import tkinter.messagebox###############################################################################
#创建栈类,后面的括号匹配和表达式求值会用到栈,不过也不一定非要用栈,直接进行字符串的处理也是可以的
class myStack(object):def __init__(self):# 初始化栈为空列表self.items = []def isEmpty(self):# 判断栈是否为空return self.items == []def peek(self): # 返回栈顶元素return self.items[len(self.items) - 1]def size(self): # 返回栈的大小return len(self.items)def push(self, item): # 进栈self.items.append(item)def pop(self): # 出栈return self.items.pop()def comlist(self):#返回整个栈return self.items###############################################################################
####多种函数的实现,该部分相当于实现math库的部分功能
def sin(x):#正弦函数,利用的泰勒展开式,这里值列出了sin cos 和tan,arcsin等也是利用泰勒展开式,原理类似,x=x*pi()/180#因此就不单独去写arcsin,arccos,arctan等函数了,注意x应该是角度而非弧度s=1sinx=0a=x#a作为分子b=1#b作为分母i=1while(abs(a/b)>=1e-15):sinx=sinx+s*a/b#累加一项a=a*x*x#求下一项分子b=b*2*i*(2*i+1)s=s*(-1)i=i+1return round(sinx,6)#这里的精度会影响到后面cos和tan的计算,所以最好多保留几位有效数字def cos(x) :#余弦函数return sin(90-x)def tan(x):#正切函数if (x+90)%180!=0:#其实这里不必单独考虑x的取值范围,因为如果x为负数,后面的表达式检测函数会进行报警tanx=sin(x)/cos(x)return round(tanx,6)def rootsqu(x):#开平方函数,这里不必单独考虑x的取值范围,因为如果x为负数,后面的表达式检测函数会进行报警root=xwhile  root*root>x:root=(root+x/root)/2return rootdef pows(x,n):#指数函数,这里利用了分治策略,可以降低时间复杂度if n<0:n=-nresult=1/pows(x,n)else:if n==0:result=1else:if n%2==0:result=pows(x*x,n/2)else:result=pows(x*x,(n-1)/2)*xreturn resultdef multi(x,n):#n个x相乘result=1if n==0:return 1for i in range(n):result =result*xreturn resultdef ln(x):#对数函数,如果要求log可以利用公式ln(a)/ln(b)求得ln10=2.302585k=0while x>1:x=x/10k=k+1y=x-1result=ln10*ks=i=1while i <100:result =s*multi(y,i)/i+results=(-1)*si=i+1return round(result,4)def percent(x):#求百分数return x/100def e():#e值计算,还是泰勒展开result=1cur=1i=1while i<=20:#迭代次数可以根据精度需求自行设置,一般设置在15次以上cur=cur *iresult=result+1/curi=i+1return resultdef pi():#圆周率值result=0for i in range(20):#迭代次数可以根据精度需求自行设置,一般设置在15次以上result =result+1/pows(16,i)*(4/(8*i+1)-2/(8*i+4)-1/(8*i+5)-1/(8*i+6))return resultdef sigmoid(x):#sigmiod函数实现return round(1.0/(1 + pows(e(),-x)),4)def rootcub(x):#求取立方根,这里利用了牛顿迭代法result=x/2while abs(result*result*result-x)>1e-5:result=result-(result*result*result-x)/(3*result*result)return round(result ,4)def log(x,y):#对数函数return round(ln(y)/ln(x),4)##############################################################################
####下面是表达式求值的核心模块,包括三个核心函数,其功能与内置的eval函数大同小异
def compute (num1,num2,operator):#遍历到操作符时压栈方法result =0if operator=="+":result=num1+num2if operator=="-":result=num2-num1#接近栈底的是被减数if operator=="*":result=num1*num2if operator=="/":if num1!=0:result=num2/num1#接近栈底的是被除数return resultdef priority(str1):#定义运算符优先级n=0if str1=="+" or str1=="-":n=-1if str1=="*" or str1=="/":n=1if str1=="(":n=-2if str1==")":n=2return ndef calculate(string):#计算后缀表达式并得到结果expression=[]expression=string.split(" ")#用空格分割字符串stack1=myStack()#实例化三个栈,一个存放后缀表达式,一个存放运算符,一个用于运算stack2=myStack()stack3=myStack()try:for i in range(len(expression)):if regulmatch(expression[i]):#正则表达式匹配浮点数stack1.push(expression[i])elif stack2.isEmpty(): #括号以及加减乘除的入栈操作stack2.push(expression[i])elif expression[i]=="(":stack2.push(expression[i])elif expression[i]==")":while not(stack2.peek()=="("):stack1.push(stack2.pop())stack2.pop()elif expression[i] in '+-*/(' :#加减乘除入栈while not(stack2.isEmpty()) and (priority(expression[i])<=priority(stack2.peek())):stack1.push(stack2.pop())stack2.push(expression[i])else :#小数点直接入栈stack1.push(expression[i])while not(stack2.isEmpty()):#当运算符栈不为空时,将运算符全部弹入 stack1stack1.push(stack2.pop())for j in range(stack1.size()):#遍历后缀表达式element=stack1.comlist()[j]if regulmatch(element):#正则表达式匹配数字stack3.push(element)else:if stack3.isEmpty():num1="0"else:num1=stack3.pop()if stack3.isEmpty():num2="0"else:num2=stack3.pop()result =compute(float(num1),float(num2),element)#计算stack3.push(str(result))return round(float(stack3.pop()),4)#为了方便,最后的结果就保留四位小数了except:tkinter.messagebox.showerror("表达式错误","表达式存在一些问题,例如数字直接连接函数或e、π,请仔细检查后重新输入")return string###############################################################################
#正则表达式匹配数字
def regulmatch(ch):return re.match( "-?\\d+\\.*\\d*",ch)#cal_raplace函数用来对对输入的表达式进行一些预处理,主要是将函数的值先求出来,让表达式不在含有各个函数,而是将其替换成函数的计算结果
#请注意,函数会优先选择匹配数字,比如输入- 2 pows 4,程序默认为输入的是-( 2 pows 4 )
#并且程序不支持链接括号,比如sin(30 + 30)是非法的,应该直接写成sin 60
def cal_replace(string):string=string.replace('  ',' ')expression=string.split(" ")#用空格分割字符串flag=0#这个标志用来记录表达式中是否有替换的地方,用以解决函数嵌套问题,例如ln ln 5的情况set1={'sin','cos','tan','rootcub','rootsqu','pows','sigmoid'}try:for i in range(len(expression)):if expression[i]=='(' and  expression[i+1]=='-':#这里的处理是为了方便复数的运算,例如1-(-2)string=string.replace('( - ','( -')flag=1if expression[i]=="e":#以下内容基本上都是求函数值,比如将表达式中的sin30替换成0.5result=e()string=string.replace('e', str(result))flag=1if expression[i]=="π":result=pi()string=string.replace('π', str(result))flag=1if expression[i] in set1 and expression[i+1] in '-' and regulmatch(expression[i+2]):string=string.replace(expression[i+1]+' '+expression[i+2], str(float(expression[i+2])*(-1)))flag=1if expression[i]=="pows" and  regulmatch(expression[i+1]) and regulmatch(expression[i-1]) and float(expression[i+1]).is_integer():result=pows(float(expression[i-1]),float(expression[i+1]))string=string.replace(expression[i-1]+' pows '+expression[i+1], str(result))flag=1if expression[i]=="log" and  regulmatch(expression[i+1]) and  regulmatch(expression[i-1]) and float(expression[i-1])>0 and float(expression[i+1])>0:result=log(float(expression[i-1]),float(expression[i+1]))string=string.replace(expression[i-1]+' log '+expression[i+1], str(result))flag=1if expression[i]=="sin"and  regulmatch(expression[i+1]):result=sin(float(expression[i+1]))string=string.replace('sin '+expression[i+1], str(result))flag=1if expression[i]=="cos" and  regulmatch(expression[i+1]):result=cos(float(expression[i+1]))string=string.replace('cos '+expression[i+1], str(result))flag=1if expression[i]=="tan" and  regulmatch(expression[i+1]) and (float(expression[i+1])+90)%180!=0:result=tan(float(expression[i+1]))string=string.replace('tan '+expression[i+1], str(result))flag=1if expression[i]=="rootsqu" and  regulmatch(expression[i+1]) and float(expression[i+1])>=0:result=rootsqu(float(expression[i+1]))string=string.replace('rootsqu '+expression[i+1], str(result))flag=1if expression[i]=="rootcub" and  regulmatch(expression[i+1]):result=rootcub(float(expression[i+1]))string=string.replace('rootcub '+expression[i+1], str(result))flag=1if expression[i]=="ln" and regulmatch(expression[i+1]) and float(expression[i+1])>0:result=ln(float(expression[i+1]))string=string.replace('ln '+expression[i+1], str(result))flag=1if expression[i]=="%" and regulmatch(expression[i-1]) :result=percent(float(expression[i-1]))string=string.replace(expression[i-1]+" %", str(result))flag=1if expression[i]=="sigmoid" and regulmatch(expression[i+1]) and float(expression[i+1]).is_integer():result=sigmoid(float(expression[i+1]))#因为用到了pows函数,pows不支持指数为小数string=string.replace('sigmoid '+expression[i+1], str(result))flag=1if flag==1:#如果表达式中出现替换,那么就需要递归调用该函数,解决例如sin sin 30的问题string=cal_replace(string)return stringexcept :#并不能保证表达式能够正常替换,比如输入tan tan tan,最后没有数字,这种情况显然不合理,因此需要抛出异常tkinter.messagebox.showerror("表达式错误",'表达式存在一些问题,请仔细检查后输入正确的表达式')return string###############################################################################
#将表达式中的括号提取出来方便处理,不过不提出来影响也不大
def find_bra(string):result=''for i in string:if i in "[]()":result=result+ireturn result#match函数是用于括号匹配的函数,因为这里的[]()存在优先级的关系,所以直接用栈来完成会很复杂,比较方便的一种方式是
#用栈配合和字符串的检测来完成
def match(string):string=find_bra(string)bracket=[]try:for i in range(len(string)):if string[i] in "[](){}" :#由于{}判断原理与()[]相同,且本计算器中没有{},所以后面的部分讨论就没有再考虑{}bracket.append(string[i])if len(bracket)%2!=0:#括号数为奇数的直接进行报错ans=stringif "()" in string:ans=ans.replace("()", "")if "[]" in string:ans=ans.replace("[]", "")tkinter.messagebox.showerror("括号匹配错误",'对不起,语法错误,'+ans+'使用不合理')return Falseif "[]" in string:#这一部分可以检测出括号优先级出错的情况,这里不需要考虑{}的情况tkinter.messagebox.showerror("括号匹配错误","对不起,语法错误,[ ]使用不合理,使用( )后才能使用[ ]")return Falseif "((" in string:tkinter.messagebox.showerror("括号匹配错误","对不起,语法错误,( )使用不合理,内层使用( )后外层应使用[ ]")return Falseif "[[" in string:tkinter.messagebox.showerror("括号匹配错误","对不起,语法错误,[ ]使用不合理,内层使用[ ]后外层应使用{ }")return Falseif "([" in string:tkinter.messagebox.showerror("括号匹配错误","对不起,语法错误,([ ])使用不合理,( )应放在[ ]内层")return FalsebraStack = myStack()#创建类实例leftbkt = "{[("rightbkt = "}])"for i in string:#这一部分用于检测左括号和右括号不匹配的情况if i in leftbkt:braStack.push(i)elif i in rightbkt:if braStack.isEmpty():tkinter.messagebox.showerror("括号匹配错误","对不起,语法错误, "+i+" 使用不合理")return Falseif rightbkt.index(i) != leftbkt.index(braStack.pop()):if '[)' in string:i='['tkinter.messagebox.showerror("括号匹配错误","对不起,语法错误, "+i+" 使用不合理")return Falseif braStack.isEmpty():return Trueelse:tkinter.messagebox.showerror("括号匹配错误","对不起,语法错误, "+braStack.pop()+" 使用不合理")return Falseexcept:tkinter.messagebox.showerror("括号匹配错误","在括号匹配过程中遇到一些错误,请重新输入表达式")return False###############################################################################
#这个函数输出字符串中小数点数目主要是用来检测小数点是否错误,比如出现1.2.3这种错误的格式
def count_char(str):dict = 0for i in str:if i =='.':dict += 1return dict#char_judge函数主要是用来解决e和π连续输入,跟数字和函数时出错的情况,例如etan、e9等情况
def char_judge(string):setwrong={'πe','eπ','ππ','ee','πt','πs','πc','πl','et','es','ec','el','πr','er'}setwrong2={'π0','π1','π2','π3','π4','π5','π6','π7','π8','π9','e1','e0','e2','e3','e4','e5','e6','e7','e8','e9'}for i in setwrong :if i in string:return Truefor i in setwrong2 :if i in string:return Truereturn False#find_wrong函数是用来检测表达式合法性的主要函数,主要检测一些比较容易描述的典型错误,主要包括复数的输入格式错误、eπ的使用问题,
#运算符、小数点的问题、函数的输入格式和取值范围问题等,一些不便于描述的错误直接统一报错
def find_wrong(string):string1=string.replace(" ", "")#一部分检测需要先将表达式中的空格去掉才能进行set1='*%/.'set3='+-*/'set2='0123456789'set4={"sin","cos",'tan',"ln","sigmoid","rootsqu","rootcub",'log','pows'}set5={'log','pows'}try:#前一部分检测的是去掉空格后的字符串if "+-" in string1 or "/-" in string1 or "--" in string1 or "*-" in string1:#表达式中间的复数最好写成(-x)的形式tkinter.messagebox.showerror('表达式输入错误',"请将复数写成0-x的形式,或者写成(-x)")return Falseif string1[0] in set1:#这些符号出现在表达式最前面显然是错误的tkinter.messagebox.showerror('表达式输入错误',"表达式的第一个符号使用不合理,请仔细检查后正确输入")return Falseif string1[-1] in set3:tkinter.messagebox.showerror('表达式输入错误',"表达式的最后一个符号使用不合理,请仔细检查后正确输入")return Falseif char_judge(string1) :tkinter.messagebox.showerror('表达式输入错误',"e或π的使用有问题,请仔细检查后正确输入")return Falsefor i in range(len(string1)):#这里的i不用考虑越界问题,因为越界后后面会抛出异常if string1[i]=='.' and i!=0 and i!=len(string1)-1:#小数点必然在两个数字之间if (string1[i-1] not in set2) or (string1[i+1] not in set2):tkinter.messagebox.showerror('表达式输入错误',".的使用不正确,请仔细检查后正确输入")return Falseif string1[i]=='/' and i!=0 and i!=len(string1)-1:#加减乘除的判断,检测是否有连续输入运算符的情况,例如2++1,3*-1if (string1[i-1]  in '+-*/') or (string1[i+1]  in '+-*/%'):tkinter.messagebox.showerror('表达式输入错误',"/的使用不正确,请仔细检查后正确输入")return Falseif string1[i]=='+' and i!=0 and i!=len(string1)-1:if (string1[i-1]  in '+-*/') or (string1[i+1]  in '+-*/%'):tkinter.messagebox.showerror('表达式输入错误',"+的使用不正确,请仔细检查后正确输入")return Falseif string1[i]=='-' and i!=0 and i!=len(string1)-1:if (string1[i-1]  in '+-*/') or (string1[i+1]  in '+-*/%'):tkinter.messagebox.showerror('表达式输入错误',"-的使用不正确,请仔细检查后正确输入")return Falseif string1[i]=='*' and i!=0 and i!=len(string1)-1:if (string1[i-1]  in '+-*/') or (string1[i+1]  in '+-*/%'):tkinter.messagebox.showerror('表达式输入错误',"*的使用不正确,请仔细检查后正确输入")return Falseif string1[i]=='%' and (string1[i+1] not in '+-*/)]' or string[i-1] in set4 ):tkinter.messagebox.showerror('表达式输入错误','%的使用不正确,请仔细检查后正确输入')return Falseif string1[i]=='/' and string1[i+1]=='0':#除数不能为0的检测tkinter.messagebox.showerror('表达式错误',"语法错误,除数不能为0,请仔细检查后正确输入")return Falseexpression=[]#以下部分需要将字符串通过空格进行分割后再检测string1=string.replace("  ", " ")expression=string.split(" ")#用空格分割字符串if expression[-1] in set4:tkinter.messagebox.showerror('表达式输入错误',"数学函数的使用存在问题,请仔细检查后正确输入")return Falseif expression[0] in set5:tkinter.messagebox.showerror('表达式输入错误',"数学函数的使用存在问题,请仔细检查后正确输入")return Falsefor i in expression:if count_char(i)>1:tkinter.messagebox.showerror('表达式输入错误',"表达式中存在不正确的数字:"+i+',请仔细检查后正确输入')return Falsefor i in range(len(expression)-1):#这里主要检测的是取值范围和函数的格式问题,例如sin(30+30)是不正确的,应该写为sin60if expression[i] in set4 and expression[i+1] =='(':tkinter.messagebox.showerror('表达式输入错误','该数学函数不支持嵌套表达式,请将括号内的式子单独计算,再使用该函数')return Falseif (expression[i]=='log' or  expression[i]=='pows' ) and expression[i-1]==')':tkinter.messagebox.showerror('表达式输入错误','该数学函数不支持嵌套表达式,请将括号内的式子单独计算,再使用该函数')return Falseif (expression[i]=='log' or  expression[i]=='pows' ) and (expression[i-1]=='' or expression[i-1] in "+-*/%."):tkinter.messagebox.showerror('表达式输入错误','函数缺少一个输入值,请仔细检查后正确输入')return Falseif (expression[i]=='log' or  expression[i]=='pows' ) and (expression[i+1]=='' or expression[i+1] in "+-*/%."):tkinter.messagebox.showerror('表达式输入错误','函数缺少一个输入值,请仔细检查后正确输入')return Falseif expression[i]=='tan' and (float(expression[i+1])+90)%180==0:tkinter.messagebox.showerror('表达式输入错误','tan取值有问题,请仔细检查后正确输入')return Falseif expression[i]=='ln'and expression[i+1]=='-':tkinter.messagebox.showerror('表达式输入错误','ln取值有问题,请仔细检查后正确输入')return Falseif expression[i]=='log'and (expression[i+1]=='-' or expression[i+1]=='0'):tkinter.messagebox.showerror('表达式输入错误','log取值有问题,请仔细检查后正确输入')return Falseif expression[i]=='ln'and (expression[i+1]=='0' or float(expression[i+1])<0):tkinter.messagebox.showerror('表达式输入错误','ln取值有问题,请仔细检查后正确输入')return Falseif expression[i]=='log'and (float(expression[i-1])<=0 or float(expression[i+1])<=0):tkinter.messagebox.showerror('表达式输入错误','log取值有问题,请仔细检查后正确输入')return Falseif expression[i]=='rootsqu'and float(expression[i+1])<0 :tkinter.messagebox.showerror('表达式输入错误','rootsqu取值有问题,请仔细检查后正确输入')return False if (expression[i]=='sigmoid' or  expression[i]=='pows' ) and  not(float(expression[i+1]).is_integer()):tkinter.messagebox.showerror('表达式输入错误','该数学函数不支持小数,请仔细检查后正确输入')return Falsereturn Trueexcept:#这里主要是考虑到异常输入会使前面的检测出现越界的情况,比如tan%等,还有一些不便于描述的错误,在这里一起抛出tkinter.messagebox.showerror('表达式输入错误','表达式输入存在错误,请仔细检查后正确输入')return False###############################################################################
#######下面主要是和GUI生成的模块
#这个函数将[]转换成(),在进行括号匹配检测后,[]已经没有意义,正常的计算器只需要()就足够了,calculate函数也不需要单独用[]
def transbra(string):string=string.replace('[','(')string=string.replace(']',')')return stringdef button_click(string):#数字和函数按钮的相应global calc_operator#calc_operator必须设置成全局变量,因为在多个函数中都将对其进行操作calc_operator=calc_operator+stringcalc_operator1=calc_operator.replace(' ',"")#将表达式中大量的空格去掉后再显示text_input.set(calc_operator1)def button_clear_all():#AC按钮的响应global calc_operatorcalc_operator=""text_input.set("")def button_delete():#⌫按钮的响应global calc_operatortext=calc_operator[:-1]calc_operator=texttext_input.set(text)def button_equal():#=按钮的响应global calc_operatorstring=calc_operatorstring=string.replace('  ',' ')#部分情况下表达式中会出现连续两个空格,这时候需要去掉一个空格,以免影响后续使用a=match(string)string=cal_replace(string)b=find_wrong(string)#这里可以改成a为True的时候才执行,就可以减少执行量,提高速度if a and b:string=transbra(string)#[]转化为()print('计算的表达式为:',calc_operator.replace(' ',''))temp_op=str(calculate(string))text_input.set(temp_op)calc_operator=temp_opelse:print('计算的表达式为:',string.replace(' ',''))calc_operator=""tk_calculate=Tk()
tk_calculate.configure(bg="#d3d7d4", bd=10)
tk_calculate.title("网宿科技")calc_operator=""
text_input=StringVar()#文本框
text_display=Entry(tk_calculate, font=('sans-serif', 27, 'bold'), textvariable=text_input,bd=5, insertwidth = 5, bg='#fffffb', justify='right').grid(columnspan=5, padx = 10, pady = 15)
button_params={'bd':5, 'fg':'#BBB', 'bg':'#3C3636', 'font':('sans-serif', 20, 'bold')}#按钮格式的统一设置
button_params_main={'bd':5, 'fg':'#000', 'bg':'#BBB', 'font':('sans-serif', 20, 'bold')}#下面是各个按钮的排列,格式基本上都一样
#第一行
button_sin=Button(tk_calculate, button_params, text='sin',command=lambda:button_click('sin ')).grid(row=1, column=0, sticky="nsew")
button_cos=Button(tk_calculate, button_params, text='cos',command=lambda:button_click('cos ')).grid(row=1, column=1, sticky="nsew")
button_tan=Button(tk_calculate, button_params, text='tan',command=lambda:button_click('tan ')).grid(row=1, column=2, sticky="nsew")
button_e=Button(tk_calculate, button_params, text=' e ',command=lambda:button_click('e')).grid(row=1, column=3, sticky="nsew")
button_pi=Button(tk_calculate, button_params, text=' π ',command=lambda:button_click('π')).grid(row=1, column=4, sticky="nsew")
#第二行
button_root2=Button(tk_calculate, button_params, text='\u00B2\u221A',command=lambda:button_click('rootsqu ')).grid(row=2, column=0, sticky="nsew")
button_root3=Button(tk_calculate, button_params, text='\u00B3\u221A',command=lambda:button_click('rootcub ')).grid(row=2, column=1, sticky="nsew")
button_pows=Button(tk_calculate, button_params, text='x^n',command=lambda:button_click(' pows ')).grid(row=2, column=2, sticky="nsew")
button_sigmoid=Button(tk_calculate, button_params, text='sigmoid',font=("宋体",10),command=lambda:button_click('sigmoid ')).grid(row=2,  column=3, sticky="nsew")
button_log=Button(tk_calculate, button_params, text='log ',command=lambda:button_click(' log ')).grid(row=2,  column=4, sticky="nsew")
#第三行
button_kuol1=Button(tk_calculate, button_params, text='(',command=lambda:button_click('( ')).grid(row=3, column=0, sticky="nsew")
button_kuor1=Button(tk_calculate, button_params, text=')',command=lambda:button_click(' )')).grid(row=3, column=1, sticky="nsew")
button_kuol2=Button(tk_calculate, button_params, text='[',command=lambda:button_click('[ ')).grid(row=3, column=2, sticky="nsew")
button_kuor2=Button(tk_calculate, button_params, text=']',command=lambda:button_click(' ]')).grid(row=3, column=3, sticky="nsew")
button_ln=Button(tk_calculate, button_params, text='ln', font=('sans-serif', 15, 'bold'),command=lambda:button_click('ln ')).grid(row=3, column=4, sticky="nsew")
#第四行
button_0=Button(tk_calculate, button_params_main, text='0',command=lambda:button_click('0')).grid(row=4, column=0, sticky="nsew")
button_point=Button(tk_calculate, button_params_main, text='.',command=lambda:button_click('.')).grid(row=4, column=1, sticky="nsew")
button_percent=Button(tk_calculate, button_params_main, text='%',command=lambda:button_click(' %')).grid(row=4, column=2, sticky="nsew")
button_delete_one=Button(tk_calculate, bd=5, fg='#000', font=('sans-serif', 20, 'bold'),text='⌫', command=button_delete, bg='#db701f').grid(row=4, column=3, sticky="nsew")
button_delete_all=Button(tk_calculate, bd=5, fg='#000', font=('sans-serif', 20, 'bold'),text='AC', command=button_clear_all, bg='#db701f').grid(row=4, column=4, sticky="nsew")
#第五行
button_7=Button(tk_calculate, button_params_main, text='7',command=lambda:button_click('7')).grid(row=5, column=0, sticky="nsew")
button_8=Button(tk_calculate, button_params_main, text='8',command=lambda:button_click('8')).grid(row=5, column=1, sticky="nsew")
button_9=Button(tk_calculate, button_params_main, text='9',command=lambda:button_click('9')).grid(row=5, column=2, sticky="nsew")
button_mul=Button(tk_calculate, button_params_main, text='*',command=lambda:button_click(' * ')).grid(row=5, column=3, sticky="nsew")
button_dev=Button(tk_calculate, button_params_main, text='/',command=lambda:button_click(' / ')).grid(row=5, column=4, sticky="nsew")
#第六行
button_4=Button(tk_calculate, button_params_main, text='4',command=lambda:button_click('4')).grid(row=6, column=0, sticky="nsew")
button_5=Button(tk_calculate, button_params_main, text='5',command=lambda:button_click('5')).grid(row=6, column=1, sticky="nsew")
button_6=Button(tk_calculate, button_params_main, text='6',command=lambda:button_click('6')).grid(row=6, column=2, sticky="nsew")
button_add=Button(tk_calculate, button_params_main, text='+',command=lambda:button_click(' + ')).grid(row=6, column=3, sticky="nsew")
button_sub=Button(tk_calculate, button_params_main, text='-',command=lambda:button_click(' - ')).grid(row=6, column=4, sticky="nsew")
#第七行
button_1=Button(tk_calculate, button_params_main, text='1',command=lambda:button_click('1')).grid(row=7, column=0, sticky="nsew")
button_2=Button(tk_calculate, button_params_main, text='2',command=lambda:button_click('2')).grid(row=7, column=1, sticky="nsew")
button_3=Button(tk_calculate, button_params_main, text='3', font=('sans-serif', 16, 'bold'),command=lambda:button_click('3')).grid(row=7, column=2, sticky="nsew")
button_equal=Button(tk_calculate, button_params_main, text='=',command=button_equal).grid(row=7, columnspan=2, column=3, sticky="nsew")tk_calculate.mainloop()
#转载请注明作者

Python计算器程序实现,支持括号与符号检测、小数、负数运算相关推荐

  1. Python计算器程序(两位数的加减乘除操作)

    Python计算器程序(两位数的加减乘除操作) input("第一个数") input("操作符.运算符号") input("第二个数") ...

  2. python计算器程序_利用Python代码编写计算器小程序

    1 importtkinter2 importtkinter.messagebox3 importmath4 classJSQ:5 6 7 def __init__(self):8 #创建主界面 9 ...

  3. python计算器程序代码_7_python之路之python计算器

    #!/usr/bin/env python#_*_ coding: utf-8 _*_#author:chenjianwen#email:1071179133@qq.com importreimpor ...

  4. python计算器程序_python练习 计算器模拟程序

    importtkinterimportmathfrom functools importpartial#按钮输入调用 defget_input(entry, argu):#从entry窗口展示中获取输 ...

  5. Python计算器简单程序

    Python计算器 (Python Calculator) In this quick post, we will see how we can create a very simple python ...

  6. python编写程序-30分钟学会用Python编写简单程序

    参与文末每日话题讨论,赠送异步新书 异步图书君 学习目标 知道有序的软件开发过程的步骤. 了解遵循输入.处理.输出(IPO)模式的程序,并能够以简单的方式修改它们. 了解构成有效Python标识符和表 ...

  7. python计算器小程序源码_python编写计算器程序-python代码编写计算器小程序

    本文实例为大家分享了python计算器小程序的具体代码,供大家参考,具体内容如下 import tkinter import tkinter.messagebox import math class ...

  8. Java实现图形化计算器(支持括号、开平方)

    Java实现图形化计算器(支持括号.开平方) Java实习作业,实现一个图形化计算器,正好赶上周末作业不多就写了一个计算器.功能不是特别全,但是能满足一些最基本的需求(比如小学三年级及以下的数学题,, ...

  9. Python编程学习——运用python编写简单的计算器程序

    简单计算器的程序实现 一.功能要求: 实现用户输入算式,其中包含加减乘除,括号以及空格的算式,并且优先计算最里的括号的算式,例如1+ 22*3 2-2 2*(2 3 1+34*33/4+55*(1 2 ...

  10. python计算器小程序源码_python代码编写计算器小程序

    本文实例为大家分享了python计算器小程序的具体代码,供大家参考,具体内容如下 import tkinter import tkinter.messagebox import math class ...

最新文章

  1. 783. 二叉搜索树节点最小距离
  2. new file https 找不到路径_Python3用pathlib模块替代os.path进行文件路径的操作
  3. MyBatis 传递多个参数
  4. webservice 启用代理服务器
  5. “头号大厂铁粉”微软宣布关闭区块链服务
  6. Python第七章-面向对象高级
  7. c++incline函数
  8. windows2008不能显示图片缩略图设置
  9. 将20180608141920转成date格式
  10. libQt5Core.so: undefined reference to `dlclose@GLIBC_2.4'
  11. Python输入一个表示星期的数字(1表示星期一,2表示星期二......6表示星期六,7表示星期日),输出对应的星期英文单词
  12. Pytorch实战__反向攻击(Adversarial Attack)
  13. 叉烧fan的arduino自学笔记(一) 人体红外报警器
  14. matlab仿真中pv,PV的matlab仿真
  15. RHEL5 Oracle 11G R2 RAC 静默安装 (三) rdbms安装 dbca 建库
  16. DPM learn.cc编译
  17. crash 和 anr的区别
  18. 2-1. Python 数据类型、运算
  19. win10 ,在任务栏上,设置快速启动栏
  20. WiFi共享精灵手机版隆重上线:流量“变现”WiFi

热门文章

  1. 深入浅出设计模式---6、装饰者模式
  2. Extjs4.0 视频教程
  3. vc2017 c语言运行,vc++2017下载|vc++2017运行库 32位64位 官方版 - 软件下载 - 绿茶软件园|33LC.com...
  4. ASCIIMathML技术简介~
  5. TCPIP详解卷一概述 学习记录 2020/4/13
  6. 2022年PMP培训机构如何挑选?哪家好?
  7. revit2016与2017区别_revit2017下载及新功能介绍
  8. php的jquery ajax请求,jquery ajax请求
  9. 【LeetCode】贪心算法:常见典例
  10. java开发按键精灵_一个使用JAVA编写的类似按键精灵的程序