hzhu 7 years ago
parent
commit
945a0e73b0
1 changed files with 128 additions and 0 deletions
  1. 128 0
      Ende.py

+ 128 - 0
Ende.py

@@ -0,0 +1,128 @@
1
+from Tkinter import *
2
+from tkMessageBox import showinfo
3
+#Verwendet Module,Tkinter zur Erstellung vom Taschenrechner,showinfo Fehler abzufangen
4
+#----------------------------------
5
+#Erstellung eines Taschenrechners
6
+def UPN(): 
7
+    root.title('umgekehrte polnische Notation')
8
+    root.geometry('310x440+200+100')#Die Groesse 
9
+
10
+    UI = LabelFrame(root)
11
+    UI.place(x = 1,y = 1,width = 308,height = 124)
12
+
13
+    UPN.text = Text(UI)#Das Fenster zur Ausgabe
14
+    UPN.text.place(x=1,y=1,width=300,height=116)
15
+
16
+    Li=[['7','8','9','+','DEL'],
17
+        ['4','5','6','-','Enter'],
18
+        ['1','2','3','*','='],
19
+        ['0','/','Clear','','']]
20
+	#Erstellung der Tasten mit Hilfe von Schleife
21
+    for i in range(len(Li[0])):
22
+        for j in range(len(Li)):
23
+            bts = Li[j][i]
24
+            if bts:
25
+                bt = Button(root)
26
+                bt.place(x=i*60+10,y=120+(j+1)*60,width=50,height=50)
27
+                bt['text'] = bts
28
+                bt.bind('<Button-1>',response,add=(root,))
29
+#----------------------------------
30
+def response(event):
31
+    I = event.widget['text']#Die Zeichnen auf der gedrueckten Taste zu bekommen
32
+    
33
+    texts = UPN.text.get(1.0,END+'-1c')#Die Inhalte vom Text zu bekommen
34
+    #print texts
35
+
36
+    if I == 'DEL':
37
+        UPN.text.delete(INSERT + '-1c',INSERT)
38
+    elif I == 'Clear':
39
+        UPN.text.delete(1.0,END)
40
+    elif I == 'Enter':
41
+        UPN.text.insert(INSERT,'\n')
42
+    elif I == '=':
43
+        try:
44
+            s = toList(texts)
45
+            #print s
46
+            UPN.text.insert(END,'\n=\n')
47
+            UPN.text.insert(END,Stack(s))#Das Ergebnis abzugebn
48
+            UPN.text.see('end')
49
+        except:
50
+            showinfo('Achtung:', 'Falsche Eingabe,bitte ueberpruefen Sie die Eingabe!')
51
+    else:
52
+        UPN.text.insert(INSERT,I)
53
+
54
+    UPN.text.focus()#Cursor auf der Taste
55
+#----------------------------------
56
+def toList(text):
57
+	#Die mit Zeilenvorschubzeichen Zeichenkette zu List umwandeln
58
+    a = text.splitlines()
59
+    L = []
60
+    for i in a:
61
+            s = ""
62
+            z = True
63
+            for j in i:
64
+                    #print
65
+                    if j not in '+-*/':
66
+                            s += j
67
+                            z = True
68
+                    else:
69
+                            if s:
70
+                                    L.append(s)
71
+                            if j:
72
+                                    L.append(j)
73
+                            s = ""
74
+                            z = False
75
+            if i[-1] in '+-*/':
76
+                    if z and j:
77
+                            L.append(j)
78
+            else:
79
+                    if z and s:
80
+                            L.append(s)
81
+    return L
82
+
83
+def Stack(s,S = None,i = 0):
84
+	#Definition von Stack
85
+    if not S:
86
+        S = []
87
+        
88
+    if i == len(s):
89
+        return S[0]
90
+
91
+    a = s[i]#Das Element wird nacheinander ausgenommen
92
+    if a.isdigit():#Ob es nur aus Zahlen besteht
93
+        S.append(a)
94
+        return Stack(s, S, i+1)
95
+    else:
96
+        c = S.pop()
97
+        d = S.pop()
98
+        
99
+        S.append(Grundrechenarten(d,a,c))#rechnen und einfuellen
100
+        return Stack(s, S, i+1)
101
+
102
+def Grundrechenarten(c,a,d):
103
+
104
+    if a == '+':
105
+        result = float(c)+float(d)
106
+    elif a == '-':
107
+        result = float(c)-float(d)
108
+    elif a == '*':
109
+        result = float(c)*float(d)
110
+    elif a == '/':
111
+        result = float(c)/float(d)
112
+
113
+    if result == int(result):
114
+        result = int(result)
115
+
116
+    return result
117
+#----------------------------------
118
+def main():
119
+    UPN()
120
+    root.mainloop()
121
+#main-Funktion
122
+if __name__ == '__main__':
123
+    print 'Dieses Programm ist ein Taschenrechner mit UPN-Eingabe.Es verwendet man zur Rechnung der Grundechenarten.'
124
+    print 'Programmierer: Zhu,Hongrui'
125
+    print '               Hu,Fangyu'
126
+    print '               Chen,Siyi'
127
+    root = Tk()
128
+    main()