[2sem] pb21 done

This commit is contained in:
Andrei "Nuark" G 2020-05-11 02:16:57 +07:00
parent 33fa9edd0f
commit 33dc955c79
7 changed files with 488 additions and 0 deletions

View file

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

View file

@ -0,0 +1,207 @@
// pb_21_100.cpp
// Горбацевич Андрей
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <cstring>
#include <QtCore/QTextStream>
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<char*>(&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<char*>(&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<DB_record> 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();
}
};
}

View file

@ -0,0 +1,12 @@
// pb_21_100.cpp
// Горбацевич Андрей
#include "mainwindow.hpp"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

View file

@ -0,0 +1,48 @@
// pb_21_100.cpp
// Горбацевич Андрей
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include "dbmanager.cpp"
#include <QIODevice>
#include <QMessageBox>
#include <QString>
#include <QTextStream>
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);
}

View file

@ -0,0 +1,26 @@
// pb_21_100.cpp
// Горбацевич Андрей
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
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

View file

@ -0,0 +1,147 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>937</width>
<height>526</height>
</rect>
</property>
<property name="windowTitle">
<string>Практическая работа</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGroupBox" name="inputGroupBox">
<property name="title">
<string>Ввод</string>
</property>
<layout class="QFormLayout" name="formLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="textFileLabel">
<property name="text">
<string>Текстовый файл</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="textFileLineEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="binFileLabel">
<property name="text">
<string>Двоичный файл</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="binFileLineEdit"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="controlGroupBox">
<property name="title">
<string>Управление</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QRadioButton" name="mode1RadioButton">
<property name="text">
<string>Режим 1</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">modeButtonGroup</string>
</attribute>
</widget>
</item>
<item>
<widget class="QRadioButton" name="mode2RadioButton">
<property name="text">
<string>Режим 2</string>
</property>
<attribute name="buttonGroup">
<string notr="true">modeButtonGroup</string>
</attribute>
</widget>
</item>
<item>
<widget class="QPushButton" name="execPushButton">
<property name="text">
<string>Выполнить</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="outGroupBox">
<property name="title">
<string>Вывод</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QPlainTextEdit" name="outTextEdit">
<property name="enabled">
<bool>true</bool>
</property>
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="plainText">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>937</width>
<height>30</height>
</rect>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
<buttongroups>
<buttongroup name="modeButtonGroup"/>
</buttongroups>
</ui>

View file

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