-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
165 lines (129 loc) · 4.74 KB
/
calculator.py
File metadata and controls
165 lines (129 loc) · 4.74 KB
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
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import math
import operator
from MainWindow import Ui_MainWindow
# Константы
READY = 0
INPUT = 1
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
# Задаем числа
for n in range(0, 10):
getattr(self, 'pushButton_n%s' % n).pressed.connect(lambda v=n: self.input_number(v))
# Задаем операции
self.pushButton_add.pressed.connect(lambda: self.operation(operator.add))
self.pushButton_sub.pressed.connect(lambda: self.operation(operator.sub))
self.pushButton_mul.pressed.connect(lambda: self.operation(operator.mul))
self.pushButton_div.pressed.connect(lambda: self.operation(operator.truediv))
self.pushButton_ste.pressed.connect(lambda: self.operation(operator.pow))
self.pushButton_root1.pressed.connect(self.operation_root1)
self.pushButton_root2.pressed.connect(self.operation_root2)
self.pushButton_pow1.pressed.connect(self.operation_pow1)
self.pushButton_pow2.pressed.connect(self.operation_pow2)
self.pushButton_inv.pressed.connect(self.operation_inv)
self.pushButton_pi.pressed.connect(self.operation_pi)
self.pushButton_pc.pressed.connect(self.operation_pc)
self.pushButton_fact.pressed.connect(self.operation_fact)
self.pushButton_exp.pressed.connect(self.operation_exp)
self.pushButton_log.pressed.connect(self.operation_log)
self.pushButton_eq.pressed.connect(self.equals)
# Задаем действия
self.actionReset.triggered.connect(self.reset)
self.pushButton_ac.pressed.connect(self.reset)
self.pushButton_m.pressed.connect(self.memory_store)
self.pushButton_mr.pressed.connect(self.memory_recall)
self.memory = 0
self.reset()
self.show()
def display(self):
self.lcdNumber.display(self.stack[-1])
def reset(self):
self.state = READY
self.stack = [0]
self.last_operation = None
self.current_op = None
self.display()
def memory_store(self):
self.memory = self.lcdNumber.value()
def memory_recall(self):
self.state = INPUT
self.stack[-1] = self.memory
self.display()
def input_number(self, v):
if self.state == READY:
self.state = INPUT
self.stack[-1] = v
else:
self.stack[-1] = self.stack[-1] * 10 + v
self.display()
def operation(self, op):
if self.current_op: # Завершить текущую операцию
self.equals()
self.stack.append(0)
self.state = INPUT
self.current_op = op
def operation_pc(self):
self.state = INPUT
self.stack[-1] *= 0.01
self.display()
def operation_inv(self):
self.state = INPUT
self.stack[-1] *= -1
self.display()
def operation_root1(self):
self.state = INPUT
self.stack[-1] **= 0.5
self.display()
def operation_root2(self):
self.state = INPUT
self.stack[-1] **= (1/3)
self.display()
def operation_pow1(self):
self.state = INPUT
self.stack[-1] **= 2
self.display()
def operation_pow2(self):
self.state = INPUT
self.stack[-1] **= 3
self.display()
def operation_pi(self):
self.state = INPUT
self.stack[-1] = 3.14159265359
self.display()
def operation_exp(self):
self.state = INPUT
i = 1
e = 2.71828182846
print(self.stack[-1])
while i != self.stack[-1]:
i += 1
e *= 2.71828182846
self.stack[-1] = e
self.display()
def operation_fact(self):
self.state = INPUT
self.stack[-1] = math.factorial(self.stack[-1])
self.display()
def operation_log(self):
self.state = INPUT
self.stack[-1] = math.log(self.stack[-1])
self.display()
def equals(self):
if self.state == READY and self.last_operation:
s, self.current_op = self.last_operation
self.stack.append(s)
if self.current_op:
self.last_operation = self.stack[-1], self.current_op
self.stack = [self.current_op(*self.stack)]
self.current_op = None
self.state = READY
self.display()
if __name__ == '__main__':
app = QApplication([])
app.setApplicationName("Calculator")
window = MainWindow()
app.exec_()