From 586e5e3258fc1e2835ea209a291144f9e081d874 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Fri, 26 Jul 2024 19:19:09 +0300 Subject: [PATCH] Add Lesson 5 Task 3 --- Lesson_5/Task_3/.gitignore | 74 +++++++++++ Lesson_5/Task_3/Task_3.pro | 24 ++++ Lesson_5/Task_3/calculator.cpp | 226 +++++++++++++++++++++++++++++++++ Lesson_5/Task_3/calculator.h | 54 ++++++++ Lesson_5/Task_3/calculator.ui | 190 +++++++++++++++++++++++++++ Lesson_5/Task_3/main.cpp | 11 ++ 6 files changed, 579 insertions(+) create mode 100644 Lesson_5/Task_3/.gitignore create mode 100644 Lesson_5/Task_3/Task_3.pro create mode 100644 Lesson_5/Task_3/calculator.cpp create mode 100644 Lesson_5/Task_3/calculator.h create mode 100644 Lesson_5/Task_3/calculator.ui create mode 100644 Lesson_5/Task_3/main.cpp diff --git a/Lesson_5/Task_3/.gitignore b/Lesson_5/Task_3/.gitignore new file mode 100644 index 0000000..4a0b530 --- /dev/null +++ b/Lesson_5/Task_3/.gitignore @@ -0,0 +1,74 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* +CMakeLists.txt.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/Lesson_5/Task_3/Task_3.pro b/Lesson_5/Task_3/Task_3.pro new file mode 100644 index 0000000..84aa065 --- /dev/null +++ b/Lesson_5/Task_3/Task_3.pro @@ -0,0 +1,24 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + calculator.cpp + +HEADERS += \ + calculator.h + +FORMS += \ + calculator.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/Lesson_5/Task_3/calculator.cpp b/Lesson_5/Task_3/calculator.cpp new file mode 100644 index 0000000..95cc533 --- /dev/null +++ b/Lesson_5/Task_3/calculator.cpp @@ -0,0 +1,226 @@ +#include "calculator.h" +#include "ui_calculator.h" + +Calculator::Calculator(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::Calculator) +{ + ui->setupUi(this); + connect(ui->b_0, &QPushButton::clicked, this, &Calculator::press0); + connect(ui->b_1, &QPushButton::clicked, this, &Calculator::press1); + connect(ui->b_2, &QPushButton::clicked, this, &Calculator::press2); + connect(ui->b_3, &QPushButton::clicked, this, &Calculator::press3); + connect(ui->b_4, &QPushButton::clicked, this, &Calculator::press4); + connect(ui->b_5, &QPushButton::clicked, this, &Calculator::press5); + connect(ui->b_6, &QPushButton::clicked, this, &Calculator::press6); + connect(ui->b_7, &QPushButton::clicked, this, &Calculator::press7); + connect(ui->b_8, &QPushButton::clicked, this, &Calculator::press8); + connect(ui->b_9, &QPushButton::clicked, this, &Calculator::press9); + connect(ui->b_add, &QPushButton::clicked, this, &Calculator::pressAdd); + connect(ui->b_subtract, &QPushButton::clicked, this, &Calculator::pressSubtract); + connect(ui->b_multiply, &QPushButton::clicked, this, &Calculator::pressMultiply); + connect(ui->b_divide, &QPushButton::clicked, this, &Calculator::pressDivide); + connect(ui->b_equals, &QPushButton::clicked, this, &Calculator::pressEquals); + connect(ui->b_dot, &QPushButton::clicked, this, &Calculator::pressDot); + connect(ui->b_ce, &QPushButton::clicked, this, &Calculator::pressCe); + connect(ui->b_ac, &QPushButton::clicked, this, &Calculator::pressAc); +} + +Calculator::~Calculator() +{ + delete ui; +} + +void Calculator::press0() +{ + keyPressed("0"); +} + +void Calculator::press1() +{ + keyPressed("1"); +} + +void Calculator::press2() +{ + keyPressed("2"); +} + +void Calculator::press3() +{ + keyPressed("3"); +} + +void Calculator::press4() +{ + keyPressed("4"); +} + +void Calculator::press5() +{ + keyPressed("5"); +} + +void Calculator::press6() +{ + keyPressed("6"); +} + +void Calculator::press7() +{ + keyPressed("7"); +} + +void Calculator::press8() +{ + keyPressed("8"); +} + +void Calculator::press9() +{ + keyPressed("9"); +} + +void Calculator::pressAdd() +{ + keyPressed("+"); +} + +void Calculator::pressSubtract() +{ + keyPressed("-"); +} + +void Calculator::pressMultiply() +{ + keyPressed("*"); +} + +void Calculator::pressDivide() +{ + keyPressed("/"); +} + +void Calculator::pressEquals() +{ + keyPressed("="); +} + +void Calculator::pressDot() +{ + keyPressed("."); +} + +void Calculator::pressCe() +{ + keyPressed("CE"); +} + +void Calculator::pressAc() +{ + keyPressed("AC"); +} + +bool Calculator::isDigit(QString value) +{ + if (value == "0" + || value == "1" + || value == "2" + || value == "3" + || value == "4" + || value == "5" + || value == "6" + || value == "7" + || value == "8" + || value == "9" + ) + return true; + return false; +} + +bool Calculator::isOperator(QString value) +{ + if (value == "+" + || value == "-" + || value == "*" + || value == "/" + || value == "=" + ) + return true; + return false; +} + +void Calculator::calculate(bool clearOperand) +{ + m_operand2 = ui->e_display->text().toFloat(); + if (m_operator == '+') + m_operand1 = m_operand1 + m_operand2; + else if (m_operator == '-') + m_operand1 = m_operand1 - m_operand2; + else if (m_operator == '*') + m_operand1 = m_operand1 * m_operand2; + else if (m_operator == '/') + m_operand1 = m_operand1 / m_operand2; + ui->e_display->setText(QString::number(m_operand1)); + m_done = true; + if (clearOperand) + m_operand1Set = false; +} + +void Calculator::clearIfDone() +{ + if (m_done) { + ui->e_display->setText("0"); + m_done = false; + } +} + +void Calculator::keyPressed(QString keyName) +{ + if (isDigit(keyName)) { + clearIfDone(); + + if (ui->e_display->text() == "0") + ui->e_display->setText(keyName); + else + ui->e_display->setText(ui->e_display->text() + keyName); + } + else if (keyName == ".") { + clearIfDone(); + + if (ui->e_display->text().contains(".")) + return; + + ui->e_display->setText(ui->e_display->text() + keyName); + } + else if (isOperator(keyName)) { + if (!m_operand1Set && keyName != "=") { + m_operand1Set = true; + m_operand1 = ui->e_display->text().toFloat(); + m_operator = QChar(keyName[0]); + ui->e_display->setText("0"); + } else if (m_operand1Set && !m_done) { + if (keyName == "=") + calculate(true); + else { + calculate(false); + m_operator = QChar(keyName[0]); + } + + } + } + else if (keyName == "CE") { + QString text = ui->e_display->text(); + if (text.size() > 1) + text.chop(1); + else + text = "0"; + ui->e_display->setText(text); + } + else if (keyName == "AC") { + ui->e_display->setText("0"); + m_operand1Set = false; + m_done = false; + } +} + diff --git a/Lesson_5/Task_3/calculator.h b/Lesson_5/Task_3/calculator.h new file mode 100644 index 0000000..5d6e5f9 --- /dev/null +++ b/Lesson_5/Task_3/calculator.h @@ -0,0 +1,54 @@ +#ifndef CALCULATOR_H +#define CALCULATOR_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { +class Calculator; +} +QT_END_NAMESPACE + +class Calculator : public QMainWindow +{ + Q_OBJECT + +public: + Calculator(QWidget *parent = nullptr); + ~Calculator(); + +private slots: + void press0(); + void press1(); + void press2(); + void press3(); + void press4(); + void press5(); + void press6(); + void press7(); + void press8(); + void press9(); + void pressAdd(); + void pressSubtract(); + void pressMultiply(); + void pressDivide(); + void pressEquals(); + void pressDot(); + void pressCe(); + void pressAc(); + bool isDigit(QString); + bool isOperator(QString); + void calculate(bool); + void clearIfDone(); + +private: + Ui::Calculator *ui; + void keyPressed(QString); + float m_operand1; + bool m_operand1Set = false; + float m_operand2; + QChar m_operator = 0; + bool m_done = false; + +}; +#endif // CALCULATOR_H diff --git a/Lesson_5/Task_3/calculator.ui b/Lesson_5/Task_3/calculator.ui new file mode 100644 index 0000000..eda01bd --- /dev/null +++ b/Lesson_5/Task_3/calculator.ui @@ -0,0 +1,190 @@ + + + Calculator + + + + 0 + 0 + 358 + 256 + + + + Calculator + + + + + + + + + 0 + + + true + + + + + + + CE + + + + + + + AC + + + + + + + + + + + 7 + + + + + + + 8 + + + + + + + 9 + + + + + + + / + + + + + + + + + + + 4 + + + + + + + 5 + + + + + + + 6 + + + + + + + * + + + + + + + + + + + 1 + + + + + + + 2 + + + + + + + 3 + + + + + + + - + + + + + + + + + + + 0 + + + + + + + . + + + + + + + = + + + + + + + + + + + + + + + + + + + 0 + 0 + 358 + 25 + + + + + + + + diff --git a/Lesson_5/Task_3/main.cpp b/Lesson_5/Task_3/main.cpp new file mode 100644 index 0000000..eb67047 --- /dev/null +++ b/Lesson_5/Task_3/main.cpp @@ -0,0 +1,11 @@ +#include "calculator.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Calculator w; + w.show(); + return a.exec(); +}