1 | //! [0]
|
---|
2 | function Calculator(ui)
|
---|
3 | {
|
---|
4 | this.ui = ui;
|
---|
5 |
|
---|
6 | this.pendingAdditiveOperator = "";
|
---|
7 | this.pendingMultiplicativeOperator = "";
|
---|
8 | this.sumInMemory = 0;
|
---|
9 | this.sumSoFar = 0;
|
---|
10 | this.factorSoFar = 0;
|
---|
11 | this.waitingForOperand = true;
|
---|
12 |
|
---|
13 | with (ui) {
|
---|
14 | display.text = "0";
|
---|
15 |
|
---|
16 | zeroButton.clicked.connect(this, this.digitClicked);
|
---|
17 | oneButton.clicked.connect(this, "digitClicked");
|
---|
18 | twoButton.clicked.connect(this, "digitClicked");
|
---|
19 | threeButton.clicked.connect(this, "digitClicked");
|
---|
20 | fourButton.clicked.connect(this, "digitClicked");
|
---|
21 | fiveButton.clicked.connect(this, "digitClicked");
|
---|
22 | sixButton.clicked.connect(this, "digitClicked");
|
---|
23 | sevenButton.clicked.connect(this, "digitClicked");
|
---|
24 | eightButton.clicked.connect(this, "digitClicked");
|
---|
25 | nineButton.clicked.connect(this, "digitClicked");
|
---|
26 |
|
---|
27 | pointButton.clicked.connect(this, "pointClicked");
|
---|
28 | changeSignButton.clicked.connect(this, "changeSignClicked");
|
---|
29 |
|
---|
30 | backspaceButton.clicked.connect(this, "backspaceClicked");
|
---|
31 | clearButton.clicked.connect(this, "clear");
|
---|
32 | clearAllButton.clicked.connect(this, "clearAll");
|
---|
33 |
|
---|
34 | clearMemoryButton.clicked.connect(this, "clearMemory");
|
---|
35 | readMemoryButton.clicked.connect(this, "readMemory");
|
---|
36 | setMemoryButton.clicked.connect(this, "setMemory");
|
---|
37 | addToMemoryButton.clicked.connect(this, "addToMemory");
|
---|
38 |
|
---|
39 | divisionButton.clicked.connect(this, "multiplicativeOperatorClicked");
|
---|
40 | timesButton.clicked.connect(this, "multiplicativeOperatorClicked");
|
---|
41 | minusButton.clicked.connect(this, "additiveOperatorClicked");
|
---|
42 | plusButton.clicked.connect(this, "additiveOperatorClicked");
|
---|
43 |
|
---|
44 | squareRootButton.clicked.connect(this, "unaryOperatorClicked");
|
---|
45 | powerButton.clicked.connect(this, "unaryOperatorClicked");
|
---|
46 | reciprocalButton.clicked.connect(this, "unaryOperatorClicked");
|
---|
47 | equalButton.clicked.connect(this, "equalClicked");
|
---|
48 | }
|
---|
49 | }
|
---|
50 | //! [0]
|
---|
51 |
|
---|
52 | Calculator.prototype.abortOperation = function()
|
---|
53 | {
|
---|
54 | this.clearAll();
|
---|
55 | this.ui.display.text = "####";
|
---|
56 | }
|
---|
57 |
|
---|
58 | Calculator.prototype.calculate = function(rightOperand, pendingOperator)
|
---|
59 | {
|
---|
60 | if (pendingOperator == "+") {
|
---|
61 | this.sumSoFar += rightOperand;
|
---|
62 | } else if (pendingOperator == "-") {
|
---|
63 | this.sumSoFar -= rightOperand;
|
---|
64 | } else if (pendingOperator == "*") {
|
---|
65 | this.factorSoFar *= rightOperand;
|
---|
66 | } else if (pendingOperator == "/") {
|
---|
67 | if (rightOperand == 0)
|
---|
68 | return false;
|
---|
69 | this.factorSoFar /= rightOperand;
|
---|
70 | }
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | //! [1]
|
---|
75 | Calculator.prototype.digitClicked = function()
|
---|
76 | {
|
---|
77 | var digitValue = __qt_sender__.text - 0;
|
---|
78 | if ((digitValue == 0) && (this.ui.display.text == "0"))
|
---|
79 | return;
|
---|
80 | if (this.waitingForOperand) {
|
---|
81 | this.ui.display.clear();
|
---|
82 | this.waitingForOperand = false;
|
---|
83 | }
|
---|
84 | this.ui.display.text += digitValue;
|
---|
85 | }
|
---|
86 | //! [1]
|
---|
87 |
|
---|
88 | Calculator.prototype.unaryOperatorClicked = function()
|
---|
89 | {
|
---|
90 | var operand = this.ui.display.text - 0;
|
---|
91 | var result = 0;
|
---|
92 | if (__qt_sender__.text == "Sqrt") {
|
---|
93 | if (operand < 0) {
|
---|
94 | this.abortOperation();
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | result = Math.sqrt(operand);
|
---|
98 | } else if (__qt_sender__.text == "x^2") {
|
---|
99 | result = Math.pow(operand, 2);
|
---|
100 | } else if (__qt_sender__.text == "1/x") {
|
---|
101 | if (operand == 0.0) {
|
---|
102 | this.abortOperation();
|
---|
103 | return;
|
---|
104 | }
|
---|
105 | result = 1 / operand;
|
---|
106 | }
|
---|
107 | this.ui.display.text = result + "";
|
---|
108 | this.waitingForOperand = true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | Calculator.prototype.additiveOperatorClicked = function()
|
---|
112 | {
|
---|
113 | var operand = this.ui.display.text - 0;
|
---|
114 |
|
---|
115 | if (this.pendingMultiplicativeOperator.length != 0) {
|
---|
116 | if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
|
---|
117 | this.abortOperation();
|
---|
118 | return;
|
---|
119 | }
|
---|
120 | this.ui.display.text = this.factorSoFar + "";
|
---|
121 | operand = this.factorSoFar;
|
---|
122 | this.factorSoFar = 0;
|
---|
123 | this.pendingMultiplicativeOperator = "";
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (this.pendingAdditiveOperator.length != 0) {
|
---|
127 | if (!this.calculate(operand, this.pendingAdditiveOperator)) {
|
---|
128 | this.abortOperation();
|
---|
129 | return;
|
---|
130 | }
|
---|
131 | this.ui.display.text = this.sumSoFar + "";
|
---|
132 | } else {
|
---|
133 | this.sumSoFar = operand;
|
---|
134 | }
|
---|
135 |
|
---|
136 | this.pendingAdditiveOperator = __qt_sender__.text;
|
---|
137 | this.waitingForOperand = true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | Calculator.prototype.multiplicativeOperatorClicked = function()
|
---|
141 | {
|
---|
142 | var operand = this.ui.display.text - 0;
|
---|
143 |
|
---|
144 | if (this.pendingMultiplicativeOperator.length != 0) {
|
---|
145 | if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
|
---|
146 | this.abortOperation();
|
---|
147 | return;
|
---|
148 | }
|
---|
149 | this.ui.display.text = this.factorSoFar + "";
|
---|
150 | } else {
|
---|
151 | this.factorSoFar = operand;
|
---|
152 | }
|
---|
153 |
|
---|
154 | this.pendingMultiplicativeOperator = __qt_sender__.text;
|
---|
155 | this.waitingForOperand = true;
|
---|
156 | }
|
---|
157 |
|
---|
158 | Calculator.prototype.equalClicked = function()
|
---|
159 | {
|
---|
160 | var operand = this.ui.display.text - 0;
|
---|
161 |
|
---|
162 | if (this.pendingMultiplicativeOperator.length != 0) {
|
---|
163 | if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
|
---|
164 | this.abortOperation();
|
---|
165 | return;
|
---|
166 | }
|
---|
167 | operand = this.factorSoFar;
|
---|
168 | this.factorSoFar = 0.0;
|
---|
169 | this.pendingMultiplicativeOperator = "";
|
---|
170 | }
|
---|
171 | if (this.pendingAdditiveOperator.length != 0) {
|
---|
172 | if (!this.calculate(operand, this.pendingAdditiveOperator)) {
|
---|
173 | this.abortOperation();
|
---|
174 | return;
|
---|
175 | }
|
---|
176 | this.pendingAdditiveOperator = "";
|
---|
177 | } else {
|
---|
178 | this.sumSoFar = operand;
|
---|
179 | }
|
---|
180 |
|
---|
181 | this.ui.display.text = this.sumSoFar + "";
|
---|
182 | this.sumSoFar = 0.0;
|
---|
183 | this.waitingForOperand = true;
|
---|
184 | }
|
---|
185 |
|
---|
186 | Calculator.prototype.pointClicked = function()
|
---|
187 | {
|
---|
188 | if (this.waitingForOperand)
|
---|
189 | this.ui.display.text = "0";
|
---|
190 | if (this.ui.display.text.indexOf(".") == -1)
|
---|
191 | this.ui.display.text += ".";
|
---|
192 | this.waitingForOperand = false;
|
---|
193 | }
|
---|
194 |
|
---|
195 | //! [2]
|
---|
196 | Calculator.prototype.changeSignClicked = function()
|
---|
197 | {
|
---|
198 | var text = this.ui.display.text;
|
---|
199 | var value = text - 0;
|
---|
200 |
|
---|
201 | if (value > 0) {
|
---|
202 | text = "-" + text;
|
---|
203 | } else if (value < 0) {
|
---|
204 | text = text.slice(1);
|
---|
205 | }
|
---|
206 | this.ui.display.text = text;
|
---|
207 | }
|
---|
208 | //! [2]
|
---|
209 |
|
---|
210 | Calculator.prototype.backspaceClicked = function()
|
---|
211 | {
|
---|
212 | if (this.waitingForOperand)
|
---|
213 | return;
|
---|
214 |
|
---|
215 | var text = this.ui.display.text;
|
---|
216 | text = text.slice(0, -1);
|
---|
217 | if (text.length == 0) {
|
---|
218 | text = "0";
|
---|
219 | this.waitingForOperand = true;
|
---|
220 | }
|
---|
221 | this.ui.display.text = text;
|
---|
222 | }
|
---|
223 |
|
---|
224 | Calculator.prototype.clear = function()
|
---|
225 | {
|
---|
226 | if (this.waitingForOperand)
|
---|
227 | return;
|
---|
228 |
|
---|
229 | this.ui.display.text = "0";
|
---|
230 | this.waitingForOperand = true;
|
---|
231 | }
|
---|
232 |
|
---|
233 | Calculator.prototype.clearAll = function()
|
---|
234 | {
|
---|
235 | this.sumSoFar = 0.0;
|
---|
236 | this.factorSoFar = 0.0;
|
---|
237 | this.pendingAdditiveOperator = "";
|
---|
238 | this.pendingMultiplicativeOperator = "";
|
---|
239 | this.ui.display.text = "0";
|
---|
240 | this.waitingForOperand = true;
|
---|
241 | }
|
---|
242 |
|
---|
243 | Calculator.prototype.clearMemory = function()
|
---|
244 | {
|
---|
245 | this.sumInMemory = 0.0;
|
---|
246 | }
|
---|
247 |
|
---|
248 | Calculator.prototype.readMemory = function()
|
---|
249 | {
|
---|
250 | this.ui.display.text = this.sumInMemory + "";
|
---|
251 | this.waitingForOperand = true;
|
---|
252 | }
|
---|
253 |
|
---|
254 | Calculator.prototype.setMemory = function()
|
---|
255 | {
|
---|
256 | this.equalClicked();
|
---|
257 | this.sumInMemory = this.ui.display.text - 0;
|
---|
258 | }
|
---|
259 |
|
---|
260 | Calculator.prototype.addToMemory = function()
|
---|
261 | {
|
---|
262 | this.equalClicked();
|
---|
263 | this.sumInMemory += this.ui.display.text - 0;
|
---|
264 | }
|
---|