Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "rekenmachine.h"
- #include "ui_rekenmachine.h"
- double currentValue = 0.0;
- double prevValue = 0.0;
- enum LastButton {plus, min, equals, multi, null};
- LastButton lastButton = null;
- Rekenmachine::Rekenmachine(QWidget *parent) : QMainWindow(parent), ui(new Ui::Rekenmachine)
- {
- ui->setupUi(this);
- ui->Display->setText(QString::number(currentValue));
- QPushButton *buttons[3];
- for (int i = 0; i < 3; ++i)
- {
- QString buttonName = "Button" + QString::number(i);
- buttons[i] = Rekenmachine::findChild<QPushButton *>(buttonName);
- connect(buttons[i], SIGNAL(released()), this, SLOT(pressNumber()));
- }
- connect(ui->Add, SIGNAL(released()), this, SLOT(pressMath()));
- connect(ui->Subtract, SIGNAL(released()), this, SLOT(pressMath()));
- connect(ui->Multiply, SIGNAL(released()), this, SLOT(pressMath()));
- connect(ui->Result, SIGNAL(released()), this, SLOT(pressEqual()));
- }
- Rekenmachine::~Rekenmachine()
- {
- delete ui;
- }
- void Rekenmachine::pressNumber()
- {
- QPushButton *button = (QPushButton *)sender();
- QString buttonValue = button->text();
- QString displayValue= ui->Display->text();
- if (displayValue.toDouble() == 0 || displayValue.toDouble() == 0.0)
- {
- ui->Display->setText(buttonValue);
- currentValue = buttonValue.toDouble();
- }
- else
- {
- ui->Display->setText(QString::number((displayValue + buttonValue).toDouble()));
- currentValue = (displayValue + buttonValue).toDouble();
- }
- }
- void Rekenmachine::pressMath()
- {
- lastButton = null;
- prevValue = currentValue;
- ui->lineEdit->setText(QString::number(prevValue));
- QPushButton *button = (QPushButton *)sender();
- if (button->text() == "+")
- {
- lastButton = plus;
- }
- if (button->text() == "-")
- {
- lastButton = min;
- }
- if (button->text() == "*")
- {
- lastButton = multi;
- }
- currentValue = 0;
- ui->Display->setText(QString::number(currentValue));
- }
- void Rekenmachine::pressEqual()
- {
- if (lastButton = plus)
- {
- currentValue = prevValue + currentValue;
- }
- if (lastButton = min)
- {
- currentValue = prevValue - currentValue;
- }
- if (lastButton = multi)
- {
- currentValue = prevValue * currentValue;
- }
- ui->Display->setText(QString::number(currentValue));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement