From 33dc955c798817a193fd6dba00d68fa93ed01f2a Mon Sep 17 00:00:00 2001 From: "Andrei \"Nuark\" G" Date: Mon, 11 May 2020 02:16:57 +0700 Subject: [PATCH] [2sem] pb21 done --- .../21/prac_qt/CMakeLists.txt | 13 ++ .../21/prac_qt/dbmanager.cpp | 207 ++++++++++++++++++ 2sem/programming basics/21/prac_qt/main.cpp | 12 + .../21/prac_qt/mainwindow.cpp | 48 ++++ .../21/prac_qt/mainwindow.hpp | 26 +++ .../21/prac_qt/mainwindow.ui | 147 +++++++++++++ .../21/prac_qt/pract_qt.pro | 35 +++ 7 files changed, 488 insertions(+) create mode 100644 2sem/programming basics/21/prac_qt/CMakeLists.txt create mode 100644 2sem/programming basics/21/prac_qt/dbmanager.cpp create mode 100644 2sem/programming basics/21/prac_qt/main.cpp create mode 100644 2sem/programming basics/21/prac_qt/mainwindow.cpp create mode 100644 2sem/programming basics/21/prac_qt/mainwindow.hpp create mode 100644 2sem/programming basics/21/prac_qt/mainwindow.ui create mode 100644 2sem/programming basics/21/prac_qt/pract_qt.pro diff --git a/2sem/programming basics/21/prac_qt/CMakeLists.txt b/2sem/programming basics/21/prac_qt/CMakeLists.txt new file mode 100644 index 0000000..7b524c8 --- /dev/null +++ b/2sem/programming basics/21/prac_qt/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.16) +project(prac_qt) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) +set(CMAKE_CXX_STANDARD 14) + +find_package (Qt5Core) +find_package (Qt5Widgets) + +qt5_wrap_ui(QT_TEST_UI_HEADERS mainwindow.ui) +add_executable(prac_qt main.cpp dbmanager.cpp mainwindow.cpp ${QT_TEST_UI_HEADERS}) +target_link_libraries(prac_qt ${Qt5Core_LIBRARIES} ${Qt5Widgets_LIBRARIES}) diff --git a/2sem/programming basics/21/prac_qt/dbmanager.cpp b/2sem/programming basics/21/prac_qt/dbmanager.cpp new file mode 100644 index 0000000..af12904 --- /dev/null +++ b/2sem/programming basics/21/prac_qt/dbmanager.cpp @@ -0,0 +1,207 @@ +// pb_21_100.cpp +// Горбацевич Андрей +#include +#include +#include +#include +#include +#include + +namespace nuark { + class DB_record { + std::string surname = ""; + unsigned short age = 0; + std::string city = ""; + enum scholarship { + HI_ED = 0, SC_ED = 1, SV_RD = 2 + } scs = SV_RD; + + public: + DB_record() = default; + + void load_txt(std::istream &ist) { + ist >> this->surname; + if (this->surname.length() > 32) { + this->surname = this->surname.substr(0, 32); + } + ist >> this->age; + ist >> this->city; + if (this->city.length() > 32) { + this->city = this->city.substr(0, 32); + } + { + std::string comp_str; + ist >> comp_str; + if (comp_str[0] == '"') { + std::string sub_comp_str; + ist >> sub_comp_str; + comp_str += " " + sub_comp_str; + } + if (comp_str == "higher") { + this->scs = HI_ED; + } + else if (comp_str == "secondary") { + this->scs = SC_ED; + } + else if (comp_str == "\"secondary vocational\"") { + this->scs = SV_RD; + } + } + } + + void load_bin(std::istream &ist) { + struct wrapper { + char _surname[32]; + unsigned short _age; + char _city[32]; + scholarship _scs; + } wr{}; + ist.read(reinterpret_cast(&wr), sizeof(wrapper)); + + this->surname = std::string(wr._surname); + this->age = wr._age; + this->city = std::string(wr._city); + this->scs = wr._scs; + } + + void save_bin(std::ostream &ost) const { + struct wrapper { + char _surname[32]; + unsigned short _age; + char _city[32]; + scholarship _scs; + } wr{}; + std::strcpy(wr._surname, this->surname.c_str()); + wr._age = this->age; + std::strcpy(wr._city, this->city.c_str()); + wr._scs = this->scs; + + ost.write(reinterpret_cast(&wr), sizeof(wrapper)); + } + + static void print_table_head(QTextStream &ost) { + auto ss = std::stringstream(); + ss.setf(std::ios::left); + ss << std::string(102, '=') << std::endl; + ss << std::setw(34) << "| surname"; + ss << std::setw(8) << " | age"; + ss << std::setw(35) << " | city"; + ss << std::setw(23) << " | scholarship" << " |\n"; + ss << std::string(102, '=') << std::endl; + + ost << ss.str().c_str(); + } + + void print_table_row(QTextStream &ost) const { + auto ss = std::stringstream(); + ss.setf(std::ios::left); + ss << "| " << std::setw(32) << this->surname; + ss << " | " << std::setw(5) << this->age; + ss << " | " << std::setw(32) << this->city; + ss << " | " << std::setw(20); + switch (this->scs) { + case HI_ED: + ss << "higher"; + break; + case SC_ED: + ss << "secondary"; + break; + case SV_RD: + ss << "secondary vocational"; + break; + } + ss << " |\n"; + auto t = ss.str(); + ost << ss.str().c_str(); + } + + bool validate() { + return this->surname.length() > 0 && this->age > 0 && this->city.length() > 0; + } + }; + + struct DBManager { + typedef std::vector Database; + + std::size_t print_table(const Database &data, QTextStream &ost) { + std::size_t c = 0; + DB_record::print_table_head(ost); + for (const auto& rec : data) { + rec.print_table_row(ost); + c++; + } + return c; + } + + std::size_t load_txt(Database &data, std::istream &ist) { + std::size_t c = 0; + while (!ist.fail() && !ist.eof()) { + DB_record record{}; + record.load_txt(ist); + if (record.validate()) { + data.push_back(record); + c++; + } + } + return c; + } + + std::size_t load_bin(Database &data, std::istream &ist) { + std::size_t c = 0; + while (!ist.fail() && !ist.eof()) { + DB_record record{}; + record.load_bin(ist); + if (record.validate()) { + data.push_back(record); + c++; + } + } + return c; + } + + std::size_t save_bin(const Database &data, std::ostream &ost) { + std::size_t c = 0; + for (const auto& rec : data) { + rec.save_bin(ost); + c++; + } + return c; + } + + void first_mode(const QString &path_in, const QString &path_out, QTextStream &stream) { + std::ifstream ist(path_in.toStdString()); + std::ofstream ost(path_out.toStdString(), std::ios::binary); + if (!ist.is_open()) + { + stream << "Unable to open `" << path_in << "`\n"; + } + else { + Database db; + std::size_t trrc = load_txt(db, ist); + stream << "Read " << trrc << " rows from txt\n"; + std::size_t prc = print_table(db, stream); + stream << "Print " << prc << " rows from db\n"; + std::size_t brwc = save_bin(db, ost); + stream << "Write " << brwc << " rows to bin\n"; + } + ist.close(); + ost.close(); + } + + void second_mode(const QString &path_in, QTextStream &stream) { + std::ifstream ist(path_in.toStdString(), std::ios::binary); + if (!ist.is_open()) + { + stream << "Unable to open `" << path_in << "`\n"; + } + else { + Database db; + std::size_t brrc = load_bin(db, ist); + stream << "Read " << brrc << " rows from bin\n"; + std::size_t prc = print_table(db, stream); + stream << "Print " << prc << " rows from db\n"; + } + ist.close(); + } + }; +} diff --git a/2sem/programming basics/21/prac_qt/main.cpp b/2sem/programming basics/21/prac_qt/main.cpp new file mode 100644 index 0000000..3ec7ff9 --- /dev/null +++ b/2sem/programming basics/21/prac_qt/main.cpp @@ -0,0 +1,12 @@ +// pb_21_100.cpp +// Горбацевич Андрей +#include "mainwindow.hpp" +#include + +int main(int argc, char *argv[]) { + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/2sem/programming basics/21/prac_qt/mainwindow.cpp b/2sem/programming basics/21/prac_qt/mainwindow.cpp new file mode 100644 index 0000000..e16144e --- /dev/null +++ b/2sem/programming basics/21/prac_qt/mainwindow.cpp @@ -0,0 +1,48 @@ +// pb_21_100.cpp +// Горбацевич Андрей +#include "mainwindow.hpp" +#include "ui_mainwindow.h" +#include "dbmanager.cpp" + +#include +#include +#include +#include + +MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { + ui->setupUi(this); + + ui->modeButtonGroup->setId(ui->mode1RadioButton, 1); + ui->modeButtonGroup->setId(ui->mode2RadioButton, 2); +} + +MainWindow::~MainWindow() { + delete ui; +} + +void MainWindow::on_execPushButton_clicked() { + nuark::DBManager manager; + + QString out_text; + QTextStream ost(&out_text, QIODevice::WriteOnly); + ost << tr("Текстовый файл: ") << ui->textFileLineEdit->text() << "\n" + << tr("Двоичный файл: ") << ui->binFileLineEdit->text() << "\n" + << tr("Режим: ") << ui->modeButtonGroup->checkedId() << "\n"; + + switch (ui->modeButtonGroup->checkedId()) { + case 1: + manager.first_mode( + ui->textFileLineEdit->text(), + ui->binFileLineEdit->text(), + ost + ); + break; + case 2: + manager.second_mode(ui->binFileLineEdit->text(), ost); + break; + default: + ost << "No way!\n"; + } + + ui->outTextEdit->setPlainText(out_text); +} diff --git a/2sem/programming basics/21/prac_qt/mainwindow.hpp b/2sem/programming basics/21/prac_qt/mainwindow.hpp new file mode 100644 index 0000000..89f8b44 --- /dev/null +++ b/2sem/programming basics/21/prac_qt/mainwindow.hpp @@ -0,0 +1,26 @@ +// pb_21_100.cpp +// Горбацевич Андрей +#ifndef MAINWINDOW_HPP +#define MAINWINDOW_HPP + +#include + +namespace Ui { + class MainWindow; +} + +class MainWindow : public QMainWindow { + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private slots: + void on_execPushButton_clicked(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_HPP diff --git a/2sem/programming basics/21/prac_qt/mainwindow.ui b/2sem/programming basics/21/prac_qt/mainwindow.ui new file mode 100644 index 0000000..d00568f --- /dev/null +++ b/2sem/programming basics/21/prac_qt/mainwindow.ui @@ -0,0 +1,147 @@ + + + MainWindow + + + + 0 + 0 + 937 + 526 + + + + Практическая работа + + + + + + + Ввод + + + + + + Текстовый файл + + + + + + + + + + Двоичный файл + + + + + + + + + + + + + Управление + + + + + + Режим 1 + + + true + + + modeButtonGroup + + + + + + + Режим 2 + + + modeButtonGroup + + + + + + + Выполнить + + + true + + + false + + + + + + + + + + Вывод + + + + + + true + + + + Monospace + + + + true + + + + + + + + + + + + + + + TopToolBarArea + + + false + + + + + + 0 + 0 + 937 + 30 + + + + + + + + + + + diff --git a/2sem/programming basics/21/prac_qt/pract_qt.pro b/2sem/programming basics/21/prac_qt/pract_qt.pro new file mode 100644 index 0000000..28496b9 --- /dev/null +++ b/2sem/programming basics/21/prac_qt/pract_qt.pro @@ -0,0 +1,35 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2020-04-26T10:59:45 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = pract_qt +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + + +SOURCES += \ + main.cpp \ + dbmanager.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.hpp + +FORMS += \ + mainwindow.ui