65 , pendingAdditiveOperator(QString())
66 , pendingMultiplicativeOperator(QString())
67 , waitingForOperand(true)
68 , display(new QLineEdit(
"0"))
75 display->setAlignment(Qt::AlignRight);
79 font.setPointSize(font.pointSize() + 8);
112 QGridLayout *mainLayout =
new QGridLayout;
114 mainLayout->setSizeConstraint(QLayout::SetFixedSize);
115 mainLayout->addWidget(
display, 0, 0, 1, 6);
116 mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
117 mainLayout->addWidget(clearButton, 1, 2, 1, 2);
118 mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);
120 mainLayout->addWidget(clearMemoryButton, 2, 0);
121 mainLayout->addWidget(readMemoryButton, 3, 0);
122 mainLayout->addWidget(setMemoryButton, 4, 0);
123 mainLayout->addWidget(addToMemoryButton, 5, 0);
126 int row = ((9 - i) / 3) + 2;
127 int column = ((i - 1) % 3) + 1;
132 mainLayout->addWidget(pointButton, 5, 2);
133 mainLayout->addWidget(changeSignButton, 5, 3);
135 mainLayout->addWidget(divisionButton, 2, 4);
136 mainLayout->addWidget(timesButton, 3, 4);
137 mainLayout->addWidget(minusButton, 4, 4);
138 mainLayout->addWidget(plusButton, 5, 4);
140 mainLayout->addWidget(squareRootButton, 2, 5);
141 mainLayout->addWidget(powerButton, 3, 5);
142 mainLayout->addWidget(reciprocalButton, 4, 5);
143 mainLayout->addWidget(equalButton, 5, 5);
144 setLayout(mainLayout);
146 setWindowTitle(tr(
"Calculator"));
157 Button *clickedButton = qobject_cast<Button *>(sender());
158 int digitValue = clickedButton->text().toInt();
159 if (
display->text() ==
"0" && digitValue == 0 )
174 Button *clickedButton = qobject_cast<Button *>(sender());
175 QString clickedOperator = clickedButton->text();
176 double operand =
display->text().toDouble();
179 if (clickedOperator == tr(
"Sqrt")) {
184 result = std::sqrt(operand);
185 }
else if (clickedOperator == tr(
"x\302\262")) {
186 result = std::pow(operand, 2.0);
187 }
else if (clickedOperator == tr(
"1/x")) {
188 if (qFuzzyIsNull(operand))
193 result = 1.0 / operand;
195 display->setText(QString::number(result));
204 Button *clickedButton = qobject_cast<Button *>(sender());
205 QString clickedOperator = clickedButton->text();
206 double operand =
display->text().toDouble();
243 Button *clickedButton = qobject_cast<Button *>(sender());
244 QString clickedOperator = clickedButton->text();
245 double operand =
display->text().toDouble();
265 double operand =
display->text().toDouble();
297 if (!
display->text().contains(
'.'))
306 QString text =
display->text();
307 double value = text.toDouble();
310 text.prepend(tr(
"-"));
311 }
else if (value < 0.0) {
324 QString text =
display->text();
326 if (text.isEmpty()) {
385 connect(button, SIGNAL(clicked()),
this, member);
401 if (pendingOperator == tr(
"+")) {
403 }
else if (pendingOperator == tr(
"-")) {
405 }
else if (pendingOperator == tr(
"\303\227")) {
407 }
else if (pendingOperator == tr(
"\303\267")) {
408 if (qFuzzyIsNull(rightOperand))
bool calculate(double rightOperand, const QString &pendingOperator)
[36]
void backspaceClicked()
[24]
void unaryOperatorClicked()
[7]
QString pendingMultiplicativeOperator
[6] //! [7]
CalculatorUtil(QWidget *parent=nullptr)
[0]
void changeSignClicked()
[22]
virtual ~CalculatorUtil()
[6]
bool waitingForOperand
[7] //! [8]
double sumSoFar
[3] //! [4]
void additiveOperatorClicked()
[9]
Button * createButton(const QString &text, const char *member)
[0]
Button * digitButtons[NumDigitButtons]
QString pendingAdditiveOperator
[5] //! [6]
void multiplicativeOperatorClicked()
[17]
void abortOperation()
[34]
double factorSoFar
[4] //! [5]