1
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the example classes of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41
|
|
42 module calculator;
|
|
43
|
210
|
44 import button;
|
1
|
45 import qt.gui.QDialog;
|
|
46 import qt.gui.QGridLayout;
|
|
47 import qt.gui.QLineEdit;
|
|
48 import qt.gui.QFont;
|
|
49
|
210
|
50 version(Tango)
|
|
51 {
|
|
52 import tango.math.Math;
|
|
53 import tango.math.Math : pow, sqrt;
|
|
54 import Float = tango.text.convert.Float;
|
|
55 import Integer = tango.text.convert.Integer;
|
|
56 import tango.core.Array;
|
|
57 string ToString(int x)
|
|
58 {
|
|
59 return Integer.toString(x);
|
|
60 }
|
|
61 string ToString(double x, int c = 0)
|
|
62 {
|
|
63 return Float.toString(x, c);
|
|
64 }
|
|
65 int ToInt(string s)
|
|
66 {
|
|
67 return Integer.toInt(s);
|
|
68 }
|
|
69 float ToFloat(string s)
|
|
70 {
|
|
71 return Float.toFloat(s);
|
|
72 }
|
|
73 }
|
|
74 else
|
|
75 {
|
|
76 import std.math;
|
|
77 import std.conv;
|
|
78 import std.string;
|
|
79 string ToString(int x)
|
|
80 {
|
|
81 return std.string.format("%d", x);
|
|
82 }
|
|
83 string ToString(double x, int c = 0)
|
|
84 {
|
|
85 return std.string.format("%g", x);
|
|
86 }
|
|
87 int ToInt(string s)
|
|
88 {
|
211
|
89 return to!(int)(s);
|
210
|
90 }
|
|
91 float ToFloat(string s)
|
|
92 {
|
211
|
93 return to!(float)(s);
|
210
|
94 }
|
|
95 int find(string s, dchar c)
|
|
96 {
|
|
97 return indexOf(s,c);
|
|
98 }
|
|
99 }
|
1
|
100
|
|
101 class Calculator : public QDialog
|
|
102 {
|
|
103
|
|
104 public:
|
|
105
|
|
106 this(QWidget parent = null)
|
|
107 {
|
|
108 super(parent);
|
|
109
|
|
110 sumInMemory = 0.0;
|
|
111 sumSoFar = 0.0;
|
|
112 factorSoFar = 0.0;
|
|
113 waitingForOperand = true;
|
|
114
|
|
115 display = new QLineEdit("0");
|
|
116 display.setReadOnly(true);
|
|
117 display.setAlignment(Qt.AlignRight);
|
|
118 display.setMaxLength(15);
|
|
119
|
210
|
120 auto font = new QFont(display.font());
|
1
|
121 font.setPointSize(font.pointSize() + 8);
|
|
122 display.setFont(font);
|
|
123
|
|
124 for (int i = 0; i < NumDigitButtons; ++i) {
|
210
|
125 digitButtons[i] = createButton(ToString(i), &digitClicked);
|
1
|
126 }
|
|
127
|
|
128 Button pointButton = createButton(tr("."), &pointClicked);
|
|
129 Button changeSignButton = createButton(tr("+/-"), &changeSignClicked);
|
|
130
|
|
131 Button backspaceButton = createButton(tr("Backspace"), &backspaceClicked);
|
|
132 Button clearButton = createButton(tr("Clear"), &clear);
|
|
133 Button clearAllButton = createButton(tr("Clear All"), &clearAll);
|
|
134
|
|
135 Button clearMemoryButton = createButton(tr("MC"), &clearMemory);
|
|
136 Button readMemoryButton = createButton(tr("MR"), &readMemory);
|
|
137 Button setMemoryButton = createButton(tr("MS"), &setMemory);
|
|
138 Button addToMemoryButton = createButton(tr("M+"), &addToMemory);
|
|
139
|
|
140 Button divisionButton = createButton(tr("/"), &multiplicativeOperatorClicked);
|
|
141 Button timesButton = createButton(tr("*"), &multiplicativeOperatorClicked);
|
|
142 Button minusButton = createButton(tr("-"), &additiveOperatorClicked);
|
|
143 Button plusButton = createButton(tr("+"), &additiveOperatorClicked);
|
|
144
|
|
145 Button squareRootButton = createButton(tr("Sqrt"), &unaryOperatorClicked);
|
|
146 Button powerButton = createButton(tr("x^2"), &unaryOperatorClicked);
|
|
147 Button reciprocalButton = createButton(tr("1/x"), &unaryOperatorClicked);
|
|
148 Button equalButton = createButton(tr("="), &equalClicked);
|
|
149
|
|
150 QGridLayout mainLayout = new QGridLayout();
|
|
151
|
|
152 mainLayout.setSizeConstraint(QLayout.SetFixedSize);
|
|
153
|
|
154 mainLayout.addWidget(display, 0, 0, 1, 6);
|
|
155 mainLayout.addWidget(backspaceButton, 1, 0, 1, 2);
|
|
156 mainLayout.addWidget(clearButton, 1, 2, 1, 2);
|
|
157 mainLayout.addWidget(clearAllButton, 1, 4, 1, 2);
|
|
158
|
|
159 mainLayout.addWidget(clearMemoryButton, 2, 0);
|
|
160 mainLayout.addWidget(readMemoryButton, 3, 0);
|
|
161 mainLayout.addWidget(setMemoryButton, 4, 0);
|
|
162 mainLayout.addWidget(addToMemoryButton, 5, 0);
|
|
163
|
|
164 for (int i = 1; i < NumDigitButtons; ++i) {
|
|
165 int row = ((9 - i) / 3) + 2;
|
|
166 int column = ((i - 1) % 3) + 1;
|
|
167 mainLayout.addWidget(digitButtons[i], row, column);
|
|
168 }
|
|
169
|
|
170 mainLayout.addWidget(digitButtons[0], 5, 1);
|
|
171 mainLayout.addWidget(pointButton, 5, 2);
|
|
172 mainLayout.addWidget(changeSignButton, 5, 3);
|
|
173
|
|
174 mainLayout.addWidget(divisionButton, 2, 4);
|
|
175 mainLayout.addWidget(timesButton, 3, 4);
|
|
176 mainLayout.addWidget(minusButton, 4, 4);
|
|
177 mainLayout.addWidget(plusButton, 5, 4);
|
|
178
|
|
179 mainLayout.addWidget(squareRootButton, 2, 5);
|
|
180 mainLayout.addWidget(powerButton, 3, 5);
|
|
181 mainLayout.addWidget(reciprocalButton, 4, 5);
|
|
182 mainLayout.addWidget(equalButton, 5, 5);
|
|
183 setLayout(mainLayout);
|
|
184
|
|
185 setWindowTitle(tr("Calculator"));
|
|
186 }
|
|
187
|
|
188 //private slots:
|
|
189 void digitClicked()
|
|
190 {
|
|
191 Button clickedButton = cast(Button) signalSender();
|
210
|
192 int digitValue = ToInt(clickedButton.text);
|
1
|
193 if (display.text() == "0" && digitValue == 0.0)
|
|
194 return;
|
|
195
|
|
196 if (waitingForOperand) {
|
|
197 display.clear();
|
|
198 waitingForOperand = false;
|
|
199 }
|
210
|
200 display.setText(display.text() ~ ToString(digitValue));
|
1
|
201 }
|
|
202
|
|
203 void unaryOperatorClicked()
|
|
204 {
|
|
205 Button clickedButton = cast(Button) signalSender();
|
210
|
206 string clickedOperator = clickedButton.text();
|
|
207 double operand = ToFloat(display.text);
|
1
|
208 double result = 0.0;
|
|
209
|
|
210 if (clickedOperator == tr("Sqrt")) {
|
|
211 if (operand < 0.0) {
|
|
212 abortOperation();
|
|
213 return;
|
|
214 }
|
|
215 result = sqrt(operand);
|
|
216 } else if (clickedOperator == tr("x^2")) {
|
|
217 result = pow(operand, 2.0);
|
|
218 } else if (clickedOperator == tr("1/x")) {
|
|
219 if (operand == 0.0) {
|
|
220 abortOperation();
|
|
221 return;
|
|
222 }
|
|
223 result = 1.0 / operand;
|
|
224 }
|
210
|
225 display.setText(ToString(result, 4));
|
1
|
226 waitingForOperand = true;
|
|
227 }
|
|
228
|
|
229 void additiveOperatorClicked()
|
|
230 {
|
|
231 Button clickedButton = cast(Button) signalSender();
|
210
|
232 string clickedOperator = clickedButton.text();
|
|
233 double operand = ToFloat(display.text);
|
1
|
234
|
|
235 if (pendingMultiplicativeOperator.length) {
|
|
236 if (!calculate(operand, pendingMultiplicativeOperator)) {
|
|
237 abortOperation();
|
|
238 return;
|
|
239 }
|
210
|
240 display.setText(ToString(factorSoFar, 4));
|
1
|
241 operand = factorSoFar;
|
|
242 factorSoFar = 0.0;
|
|
243 pendingMultiplicativeOperator = null;
|
|
244 }
|
|
245
|
|
246 if (pendingAdditiveOperator.length) {
|
|
247 if (!calculate(operand, pendingAdditiveOperator)) {
|
|
248 abortOperation();
|
|
249 return;
|
|
250 }
|
210
|
251 display.setText(ToString(sumSoFar, 4));
|
1
|
252 } else {
|
|
253 sumSoFar = operand;
|
|
254 }
|
|
255
|
|
256 pendingAdditiveOperator = clickedOperator;
|
|
257 waitingForOperand = true;
|
|
258 }
|
|
259
|
|
260 void multiplicativeOperatorClicked()
|
|
261 {
|
|
262 Button clickedButton = cast(Button) signalSender();
|
210
|
263 string clickedOperator = clickedButton.text();
|
|
264 double operand = ToFloat(display.text);
|
1
|
265
|
|
266 if (pendingMultiplicativeOperator.length) {
|
|
267 if (!calculate(operand, pendingMultiplicativeOperator)) {
|
|
268 abortOperation();
|
|
269 return;
|
|
270 }
|
210
|
271 display.setText(ToString(factorSoFar/*, 4*/));
|
1
|
272 } else {
|
|
273 factorSoFar = operand;
|
|
274 }
|
|
275
|
|
276 pendingMultiplicativeOperator = clickedOperator;
|
|
277 waitingForOperand = true;
|
|
278 }
|
|
279
|
|
280 void equalClicked()
|
|
281 {
|
210
|
282 double operand = ToFloat(display.text);
|
1
|
283
|
|
284 if (pendingMultiplicativeOperator.length) {
|
|
285 if (!calculate(operand, pendingMultiplicativeOperator)) {
|
|
286 abortOperation();
|
|
287 return;
|
|
288 }
|
|
289 operand = factorSoFar;
|
|
290 factorSoFar = 0.0;
|
|
291 pendingMultiplicativeOperator = null;
|
|
292 }
|
|
293 if (pendingAdditiveOperator.length) {
|
|
294 if (!calculate(operand, pendingAdditiveOperator)) {
|
|
295 abortOperation();
|
|
296 return;
|
|
297 }
|
|
298 pendingAdditiveOperator = null;
|
|
299 } else {
|
|
300 sumSoFar = operand;
|
|
301 }
|
|
302
|
210
|
303 display.setText(ToString(sumSoFar, 4));
|
1
|
304 sumSoFar = 0.0;
|
|
305 waitingForOperand = true;
|
|
306 }
|
|
307
|
|
308 void pointClicked()
|
|
309 {
|
210
|
310 string text = display.text;
|
1
|
311
|
|
312 if (waitingForOperand)
|
|
313 display.setText("0");
|
|
314
|
|
315 if (find(text, '.') >= text.length)
|
|
316 display.setText(text ~ tr("."));
|
|
317
|
|
318 waitingForOperand = false;
|
|
319 }
|
|
320
|
|
321 void changeSignClicked()
|
|
322 {
|
210
|
323 string text = display.text();
|
|
324 double value = ToFloat(text);
|
1
|
325
|
|
326 if (value > 0.0) {
|
|
327 text = "-" ~ text;
|
|
328 } else if (value < 0.0) {
|
|
329 text = text[1..$];
|
|
330 }
|
|
331 display.setText(text);
|
|
332 }
|
|
333
|
|
334 void backspaceClicked()
|
|
335 {
|
|
336 if (waitingForOperand)
|
|
337 return;
|
|
338
|
210
|
339 string text = display.text();
|
1
|
340 text = text[0..$-1];
|
|
341 if (text.length == 0) {
|
|
342 text = "0";
|
|
343 waitingForOperand = true;
|
|
344 }
|
|
345 display.setText(text);
|
|
346 }
|
|
347
|
|
348
|
|
349 void clear()
|
|
350 {
|
|
351 if (waitingForOperand)
|
|
352 return;
|
|
353
|
|
354 display.setText("0");
|
|
355 waitingForOperand = true;
|
|
356 }
|
|
357
|
|
358 void clearAll()
|
|
359 {
|
|
360 sumSoFar = 0.0;
|
|
361 factorSoFar = 0.0;
|
|
362 pendingAdditiveOperator = null;
|
|
363 pendingMultiplicativeOperator = null;
|
|
364 display.setText("0");
|
|
365 waitingForOperand = true;
|
|
366 }
|
|
367
|
|
368 void clearMemory()
|
|
369 {
|
|
370 sumInMemory = 0.0;
|
|
371 }
|
|
372
|
|
373 void readMemory()
|
|
374 {
|
210
|
375 display.setText(ToString(sumInMemory, 4));
|
1
|
376 waitingForOperand = true;
|
|
377 }
|
|
378
|
|
379 void setMemory()
|
|
380 {
|
|
381 equalClicked();
|
210
|
382 sumInMemory = ToFloat(display.text);
|
1
|
383 }
|
|
384
|
|
385 void addToMemory()
|
|
386 {
|
|
387 equalClicked();
|
210
|
388 sumInMemory += ToFloat(display.text);
|
1
|
389 }
|
|
390
|
|
391 private:
|
|
392
|
210
|
393 Button createButton(string text, void delegate() member)
|
1
|
394 {
|
|
395 Button button = new Button(text);
|
|
396 button.clicked.connect(member);
|
|
397 return button;
|
|
398 }
|
|
399
|
|
400 void abortOperation()
|
|
401 {
|
|
402 clearAll();
|
|
403 display.setText(tr("####"));
|
|
404 }
|
|
405
|
210
|
406 bool calculate(double rightOperand, string pendingOperator)
|
1
|
407 {
|
|
408 if (pendingOperator == tr("+")) {
|
|
409 sumSoFar += rightOperand;
|
|
410 } else if (pendingOperator == tr("-")) {
|
|
411 sumSoFar -= rightOperand;
|
|
412 } else if (pendingOperator == tr("*")) {
|
|
413 factorSoFar *= rightOperand;
|
|
414 } else if (pendingOperator == tr("/")) {
|
|
415 if (rightOperand == 0.0)
|
|
416 return false;
|
|
417 factorSoFar /= rightOperand;
|
|
418 }
|
|
419 return true;
|
|
420 }
|
|
421
|
|
422 double sumInMemory;
|
|
423 double sumSoFar;
|
|
424 double factorSoFar;
|
210
|
425 string pendingAdditiveOperator;
|
|
426 string pendingMultiplicativeOperator;
|
1
|
427 bool waitingForOperand;
|
|
428
|
|
429 QLineEdit display;
|
|
430
|
|
431 enum { NumDigitButtons = 10 };
|
|
432 Button[NumDigitButtons] digitButtons;
|
|
433 }
|