Vla_DOS

Untitled

May 10th, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. class Hospital
  9. {
  10. public:
  11.     string doctor_name, patient_name, patient_diagnos, num_palata;
  12.  
  13.     Hospital(const char* doctor_name = "",
  14.         const char* patient_name = "",
  15.         const char* patient_diagnos = "",
  16.         const char* num_palata = "")
  17.         :doctor_name(doctor_name), patient_name(patient_name), patient_diagnos(patient_diagnos), num_palata(num_palata){}
  18.  
  19.     void write(ostream& f) const
  20.     {
  21.         writeStr(doctor_name, f);
  22.         writeStr(patient_name, f);
  23.         writeStr(patient_diagnos, f);
  24.         writeStr(num_palata, f);
  25.     }
  26.     void read(istream& f)
  27.     {
  28.         readStr(doctor_name, f);
  29.         readStr(patient_name, f);
  30.         readStr(patient_diagnos, f);
  31.         readStr(num_palata, f);
  32.     }
  33.     static void ReadSortByPatientName(string path) {
  34.         list<Hospital> l;
  35.  
  36.         ifstream fin;
  37.         fin.open(path);
  38.  
  39.         if (!fin.is_open())
  40.             cout << "Error open!" << endl;
  41.         else {
  42.             Hospital b;
  43.  
  44.             while (fin.read((char*)&b, sizeof(Hospital)))
  45.                 l.push_back(b);
  46.  
  47.             SortByPatientName(l);
  48.             for (auto i : l)
  49.                 cout << "doctor_name: " << i.doctor_name << "\tpatient_name: " << i.patient_name << "\tpatient_diagnos: " << i.patient_diagnos << "\tnum_palata: " << i.num_palata << endl;
  50.         }
  51.         fin.close();
  52.     }
  53.  
  54.     friend ostream& operator << (ostream& f, const Hospital& i)
  55.     {
  56.         return f << "doctor_name: " << i.doctor_name << "\tpatient_name: " << i.patient_name << "\tpatient_diagnos: " << i.patient_diagnos << "\tnum_palata: " << i.num_palata;
  57.     }
  58.  
  59. private:
  60.  
  61.     static void writeStr(const string& s, ostream& f)
  62.     {
  63.         size_t l = s.length();
  64.         f.write((const char*)&l, sizeof(size_t));
  65.         f.write(s.data(), l);
  66.     }
  67.     static void readStr(string& s, istream& f)
  68.     {
  69.         size_t l;
  70.         f.read((char*)&l, sizeof(size_t));
  71.         char* str = new char[l + 1];
  72.         f.read(str, l);
  73.         str[l] = 0;
  74.         s = str;
  75.         delete[] str;
  76.     }
  77.     static void SortByPatientName(list<Hospital>& list) {
  78.         list.sort([](Hospital& l1, Hospital& l2) { return l1.patient_name < l2.patient_name; });
  79.     }
  80. };
  81.  
  82. int main(int argc, const char* argv[])
  83. {
  84.     setlocale(0, "");
  85.     Hospital x("Богданець Віктор Юрійович", "Власик Олексій Юрійович", "Загостренний апендицит", "3"), y("Богданець Віктор Юрійович", "Мельник Олексій Юрійович", "Застуда", "5");
  86.     Hospital u, v, c;
  87.     cout << u << "\n" << v << "\n\n";
  88.     {
  89.         ofstream out("text.txt", ios::binary);
  90.         x.write(out);
  91.         y.write(out);
  92.     }
  93.     {
  94.         ifstream in("text.txt", ios::binary);
  95.         u.read(in);
  96.         v.read(in);
  97.         cout << u << "\n" << v << "\n\n";
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment