Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <xlnt\xlnt.hpp>
- #include <Windows.h>
- #undef max
- using namespace xlnt;
- enum encode_mode {
- UTFtoWIN,
- WINtoUTF
- };
- std::string file_open(std::string file_name);
- std::string backup_file(std::string file_name);
- double valueCheck(std::string &value);
- std::string encoder(std::string text_to_encode, int encode_mode);
- class work_with_file {
- workbook file;
- worksheet open_sheet;
- alignment alig;
- font ft;
- std::string file_name, operation, position_name;
- std::string cell_string_filename = "file.temp";
- std::fstream log_file;
- int operation_num, calculation = 0;
- char operation_symbol;
- unsigned int position_num;
- int main_menu() {
- log_file << "===ОСНОВНОЕ МЕНЮ==="
- << "\nВыбор операции";
- std::cout << "Список операций:"
- << "\n1 - Списание продаж"
- << "\n2 - Изменение позиций"
- << "\n3 - Отобразить общие суммы"
- << "\n4 - Сохранить изменения"
- << "\n5 - Закончить работу";
- while (true) {
- std::cout << "\nВведите номер операции, которую необходимо выполнить: ";
- std::cin >> operation;
- operation_num = static_cast<int>(valueCheck(operation));
- if (operation_num > 5 || operation_num < 1) {
- system("cls");
- std::cout << "Введен неверный номер операции" << std::endl;
- continue;
- }
- else {
- break;
- }
- }
- system("cls");
- return operation_num;
- }
- int main_menu_switch(int operation_num) {
- switch (operation_num) {
- case 1:
- log_file << "\n\nБыла выбрана операция 1 - Списание продаж:\n";
- operation_1();
- break;
- case 2:
- log_file << "\n\nБыла выбрана операция 2 - Изменение позиций:";
- operation_2();
- break;
- case 3:
- total_calculation();
- log_file << "\n\nБыла выбрана операция 3 - Отобразить общие суммы\n";
- std::cout << "Общая сумма: " << open_sheet.cell("F341").value<int>() << " руб" << std::endl;
- log_file << "Общая сумма: " << open_sheet.cell("F341").value<int>() << " руб" << std::endl;
- std::cout << "Сумма на Ленинском: " << open_sheet.cell("F342").value<int>() << " руб" << std::endl;
- log_file << "Сумма на Ленинском: " << open_sheet.cell("F342").value<int>() << " руб" << std::endl;
- std::cout << "Сумма на БХ: " << open_sheet.cell("F343").value<int>() << " руб" << std::endl;
- log_file << "Сумма на БХ: " << open_sheet.cell("F343").value<int>() << " руб" << std::endl;
- std::cout << "Сумма на складе: " << open_sheet.cell("F344").value<int>() << " руб" << std::endl;
- log_file << "Сумма на складе: " << open_sheet.cell("F344").value<int>() << " руб\n\n" << std::endl;
- system("pause");
- system("cls");
- break;
- case 4:
- log_file << "\n\nБыла выбрана операция 4 - Сохранить изменения\n\n";
- file.save(file_name);
- file_name = backup_file(file_name);
- file.load(file_name);
- open_sheet = file.active_sheet();
- break;
- case 5:
- log_file << "\n\nБыла выбрана операция 5 - Закончить работу\n";
- return 5;
- break;
- }
- }
- void operation_1() {
- while (true) {
- std::cout << "Введите номер позиции (для выхода введите 0): ";
- std::cin >> position_num;
- system("cls");
- if (position_num == 0) {
- log_file << "Введен 0. Завершение работы с изменением позиций.\n\n";
- system("cls");
- break;
- }
- else if (position_num > 338) {
- std::cout << "Неверно введен номер позиции" << std::endl;
- log_file << "\nНеверно введен номер позиции\n\n";
- system("pause");
- system("cls");
- continue;
- }
- else {
- bool was_change = false;
- log_file << "\nСписание " << position_num << " позиции {";
- while (true) {
- unsigned int was_operation = operation_num;
- std::cout << "Номер позиции: " << open_sheet.cell("A" + std::to_string(position_num + 2)).value<int>() << std::endl;
- position_name = xlnt_stream_to_cout("B" + std::to_string(position_num + 2), cell_string_filename);
- std::cout << "Наименование: " << encoder(position_name, encode_mode::UTFtoWIN) << std::endl;
- std::cout << "Цена: " << open_sheet.cell("C" + std::to_string(position_num + 2)).value<int>() << " руб" << std::endl;
- std::cout << "Общее количество: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
- std::cout << "Количество на Ленинском: " << open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
- std::cout << "Количество на Богдана Хмельницкого: " << open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
- std::cout << "Количество на складе: " << open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
- std::cout << "\n1 - Отнять с Ленинского\n2 - Отнять с БХ\n3 - Отнять со склада"
- << "\n4 - Отмена последнего изменения\n0 - назад"
- << "\n\nОткуда необходимо отнять? ";
- std::cin >> operation;
- operation_num = static_cast<int>(valueCheck(operation));
- system("cls");
- if (operation_num == 0) {
- total_calculation();
- break;
- }
- if (operation_num > 4) {
- do {
- std::cout << "Введен неверный ответ " << std::endl;
- std::cout << "Откуда необходимо отнять? ";
- std::cin >> operation;
- operation_num = static_cast<int>(valueCheck(operation));
- system("cls");
- } while (operation_num > 4);
- }
- if (operation_num == 4) {
- if (was_change) {
- was_change = operation_1_switch(was_operation, true);
- }
- else if (!was_change && was_operation == 4) {
- std::cout << "Не было сделано изменений, которые можно отменить." << std::endl;
- system("pause");
- }
- }
- else {
- was_change = operation_1_switch(operation_num);
- }
- system("cls");
- }
- log_file << "}\n";
- }
- }
- }
- bool operation_1_switch(int operation_num, bool bring_back = false) {
- int quantity;
- std::string operation_name, col_name;
- if (operation_num == 1) {
- operation_name = " ленинском";
- col_name = "E";
- }
- else if (operation_num == 2) {
- operation_name = " БХ";
- col_name = "F";
- }
- else if (operation_num == 3) {
- operation_name = " складе";
- col_name = "G";
- }
- if (bring_back) {
- log_file << "\n\tПрибавление позиции на " << operation_name << ":";
- log_file << "\n\tБыло: " << open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() << "\n";
- quantity = open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() + 1;
- open_sheet.cell(col_name + std::to_string(position_num + 2)).value(quantity);
- log_file << "\tСтало: " << open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() << "\n";
- log_file << "\n\tИзменение общего количества\n\tБыло: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
- open_sheet.cell("D" + std::to_string(position_num + 2)).value(open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>());
- log_file << "\tСтало: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
- return false;
- }
- else {
- log_file << "\n\tСписание на " << operation_name << ":";
- log_file << "\n\tБыло: " << open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() << "\n";
- quantity = open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() - 1;
- if (quantity < 0) {
- std::cout << "Ошибка: отрицательное количество." << std::endl;
- log_file << "\tОшибка: отрицательное количество.";
- system("pause");
- return false;
- }
- else {
- open_sheet.cell(col_name + std::to_string(position_num + 2)).value(quantity);
- log_file << "\tСтало: " << open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() << "\n";
- log_file << "\n\tИзменение общего количества\n\tБыло: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
- open_sheet.cell("D" + std::to_string(position_num + 2)).value(open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>());
- log_file << "\tСтало: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
- return true;
- }
- }
- }
- void operation_2() {
- while (true) {
- std::cout << "Введите номер позиции (для выхода введите 0): ";
- std::cin >> position_num;
- system("cls");
- if (position_num == 0) {
- log_file << "Введен 0. Завершение работы с изменением позиций.\n\n";
- system("cls");
- break;
- }
- else if (position_num > 338) {
- std::cout << "Неверно введен номер позиции" << std::endl;
- log_file << "\nНеверно введен номер позиции\n\n";
- system("pause");
- system("cls");
- continue;
- }
- else {
- log_file << "\nИзменение " << position_num << " позиции {";
- while (true) {
- std::cout << "Номер позиции: " << open_sheet.cell("A" + std::to_string(position_num + 2)).value<int>() << std::endl;
- position_name = xlnt_stream_to_cout("B" + std::to_string(position_num + 2), cell_string_filename);
- std::cout << "Наименование: " << encoder(position_name, encode_mode::UTFtoWIN) << std::endl;
- std::cout << "Цена: " << open_sheet.cell("C" + std::to_string(position_num + 2)).value<int>() << " руб" << std::endl;
- std::cout << "Общее количество: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
- std::cout << "Количество на Ленинском: " << open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
- std::cout << "Количество на Богдана Хмельницкого: " << open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
- std::cout << "Количество на складе: " << open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
- std::cout << "\n1 - Наименование\n2 - Цена\n3 - Количество на Ленинском"
- << "\n4 - Количество на БХ\n5 - Количество на складе\n6 - выровнять\n7 - сделать текст жирным/убрать жирность\n0 - назад"
- << "\n\nЧто необходимо изменить? ";
- std::cin >> operation;
- operation_num = static_cast<int>(valueCheck(operation));
- system("cls");
- if (operation_num == 0) {
- total_calculation();
- break;
- }
- if (operation_num > 7) {
- do {
- std::cout << "Введен неверный ответ " << std::endl;
- std::cout << "Что необходимо изменить? ";
- std::cin >> operation;
- operation_num = static_cast<int>(valueCheck(operation));
- system("cls");
- } while (operation_num > 7);
- }
- operation_2_switch(operation_num);
- system("cls");
- }
- log_file << "}\n";
- }
- }
- }
- void operation_2_switch(int num_of_operation) {
- std::string cell_symbol, text_operation;
- if (num_of_operation == 1) {
- cell_symbol = "B";
- text_operation = " наименование";
- }
- else if (num_of_operation == 2) {
- cell_symbol = "C";
- text_operation = " цену";
- }
- else if (num_of_operation == 3) {
- cell_symbol = "E";
- text_operation = " количество на Ленинском";
- }
- else if (num_of_operation == 4) {
- cell_symbol = "F";
- text_operation = " количество на БХ";
- }
- else if (num_of_operation == 5) {
- cell_symbol = "G";
- text_operation = " количество на складе";
- }
- log_file << "\n\tМеняем" << text_operation;
- if (num_of_operation == 1) {
- log_file << "\n\tБыло: " << encoder(position_name, encode_mode::UTFtoWIN) << "\n";
- std::cout << "Введите новое" << text_operation << ": ";
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- SetConsoleCP(1251);
- std::getline(std::cin, position_name);
- SetConsoleCP(866);
- log_file << "\tСтало: " << position_name << "\n";
- open_sheet.cell("B" + std::to_string(position_num + 2)).value(encoder(position_name, encode_mode::WINtoUTF));
- system("cls");
- }
- else if (num_of_operation > 1 && num_of_operation < 6) {
- log_file << "\n\tБыло: " << open_sheet.cell(cell_symbol + std::to_string(position_num + 2)).value<int>() << "\n";
- std::cout << "Введите новое" << text_operation << ": ";
- std::cin >> operation_num;
- open_sheet.cell(cell_symbol + std::to_string(position_num + 2)).value(operation_num);
- log_file << "\tСтало: " << operation_num << "\n";
- system("cls");
- if (num_of_operation > 2) {
- log_file << "\n\tИзменение общего количества\n\tБыло: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
- open_sheet.cell("D" + std::to_string(position_num + 2)).value(open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>());
- log_file << "\tСтало: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
- }
- }
- if (num_of_operation == 6) {
- while (true) {
- std::cout << "1 - по левому краю\n2 - по правому краю\n3 - по центру\nКак выровнять? ";
- std::cin >> operation;
- operation_num = static_cast<int>(valueCheck(operation));
- if (operation_num > 3) {
- std::cout << "Неверный ответ." << std::endl;
- continue;
- }
- else {
- break;
- }
- }
- switch (operation_num) {
- case 1:
- {
- alig.horizontal(horizontal_alignment::left);
- open_sheet.cell("B" + std::to_string(position_num + 2)).alignment(alig);
- break;
- }
- case 2:
- {
- alig.horizontal(horizontal_alignment::right);
- open_sheet.cell("B" + std::to_string(position_num + 2)).alignment(alig);
- break;
- }
- case 3:
- {
- alig.horizontal(horizontal_alignment::center);
- open_sheet.cell("B" + std::to_string(position_num + 2)).alignment(alig);
- break;
- }
- }
- system("cls");
- }
- if (num_of_operation == 7) {
- while (true) {
- std::cout << "1 - сделать жирным\n2 - убрать жирность\nКакую операцию выполнить? ";
- std::cin >> operation;
- operation_num = static_cast<int>(valueCheck(operation));
- if (operation_num > 2) {
- std::cout << "Неверный ответ." << std::endl;
- continue;
- }
- else {
- break;
- }
- }
- switch (operation_num) {
- case 1:
- open_sheet.cell("B" + std::to_string(position_num + 2)).font(ft.bold(true));
- break;
- case 2:
- open_sheet.cell("B" + std::to_string(position_num + 2)).font(ft.bold(false));
- break;
- }
- system("cls");
- }
- }
- void total_calculation() {
- for (int i = 1; i <= 338; i++) {
- calculation += open_sheet.cell("D" + std::to_string(i + 2)).value<int>()*open_sheet.cell("C" + std::to_string(i + 2)).value<int>();
- }
- open_sheet.cell("F341").value(calculation);
- calculation = 0;
- for (int i = 1; i <= 338; i++) {
- calculation += open_sheet.cell("E" + std::to_string(i + 2)).value<int>()*open_sheet.cell("C" + std::to_string(i + 2)).value<int>();
- }
- open_sheet.cell("F342").value(calculation);
- calculation = 0;
- for (int i = 1; i <= 338; i++) {
- calculation += open_sheet.cell("F" + std::to_string(i + 2)).value<int>()*open_sheet.cell("C" + std::to_string(i + 2)).value<int>();
- }
- open_sheet.cell("F343").value(calculation);
- calculation = 0;
- for (int i = 1; i <= 338; i++) {
- calculation += open_sheet.cell("G" + std::to_string(i + 2)).value<int>()*open_sheet.cell("C" + std::to_string(i + 2)).value<int>();
- }
- open_sheet.cell("F344").value(calculation);
- calculation = 0;
- }
- std::string xlnt_stream_to_cout(std::string cell_num, std::string temp_file) {
- std::fstream object;
- std::string text;
- object.open(temp_file, std::fstream::app | std::fstream::in | std::fstream::out);
- int current_position, current_position_2, text_size;
- object.seekg(0, std::ios::end);
- current_position = object.tellg();
- object << open_sheet.cell(cell_num);
- object.seekg(0, std::ios::cur);
- current_position_2 = object.tellg();
- text_size = current_position_2 - current_position;
- object.seekg(-text_size, std::ios::end);
- std::getline(object, text);
- return text;
- }
- public:
- work_with_file(std::string file_name) {
- this->file_name = file_name;
- file.load(this->file_name);
- open_sheet = file.active_sheet();
- log_file.open(this->file_name + ".log", std::fstream::app | std::fstream::out);
- }
- void work() {
- while (true) {
- if (main_menu_switch(main_menu()) == 5) {
- break;
- }
- }
- }
- ~work_with_file() {
- file.save(file_name);
- log_file.close();
- std::remove("file.temp");
- }
- };
- int main() {
- setlocale(LC_ALL, "ru");
- std::cout << "Введите имя файла: ";
- std::string file_name;
- SetConsoleCP(1251);
- std::getline(std::cin, file_name);
- SetConsoleCP(866);
- file_name += ".xlsx";
- file_name = file_open(file_name);
- file_name = backup_file(file_name);
- work_with_file xlsx_file(file_name);
- system("cls");
- xlsx_file.work();
- return 0;
- }
- std::string file_open(std::string file_name) {
- std::fstream file;
- file_name = "C:\\Users\\trane\\Desktop\\Товарный отчёт\\" + file_name;
- file.open(file_name);
- if (file.is_open()) {
- file.close();
- return file_name;
- }
- else {
- while (true) {
- std::cout << "Невозможно открыть файл. Возможно неверно введено имя файла или изменилась директория";
- std::cout << "\nВведите адрес до файла(включая имя файла и расширение): ";
- SetConsoleCP(1251);
- std::getline(std::cin, file_name);
- SetConsoleCP(866);
- system("cls");
- file.open(file_name);
- if (file.is_open()) {
- break;
- }
- else {
- continue;
- }
- }
- file.close();
- return file_name;
- }
- }
- std::string backup_file(std::string file_name) {
- std::fstream file_backup;
- std::fstream file_copy;
- file_backup.open(file_name, std::fstream::in | std::fstream::app | std::fstream::binary);
- std::string new_file_name;
- for (int i = 0; i < file_name.size(); i++) {
- new_file_name += file_name[i];
- if (file_name[i] == '\\') {
- new_file_name.clear();
- }
- }
- bool is_copy;
- std::fstream check;
- char ch = 49;
- do {
- if (ch > 57) {
- std::cout << "Слишком много копий" << std::endl;
- return 0;
- }
- check.open(new_file_name);
- if (check.is_open()) {
- is_copy = true;
- if (new_file_name[new_file_name.size() - 6] == ')') {
- if (new_file_name[new_file_name.size() - 7] == ch) {
- ch++;
- }
- else {
- new_file_name[new_file_name.size() - 7] = ch;
- }
- }
- else {
- new_file_name[new_file_name.size() - 5] = '(';
- new_file_name[new_file_name.size() - 4] = '1';
- new_file_name[new_file_name.size() - 3] = ')';
- new_file_name[new_file_name.size() - 2] = '.';
- new_file_name[new_file_name.size() - 1] = 'x';
- new_file_name += "lsx";
- ch++;
- }
- }
- else {
- is_copy = false;
- }
- check.close();
- } while (is_copy);
- file_copy.open(new_file_name, std::fstream::out | std::fstream::binary | std::fstream::app);
- char symbol, symbol_1;
- file_backup.seekg(0, std::ios::end);
- int size = file_backup.tellg();
- file_backup.seekg(0, std::ios::beg);
- for (int i = 0; i < size; i++) {
- file_backup.read((char*)&symbol, sizeof(char));
- file_copy.write((char*)&symbol, sizeof(char));
- }
- file_backup.close();
- file_copy.close();
- return new_file_name;
- }
- std::string encoder(std::string text_to_encode, int encode_mode) {
- BSTR bstrWide;
- int length;
- char *text = new char[text_to_encode.size() + 1];
- for (int i = 0; i < text_to_encode.size(); i++) {
- text[i] = text_to_encode[i];
- }
- text[text_to_encode.size()] = '\0';
- if (encode_mode == encode_mode::UTFtoWIN) {
- length = MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), NULL, NULL);
- bstrWide = SysAllocStringLen(NULL, length);
- MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), bstrWide, length);
- length = WideCharToMultiByte(1251, 0, bstrWide, -1, NULL, 0, NULL, NULL);
- char *text_1251 = new char[length];
- WideCharToMultiByte(1251, 0, bstrWide, -1, text_1251, length, NULL, NULL);
- SysFreeString(bstrWide);
- text_to_encode.clear();
- for (int i = 0; i < strlen(text_1251); i++) {
- text_to_encode += text_1251[i];
- }
- delete[]text_1251;
- }
- else if (encode_mode == encode_mode::WINtoUTF) {
- length = MultiByteToWideChar(1251, 0, text, strlen(text), NULL, NULL);
- bstrWide = SysAllocStringLen(NULL, length);
- MultiByteToWideChar(1251, 0, text, strlen(text), bstrWide, length);
- length = WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, NULL, 0, NULL, NULL);
- char *text_u8 = new char[length];
- WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, text_u8, length, NULL, NULL);
- SysFreeString(bstrWide);
- text_to_encode.clear();
- for (int i = 0; i < strlen(text_u8); i++) {
- text_to_encode += text_u8[i];
- }
- delete[]text_u8;
- }
- delete[]text;
- return text_to_encode;
- }
- double valueCheck(std::string &value) {
- int temp;
- for (int i = 0; i < value.size(); i++) {
- if (value[i] == '.') {
- value[i] = ',';
- }
- if (value[i] == ',') {
- temp = i + 1;
- for (; temp < value.size(); temp++) {
- if (value[temp] == ',' || value[temp] == '.') {
- for (; temp < value.size(); temp++) {
- value[temp] = value[temp + 1];
- }
- temp = i + 1;
- }
- }
- }
- if ((value[i] < 48 && value[i] != 43 && value[i] != 44 && value[i] != 45 && value[i] != 0) || value[i] > 57) {
- temp = i;
- for (; temp < value.size(); temp++) {
- value[temp] = value[temp + 1];
- }
- i--;
- }
- }
- if (value[0] == '\0') {
- value[0] = '0';
- value[1] = '\0';
- }
- return std::stod(value);
- }
Advertisement
Add Comment
Please, Sign In to add comment