mirea-projects/Third term/Industrial programming technologies/1_Calculator/mainwindow.cpp
2024-09-24 02:22:33 +03:00

171 lines
4.7 KiB
C++
Executable File

#include <cmath>
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, currentValue(0)
, storedValue(0)
, doubleDepth(0)
{
ui->setupUi(this);
connect(ui->button0, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button1, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button2, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button3, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button4, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button5, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button6, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button7, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button8, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->button9, &QPushButton::clicked, this, &MainWindow::digitClicked);
connect(ui->buttonDot, &QPushButton::clicked, this, &MainWindow::dotClicked);
connect(ui->buttonAdd, &QPushButton::clicked, this, &MainWindow::addClicked);
connect(ui->buttonSubtract, &QPushButton::clicked, this, &MainWindow::subtractClicked);
connect(ui->buttonMultiply, &QPushButton::clicked, this, &MainWindow::multiplyClicked);
connect(ui->buttonDivide, &QPushButton::clicked, this, &MainWindow::divideClicked);
connect(ui->buttonEquals, &QPushButton::clicked, this, &MainWindow::equalsClicked);
connect(ui->buttonClear, &QPushButton::clicked, this, &MainWindow::clearClicked);
connect(ui->buttonChangeSign, &QPushButton::clicked, this, &MainWindow::changeSignClicked);
connect(ui->buttonPow, &QPushButton::clicked, this, &MainWindow::powClicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::printDisplay()
{
if (pendingOperator == "") {
ui->display->setText(QString::number(currentValue));
} else {
QString out;
if (storedValue < 0) {
out += "(" + QString::number(storedValue) + ")";
} else {
out += QString::number(storedValue);
}
out += pendingOperator;
if (currentValue < 0) {
out += "(" + QString::number(currentValue) + ")";
} else {
out += QString::number(currentValue);
}
ui->display->setText(out);
}
}
void MainWindow::digitClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
int digitValue = clickedButton->text().toInt();
if (currentValue < 0) digitValue *= -1;
if (doubleDepth != 0) {
currentValue += digitValue * std::pow(0.1, doubleDepth);
doubleDepth++;
} else {
currentValue = currentValue * 10 + digitValue;
}
printDisplay();
}
void MainWindow::dotClicked() {
doubleDepth = 1;
ui->display->setText(QString::number(currentValue));
}
void MainWindow::addClicked()
{
storedValue = currentValue;
currentValue = 0;
doubleDepth = 0;
pendingOperator = "+";
printDisplay();
}
void MainWindow::subtractClicked()
{
storedValue = currentValue;
currentValue = 0;
doubleDepth = 0;
pendingOperator = "-";
printDisplay();
}
void MainWindow::multiplyClicked()
{
storedValue = currentValue;
currentValue = 0;
doubleDepth = 0;
pendingOperator = "*";
printDisplay();
}
void MainWindow::divideClicked()
{
storedValue = currentValue;
currentValue = 0;
doubleDepth = 0;
pendingOperator = "/";
printDisplay();
}
void MainWindow::powClicked()
{
storedValue = currentValue;
currentValue = 0;
doubleDepth = 0;
pendingOperator = "^";
printDisplay();
}
void MainWindow::equalsClicked()
{
if (pendingOperator == "+") {
currentValue = storedValue + currentValue;
} else if (pendingOperator == "-") {
currentValue = storedValue - currentValue;
} else if (pendingOperator == "*") {
currentValue = storedValue * currentValue;
} else if (pendingOperator == "/") {
if (currentValue != 0) {
currentValue = storedValue / currentValue;
} else {
ui->display->setText("Error");
return;
}
} else if (pendingOperator == "^") {
if (currentValue != 0) {
currentValue = std::pow(storedValue, currentValue);
} else {
ui->display->setText("Error");
return;
}
}
pendingOperator = "";
printDisplay();
}
void MainWindow::clearClicked()
{
currentValue = 0;
storedValue = 0;
pendingOperator = "";
printDisplay();
}
void MainWindow::changeSignClicked()
{
currentValue *= -1;
printDisplay();
}