-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.cpp
214 lines (190 loc) · 6.03 KB
/
calculator.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "calculator.h"
#include "ui_calculator.h"
#include <QKeyEvent>
#include <QShortcut>
const uint16_t DEFAULT_LENGTH = 20;
double currentValue = 0.0;
double operationValue = 0.0;
double memory = 0.0;
bool addClicked = false;
bool minusClicked = false;
bool multiplyClicked = false;
bool divideClicked = false;
Calculator::Calculator(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::Calculator)
{
ui->setupUi(this);
// ui->Widget
ui->Display->setText(QString::number(currentValue));
QPushButton* numButtons[10];
for (int i = 0; i < 10; ++i)
{
QString name = "Button" + QString::number(i);
numButtons[i] = Calculator::findChild<QPushButton*>(name);
// trigger NumPressed when it's clicked
numButtons[i]->setShortcut(QKeySequence(QString::number(i)));
connect(numButtons[i], SIGNAL(released()), this, SLOT(NumPressed()));
}
ui->Dot->setShortcut(QKeySequence("."));
connect(ui->Dot, SIGNAL(released()), this, SLOT(DotPressed()));
ui->Add->setShortcut(QKeySequence("+"));
connect(ui->Add, SIGNAL(released()), this, SLOT(OperationPressed()));
ui->Minus->setShortcut(QKeySequence("-"));
connect(ui->Minus, SIGNAL(released()), this, SLOT(OperationPressed()));
ui->Times->setShortcut(QKeySequence("*"));
connect(ui->Times, SIGNAL(released()), this, SLOT(OperationPressed()));
ui->Divided->setShortcut(QKeySequence("/"));
connect(ui->Divided, SIGNAL(released()), this, SLOT(OperationPressed()));
ui->Equal->setShortcut(QKeySequence(Qt::Key_Enter));
connect(ui->Equal, SIGNAL(released()), this, SLOT(EqualPressed()));
ui->Sign->setShortcut(QKeySequence("s"));
connect(ui->Sign, SIGNAL(released()), this, SLOT(FlipSignPressed()));
ui->AC->setShortcut(QKeySequence("c"));
connect(ui->AC, SIGNAL(released()), this, SLOT(ClearDisplay()));
ui->Conste->setShortcut(QKeySequence("e"));
connect(ui->Conste, SIGNAL(released()), this, SLOT(ePressed()));
ui->Constpi->setShortcut(QKeySequence("p"));
connect(ui->Constpi, SIGNAL(released()), this, SLOT(piPressed()));
ui->Off->setShortcut(QKeySequence(Qt::Key_Escape));
connect(ui->Off, SIGNAL(released()), this, SLOT(offPressed()));
connect(ui->MemoryAdd, SIGNAL(released()), this, SLOT(memPlusPressed()));
connect(ui->MemoryGet, SIGNAL(released()), this, SLOT(memRecallPressed()));
connect(ui->MemoryClear, SIGNAL(released()), this, SLOT(memClearPressed()));
}
Calculator::~Calculator()
{
delete ui;
}
void Calculator::ePressed()
{
currentValue = 2.7182818284590452353602874713526624977572470936999;
ui->Display->setText(QString::number(currentValue, 'g', DEFAULT_LENGTH));
}
void Calculator::piPressed()
{
currentValue = 3.1415926535897932384626433832795028841971693993751;
ui->Display->setText(QString::number(currentValue, 'g', DEFAULT_LENGTH));
}
void Calculator::NumPressed()
{
QPushButton* button = (QPushButton*)sender();
QString value = button->text();
QString displayValue = ui->Display->text();
QRegExp regex("^(0.)");
QRegExp regexHasDot(".*[.].*");
if (regexHasDot.exactMatch(displayValue))
{
ui->Display->setText(displayValue + QString(value));
}
else if (regex.exactMatch(displayValue))
{
ui->Display->setText(displayValue + QString(value));
}
else if (displayValue.toDouble() == 0 || displayValue.toDouble() == 0.0)
{
ui->Display->setText(value);
}
else
{
QString newValue = displayValue + value;
double newValueDouble = newValue.toDouble();
ui->Display->setText(QString::number(newValueDouble));
}
}
void Calculator::DotPressed()
{
QString displayValue = ui->Display->text();
QRegExp regex("[^.]+");
if (regex.exactMatch(displayValue))
{
ui->Display->setText(displayValue + QString("."));
}
}
void Calculator::OperationPressed()
{
addClicked = false;
minusClicked = false;
multiplyClicked = false;
divideClicked = false;
QString displayValue = ui->Display->text();
currentValue = displayValue.toDouble();
QPushButton* button = (QPushButton*)sender();
QString buttonValue = button->text();
if (QString::compare(buttonValue, "+", Qt::CaseSensitive) == 0)
{
addClicked = true;
}
else if (QString::compare(buttonValue, "-", Qt::CaseSensitive) == 0)
{
minusClicked = true;
}
else if (QString::compare(buttonValue, "*", Qt::CaseSensitive) == 0)
{
multiplyClicked = true;
}
else if (QString::compare(buttonValue, "/", Qt::CaseSensitive) == 0)
{
divideClicked = true;
}
ui->Display->setText("");
};
void Calculator::EqualPressed()
{
double result = 0.0;
QString displayValue = ui->Display->text();
double displayValueDouble = displayValue.toDouble();
if (addClicked || minusClicked || multiplyClicked || divideClicked)
{
operationValue = displayValueDouble;
}
if (addClicked)
{
result = currentValue + operationValue;
}
else if (minusClicked)
{
result = currentValue - operationValue;
}
else if (multiplyClicked)
{
result = currentValue * operationValue;
}
else if (divideClicked)
{
result = currentValue / operationValue;
}
ui->Display->setText(QString::number(result));
};
void Calculator::FlipSignPressed()
{
QString displayValue = ui->Display->text();
QRegExp regex("[-]?[0-9.]*");
if (regex.exactMatch(displayValue))
{
double displayValueDouble = -1 * displayValue.toDouble();
ui->Display->setText(QString::number(displayValueDouble));
}
};
void Calculator::memPlusPressed()
{
QString displayValue = ui->Display->text();
memory += displayValue.toDouble();
};
void Calculator::memClearPressed()
{
memory = 0.0;
};
void Calculator::memRecallPressed()
{
ui->Display->setText(QString::number(memory));
};
void Calculator::ClearDisplay()
{
currentValue = 0.0;
ui->Display->setText(QString::number(currentValue));
}
void Calculator::offPressed()
{
this->close();
}