From 7198257df5f0de9e5341245705ef5a474a145c3e Mon Sep 17 00:00:00 2001 From: NuarkNoir Date: Wed, 26 Feb 2020 12:52:49 +0700 Subject: [PATCH] [2sem]pb 16 --- 2sem/programming basics/16/100.cpp | 114 +++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 2sem/programming basics/16/100.cpp diff --git a/2sem/programming basics/16/100.cpp b/2sem/programming basics/16/100.cpp new file mode 100644 index 0000000..f98997f --- /dev/null +++ b/2sem/programming basics/16/100.cpp @@ -0,0 +1,114 @@ +// pb_16_100.cpp +// Горбацевич Андрей +#include +#include +#include +#include +#include + +using namespace std; + +enum scholarship { + HI_ED = 0, SC_ED = 1, SV_RD = 2 +}; + +struct DB_record { + char surname[32]; + unsigned short age; + char city[32]; + scholarship scs; + + bool operator < (const DB_record& rhs) const { + string l = string(surname); + transform(l.begin(), l.end(), l.begin(), [](unsigned char c){ return tolower(c); }); + string r = string(rhs.surname); + transform(r.begin(), r.end(), r.begin(), [](unsigned char c){ return tolower(c); }); + return l < r; + } +}; + +vector parseDB(ifstream &ifs); + +int main() { + string path_in = "in.txt"; // путь до входного файла + string path_out = "out.bin"; // путь до выходного файла + + ifstream ifs(path_in); + ofstream ofs(path_out, ios::binary); + if (!ifs.is_open()) + { + cerr << "Unable to open file" << endl; + return 1; + } + + vector records = parseDB(ifs); + for (auto record : records) { + ofs.write(reinterpret_cast(&record), sizeof(record)); + } + + ifs.close(); + ofs.close(); + + vector records_fb; + ifstream binfs(path_out, ios::binary); + while (!binfs.eof() && !binfs.fail()) { + DB_record record{}; + binfs.read(reinterpret_cast(&record), sizeof(record)); + if (record.age == 0) { + continue; + } + records_fb.push_back(record); + } + sort(records_fb.begin(), records_fb.end()); + for (auto record : records_fb) { + cout << record.surname << endl; + cout << "\tAge: " << record.age << endl; + cout << "\tCity: " << record.city << endl; + cout << "\tScholarship: "; + switch (record.scs) { + case HI_ED: + cout << "higher"; + break; + case SC_ED: + cout << "secondary"; + break; + case SV_RD: + cout << "secondary vocational"; + break; + } + cout << endl; + } + + return 0; +} + +vector parseDB(ifstream &ifs) { + vector out; + while (!ifs.fail() && !ifs.eof()) { + DB_record record{}; + ifs >> record.surname; + ifs >> record.age; + ifs >> record.city; + { + string comp_str; + ifs >> comp_str; + if (comp_str[0] == '"') { + string sub_comp_str; + ifs >> sub_comp_str; + comp_str += " " + sub_comp_str; + } + if (comp_str == "higher") { + record.scs = HI_ED; + } + else if (comp_str == "secondary") { + record.scs = SC_ED; + } + else if (comp_str == "\"secondary vocational\"") { + record.scs = SV_RD; + } + } + out.push_back(record); + } + return out; +} +