Add Lesson 5 Task 3

This commit is contained in:
Stanislav Mykhailenko 2024-07-26 19:19:09 +03:00
parent 0172aee4af
commit 586e5e3258
GPG key ID: 1E95E66A9C9D6A36
6 changed files with 579 additions and 0 deletions

74
Lesson_5/Task_3/.gitignore vendored Normal file
View file

@ -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

View file

@ -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

View file

@ -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;
}
}

View file

@ -0,0 +1,54 @@
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <QMainWindow>
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

View file

@ -0,0 +1,190 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Calculator</class>
<widget class="QMainWindow" name="Calculator">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>358</width>
<height>256</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLineEdit" name="e_display">
<property name="text">
<string>0</string>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_ce">
<property name="text">
<string>CE</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_ac">
<property name="text">
<string>AC</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="b_7">
<property name="text">
<string>7</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_8">
<property name="text">
<string>8</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_9">
<property name="text">
<string>9</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_divide">
<property name="text">
<string>/</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="b_4">
<property name="text">
<string>4</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_5">
<property name="text">
<string>5</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_6">
<property name="text">
<string>6</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_multiply">
<property name="text">
<string>*</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="b_1">
<property name="text">
<string>1</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_2">
<property name="text">
<string>2</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_3">
<property name="text">
<string>3</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_subtract">
<property name="text">
<string>-</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QPushButton" name="b_0">
<property name="text">
<string>0</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_dot">
<property name="text">
<string>.</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_equals">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_add">
<property name="text">
<string>+</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>358</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

11
Lesson_5/Task_3/main.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "calculator.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Calculator w;
w.show();
return a.exec();
}