Vla_DOS

Untitled

May 14th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. class Hospital
  10. {
  11. private:
  12.     string doctor_name;
  13.     string patient_name;
  14.     string patient_diagnos;
  15.     string num_palata;
  16. public:
  17.     Hospital(const char* doctor_name = "",
  18.         const char* patient_name = "",
  19.         const char* patient_diagnos = "",
  20.         const char* num_palata = "")
  21.         : doctor_name(doctor_name),
  22.         patient_name(patient_name),
  23.         patient_diagnos(patient_diagnos),
  24.         num_palata(num_palata) {}
  25.  
  26.     ostream& write(ostream& f) const
  27.     {
  28.         f << "Ім'я лiкаря: " << doctor_name << "\n";
  29.         f << "Iм'я пацiєнта: " << patient_name << "\n";
  30.         f << "Дiагноз: " << patient_diagnos << "\n";
  31.         f << "Номер палати: " << num_palata << "\n";
  32.         return f;
  33.     }
  34.  
  35.     istream& read(istream& f)
  36.     {
  37.         if (!getline(f, doctor_name))
  38.             return f;
  39.         getline(f, patient_name);
  40.         getline(f, patient_diagnos);
  41.         getline(f, num_palata);
  42.         return f;
  43.     }
  44.  
  45.     friend ostream& operator << (ostream& f, const Hospital& i)
  46.     {
  47.         return i.write(f);
  48.     }
  49.  
  50.     friend istream& operator >> (istream& f, Hospital& i)
  51.     {
  52.         return i.read(f);
  53.     }
  54.  
  55.     friend bool cmpByDoctor(const Hospital& h1, const Hospital& h2);
  56. };
  57.  
  58.  
  59. void appendToFile(string filename, const Hospital& h) {
  60.     ofstream out(filename, ios::app);
  61.     out << h;
  62. }
  63.  
  64. void readFromFile(string filename, vector<Hospital>& hospitals) {
  65.     ifstream in(filename);
  66.     Hospital h;
  67.     while (in >> h)
  68.         hospitals.push_back(h);
  69. }
  70.  
  71. bool cmpByDoctor(const Hospital& h1, const Hospital& h2) {
  72.     return h1.doctor_name < h2.doctor_name;
  73. }
  74.  
  75. void sortHospitalsByDoctor(vector<Hospital>& hospitals) {
  76.     sort(hospitals.begin(), hospitals.end(), cmpByDoctor);
  77. }
  78.  
  79. void showHospitals(const vector<Hospital>& hospitals) {
  80.     for (const auto& h : hospitals)
  81.         cout << h << "\n";
  82. }
  83. void Input(string file) {
  84.     cout << "Iм'я лiкаря: ";
  85.     string doctor_name;
  86.     getline(cin, doctor_name);
  87.     char* cstr_doctor_name = new char[doctor_name.length() + 1];
  88.    
  89.     cout << "Iм'я пацiєнта: ";
  90.     string patient_name;
  91.     getline(cin, patient_name);
  92.     char* cstr_patient_name = new char[patient_name.length() + 1];
  93.  
  94.     cout << "Дiагноз: ";
  95.     string patient_diagnos;
  96.     getline(cin, patient_diagnos);
  97.     char* cstr_patient_diagnos = new char[patient_diagnos.length() + 1];
  98.  
  99.     cout << "Номер палати: ";
  100.     string num_palata;
  101.     getline(cin, num_palata);
  102.     char* cstr_num_palata = new char[num_palata.length() + 1];
  103.  
  104.     Hospital h(doctor_name.c_str(), patient_name.c_str(), patient_diagnos.c_str(), num_palata.c_str());
  105.     appendToFile(file, h);
  106. }
  107. void MenuAdmin() {
  108.     cout << " 1. Додати данi;\n 2. Прочитати данi;\n 3. Вивести вiдсортованi данi;\n 0. Вихiд.\n";
  109. }
  110. void MenuUser() {
  111.     cout << " 1. Прочитати данi;\n 2. Вивести вiдсортованi данi;\n 0. Вихiд.\n";
  112. }
  113. int main()
  114. {
  115.     string file = "text.txt";
  116.     setlocale(0, "");
  117.     vector<Hospital> h;
  118.     cout << " Вхiд...\n Хто ви?\n (admin/user)\n";
  119.     string log;
  120.     cin >> log;
  121.     if (log == "admin") {
  122.         int a = 0;
  123.         do {
  124.             MenuAdmin();
  125.  
  126.             cin >> a;
  127.             switch (a)
  128.             {
  129.             case 1: {
  130.                 Input(file);
  131.             }break;
  132.             case 2: {
  133.                 readFromFile(file, h);
  134.                 showHospitals(h);
  135.             }break;
  136.             case 3: {
  137.                 readFromFile(file, h);
  138.                 sortHospitalsByDoctor(h);
  139.                 showHospitals(h);
  140.             }break;
  141.             default:
  142.                 break;
  143.             }
  144.         } while (a != 0);
  145.     }
  146.     else if (log == "user") {
  147.         int u = 0;
  148.         do {
  149.             MenuUser();
  150.  
  151.             cin >> u;
  152.             switch (u)
  153.             {
  154.             case 1: {
  155.                 readFromFile(file, h);
  156.                 showHospitals(h);
  157.             }break;
  158.             case 2: {
  159.                 readFromFile(file, h);
  160.                 sortHospitalsByDoctor(h);
  161.                 showHospitals(h);
  162.             }break;
  163.             default:
  164.                 break;
  165.             }
  166.         } while (u != 0);
  167.     }
  168.     else
  169.         cout << "Будь ласка, введіть дані правильно!";
  170.     system("pause");
  171. }
Advertisement
Add Comment
Please, Sign In to add comment