Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- class Hospital
- {
- private:
- string doctor_name;
- string patient_name;
- string patient_diagnos;
- string num_palata;
- public:
- Hospital(const char* doctor_name = "",
- const char* patient_name = "",
- const char* patient_diagnos = "",
- const char* num_palata = "")
- : doctor_name(doctor_name),
- patient_name(patient_name),
- patient_diagnos(patient_diagnos),
- num_palata(num_palata) {}
- ostream& write(ostream& f) const
- {
- f << "Ім'я лiкаря: " << doctor_name << "\n";
- f << "Iм'я пацiєнта: " << patient_name << "\n";
- f << "Дiагноз: " << patient_diagnos << "\n";
- f << "Номер палати: " << num_palata << "\n";
- return f;
- }
- istream& read(istream& f)
- {
- if (!getline(f, doctor_name))
- return f;
- getline(f, patient_name);
- getline(f, patient_diagnos);
- getline(f, num_palata);
- return f;
- }
- friend ostream& operator << (ostream& f, const Hospital& i)
- {
- return i.write(f);
- }
- friend istream& operator >> (istream& f, Hospital& i)
- {
- return i.read(f);
- }
- friend bool cmpByDoctor(const Hospital& h1, const Hospital& h2);
- };
- void appendToFile(string filename, const Hospital& h) {
- ofstream out(filename, ios::app);
- out << h;
- }
- void readFromFile(string filename, vector<Hospital>& hospitals) {
- ifstream in(filename);
- Hospital h;
- while (in >> h)
- hospitals.push_back(h);
- }
- bool cmpByDoctor(const Hospital& h1, const Hospital& h2) {
- return h1.doctor_name < h2.doctor_name;
- }
- void sortHospitalsByDoctor(vector<Hospital>& hospitals) {
- sort(hospitals.begin(), hospitals.end(), cmpByDoctor);
- }
- void showHospitals(const vector<Hospital>& hospitals) {
- for (const auto& h : hospitals)
- cout << h << "\n";
- }
- void Input(string file) {
- cout << "Iм'я лiкаря: ";
- string doctor_name;
- getline(cin, doctor_name);
- char* cstr_doctor_name = new char[doctor_name.length() + 1];
- cout << "Iм'я пацiєнта: ";
- string patient_name;
- getline(cin, patient_name);
- char* cstr_patient_name = new char[patient_name.length() + 1];
- cout << "Дiагноз: ";
- string patient_diagnos;
- getline(cin, patient_diagnos);
- char* cstr_patient_diagnos = new char[patient_diagnos.length() + 1];
- cout << "Номер палати: ";
- string num_palata;
- getline(cin, num_palata);
- char* cstr_num_palata = new char[num_palata.length() + 1];
- Hospital h(doctor_name.c_str(), patient_name.c_str(), patient_diagnos.c_str(), num_palata.c_str());
- appendToFile(file, h);
- }
- void MenuAdmin() {
- cout << " 1. Додати данi;\n 2. Прочитати данi;\n 3. Вивести вiдсортованi данi;\n 0. Вихiд.\n";
- }
- void MenuUser() {
- cout << " 1. Прочитати данi;\n 2. Вивести вiдсортованi данi;\n 0. Вихiд.\n";
- }
- int main()
- {
- string file = "text.txt";
- setlocale(0, "");
- vector<Hospital> h;
- cout << " Вхiд...\n Хто ви?\n (admin/user)\n";
- string log;
- cin >> log;
- if (log == "admin") {
- int a = 0;
- do {
- MenuAdmin();
- cin >> a;
- switch (a)
- {
- case 1: {
- Input(file);
- }break;
- case 2: {
- readFromFile(file, h);
- showHospitals(h);
- }break;
- case 3: {
- readFromFile(file, h);
- sortHospitalsByDoctor(h);
- showHospitals(h);
- }break;
- default:
- break;
- }
- } while (a != 0);
- }
- else if (log == "user") {
- int u = 0;
- do {
- MenuUser();
- cin >> u;
- switch (u)
- {
- case 1: {
- readFromFile(file, h);
- showHospitals(h);
- }break;
- case 2: {
- readFromFile(file, h);
- sortHospitalsByDoctor(h);
- showHospitals(h);
- }break;
- default:
- break;
- }
- } while (u != 0);
- }
- else
- cout << "Будь ласка, введіть дані правильно!";
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment