diff --git a/1_part/1_part.sln b/1_part/1_part.sln new file mode 100644 index 0000000000000000000000000000000000000000..283304afe130967662737b84f7be3191cfe2e789 --- /dev/null +++ b/1_part/1_part.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33815.320 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "1_part", "1_part.vcxproj", "{B73B6830-170B-4396-BBF5-50D0B3CE164C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B73B6830-170B-4396-BBF5-50D0B3CE164C}.Debug|x64.ActiveCfg = Debug|x64 + {B73B6830-170B-4396-BBF5-50D0B3CE164C}.Debug|x64.Build.0 = Debug|x64 + {B73B6830-170B-4396-BBF5-50D0B3CE164C}.Debug|x86.ActiveCfg = Debug|Win32 + {B73B6830-170B-4396-BBF5-50D0B3CE164C}.Debug|x86.Build.0 = Debug|Win32 + {B73B6830-170B-4396-BBF5-50D0B3CE164C}.Release|x64.ActiveCfg = Release|x64 + {B73B6830-170B-4396-BBF5-50D0B3CE164C}.Release|x64.Build.0 = Release|x64 + {B73B6830-170B-4396-BBF5-50D0B3CE164C}.Release|x86.ActiveCfg = Release|Win32 + {B73B6830-170B-4396-BBF5-50D0B3CE164C}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0107BD0E-A100-43CA-9E22-514CB9955CBD} + EndGlobalSection +EndGlobal diff --git a/1_part/Application.h b/1_part/Application.h new file mode 100644 index 0000000000000000000000000000000000000000..166767a1d681e43a4d0363109591e4a56e23ac98 --- /dev/null +++ b/1_part/Application.h @@ -0,0 +1,190 @@ +#include "Functions.h" +#include + +void clearScreen() { +#ifdef _WIN32 + system("cls"); +#else + system("clear"); +#endif +}; + +void pauseScreen() { +#ifdef _WIN32 + system("pause"); +#else + system("read -p 'Press Enter to continue...' var"); +#endif +}; + +class Menu { +public: + Menu(const std::string& filename) : m_filename(filename) { + loadMenuItems(); + } + + void displayMainMenu() { + for (const auto& item : menuItems) { + std::cout << item << std::endl; + } + } + +private: + std::vector menuItems; + std::string m_filename; + + void loadMenuItems() { + std::ifstream file(m_filename); + if (!file.is_open()) { + std::cerr << "Unable to open file: " << m_filename << std::endl; + return; + } + + std::string line; + while (std::getline(file, line)) { + menuItems.push_back(line); + } + + file.close(); + } +}; + + +template +class Application { +public: + void run() { + clearScreen(); + Menu menu("menu.txt"); + menu.displayMainMenu(); + int choice; + std::cout << "Enter your choice: "; + std::cin >> choice; + switch (choice) + { + case 1: + clearScreen(); createDataMenu(); break; + case 2: + clearScreen(); loadDataMenu(); break; + case 3: + clearScreen(); modifyDataMenu(); break; + case 4: + clearScreen(); outputResultMenu(); break; + case 5: + std::cout << "Exiting the application" << std::endl; pauseScreen(); return; + default: + clearScreen(); std::cout << "Invalid choice." << std::endl; pauseScreen(); + } + }; +private: + Container m_cont; + std::string m_filename; + void createDataMenu() + { + int choice; + std::cout << "Select data creation method:" << std::endl; + std::cout << "1. Using File Streams" << std::endl; + std::cout << "2. Using Generate and Copy" << std::endl; + std::cin >> choice; + std::cout << "Enter the filename: "; + std::cin >> m_filename; + switch (choice) + { + case 1: + clearScreen(); createDataStream(m_filename); pauseScreen(); break; + case 2: + clearScreen(); createDataGenerate(m_filename); pauseScreen(); break; + default: + clearScreen(); std::cout << "Invalid choice." << std::endl; pauseScreen(); + } + } + + void loadDataMenu() + { + int choice; + std::cout << "Select data loading method:" << std::endl; + std::cout << "1. Load Data from File" << std::endl; + std::cout << "2. Load Data using Iterators" << std::endl; + std::cin >> choice; + std::cout << "Enter the filename: "; + std::cin >> m_filename; + switch (choice) + { + case 1: + { + clearScreen(); loadDataStream(m_filename, m_cont); printContainer(m_cont); pauseScreen(); break; + } + case 2: + { + clearScreen(); loadDataIterator(m_filename, m_cont); printContainer(m_cont); pauseScreen(); break; + } + default: + clearScreen(); std::cout << "Invalid choice." << std::endl; pauseScreen(); + } + } + + void modifyDataMenu() + { + int choice; + std::cout << "Select data modification method:" << std::endl; + std::cout << "1. Modify using template function" << std::endl; + std::cout << "2. Modify using iterators" << std::endl; + std::cout << "3. Modify using for_each" << std::endl; + std::cout << "4. Modify using transform" << std::endl; + std::cin >> choice; + switch (choice) + { + case 1: + { + clearScreen(); modifyTemplate(m_cont); printContainer(m_cont); pauseScreen(); break; + } + case 2: + { + clearScreen(); modifyIter(m_cont.begin(), m_cont.end()); printContainer(m_cont); pauseScreen(); break; + } + case 3: + { + clearScreen(); modifyForEach(m_cont); printContainer(m_cont); pauseScreen(); break; + } + case 4: + { + std::cout << "Enter original file" << std::endl; + std::string origfile; + std::cin >> origfile; + std::cout << "Enter final file" << std::endl; + std::string finfile; + std::cin >> finfile; + clearScreen(); + modifyTransform(origfile, finfile); + pauseScreen(); + break; + } + default: + clearScreen(); std::cout << "Invalid choice." << std::endl; pauseScreen(); + } + } + + void outputResultMenu() + { + int choice; + std::cout << "Select data output method:" << std::endl; + std::cout << "1. Output Result to File" << std::endl; + std::cout << "2. Output Result using Copy" << std::endl; + std::cin >> choice; + std::cout << "Enter the output filename: "; + std::cin >> m_filename; + switch (choice) + { + case 1: + { + clearScreen(); outputResultStream(m_filename, m_cont); pauseScreen(); break; + } + case 2: + { + clearScreen(); outputResultCopy(m_filename, m_cont); pauseScreen(); break; + } + default: + clearScreen(); std::cout << "Invalid choice." << std::endl; pauseScreen(); + } + } +}; diff --git a/1_part/Functions.h b/1_part/Functions.h new file mode 100644 index 0000000000000000000000000000000000000000..9b05ea8e63482e0ddd117580abd57d22c61dde89 --- /dev/null +++ b/1_part/Functions.h @@ -0,0 +1,165 @@ +#include "iostream" +#include "fstream" +#include "list" +#include "deque" +#include "numeric" +#include "vector" +#include "list" +#include "algorithm" +#include "string" + +void createDataStream(const std::string& filename) { + std::srand(std::time(nullptr)); + std::ofstream file(filename, std::ios::trunc); + if (file.is_open()) { + for (int i = 0; i < 100; i++) { + file << rand() % 101 - 50 << " "; + }; + std::cout << "file was succesfully created using file streams: " << filename << std::endl; + } + else + std::cerr << "incapable to create file" << std::endl; +}; + +void createDataGenerate(const std::string& filename) { + std::srand(std::time(nullptr)); + std::vector data(100); + std::generate(data.begin(), data.end(), []() { return std::rand() % 101 - 50; }); + std::ofstream file(filename, std::ios::trunc); + if (file.is_open()) + { + std::copy(data.begin(), data.end(), std::ostream_iterator(file, " ")); + std::cout << "file was succesfully created using generate and copy: " << filename << std::endl; + } + else + std::cerr << "incapable to create file" << std::endl; +}; + +template +void loadDataStream(const std::string& filename, Container& cont) { + std::ifstream file(filename); + using value_type = typename Container::value_type; + value_type value; + if (file.is_open()) { + while (!file.eof()) + { + while (file >> value) + { + cont.push_back(std::move(value)); + } + }; + } + else + std::cerr << "incapable to create file" << std::endl; +}; + +template +void loadDataIterator(const std::string& filename, Container& cont) +{ + std::ifstream file(filename); + if (file.is_open()) + { + auto beg = std::istream_iterator(file); + auto end = std::istream_iterator(); + std::copy(beg, end, std::back_inserter(cont)); + } + else + std::cerr << "incapable to open file" << std::endl; +}; + +template +void modifyTemplate(Container& cont) { + using value_type = typename Container::value_type; + std::vector negatives; + std::copy_if(cont.begin(), cont.end(), std::back_inserter(negatives), [](const value_type& elem) { + return elem < 0; + }); + value_type halfSum = std::accumulate(negatives.begin(), negatives.end(), value_type{}) / 2; + std::for_each(cont.begin(), cont.end(), [halfSum](value_type& elem) { + elem += halfSum; + }); + value_type sum = std::accumulate(cont.begin(), cont.end(), value_type{}); + value_type average = std::accumulate(cont.begin(), cont.end(), value_type{}, [](int accumulator, int element) { + return accumulator + std::abs(element); }) / cont.size(); + cont.push_back(std::move(sum)); + cont.push_back(std::move(average)); +}; + +template +void modifyIter(Iterator begin, Iterator end) { + using value_type = typename std::iterator_traits::value_type; + std::vector negatives; + std::copy_if(begin, end, std::back_inserter(negatives), [](const value_type& elem) { + return elem < 0; + }); + value_type halfSum = std::accumulate(negatives.begin(), negatives.end(), value_type{}) / 2; + std::for_each(begin, end, [halfSum](value_type& elem) { + elem += halfSum; + }); +}; + +template +void modifyForEach(Container& cont) { + using value_type = typename Container::value_type; + std::vector negatives; + std::copy_if(cont.begin(), cont.end(), std::back_inserter(negatives), [](const value_type& elem) { + return elem < 0; + }); + value_type halfSum = std::accumulate(negatives.begin(), negatives.end(), value_type{}) / 2; + std::for_each(cont.begin(), cont.end(), [halfSum](value_type& elem) { + elem += halfSum; + }); +}; + +void modifyTransform(const std::string& in_fname, const std::string& out_fname) { + std::ifstream fin(in_fname); + std::ofstream fout(out_fname); + std::vector vec; + std::copy(std::istream_iterator(fin), std::istream_iterator(), std::back_inserter(vec)); + std::vector negatives; + std::copy_if(vec.begin(), vec.end(), std::back_inserter(negatives), [](const int& elem) { + return elem < 0; + }); + int halfSum = std::accumulate(negatives.begin(), negatives.end(), 0) / 2; + std::transform(vec.begin(), vec.end(), std::ostream_iterator(fout, " "), [halfSum](int& elem) { + return elem + halfSum; + }); + fout << std::endl; +}; + +template +void printContainer(const Container& cont) +{ + std::cout << "container contents: "; + for (auto it = cont.begin(); it != cont.end(); ++it) + std::cout << *it << " "; +}; + +template +void outputResultStream(const std::string& filename, const Container& cont) +{ + using value_type = typename Container::value_type; + std::ofstream outFile(filename); + if (outFile.is_open()) + { + for (const value_type& elem : cont) + outFile << elem << " "; + outFile << std::endl; + std::cout << "data output to file: " << filename << std::endl; + } + else + std::cerr << "nncapable to create file" << std::endl; +}; + +template +void outputResultCopy(const std::string& filename, const Container& cont) +{ + std::ofstream outFile(filename); + if (outFile.is_open()) + { + std::copy(cont.begin(), cont.end(), std::ostream_iterator(outFile, " ")); + std::cout << "data output to file: " << filename << std::endl; + } + else + std::cerr << "incapable to create file" << std::endl; +}; \ No newline at end of file diff --git a/1_part/main.cpp b/1_part/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7bd1f4048bc82743a89c2cc4356ee5c3a9c305b --- /dev/null +++ b/1_part/main.cpp @@ -0,0 +1,38 @@ +#include "iostream" +#include "Application.h" +int main() +{ + bool flag = true; + while (flag) + { + std::cout << "Choose a container to test" << std::endl; + std::cout << "1. Vector" << std::endl; + std::cout << "2. List" << std::endl; + std::cout << "3. Deque" << std::endl; + int choice; + std::cin >> choice; + switch (choice) + { + case 1: + { + Application > app; flag = false; + app.run(); + break; + } + case 2: + { + Application > app; flag = false; + app.run(); + break; + } + case 3: + { + Application > app; flag = false; + app.run(); + break; + } + default: clearScreen(); std::cout << "Invalid choice." << std::endl; pauseScreen(); + } + } + return 0; +} \ No newline at end of file diff --git a/1_part/menu.txt b/1_part/menu.txt new file mode 100644 index 0000000000000000000000000000000000000000..ff4f72e95ad84523c70a67b9de64c515c2c46482 --- /dev/null +++ b/1_part/menu.txt @@ -0,0 +1,6 @@ +Main Menu: + 1. Create Data + 2. Load Data + 3. Modify Data + 4. Output Result + 5. Exit \ No newline at end of file diff --git a/2_part/2_part.sln b/2_part/2_part.sln new file mode 100644 index 0000000000000000000000000000000000000000..4a7bca8dc5d7c7da63aaa82d71b46458d8bcbf54 --- /dev/null +++ b/2_part/2_part.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33815.320 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2_part", "2_part.vcxproj", "{95BC5136-2C4D-4706-B160-84CEB5810F3F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {95BC5136-2C4D-4706-B160-84CEB5810F3F}.Debug|x64.ActiveCfg = Debug|x64 + {95BC5136-2C4D-4706-B160-84CEB5810F3F}.Debug|x64.Build.0 = Debug|x64 + {95BC5136-2C4D-4706-B160-84CEB5810F3F}.Debug|x86.ActiveCfg = Debug|Win32 + {95BC5136-2C4D-4706-B160-84CEB5810F3F}.Debug|x86.Build.0 = Debug|Win32 + {95BC5136-2C4D-4706-B160-84CEB5810F3F}.Release|x64.ActiveCfg = Release|x64 + {95BC5136-2C4D-4706-B160-84CEB5810F3F}.Release|x64.Build.0 = Release|x64 + {95BC5136-2C4D-4706-B160-84CEB5810F3F}.Release|x86.ActiveCfg = Release|Win32 + {95BC5136-2C4D-4706-B160-84CEB5810F3F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C2487EDE-55C6-4D22-A3E4-69E7D8F91A28} + EndGlobalSection +EndGlobal diff --git a/2_part/Application.h b/2_part/Application.h new file mode 100644 index 0000000000000000000000000000000000000000..e7cf04855ebc0625c6fc4b31791a9ec5ce0b1cd2 --- /dev/null +++ b/2_part/Application.h @@ -0,0 +1,220 @@ +#include "Automobile.h" +#include +void clearScreen() { +#ifdef _WIN32 + system("cls"); +#else + system("clear"); +#endif +}; + +void pauseScreen() { +#ifdef _WIN32 + system("pause"); +#else + system("read -p 'Press Enter to continue...' var"); +#endif +}; + +class Menu { +public: + Menu(const std::string& filename) : m_filename(filename) { + loadMenuItems(); + } + + void displayMainMenu() { + for (const auto& item : menuItems) { + std::cout << item << std::endl; + } + } + +private: + std::vector menuItems; + std::string m_filename; + + void loadMenuItems() { + std::ifstream file(m_filename); + if (!file.is_open()) { + std::cerr << "Unable to open file: " << m_filename << std::endl; + return; + } + + std::string line; + while (std::getline(file, line)) { + menuItems.push_back(line); + } + + file.close(); + } +}; + +class Application +{ +private: + std::string m_filename; + std::map m_auto; +public: + void run() + { + static int id_counter = 0; + while (true) + { + clearScreen(); + Menu menu("menu.txt"); + menu.displayMainMenu(); + uint32_t choice; + std::cout << "ENTER YOUT CHOICE: "; + std::cin >> choice; + switch (choice) + { + case 1: + { + clearScreen(); + std::string filename; + std::cout << "ENTER BINARY FILENAME: "; + std::cin >> filename; + if (filename != m_filename) + m_filename = filename; + Automobile newAuto; + newAuto.m_id = id_counter; + std::cout << "ENTER AUTOMOBILE'S INFO:" << std::endl; + std::cin >> newAuto; + addAutomobile(m_auto, newAuto, m_filename); + id_counter++; + pauseScreen(); + break; + } + case 2: + { + clearScreen(); + std::string filename; + uint32_t id; + std::cout << "ENTER BINARY FILENAME: "; + std::cin >> filename; + if (filename != m_filename) + m_filename = filename; + std::cout << "ENTER AUTOMOBILE'S ID YOU WANT TO REMOVE: "; + std::cin >> id; + auto found_auto = std::find_if( + m_auto.begin(), + m_auto.end(), + [id](const auto& car) { + return car.first == id; + }); + if (found_auto == m_auto.end()) + std::cout << "THERE'S NO AUTOMOBILES WITH SUCH ID!" << std::endl; + else + removeAutomobile(m_auto, id, m_filename); + pauseScreen(); + break; + } + case 3: + { + clearScreen(); + std::string filename; + Automobile newAuto; + uint32_t id; + std::cout << "ENTER BINARY FILENAME: "; + std::cin >> filename; + if (filename != m_filename) + m_filename = filename; + std::cout << "ENTER AUTOMOBILE'S ID YOU WANT TO REPLACE: "; + std::cin >> id; + auto found_auto = std::find_if( + m_auto.begin(), + m_auto.end(), + [id](const auto& person) { + return person.first == id; + }); + if (found_auto == m_auto.end()) + std::cout << "THERE'S NO AUTOMOBILES WITH SUCH ID!" << std::endl; + else + { + std::cout << "ENTER AUTOMOBILE'S INFO YOU WANT TO REPLACE BY: " << std::endl; + newAuto.m_id = id; + std::cin >> newAuto; + replaceAutomobile(m_auto, id, newAuto, m_filename); + } + pauseScreen(); + break; + } + case 4: + { + clearScreen(); + std::string filename; + std::string outputFilename; + std::string model; + std::cout << "ENTER BINARY FILENAME: "; + std::cin >> filename; + if (filename != m_filename) + m_filename = filename; + std::cout << "ENTER MODEL OF AUTOMOBILE TO SEARCH BY: " << std::endl; + std::cin >> model; + std::cout << "ENTER OUTPUT FILENAME: "; + std::cin >> outputFilename; + searchByAutomobileModel(m_auto, outputFilename, model); + pauseScreen(); + break; + } + case 5: + { + clearScreen(); + std::string filename; + std::string outputFilename; + std::string fuel; + std::cout << "ENTER BINARY FILENAME: "; + std::cin >> filename; + if (filename != m_filename) + m_filename = filename; + std::cout << "ENTER FUEL GRADE TO SEARCH BY: " << std::endl; + std::cin >> fuel; + std::cout << "ENTER OUTPUT FILENAME: "; + std::cin >> outputFilename; + searchByFuelGrade(m_auto, outputFilename, fuel); + pauseScreen(); + break; + } + case 6: + { + clearScreen(); + std::string filename; + std::string outputFilename; + size_t engine; + std::cout << "ENTER BINARY FILENAME: "; + std::cin >> filename; + if (filename != m_filename) + m_filename = filename; + std::cout << "ENTER ENGINE POWER TO SEARCH BY: " << std::endl; + std::cin >> engine; + std::cout << "ENTER OUTPUT FILENAME: "; + std::cin >> outputFilename; + searchByEnginePower(m_auto, outputFilename, engine); + pauseScreen(); + break; + } + case 7: + { + clearScreen(); + std::string filename; + std::string outputTextFilename; + std::cout << "ENTER BINARY FILENAME: "; + std::cin >> filename; + if (filename != m_filename) + m_filename = filename; + fileToContainer(m_filename, m_auto); + std::cout << "ENTER OUTPUT FILENAME: "; + std::cin >> outputTextFilename; + convertBinaryToTextFile(m_auto, outputTextFilename); + pauseScreen(); + break; + } + case 8: + std::cout << "EXCITING THE APPLICATION " << std::endl; + pauseScreen(); + return; + default: + clearScreen(); std::cout << "Invalid choice." << std::endl; pauseScreen(); + } + } + } +}; diff --git a/2_part/Automobile.cpp b/2_part/Automobile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a64d61823916a0ed98710d60f144ed4b3716bb0 --- /dev/null +++ b/2_part/Automobile.cpp @@ -0,0 +1,166 @@ +#include "Automobile.h" +std::istream& operator >>(std::istream& p_in, Automobile& p_auto) +{ + std::cout << "OWNER'S SURNAME: "; + p_in >> p_auto.m_surnameOwner; + std::cout << "CAR MODEL CODE: "; + p_in >> p_auto.m_codeModel; + std::cout << "CAR MODEL: "; + p_in >> p_auto.m_modelAuto; + std::cout << "FUEL GRADE: "; + p_in >> p_auto.m_gradeFuel; + std::cout << "ENGINE POWER: "; + p_in >> p_auto.m_powerEngine; + std::cout << "VOLUME OF THE TANK: "; + p_in >> p_auto.m_volumeTank; + std::cout << "REMAINING FUEL: "; + p_in >> p_auto.m_remainderFuel; + std::cout << "OIL VOLUME: "; + p_in >> p_auto.m_volumeOil; + return p_in; +}; + +std::ostream& operator << (std::ostream& p_out, const Automobile& p_auto) +{ + p_out << "OWNER'S SURNAME: " << p_auto.m_surnameOwner << std::endl + << "ID: " << p_auto.m_id << std::endl + << "CAR MODEL CODE: " << p_auto.m_codeModel << std::endl + << "CAR MODEL: " << p_auto.m_modelAuto << std::endl + << "FUEL GRADE: " << p_auto.m_gradeFuel << std::endl + << "ENGINE POWER: " << p_auto.m_powerEngine << std::endl + << "VOLUME OF THE TANK: " << p_auto.m_volumeTank << std::endl + << "REMAINING FUEL: " << p_auto.m_remainderFuel << std::endl + << "OIL VOLUME: " << p_auto.m_volumeOil << std::endl; + return p_out; +}; + +void fileToContainer(const std::string& p_filename, std::map& p_auto) { + std::ifstream binaryFile(p_filename, std::ios::binary); + if (!binaryFile.is_open()) { + std::cout << "Can't open the file!" << std::endl; + return; + }; + while (binaryFile) { + Automobile car; + binaryFile.read(reinterpret_cast(&car.m_id), sizeof(car.m_id)); + if (!binaryFile) break; + readStringFromFile(binaryFile, car.m_surnameOwner); + binaryFile.read(reinterpret_cast(&car.m_codeModel), sizeof(car.m_codeModel)); + readStringFromFile(binaryFile, car.m_modelAuto); + readStringFromFile(binaryFile, car.m_gradeFuel); + binaryFile.read(reinterpret_cast(&car.m_powerEngine), sizeof(car.m_powerEngine)); + binaryFile.read(reinterpret_cast(&car.m_volumeTank), sizeof(car.m_volumeTank)); + binaryFile.read(reinterpret_cast(&car.m_remainderFuel), sizeof(car.m_remainderFuel)); + binaryFile.read(reinterpret_cast(&car.m_volumeOil), sizeof(car.m_volumeOil)); + p_auto[car.m_id] = car; + } + binaryFile.close(); +} + +void containerToFile(const std::string& p_filename, std::map& p_auto) +{ + std::ofstream binaryFile(p_filename, std::ios::binary); + std::ofstream tempFile("temp.dat", std::ios::binary); + if (!binaryFile.is_open()) + { + std::cout << "Can't open the file!" << std::endl; + return; + }; + for (const auto& pair : p_auto) { + const Automobile& car = pair.second; + binaryFile.write(reinterpret_cast(&car.m_id), sizeof(car.m_id)); + writeStringToFile(binaryFile, car.m_surnameOwner); + binaryFile.write(reinterpret_cast(&car.m_codeModel), sizeof(car.m_codeModel)); + writeStringToFile(binaryFile, car.m_modelAuto); + writeStringToFile(binaryFile, car.m_gradeFuel); + binaryFile.write(reinterpret_cast(&car.m_powerEngine), sizeof(car.m_powerEngine)); + binaryFile.write(reinterpret_cast(&car.m_volumeTank), sizeof(car.m_volumeTank)); + binaryFile.write(reinterpret_cast(&car.m_remainderFuel), sizeof(car.m_remainderFuel)); + binaryFile.write(reinterpret_cast(&car.m_volumeOil), sizeof(car.m_volumeOil)); + } + auto name = p_filename.c_str(); + std::remove(name); + std::rename("temp.dat", name); +}; + +void addAutomobile(std::map& p_auto, const Automobile& p_new_auto, const std::string& p_filename) +{ + p_auto[p_new_auto.m_id] = p_new_auto; + containerToFile(p_filename, p_auto); +}; + +void removeAutomobile(std::map& p_auto, size_t p_id, const std::string& p_filename) +{ + p_auto.erase(p_id); + containerToFile(p_filename, p_auto);; +}; + +void replaceAutomobile(std::map& p_auto, size_t p_id, const Automobile& p_new_auto, const std::string& p_filename) { + p_auto.erase(p_id); + p_auto[p_id] = p_new_auto; + containerToFile(p_filename, p_auto); +}; + +void searchByAutomobileModel(std::map& p_auto, const std::string& p_output_filename, const std::string& p_model) +{ + std::ofstream outputFile(p_output_filename); + if (!outputFile.is_open()) + { + std::cout << "Can't open the file!" << std::endl; + return; + } + for (auto& car : p_auto) + if (car.second.m_modelAuto == p_model) + outputFile << car.second << std::endl; +}; + +void searchByFuelGrade(std::map& p_auto, const std::string& p_output_filename, const std::string& p_fuel) +{ + std::ofstream outputFile(p_output_filename); + if (!outputFile.is_open()) + { + std::cout << "Can't open the file!" << std::endl; + return; + } + for (auto& car : p_auto) + if (car.second.m_gradeFuel == p_fuel) + outputFile << car.second << std::endl; +}; + +void searchByEnginePower(std::map& p_auto, const std::string& p_output_filename, size_t p_engine) +{ + std::ofstream outputFile(p_output_filename); + if (!outputFile.is_open()) + { + std::cout << "Can't open the file!" << std::endl; + return; + } + for (auto& car : p_auto) + if (car.second.m_powerEngine == p_engine) + outputFile << car.second << std::endl; +}; + +void convertBinaryToTextFile(std::map& p_auto, const std::string& p_output_filename) +{ + std::ofstream outputFile(p_output_filename); + if (!outputFile.is_open()) + { + std::cout << "Can't open the file!" << std::endl; + return; + } + for (auto& car : p_auto) + outputFile << car.second << std::endl; +} + +void writeStringToFile(std::ofstream& out, const std::string& str) { + size_t length = str.length(); + out.write(reinterpret_cast(&length), sizeof(length)); + out.write(str.c_str(), length); +} + +void readStringFromFile(std::ifstream& in, std::string& str) { + size_t length; + in.read(reinterpret_cast(&length), sizeof(length)); + str.resize(length); + in.read(&str[0], length); +} \ No newline at end of file diff --git a/2_part/Automobile.h b/2_part/Automobile.h new file mode 100644 index 0000000000000000000000000000000000000000..310ac49b3d4976487e7ede66f5f038ed80f319e1 --- /dev/null +++ b/2_part/Automobile.h @@ -0,0 +1,31 @@ +#include "iostream" +#include "map" +#include "fstream" +#include "vector" +#include "string" + +struct Automobile { + size_t m_id; + std::string m_surnameOwner; + size_t m_codeModel; + std::string m_modelAuto; + std::string m_gradeFuel; + size_t m_powerEngine; + size_t m_volumeTank; + size_t m_remainderFuel; + size_t m_volumeOil; +}; + +std::istream& operator>>(std::istream&, Automobile&); +std::ostream& operator<<(std::ostream&, const Automobile&); +void fileToContainer(const std::string&, std::map&); +void containerToFile(const std::string&, std::map&); +void addAutomobile(std::map&, const Automobile&, const std::string&); +void removeAutomobile(std::map&, size_t, const std::string&); +void replaceAutomobile(std::map&, size_t, const Automobile&, const std::string&); +void searchByAutomobileModel(std::map&, const std::string&, const std::string&); +void searchByFuelGrade(std::map&, const std::string&, const std::string&); +void searchByEnginePower(std::map&, const std::string&, size_t); +void convertBinaryToTextFile(std::map&, const std::string&); +void writeStringToFile(std::ofstream&, const std::string&); +void readStringFromFile(std::ifstream&, std::string&); diff --git a/2_part/main.cpp b/2_part/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20413dd3d4af753b991829fd9a042bbf79576ea7 --- /dev/null +++ b/2_part/main.cpp @@ -0,0 +1,7 @@ +#include "Application.h" +int main() +{ + Application app; + app.run(); + return 0; +}; \ No newline at end of file diff --git a/2_part/menu.txt b/2_part/menu.txt new file mode 100644 index 0000000000000000000000000000000000000000..21ce0b0ddd9a992c0c3af62a0b4d581ce3e61cfd --- /dev/null +++ b/2_part/menu.txt @@ -0,0 +1,9 @@ +MENU: + 1. ADD AUTOMOBILE + 2. REMOVE AUTOMOBILE + 3. REPLACE AUTOMOBILE + 4. SEARCH AND SAVE AUTOMOBILE BY AUTOMOBILE MODEL + 5. SEARCH AND SAVE AUTOMOBILE BY FUEL GRADE + 6. SEARCH AND SAVE AUTOMOBILE BY ENGINE POWER + 7. CONVERT BINARY TO TEXT + 8. EXIT \ No newline at end of file