-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc-be.pyw
174 lines (169 loc) · 4.3 KB
/
calc-be.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sys
from calcwithbtn import *
from pythonds.basic.stack import Stack
a=''
b=''
c=''
def postfixEval(postfixExpr):
operandStack = Stack()
tokenList = postfixExpr.split()
for token in tokenList:
if token not in "+-/*":
operandStack.push(float(token))
else:
operand2 = operandStack.pop()
operand1 = operandStack.pop()
result = doMath(token,operand1,operand2)
operandStack.push(result)
return operandStack.pop()
def doMath(op, op1, op2):
if op == "*":
return op1 * op2
elif op == "/":
return op1 / op2
elif op == "+":
return op1 + op2
else:
return op1 - op2
def infixToPostfix(infixexpr):
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()
for token in tokenList:
if token not in "+-/*":
postfixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.isEmpty()) and \
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)
while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)
class MyForm(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui=Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.b1,QtCore.SIGNAL('clicked()'),self.e1)
QtCore.QObject.connect(self.ui.b2,QtCore.SIGNAL('clicked()'),self.e2)
QtCore.QObject.connect(self.ui.b3,QtCore.SIGNAL('clicked()'),self.e3)
QtCore.QObject.connect(self.ui.b4,QtCore.SIGNAL('clicked()'),self.e4)
QtCore.QObject.connect(self.ui.b5,QtCore.SIGNAL('clicked()'),self.e5)
QtCore.QObject.connect(self.ui.b6,QtCore.SIGNAL('clicked()'),self.e6)
QtCore.QObject.connect(self.ui.b7,QtCore.SIGNAL('clicked()'),self.e7)
QtCore.QObject.connect(self.ui.b8,QtCore.SIGNAL('clicked()'),self.e8)
QtCore.QObject.connect(self.ui.b9,QtCore.SIGNAL('clicked()'),self.e9)
QtCore.QObject.connect(self.ui.b0,QtCore.SIGNAL('clicked()'),self.e0)
QtCore.QObject.connect(self.ui.add,QtCore.SIGNAL('clicked()'),self.ad)
QtCore.QObject.connect(self.ui.sub,QtCore.SIGNAL('clicked()'),self.su)
QtCore.QObject.connect(self.ui.mul,QtCore.SIGNAL('clicked()'),self.mu)
QtCore.QObject.connect(self.ui.div,QtCore.SIGNAL('clicked()'),self.di)
QtCore.QObject.connect(self.ui.calc,QtCore.SIGNAL('clicked()'),self.cal)
QtCore.QObject.connect(self.ui.bclear,QtCore.SIGNAL('clicked()'),self.cl)
QtCore.QObject.connect(self.ui.dotbtn,QtCore.SIGNAL('clicked()'),self.dt)
QtCore.QObject.connect(self.ui.bspace,QtCore.SIGNAL('clicked()'),self.bs)
def e1(self):
global a
a=a+'1'
self.ui.num.setText(a)
def e2(self):
global a
a=a+'2'
self.ui.num.setText(a)
def e3(self):
global a
a=a+'3'
self.ui.num.setText(a)
def e4(self):
global a
a=a+'4'
self.ui.num.setText(a)
def e5(self):
global a
a=a+'5'
self.ui.num.setText(a)
def e6(self):
global a
a=a+'6'
self.ui.num.setText(a)
def e7(self):
global a
a=a+'7'
self.ui.num.setText(a)
def e8(self):
global a
a=a+'8'
self.ui.num.setText(a)
def e9(self):
global a
a=a+'9'
self.ui.num.setText(a)
def e0(self):
global a
a=a+'0'
self.ui.num.setText(a)
def bs(self):
global a
a=a[:-1]
self.ui.num.setText(a)
def dt(self):
global a
a=a+'.'
self.ui.num.setText(a)
def cl(self):
global a
a=''
self.ui.num.clear()
def ad(self):
global a
a=a+' '
a=a+'+'
a=a+' '
self.ui.num.setText(a)
def su(self):
global a
a=a+' '
a=a+'-'
a=a+' '
self.ui.num.setText(a)
def mu(self):
global a
a=a+' '
a=a+'*'
a=a+' '
self.ui.num.setText(a)
def di(self):
global a
a=a+' '
a=a+'/'
a=a+' '
self.ui.num.setText(a)
def cal(self):
global a
global b
global c
b=infixToPostfix(a)
c=postfixEval(b)
a=str(c)
self.ui.num.setText(a)
b=''
c=''
if __name__=='__main__':
app=QtGui.QApplication(sys.argv)
ex=MyForm()
ex.show()
sys.exit(app.exec_())