-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
168 lines (143 loc) · 5.21 KB
/
mainwindow.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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDebug>
/**
* @brief global variable section.
*/
double firstNum;
bool userIsTypingSecondNumber = false;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// number buttons
connect(ui->pushButton_0, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_1, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_2, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_3, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_4, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_5, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_6, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_7, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_8, SIGNAL(released()), this, SLOT(digit_pressed()));
connect(ui->pushButton_9, SIGNAL(released()), this, SLOT(digit_pressed()));
// unary operation buttons.
connect(ui->pushButton_plusMinus, SIGNAL(released()), this, SLOT(unary_operation_pressed()));
connect(ui->pushButton_percent, SIGNAL(released()), this, SLOT(unary_operation_pressed()));
// binary operation buttons.
connect(ui->pushButton_add, SIGNAL(released()), this, SLOT(binary_operation_pressed()));
connect(ui->pushButton_subtrack, SIGNAL(released()), this, SLOT(binary_operation_pressed()));
connect(ui->pushButton_multiply, SIGNAL(released()), this, SLOT(binary_operation_pressed()));
connect(ui->pushButton_divide, SIGNAL(released()), this, SLOT(binary_operation_pressed()));
// setting some properties.
ui->pushButton_add->setCheckable(true);
ui->pushButton_subtrack->setCheckable(true);
ui->pushButton_multiply->setCheckable(true);
ui->pushButton_divide->setCheckable(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::digit_pressed()
{
QPushButton *button = (QPushButton *)sender();
double labelNumber;
QString newLabel;
if( (ui->pushButton_add->isChecked() || ui->pushButton_subtrack->isChecked()
|| ui->pushButton_multiply->isChecked() || ui->pushButton_divide->isChecked() ) && (!userIsTypingSecondNumber))
{
labelNumber = (button->text()).toDouble();
userIsTypingSecondNumber = true;
newLabel = QString::number(labelNumber, 'g', 15);
}
else
{
if (ui->label->text().contains('.') && button->text() == "0")
{
newLabel = ui->label->text() + button->text();
}
else
{
labelNumber = (ui->label->text() + button->text()).toDouble();
newLabel = QString::number(labelNumber, 'g', 15);
}
}
ui->label->setText(newLabel);
}
void MainWindow::on_pushButton_decimal_released()
{
ui->label->setText(ui->label->text() + ".");
}
void MainWindow::unary_operation_pressed()
{
QPushButton *button = (QPushButton *)sender();
QString newLabel;
double labelNumber;
if (button->text() == "+/-")
{
labelNumber = ui->label->text().toDouble();
labelNumber = labelNumber * -1;
newLabel = QString::number(labelNumber, 'g', 15);
ui->label->setText(newLabel);
}
if (button->text() == "%")
{
labelNumber = ui->label->text().toDouble();
labelNumber = labelNumber * 0.01;
newLabel = QString::number(labelNumber, 'g', 15);
ui->label->setText(newLabel);
}
}
void MainWindow::on_pushButton_clear_released()
{
ui->pushButton_add->setChecked(false);
ui->pushButton_subtrack->setChecked(false);
ui->pushButton_multiply->setChecked(false);
ui->pushButton_divide->setChecked(false);
userIsTypingSecondNumber = false;
ui->label->setText("0");
}
void MainWindow::on_pushButton_equals_released()
{
double labelNumber, secondNum;
QString newLabel;
secondNum = ui->label->text().toDouble();
if (ui->pushButton_add->isChecked())
{
labelNumber = firstNum + secondNum;
newLabel = QString::number(labelNumber, 'g', 15);
ui->label->setText(newLabel);
ui->pushButton_add->setChecked(false);
}
else if (ui->pushButton_subtrack->isChecked())
{
labelNumber = firstNum - secondNum;
newLabel = QString::number(labelNumber, 'g', 15);
ui->label->setText(newLabel);
ui->pushButton_subtrack->setChecked(false);
}
else if (ui->pushButton_multiply->isChecked())
{
labelNumber = firstNum * secondNum;
newLabel = QString::number(labelNumber, 'g', 15);
ui->label->setText(newLabel);
ui->pushButton_multiply->setChecked(false);
}
else if (ui->pushButton_divide->isChecked())
{
labelNumber = firstNum / secondNum;
newLabel = QString::number(labelNumber, 'g', 15);
ui->label->setText(newLabel);
ui->pushButton_divide->setChecked(false);
}
userIsTypingSecondNumber = false;
}
void MainWindow::binary_operation_pressed()
{
QPushButton *button = (QPushButton *)sender();
firstNum = ui->label->text().toDouble();
button->setChecked(true);
ui->label->setText("");
}