diff --git a/part 2/Container.cpp b/part 2/Container.cpp index 046207f5196a6e107b83f0ee60134c18f0033764..8d0085e747e2ca192275707faf2a87ec8f17617a 100644 --- a/part 2/Container.cpp +++ b/part 2/Container.cpp @@ -1,244 +1,246 @@ -#include "Container.h" -#include -#include -#include -#include -#include - - -luggage::luggage(size_t flight_id, - const char* date_time, - const char* destination, - const char* surname, - size_t num, - double weight) : - flight_id(flight_id), - num(num), - weight(weight) -{ - strcpy_s(this->date_time, 30, date_time); - strcpy_s(this->destination, 30, destination); - strcpy_s(this->surname, 30, surname); -} - -std::istream& operator>>(std::istream& in, luggage& luggage) -{ - std::cout << "Flight id:" << std::endl; - in >> luggage.flight_id; - in.ignore(std::numeric_limits::max(), '\n'); - std::cout << "Date Time:" << std::endl; - in.getline(luggage.date_time, 30); - std::cout << "Destination:" << std::endl; - in.getline(luggage.destination, 30); - std::cout << "Passanger surname:" << std::endl; - in.getline(luggage.surname, 30); - std::cout << "Luggage amount:" << std::endl; - in >> luggage.num; - std::cout << "Total luggage weight:" << std::endl; - in >> luggage.weight; - in.ignore(std::numeric_limits::max(), '\n'); - return in; -} - -std::ostream& operator<<(std::ostream& out, const luggage& luggage) -{ - out << luggage.flight_id << std::endl - << luggage.date_time << std::endl - << luggage.destination << std::endl - << luggage.surname << std::endl - << luggage.num << std::endl - << luggage.weight << std::endl; - return out; -} - -void Container::sync() -{ - std::ifstream fin(filename, std::ios::binary); - luggage elem; - size_t i = 0; - while (fin.read(reinterpret_cast(&elem), sizeof(luggage))) { - i++; - array.insert(std::make_pair(i, elem)); - } - -} - -Container::Container() : - filename("data"), - edits(0) -{} - -Container::Container(const std::string& filename) : - filename(filename), - edits(0) -{} - -Container::Container(const std::string& filename, const std::initializer_list& list) : - filename(filename), - edits(0) -{ - write(list); -} - -void Container::write(const std::initializer_list& list) -{ - std::ofstream fout(filename, std::ios::binary | std::ios::app); - for (const auto& elem : list) - fout.write(reinterpret_cast(&elem), sizeof(luggage)); - -} - -void Container::open(bool fill) -{ - std::ofstream fout(filename, std::ios::binary); - if (!fout) { - std::cerr << "open error: unable to create file."; - exit; - } - - if (fill) { - luggage elem; - bool continue_filling; - do { - std::cin >> elem; - std::cout << "Input:" << std::endl - << "0 - to stop filling;" << std::endl - << "1 - to continue;" << std::endl; - fout.write(reinterpret_cast(&elem), sizeof(luggage)); - std::cin >> continue_filling; - std::cin.ignore(std::numeric_limits::max(), '\n'); - } while (continue_filling); - } -} - -void Container::remove(size_t index) -{ - sync(); - array.erase(index); - std::ofstream fout(filename + "_edit_" + std::to_string(++edits), std::ios::binary); - for (const auto& elem : array) - fout.write(reinterpret_cast(&elem.second), sizeof(luggage)); -} - -void Container::replace(size_t index, const luggage& sub) -{ - sync(); - array[index] = sub; - std::ofstream fout(filename + "_edit_" + std::to_string(++edits), std::ios::binary); - for (const auto& elem : array) - fout.write(reinterpret_cast(&elem.second), sizeof(luggage)); -} - -void Container::to_text() -{ - sync(); - std::ofstream fout(filename + ".txt"); - - fout << "|" << std::setw(3) << "#" << "|" - << std::setw(15) << "Flight id" << "|" - << std::setw(15) << "Date time" << "|" - << std::setw(15) << "Destination" << "|" - << std::setw(15) << "Passanger surname" << "|" - << std::setw(15) << "Luggage amount" << "|" - << std::setw(15) << "Total luggage weight" << "|" << std::endl; - - for (const auto& elem : array) { - fout << "|" << std::setw(3) << elem.first << "|" - << std::setw(15) << elem.second.flight_id << "|" - << std::setw(30) << elem.second.date_time << "|" - << std::setw(30) << elem.second.destination << "|" - << std::setw(30) << elem.second.surname << "|" - << std::setw(15) << elem.second.num << "|" - << std::setw(15) << elem.second.weight << "|" - << std::endl; - } -} - -const std::string& Container::get_filename() -{ - return filename; -} - -void Container::search_by_flight_id(const size_t& id) -{ - sync(); - std::ofstream fout(filename + "_id_" + std::to_string(id), std::ios::binary); - - std::vector res; - - auto iter = array.begin(); - while (iter != array.end()) - { - iter = std::find_if(iter, array.end(), [&id](const auto& elem) { return elem.second.flight_id == id; }); - if (iter != array.end()) { - res.push_back(iter->second); - ++iter; - } - } - - for (const auto& elem : res) - fout.write(reinterpret_cast(&elem), sizeof(luggage)); -} - -void Container::search_by_date_time(const std::string& date) -{ - sync(); - std::ofstream fout(filename + "_dt_" + date, std::ios::binary); - - std::vector res; - - auto iter = array.begin(); - while (iter != array.end()) - { - iter = std::find_if(iter, array.end(), [&date](const auto& elem) { return elem.second.date_time == date; }); - if (iter != array.end()) { - res.push_back(iter->second); - ++iter; - } - } - - for (const auto& elem : res) - fout.write(reinterpret_cast(&elem), sizeof(luggage)); -} - -void Container::search_by_destination(const std::string& dest) -{ - sync(); - std::ofstream fout(filename + "_dest_" + dest, std::ios::binary); - - std::vector res; - - auto iter = array.begin(); - while (iter != array.end()) - { - iter = std::find_if(iter, array.end(), [&dest](const auto& elem) { return elem.second.destination == dest; }); - if (iter != array.end()) { - res.push_back(iter->second); - ++iter; - } - } - - for (const auto& elem : res) - fout.write(reinterpret_cast(&elem), sizeof(luggage)); -} - -void Container::search_by_weight(double weight) -{ - sync(); - std::ofstream fout(filename + "_weight_" + std::to_string(weight), std::ios::binary); - - std::vector res; - - auto iter = array.begin(); - while (iter != array.end()) - { - iter = std::find_if(iter, array.end(), [&weight](const auto& elem) { return elem.second.weight == weight; }); - if (iter != array.end()) { - res.push_back(iter->second); - ++iter; - } - } - - for (const auto& elem : res) - fout.write(reinterpret_cast(&elem), sizeof(luggage)); +#include "Container.h" +#include +#include +#include +#include +#include + + +luggage::luggage(size_t flight_id, + const char* date_time, + const char* destination, + const char* surname, + size_t num, + double weight) : + flight_id(flight_id), + num(num), + weight(weight) +{ + strcpy_s(this->date_time, 30, date_time); + strcpy_s(this->destination, 30, destination); + strcpy_s(this->surname, 30, surname); +} + +std::istream& operator>>(std::istream& in, luggage& luggage) +{ + std::cout << "Flight id:" << std::endl; + in >> luggage.flight_id; + in.ignore(std::numeric_limits::max(), '\n'); + std::cout << "Date Time:" << std::endl; + in.getline(luggage.date_time, 30); + std::cout << "Destination:" << std::endl; + in.getline(luggage.destination, 30); + std::cout << "Passanger surname:" << std::endl; + in.getline(luggage.surname, 30); + std::cout << "Luggage amount:" << std::endl; + in >> luggage.num; + std::cout << "Total luggage weight:" << std::endl; + in >> luggage.weight; + in.ignore(std::numeric_limits::max(), '\n'); + return in; +} + +std::ostream& operator<<(std::ostream& out, const luggage& luggage) +{ + out << luggage.flight_id << std::endl + << luggage.date_time << std::endl + << luggage.destination << std::endl + << luggage.surname << std::endl + << luggage.num << std::endl + << luggage.weight << std::endl; + return out; +} + +void Container::sync() +{ + std::ifstream fin(filename, std::ios::binary); + luggage elem; + size_t i = 0; + while (fin.read(reinterpret_cast(&elem), sizeof(luggage))) { + i++; + array.insert(std::make_pair(i, elem)); + } + +} + +Container::Container() : + filename("data"), + edits(0) +{} + +Container::Container(const std::string& filename) : + filename(filename), + edits(0) +{} + +Container::Container(const std::string& filename, const std::initializer_list& list) : + filename(filename), + edits(0) +{ + write(list); +} + +void Container::write(const std::initializer_list& list) +{ + std::ofstream fout(filename, std::ios::binary | std::ios::app); + for (const auto& elem : list) + fout.write(reinterpret_cast(&elem), sizeof(luggage)); + +} + +void Container::open(bool fill) +{ + std::ofstream fout(filename, std::ios::binary); + if (!fout) { + std::cerr << "open error: unable to create file."; + exit; + } + + if (fill) { + luggage elem; + bool continue_filling; + do { + std::cin >> elem; + std::cout << "Input:" << std::endl + << "0 - to stop filling;" << std::endl + << "1 - to continue;" << std::endl; + fout.write(reinterpret_cast(&elem), sizeof(luggage)); + std::cin >> continue_filling; + std::cin.ignore(std::numeric_limits::max(), '\n'); + } while (continue_filling); + } +} + +void Container::remove(size_t index) +{ + sync(); + array.erase(index); + std::ofstream fout(filename + "_edit_" + std::to_string(++edits), std::ios::binary); + for (const auto& elem : array) + fout.write(reinterpret_cast(&elem.second), sizeof(luggage)); +} + +void Container::replace(size_t index, const luggage& sub) +{ + sync(); + array[index] = sub; + std::ofstream fout(filename + "_edit_" + std::to_string(++edits), std::ios::binary); + for (const auto& elem : array) + fout.write(reinterpret_cast(&elem.second), sizeof(luggage)); +} + +void Container::to_text() +{ + sync(); + std::ofstream fout(filename + ".txt"); + + fout << "|" << std::setw(3) << "#" << "|" + << std::setw(15) << "Flight id" << "|" + << std::setw(15) << "Date time" << "|" + << std::setw(15) << "Destination" << "|" + << std::setw(15) << "Passanger surname" << "|" + << std::setw(15) << "Luggage amount" << "|" + << std::setw(15) << "Total luggage weight" << "|" << std::endl; + + for (const auto& elem : array) { + fout << "|" << std::setw(3) << elem.first << "|" + << std::setw(15) << elem.second.flight_id << "|" + << std::setw(30) << elem.second.date_time << "|" + << std::setw(30) << elem.second.destination << "|" + << std::setw(30) << elem.second.surname << "|" + << std::setw(15) << elem.second.num << "|" + << std::setw(15) << elem.second.weight << "|" + << std::endl; + } +} + +const std::string& Container::get_filename() +{ + return filename; +} + +void Container::search_by_flight_id(const size_t& id) +{ + sync(); + std::ofstream fout(filename + "_id_" + std::to_string(id), std::ios::binary); + + std::vector res; + + auto iter = array.begin(); + while (iter != array.end()) + { + iter = std::find_if(iter, array.end(), [&id](const auto& elem) { return elem.second.flight_id == id; }); + if (iter != array.end()) { + res.push_back(iter->second); + ++iter; + } + } + + for (const auto& elem : res) + fout.write(reinterpret_cast(&elem), sizeof(luggage)); +} + +void Container::search_by_date_time(const std::string& date) +{ + sync(); + std::ofstream fout(filename + "_dt_" + date, std::ios::binary); + + std::vector res; + + auto iter = array.begin(); + while (iter != array.end()) + { + iter = std::find_if(iter, array.end(), [&date](const auto& elem) { return elem.second.date_time == date; }); + if (iter != array.end()) { + res.push_back(iter->second); + ++iter; + } + } + + for (const auto& elem : res) + fout.write(reinterpret_cast(&elem), sizeof(luggage)); +} + +void Container::search_by_destination(const std::string& dest) +{ + sync(); + std::ofstream fout(filename + "_dest_" + dest, std::ios::binary); + + std::vector res; + + auto iter = array.begin(); + while (iter != array.end()) + { + iter = std::find_if(iter, array.end(), [&dest](const auto& elem) { return elem.second.destination == dest; }); + if (iter != array.end()) { + res.push_back(iter->second); + ++iter; + } + } + + for (const auto& elem : res) + fout.write(reinterpret_cast(&elem), sizeof(luggage)); +} + +void Container::search_by_weight(double weight) +{ + sync(); + std::ofstream fout(filename + "_weight_" + std::to_string(weight), std::ios::binary); + + std::vector res; + + auto iter = array.begin(); + while (iter != array.end()) + { + //!!! Нельзя сравнивать double на ==. Нужно fabs(...) < eps; + + iter = std::find_if(iter, array.end(), [&weight](const auto& elem) { return elem.second.weight == weight; }); + if (iter != array.end()) { + res.push_back(iter->second); + ++iter; + } + } + + for (const auto& elem : res) + fout.write(reinterpret_cast(&elem), sizeof(luggage)); } \ No newline at end of file