commit b6337e60153d8eb2145aa375ae73c9d5c257717e Author: Dmitry Rudakov Date: Fri Jun 5 22:19:50 2026 +0500 Upload to git diff --git a/algo.h b/algo.h new file mode 100644 index 0000000..a7c454a --- /dev/null +++ b/algo.h @@ -0,0 +1,53 @@ +#pragma once + +#include + +template +std::deque Merge(const std::deque& half1, const std::deque& half2, const Comp& comparator) { + std::deque result; + auto it1 = half1.begin(); + auto it2 = half2.begin(); + + while (it1 != half1.end() && it2 != half2.end()) { + if (!comparator(*it2, *it1)) { + result.push_back(*it1); + ++it1; + } else { + result.push_back(*it2); + ++it2; + } + } + + while (it1 != half1.end()) { + result.push_back(*it1); + ++it1; + } + + while (it2 != half2.end()) { + result.push_back(*it2); + ++it2; + } + + return result; +} + +template +std::deque MergeSort(const std::deque& src, const Comp& comparator) { + if (src.size() <= 1) { + return src; + } + + const size_t mid = src.size() / 2; + const auto mid_it = std::next(src.begin(), mid); + + const auto left = MergeSort( + std::deque(src.begin(), mid_it), + comparator + ); + const auto right = MergeSort( + std::deque(mid_it, src.end()), + comparator + ); + + return Merge(left, right, comparator); +} diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..2f71d94 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,345 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include "algo.h" + +#include +#include + +#include + +bool CaseInsensitiveLess(const std::string& lhs, const std::string& rhs) { + return QString::compare( + QString::fromStdString(lhs), + QString::fromStdString(rhs), + Qt::CaseInsensitive + ) < 0; +} + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) + , m_random_gen_(std::random_device{}()) { + ui->setupUi(this); + + ApplyModel(); +} + +MainWindow::~MainWindow() { + delete ui; +} + +void MainWindow::ApplyModel() { + const auto it_tmp = deque_model_.iterator; + const QSignalBlocker blocker(ui->list_widget); + + ui->list_widget->clear(); + + for (size_t i = 0; i < deque_model_.items.size(); ++i) { + ui->list_widget->addItem( + QString::fromStdString(std::to_string(i) + ": " + deque_model_.items[i]) + ); + } + + ui->list_widget->addItem("end"); + + deque_model_.iterator = it_tmp; + + ui->txt_size->setText(QString::number(deque_model_.items.size())); + + const bool is_empty = deque_model_.items.empty(); + ui->btn_pop_back->setDisabled(is_empty); + ui->btn_pop_front->setDisabled(is_empty); + ui->btn_erase->setDisabled(is_empty); + + ApplyIterator(); +} + +void MainWindow::ApplyIterator() { + const QSignalBlocker blocker(ui->list_widget); + + if (deque_model_.items.empty()) { + ui->list_widget->setCurrentRow(0); + ui->txt_elem_content->clear(); + ui->btn_edit->setDisabled(true); + ui->btn_erase->setDisabled(true); + ui->btn_inc_iterator->setDisabled(true); + ui->btn_dec_iterator->setDisabled(true); + return; + } + + const auto distance = std::distance(deque_model_.items.begin(), deque_model_.iterator); + const bool is_end = deque_model_.iterator == deque_model_.items.end(); + const bool is_begin = deque_model_.iterator == deque_model_.items.begin(); + + ui->list_widget->setCurrentRow(static_cast(distance)); + ui->btn_edit->setDisabled(is_end); + ui->btn_erase->setDisabled(is_end); + ui->btn_inc_iterator->setDisabled(is_end); + ui->btn_dec_iterator->setDisabled(is_begin); + + if (is_end) { + ui->txt_elem_content->clear(); + } else { + ui->txt_elem_content->setText(QString::fromStdString(*deque_model_.iterator)); + } +} + +void MainWindow::SetRandomGen(const std::mt19937& random_gen) { + m_random_gen_ = random_gen; +} + +void MainWindow::on_btn_clear_clicked() { + deque_model_.items.clear(); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_push_front_clicked() { + deque_model_.items.push_front(ui->txt_elem_content->text().toStdString()); + + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_push_back_clicked() { + deque_model_.items.push_back(ui->txt_elem_content->text().toStdString()); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_pop_back_clicked() { + if (deque_model_.items.empty()) { + return; + } + + deque_model_.items.pop_back(); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_pop_front_clicked() { + if (deque_model_.items.empty()) { + return; + } + + deque_model_.items.pop_front(); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_insert_clicked() { + deque_model_.items.insert( + deque_model_.iterator, + ui->txt_elem_content->text().toStdString() + ); + + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_erase_clicked() { + if (deque_model_.iterator == deque_model_.items.end()) { + return; + } + + deque_model_.items.erase(deque_model_.iterator); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_begin_clicked() { + deque_model_.iterator = deque_model_.items.begin(); + ApplyIterator(); +} + +void MainWindow::on_btn_end_clicked() { + deque_model_.iterator = deque_model_.items.end(); + ApplyIterator(); +} + +void MainWindow::on_btn_dec_iterator_clicked() { + if (deque_model_.iterator != deque_model_.items.begin()) { + --deque_model_.iterator; + } + + ApplyIterator(); +} + +void MainWindow::on_btn_inc_iterator_clicked() { + if (deque_model_.iterator != deque_model_.items.end()) { + ++deque_model_.iterator; + } + + ApplyIterator(); +} + +void MainWindow::on_list_widget_currentRowChanged(int currentRow) { + currentRow = std::clamp(currentRow, 0, static_cast(deque_model_.items.size())); + deque_model_.iterator = deque_model_.items.begin() + currentRow; + ApplyIterator(); +} + +void MainWindow::on_btn_tea_clicked() { + static std::deque tea { + "Чай Лунцзин", + "Эрл Грей", + "Сенча", + "Пуэр", + "Дарджилинг", + "Ассам", + "Матча", + "Ганпаудер", + "Оолонг", + "Лапсанг Сушонг" + }; + + deque_model_.items = tea; + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_cakes_clicked() { + static std::deque cakes { + "Красный бархат", + "Наполеон", + "Медовик", + "Тирамису", + "Прага", + "Чизкейк", + "Захер", + "Эстерхази", + "Морковный торт", + "Чёрный лес", + }; + deque_model_.items = cakes; + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_edit_clicked() { + if (deque_model_.iterator == deque_model_.items.end()) { + return; + } + + *deque_model_.iterator = ui->txt_elem_content->text().toStdString(); + ApplyModel(); +} + +void MainWindow::on_btn_resize_clicked() { + bool ok = false; + const int size = ui->txt_size->text().toInt(&ok); + + if (!ok || size < 0 || size > 1000) { + QMessageBox::warning(this, "Error", "Incorrect size"); + return; + } + + deque_model_.items.resize(static_cast(size)); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_find_clicked() { + deque_model_.iterator = std::find( + deque_model_.items.begin(), + deque_model_.items.end(), + ui->txt_elem_content->text().toStdString() + ); + + ApplyIterator(); +} + +void MainWindow::on_btn_count_clicked() { + const int count = static_cast(std::count( + deque_model_.items.begin(), + deque_model_.items.end(), + ui->le_count->text().toStdString() + )); + + ui->lbl_count->setText(QString::number(count)); +} + +void MainWindow::on_btn_min_element_clicked() { + deque_model_.iterator = std::min_element( + deque_model_.items.begin(), + deque_model_.items.end() + ); + + ApplyIterator(); +} + +void MainWindow::on_btn_max_element_clicked() { + deque_model_.iterator = std::max_element( + deque_model_.items.begin(), + deque_model_.items.end() + ); + + ApplyIterator(); +} + +void MainWindow::on_btn_sort_clicked() { + deque_model_.items = MergeSort( + deque_model_.items, + [](const std::string& lhs, const std::string& rhs) { + return lhs < rhs; + } + ); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_sOrT_clicked() { + deque_model_.items = MergeSort(deque_model_.items, CaseInsensitiveLess); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_shuffle_clicked() { + std::shuffle(deque_model_.items.begin(), deque_model_.items.end(), m_random_gen_); + ApplyModel(); +} + +void MainWindow::on_btn_unique_clicked() { + if (!std::is_sorted(deque_model_.items.begin(), deque_model_.items.end())) { + ApplyIterator(); + return; + } + + const auto new_end = std::unique(deque_model_.items.begin(), deque_model_.items.end()); + deque_model_.items.erase(new_end, deque_model_.items.end()); + deque_model_.iterator = deque_model_.items.begin(); + ApplyModel(); +} + +void MainWindow::on_btn_reverse_clicked() { + std::reverse(deque_model_.items.begin(), deque_model_.items.end()); + ApplyModel(); +} + +void MainWindow::on_btn_lower_bound_clicked() { if (!std::is_sorted(deque_model_.items.begin(), deque_model_.items.end())) { + return; + } + + deque_model_.iterator = std::lower_bound( + deque_model_.items.begin(), + deque_model_.items.end(), + ui->txt_elem_content->text().toStdString() + ); + + ApplyIterator(); +} + +void MainWindow::on_btn_upper_bound_clicked() { + if (!std::is_sorted(deque_model_.items.begin(), deque_model_.items.end())) { + return; + } + + deque_model_.iterator = std::upper_bound( + deque_model_.items.begin(), + deque_model_.items.end(), + ui->txt_elem_content->text().toStdString() + ); + + ApplyIterator(); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..ecd320a --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,87 @@ +#pragma once + +#include + +#include + +#include "model.h" + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow { + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + + void SetRandomGen(const std::mt19937& random_gen); + +private slots: + // Очистить список + void on_btn_clear_clicked(); + // Добавить элеменит в начало списка + void on_btn_push_front_clicked(); + // Добавить элемент в конец списка + void on_btn_push_back_clicked(); + // Удалить элемент из начала списка + void on_btn_pop_front_clicked(); + // Удалить элемент из конца списка + void on_btn_pop_back_clicked(); + // Вставить элемент в список на определенную позицию + void on_btn_insert_clicked(); + // Удалить выбранный элемент + void on_btn_erase_clicked(); + // Перейти в начало списка + void on_btn_begin_clicked(); + // Перейти в конец списка + void on_btn_end_clicked(); + // Перейти на пердыдущий элемент + void on_btn_dec_iterator_clicked(); + // Перейти на следующий элемент списка + void on_btn_inc_iterator_clicked(); + // Событие обновляет список после изменения + void on_list_widget_currentRowChanged(int currentRow); + // Заполнение списка стандарными элементами (чай, торт) + void on_btn_tea_clicked(); + void on_btn_cakes_clicked(); + + // Изменить элемент списка + void on_btn_edit_clicked(); + // Изменить количество элементов в списке + void on_btn_resize_clicked(); + // Найти элемент в списке + void on_btn_find_clicked(); + // Отобразить количество элементов в спике + void on_btn_count_clicked(); + // Перейти к минимальному элементу + void on_btn_min_element_clicked(); + // Перейти к максимаьному элементу + void on_btn_max_element_clicked(); + // Сортировать список + void on_btn_sort_clicked(); + // Сортировать список + void on_btn_sOrT_clicked(); + // Применить рандомный + void on_btn_shuffle_clicked(); + // Удалить повторящиеся элементы + void on_btn_unique_clicked(); + // Перевернуть массив + void on_btn_reverse_clicked(); + // Поиск элементов меньше и больше заданного значения + void on_btn_lower_bound_clicked(); + void on_btn_upper_bound_clicked(); + +private: + // Выводит на экран элементы списка с 0, устанавливает размер списка + void ApplyModel(); + // Выводит текущее полжение итератора, информацию об элементе + void ApplyIterator(); + +private: + Model deque_model_; + Ui::MainWindow *ui; + std::mt19937 m_random_gen_; +}; diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..1fb121d --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,478 @@ + + + MainWindow + + + + 0 + 0 + 600 + 648 + + + + std::deque demo + + + + + + 10 + 10 + 581 + 631 + + + + + + + + + + + + + + + 0 + 0 + + + + + + + + + 0 + 0 + + + + Edit + + + + + + + find + + + + + + + + + + + + + + count + + + + + + + 0 + + + + + + + + + + + + 0 + 0 + + + + pop_back + + + + + + + + 0 + 0 + + + + push_back + + + + + + + + 0 + 0 + + + + erase + + + + + + + + 0 + 0 + + + + Qt::LayoutDirection::LeftToRight + + + insert + + + + + + + + 0 + 0 + + + + -- + + + + + + + + 0 + 0 + + + + ++ + + + + + + + + 0 + 0 + + + + begin + + + + + + + + 0 + 0 + + + + end + + + + + + + + 0 + 0 + + + + = чай + + + + + + + + 0 + 0 + + + + = торты + + + + + + + + 0 + 0 + + + + Методы + + + + + + + Итератор + + + + + + + + 0 + 0 + + + + Заготовки + + + + + + + + 0 + 0 + + + + clear + + + + + + + Алгоритмы + + + + + + + + 0 + 0 + + + + min_element + + + + + + + + 0 + 0 + + + + max_element + + + + + + + + 0 + 0 + + + + merge sort + + + + + + + + 0 + 0 + + + + merge sOrT + + + + + + + + 0 + 0 + + + + shuffle + + + + + + + + 0 + 0 + + + + unique + + + + + + + + 0 + 0 + + + + reverse + + + + + + + + 0 + 0 + + + + pop_front + + + + + + + + 0 + 0 + + + + push_front + + + + + + + + 0 + 0 + + + + lower_bound + + + + + + + + 0 + 0 + + + + upper_bound + + + + + + + + + Qt::Orientation::Vertical + + + + 20 + 40 + + + + + + + + + + + + + + 0 + 0 + + + + size(): + + + + + + + + 0 + 0 + + + + + + + + resize + + + + + + + + + + + + + + diff --git a/model.h b/model.h new file mode 100644 index 0000000..941b04a --- /dev/null +++ b/model.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include +#include +#include + +struct Model { + using Deque = std::deque; + Deque items; + Deque::iterator iterator = items.begin(); +}; diff --git a/submission.pro b/submission.pro new file mode 100644 index 0000000..b9c3804 --- /dev/null +++ b/submission.pro @@ -0,0 +1,24 @@ +######## +MOCK_LIB=../../../../mocks_library +######## + +QT += core gui widgets + +CONFIG += c++20 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + algo.h \ + mainwindow.h \ + model.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target