代码中肯定还有一些思维漏洞,望参考者留言交流。
界面:
演示视频:
代码:
1 # coding=utf-8 2 from tkinter import * 3 import math 4 import re 5 6 7 #the main gui 8 window=Tk() 9 window.title('Calculator') 10 window.geometry('450x260') 11 mainFrame=Frame(bg='black') 12 13 #-----------button click event 14 def ButtonClick(buttonText): #except '=',use to show the expression on the entry 15 global showRes,res,oneInput 16 if buttonText!='clear' and buttonText!='back': 17 oneInput.append(buttonText) 18 showRes=showRes+buttonText 19 elif buttonText=='clear': 20 showRes='' 21 oneInput.clear() 22 else: 23 if showRes!='': 24 showRes=showRes[0:len(showRes)-len(oneInput.pop())] 25 res.set(showRes) 26 27 def MultDiv(expression,op):#multiply and divid operations 28 loc=expression.index(op) 29 if op=='×' and expression[loc+1]!='-': 30 tempRes=float(expression[loc-1])*float(expression[loc+1]) 31 elif op=='÷' and expression[loc+1]!='-':#calculate divid,not:divisor is 0 32 tempRes = float(expression[loc-1])/float(expression[loc+1]) 33 elif op=='×' and expression[loc+1]=='-': 34 tempRes = -float(expression[loc - 1]) * float(expression[loc + 2]) 35 del expression[loc-1] 36 elif op == '÷' and expression[loc + 1] == '-': 37 tempRes = -float(expression[loc - 1]) / float(expression[loc + 2]) 38 del expression[loc - 1] 39 del expression[loc-1],expression[loc-1],expression[loc-1] 40 expression.insert(loc-1,str(tempRes)) 41 return expression 42 43 def SingelValueFunction(expression,loc): 44 op=expression[loc] 45 if op=='sin ': 46 if expression[loc+1]!='-': 47 temRes=math.sin(float(expression[loc+1])) 48 else: 49 temRes=math.sin(-float(expression[loc+2])) 50 del expression[loc] 51 elif op=='cos ': 52 if expression[loc+1]!='-': 53 temRes=math.cos(float(expression[loc+1])) 54 else: 55 temRes=math.cos(-float(expression[loc+2])) 56 del expression[loc] 57 elif op=='tan ': 58 if expression[loc+1]!='-': 59 temRes=math.tan(float(expression[loc+1])) 60 else: 61 temRes=math.tan(-float(expression[loc+2])) 62 del expression[loc] 63 elif op=='abs ': 64 if expression[loc+1]!='-': 65 temRes=float(expression[loc+1]) 66 else: 67 temRes=float(expression[loc+2]) 68 del expression[loc] 69 elif op=='ln ': 70 if expression[loc+1]!='-': 71 temRes=math.log(2.718281828,float(expression[loc+1])) 72 else: 73 temRes = math.log(2.718281828, -float(expression[loc + 2])) 74 del expression[loc] 75 elif op=='sqrt ': 76 if expression[loc+1]!='-': 77 temRes=math.sqrt(float(expression[loc+1])) 78 else: 79 temRes=math.sqrt(-float(expression[loc+2])) 80 del expression[loc] 81 # elif 82 del expression[loc],expression[loc] 83 expression.insert(loc,str(temRes)) 84 return expression 85 86 def Pi(expression,op): 87 loc = expression.index(op) 88 temRes = 3.1415926535 89 del expression[loc] 90 expression.insert(loc,temRes) 91 return expression 92 93 def E(expression,op): 94 loc=expression.index(op) 95 temRes=2.718281828 96 del expression[loc] 97 expression.insert(loc,str(temRes)) 98 return expression 99 100 def Factorial(expression,op):101 loc=expression.index(op)102 temRes=math.factorial(float(expression[loc-1]))103 del expression[loc-1],expression[loc-1]104 expression.insert(loc-1,str(temRes))105 return expression106 107 def Power(expression,op):108 loc=expression.index(op)109 if expression[loc+1]!='-':110 temRes=math.pow(float(expression[loc-1]),float(expression[loc+1]))111 else:112 temRes = math.pow(float(expression[loc - 1]), -float(expression[loc + 2]))113 del expression[loc-1]114 del expression[loc-1],expression[loc-1],expression[loc-1]115 expression.insert(loc-1,str(temRes))116 return expression117 118 def WithoutBracketsExpressionCalculate(strExpression):119 expression = re.findall('([\d\.]+|÷|-|\+|×+|sin |cos |tan |ln |abs |sqrt |π|e|!|\^)', strExpression)120 print(expression)121 if expression[0] == '-': # negative number122 expression[0] = expression[0] + expression[1]123 del expression[1]124 sum=0125 #use value to replace the e and π126 while 'e' in expression:127 E(expression,'e')128 while 'π' in expression:129 Pi(expression,'π')130 # calculate single value function131 while '!' in expression:132 Factorial(expression,'!')133 134 singleValueFunctonIndexs=[]135 for i in range(len(expression)):136 if expression[i] in ['sin ','cos ','tan ','ln ','abs ','sqrt ']:137 singleValueFunctonIndexs.append(i)138 singleValueFunctonNums=len(singleValueFunctonIndexs)139 for j in range(singleValueFunctonNums):140 SingelValueFunction(expression,singleValueFunctonIndexs[singleValueFunctonNums-1-j])141 142 while '^' in expression:143 Power(expression,'^')144 145 while True:146 #calculate mul and div first147 if '×' in expression and '÷'not in expression:148 MultDiv(expression,'×')149 elif '×'not in expression and '÷'in expression:150 MultDiv(expression,'÷')151 elif '÷'in expression and '×'in expression:152 locMul=expression.index('×')153 locDiv=expression.index('÷')154 if locMul
测试样例: