Browse Source

更新 'taschenrechner.py'

hzhu 7 years ago
parent
commit
a0d053d258
1 changed files with 88 additions and 30 deletions
  1. 88 30
      taschenrechner.py

+ 88 - 30
taschenrechner.py

@@ -1,3 +1,49 @@
1
+from Tkinter import *
2
+
3
+def toList(text):
4
+	a = text.splitlines()
5
+    L = []
6
+    for i in a:
7
+            s = ""
8
+            z = True
9
+            for j in i:
10
+                    #print j
11
+                    if j not in '+-*/':
12
+                            s += j
13
+                            z = True
14
+                    else:
15
+                            if s:
16
+                                    L.append(s)
17
+                            if j:
18
+                                    L.append(j)
19
+                            s = ""
20
+                            z = False
21
+            if i[-1] in '+-*/':
22
+                    if z and j:
23
+                            L.append(j)
24
+            else:
25
+                    if z and s:
26
+                            L.append(s)
27
+    return L
28
+
29
+def Stack(i,s,S = None):
30
+
31
+    if not S:
32
+        S = []
33
+        
34
+    if i == len(s):
35
+        return S[0]
36
+
37
+    a = s[i]
38
+    if a.isdigit():
39
+        S.append(a)
40
+        return Stack(s, S, i+1)
41
+    else:
42
+        c = S.pop()
43
+        d = S.pop()
44
+        
45
+        S.append(Grundrechenarten(d,a,c))
46
+        return Stack(s, S, i+1)
1 47
 
2 48
 def Grundrechenarten(c,a,d):
3 49
 
@@ -15,41 +61,53 @@ def Grundrechenarten(c,a,d):
15 61
 
16 62
     return result
17 63
 
18
-def toList(s):
64
+#-----------------------------
19 65
 
20
-    L = []
21
-    for i in s:
22
-        if i != '\n':
23
-            L.append(i)
24
-    return L
66
+def response(event):
67
+    I = event.widget['text']
68
+    
69
+    texts = UPN.text.get(1.0,END+'-1c')
70
+    #print texts
25 71
 
26
-def f(i,s,S = None):
27
-
28
-    if not S:
29
-        S = []
30
-        
31
-    if i == len(s):
32
-        return S[0]
33
-
34
-    a = s[i]
35
-    if a.isdigit():
36
-        S.append(a)
37
-        return f(i+1,s,S)
38
-    else:
39
-        c = S.pop()
40
-        d = S.pop()
41
-        
42
-        S.append(Grundrechenarten(c,a,d))
43
-        return f(i+1,s,S)
72
+    if I == 'DEL':
73
+        UPN.text.delete(INSERT + '-1c',INSERT)
74
+    elif I == 'Clear':
75
+        UPN.text.delete(1.0,END)
76
+    elif I == 'Enter':
77
+        UPN.text.insert(INSERT,'\n')
78
+    elif I == '=':
79
+        s = toList(texts)
80
+        #print s
81
+        UPN.text.insert(END,'\n=\n')
82
+        UPN.text.insert(END,f(s))
83
+        UPN.text.see('end')
84
+def UPN():    
85
+    root.title('umgekehrte polnische Notation')
86
+    root.geometry('400x500+200+100')
44 87
 
88
+    UI = LabelFrame(root)
89
+    UI.place(x = 1,y = 1,width = 398,height = 124)
45 90
 
46
-x = raw_input()
47
-s = ""
48
-while x != '=\n':
49
-    s += x
50
-    x = raw_input() + '\n'
91
+    UPN.text = Text(UI)
92
+    UPN.text.place(x=1,y=1,width=390,height=116)
51 93
 
94
+    Li=[['7','8','9','+','DEL'],
95
+        ['4','5','6','-','Enter'],
96
+        ['1','2','3','*','='],
97
+        ['0','00','.','/','Clear']]
98
+ 
99
+    for i in range(len(Li[0])):
100
+        for j in range(len(Li)):
101
+            bt = Button(root)
102
+            bt.place(x=(i+1)*60,y=120+(j+1)*60,width=50,height=50)
103
+            bt['text'] = Li[j][i]
104
+            bt.bind('<Button-1>',response,add=(root,))
52 105
 
53
-print f(0,toList(s))
106
+def main():
107
+    UPN()
108
+    root.mainloop()
54 109
 
110
+if __name__ == '__main__':
111
+    root = Tk()
112
+    main()
55 113