-
Notifications
You must be signed in to change notification settings - Fork 53
/
mainwindow.cpp
74 lines (62 loc) · 2.48 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Connecting digit 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()));
// Additional connections for operations
connect(ui->pushButton_addition, SIGNAL(released()), this, SLOT(on_pushButton_addition_clicked()));
connect(ui->pushButton_subtraction, SIGNAL(released()), this, SLOT(on_pushButton_subtraction_clicked()));
connect(ui->pushButton_multiplication, SIGNAL(released()), this, SLOT(on_pushButton_multiplication_clicked()));
connect(ui->pushButton_division, SIGNAL(released()), this, SLOT(on_pushButton_division_clicked()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::digit_pressed()
{
QPushButton *button = (QPushButton*)sender();
QString newLabel;
if ((ui->label->text().contains('.')) && (button->text() == "0"))
{
newLabel = ui->label->text() + button->text();
}
else
{
double labelNumber = (ui->label->text() + button->text()).toDouble();
newLabel = QString::number(labelNumber, 'g', 15);
}
ui->label->setText(newLabel);
}
void MainWindow::on_pushButton_clear_clicked()
{
ui->label->setText("0");
firstNum = 0;
userIsTypingSecondNumber = false;
}
void MainWindow::on_pushButton_addition_clicked()
{
firstNum = ui->label->text().toDouble();
ui->label->setText("");
userIsTypingSecondNumber = true;
}
void MainWindow::on_pushButton_equals_clicked()
{
double secondNum = ui->label->text().toDouble();
double result;
result = firstNum + secondNum; // Implement other operations similarly
ui->label->setText(QString::number(result, 'g', 15));
}