main.cpp
#include "hospital.h"
int main() {
int numberOfPatients = 0;
int numberOfDoctors = 2;
File file;
Patient *patientsArray = new Patient[numberOfPatients];
Registry *appointmentsArray = new Registry[numberOfPatients];
Doctor *doctorsArray = new Doctor[numberOfDoctors]{{"Егор", "Родионов", "Аллерголог", 0},
{"Ярик", "Бачок", "Травматолог", 1}
};
file.CheckFileExists();
int select = 0;
int work = 0;
do {
cout << "Оберіть функцію: " << endl;
cout << "1 - Додати пацієнта" << endl;
cout << "2 - Видалити пацієнта" << endl;
cout << "3 - Вивести список усіх пацієнтів" << endl;
cout << "4 - Вивести список пацієнтів для певного лікаря" << endl;
cout << "0 - Завершити роботу з програмою" << endl;
cout << "➡ ";
cin >> select;
while (cin.get() != '\n');
if (select > 4 || select < 0) {
cout << "Некоректний номер функції" << endl << endl;
work = 1;
} else if (select == 0) {
delete[] patientsArray;
delete[] appointmentsArray;
delete[] doctorsArray;
exit(0);
}
switch (select) {
case 1: { // Додати пацієнта
work = 1;
file.ReadFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
appointmentsArray->AddPatient(patientsArray, numberOfPatients);
appointmentsArray->AddPatientAppointment(appointmentsArray, patientsArray, doctorsArray,
numberOfPatients, numberOfDoctors);
file.RewriteFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
cout << "Бажаєте продовжити роботу? 1 - так, 2 - ні" << endl;
cout << "➡ ";
cin >> work;
while (cin.get() != '\n');
if (work != 1) {
delete[] patientsArray;
delete[] appointmentsArray;
delete[] doctorsArray;
exit(0);
}
break;
}
case 2: { // Видалити пацієнта
work = 1;
file.ReadFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
appointmentsArray->RemovePatientAppointment(patientsArray, appointmentsArray, numberOfPatients);
file.RewriteFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
cout << "Бажаєте продовжити роботу? 1 - так, 2 - ні" << endl;
cout << "➡ ";
(cin >> work).get();
if (work != 1) {
delete[] patientsArray;
delete[] appointmentsArray;
delete[] doctorsArray;
exit(0);
}
break;
}
case 3: { // Вивести список всіх пацієнтів
work = 1;
file.ReadFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
appointmentsArray->ShowAllPatientAppointments(appointmentsArray, patientsArray, doctorsArray,
numberOfPatients);
cout << "Бажаєте продовжити роботу? 1 - так, 2 - ні" << endl;
cout << "➡ ";
cin >> work;
while (cin.get() != '\n');
if (work != 1) {
delete[] patientsArray;
delete[] appointmentsArray;
delete[] doctorsArray;
exit(0);
}
break;
}
case 4: { // Вивести список пацієнтів для певного лікаря
work = 1;
file.ReadFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
appointmentsArray->ShowDoctorAppointments(appointmentsArray, patientsArray, doctorsArray,
numberOfPatients, numberOfDoctors);
cout << "Бажаєте продовжити роботу? 1 - так, 2 - ні" << endl;
cout << "➡ ";
cin >> work;
while (cin.get() != '\n');
if (work != 1) {
delete[] patientsArray;
delete[] appointmentsArray;
delete[] doctorsArray;
exit(0);
}
break;
}
}
} while (work == true);
return 0;
}
registry-class-implement.cpp
#include "hospital.h"
Patient Registry::NewPatient() {
Patient newPatient;
string name;
string surname;
string phone;
int age;
cout << "Введіть ім'я клієнта: " << endl;
cout << "➡ ";
getline(cin, name);
newPatient.SetPatientName(name);
cout << "Введіть фамілію клієнта: " << endl;
cout << "➡ ";
getline(cin, surname);
newPatient.SetPatientSurname(surname);
cout << "Введіть номер телефону клієнта: " << endl;
cout << "➡ ";
getline(cin, phone);
newPatient.SetPatientPhone(phone);
cout << "Введіть вік клієнта" << endl;
cout << "➡ ";
cin >> age;
while (cin.get() != '\n');
newPatient.SetPatientAge(age);
return newPatient;
}
void Registry::AddPatient(Patient *&patientsArray, int &numberOfPatients) {
Patient *patientsArrayTemp = new Patient[numberOfPatients + 1];
for (int j = 0; j < numberOfPatients; j++) {
patientsArrayTemp[j] = patientsArray[j];
}
delete[] patientsArray;
patientsArray = patientsArrayTemp;
numberOfPatients++;
int i = numberOfPatients - 1;
patientsArray[i] = NewPatient();
patientsArray[i].SetPatientCode(i);
}
void Registry::RemovePatient(Patient *&patientsArray, int &numberOfPatients, int patientToDelete) {
Patient *patientsArrayTemp = new Patient[numberOfPatients - 1];
for (int i = 0; i < patientToDelete; i++) {
patientsArrayTemp[i] = patientsArray[i];
}
for (int i = patientToDelete + 1; i < numberOfPatients; i++) {
patientsArrayTemp[i - 1] = patientsArray[i];
patientsArrayTemp[i - 1].SetPatientCode(patientsArrayTemp[i - 1].GetPatientCode() - 1);
}
delete[] patientsArray;
numberOfPatients--;
patientsArray = patientsArrayTemp;
}
Registry
Registry::NewAppointment(Patient *patientsArray, Doctor *doctorsArray, int numberOfDoctors, int numberOfPatients) {
Registry newAppointment;
int choosedDoctor = 0;
cout << "Введіть дату прийому: " << endl;
cout << "Рік (0-2022): " << endl;
cout << "➡ ";
cin >> newAppointment.date.year;
while (cin.get() != '\n');
cout << "Місяць (1-12): " << endl;
cout << "➡ ";
cin >> newAppointment.date.month;
while (cin.get() != '\n');
while (newAppointment.date.month > 12 || newAppointment.date.month < 0) {
cout << "Некоректний місяць: " << newAppointment.date.month << ", " << "оберіть правильний місяць" << endl;
cout << "Місяць (1-12): " << endl;
cout << "➡ ";
cin >> newAppointment.date.month;
while (cin.get() != '\n');
}
int numberOfMonthDays = GetNumberOfMonthDays(newAppointment.date.year, newAppointment.date.month);
cout << "День (1-" << numberOfMonthDays << "): " << endl;
cout << "➡ ";
cin >> newAppointment.date.day;
while (cin.get() != '\n');
while (newAppointment.date.day > numberOfMonthDays || newAppointment.date.day < 0) {
cout << "У обраному вами місяцю, не існує дня " << newAppointment.date.day << ", " << "оберіть правильний день"
<< endl;
cout << "День (1-" << numberOfMonthDays << "): " << endl;
cout << "➡ ";
cin >> newAppointment.date.day;
while (cin.get() != '\n');
}
cout << "Оберіть номер необхідного лікаря" << endl;
for (int i = 0; i < numberOfDoctors; i++) {
cout << "Номер лікаря: " << i << endl;
cout << "Ім'я лікаря: " << doctorsArray[i].GetDoctorName() << endl;
cout << "Фамілія лікаря: " << doctorsArray[i].GetDoctorSurname() << endl;
cout << "Кваліфікація лікаря: " << doctorsArray[i].GetDoctorQualification() << endl;
cout << endl;
}
cout << "➡ ";
cin >> choosedDoctor;
while (cin.get() != '\n');
while (choosedDoctor > numberOfDoctors - 1 || choosedDoctor < 0) {
cout << "Лікаря з таким номером не існує, оберіть правильний номер" << endl;
cout << "➡ ";
cin >> choosedDoctor;
while (cin.get() != '\n');
}
newAppointment.doctorCode = choosedDoctor;
newAppointment.patientCode = patientsArray[numberOfPatients - 1].GetPatientCode();
return newAppointment;
}
void Registry::AddPatientAppointment(Registry *&appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients, int numberOfDoctors) {
Registry *appointmentsArrayTemp = new Registry[numberOfPatients + 1];
for (int i = 0; i < numberOfPatients; i++) {
appointmentsArrayTemp[i] = appointmentsArray[i];
}
delete[] appointmentsArray;
appointmentsArray = appointmentsArrayTemp;
int i = numberOfPatients - 1;
appointmentsArray[i] = appointmentsArray->NewAppointment(patientsArray, doctorsArray, numberOfDoctors,
numberOfPatients);
}
void Registry::RemovePatientAppointment(Patient *&patientsArray, Registry *&appointmentsArray, int &numberOfPatients) {
Registry *appointmentsArrayTemp = new Registry[numberOfPatients - 1];
int patientToDelete;
cout << "Введіть номер пацієнта якого необхідно видалити" << endl;
cout << "➡ ";
cin >> patientToDelete;
while (cin.get() != '\n');
if (patientToDelete > numberOfPatients - 1 || patientToDelete < 0) {
cout << "Пацієнта з таким номером не існує" << endl;
return;
}
for (int i = 0; i < patientToDelete; i++) {
appointmentsArrayTemp[i] = appointmentsArray[i];
}
for (int i = patientToDelete + 1; i < numberOfPatients; i++) {
appointmentsArrayTemp[i - 1] = appointmentsArray[i];
appointmentsArrayTemp[i - 1].patientCode = appointmentsArrayTemp[i - 1].patientCode - 1;
}
delete[] appointmentsArray;
appointmentsArray = appointmentsArrayTemp;
RemovePatient(patientsArray, numberOfPatients, patientToDelete);
}
void Registry::ShowAllPatientAppointments(Registry *appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients) {
for (int i = 0; i < numberOfPatients; i++) {
cout << endl;
cout << "Номер пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientCode() << endl;
cout << "Ім'я пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientName() << endl;
cout << "Фамілія пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientSurname() << endl;
cout << "Вік пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientAge() << endl;
cout << "Номер телефону пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientPhone()
<< endl;
cout << endl;
cout << "Назначений лікарь: " << endl;
cout << "Ім'я лікаря: " << doctorsArray[appointmentsArray[i].doctorCode].GetDoctorName() << endl;
cout << "Фамілія лікаря: " << doctorsArray[appointmentsArray[i].doctorCode].GetDoctorSurname() << endl;
cout << "Кваліфікація лікаря: " << doctorsArray[appointmentsArray[i].doctorCode].GetDoctorQualification()
<< endl;
cout << "Дата прийому: " << appointmentsArray[appointmentsArray[i].patientCode].date.day << "."
<< appointmentsArray[appointmentsArray[i].patientCode].date.month
<< "." << appointmentsArray[appointmentsArray[i].patientCode].date.year << endl;
}
if (numberOfPatients == 0) {
cout << "Пацієнтів не існує" << endl;
}
}
void Registry::ShowDoctorAppointments(Registry *appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients, int numberOfDoctors) {
int choosedDoctor;
int foundedPatients = 0;
cout << "Оберіть номер необхідного лікаря" << endl;
for (int i = 0; i < numberOfDoctors; i++) {
cout << "Номер лікаря: " << i << endl;
cout << "Ім'я лікаря: " << doctorsArray[i].GetDoctorName() << endl;
cout << "Фамілія лікаря: " << doctorsArray[i].GetDoctorSurname() << endl;
cout << "Кваліфікація лікаря: " << doctorsArray[i].GetDoctorQualification() << endl;
cout << endl;
}
cout << "➡ ";
cin >> choosedDoctor;
while ((cin.get()) != '\n');
if (choosedDoctor > numberOfDoctors - 1 || choosedDoctor < 0) {
cout << "Лікаря з таким номером не існує" << endl;
return;
}
for (int i = 0; i < numberOfPatients; i++) {
if (doctorsArray[appointmentsArray[i].doctorCode].GetDoctorCode() == choosedDoctor) {
foundedPatients++;
cout << "Номер пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientCode() << endl;
cout << "Ім'я пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientName()
<< endl;
cout << "Фамілія пацієнта: "
<< patientsArray[appointmentsArray[i].patientCode].GetPatientSurname() << endl;
cout << "Вік пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientAge()
<< endl;
cout << "Номер телефону пацієнта: "
<< patientsArray[appointmentsArray[i].patientCode].GetPatientPhone() << endl;
cout << "Дата прийому: " << appointmentsArray[appointmentsArray[i].patientCode].date.day << "."
<< appointmentsArray[appointmentsArray[i].patientCode].date.month
<< "." << appointmentsArray[appointmentsArray[i].patientCode].date.year << endl;
cout << endl;
}
}
if (foundedPatients == 0) {
cout << "Не знайдено пацієнтів для заданого лікаря" << endl;
}
}
int Registry::GetNumberOfMonthDays(int year, int month) {
int leap = (1 - (year % 4 + 2) % (year % 4 + 1)) * ((year % 100 + 2) % (year % 100 + 1)) +
(1 - (year % 400 + 2) % (year % 400 + 1));
return 28 + ((month + (month / 8)) % 2) + 2 % month + ((1 + leap) / month) + (1 / month) - (leap / month);
}
file-class-implement.cpp
#include "hospital.h"
ostream &operator<<(ostream &os, Patient &p) {
os << p.name << ' ' << p.surname << ' ' << p.phone << ' ' << p.age << ' ' << p.patientCode << endl;
return os;
}
istream &operator>>(istream &is, Patient &p) {
is >> p.name >> p.surname >> p.phone >> p.age >> p.patientCode;
return is;
}
ostream &operator<<(ostream &os, Doctor &d) {
os << d.name << ' ' << d.surname << ' ' << d.qualification << ' ' << d.doctorCode << endl;
return os;
}
istream &operator>>(istream &is, Doctor &d) {
is >> d.name >> d.surname >> d.qualification >> d.doctorCode;
return is;
}
ostream &operator<<(ostream &os, Registry &r) {
os << r.date.day << ' ' << r.date.month << ' ' << r.date.year << ' ' << r.doctorCode << ' '
<< r.patientCode << endl;
return os;
}
istream &operator>>(istream &is, Registry &r) {
is >> r.date.day >> r.date.month >> r.date.year >> r.doctorCode >> r.patientCode;
return is;
}
void File::CheckFileExists() {
ifstream patientsFile;
ifstream numberOfPatientsFile;
ifstream doctorsFile;
ifstream numberOfDoctorsFile;
ifstream appointmentsFile;
bool patientsFileExists = false;
bool numberOfPatientsFileExists = false;
bool doctorsFileExists = false;
bool numberOfDoctorsFileExists = false;
bool appointmentsFileExists = false;
patientsFile.open(patientsFilePath);
if (patientsFile) {
patientsFileExists = true;
patientsFile.close();
}
numberOfPatientsFile.open(numberOfPatientsFilePath);
if (numberOfPatientsFile) {
numberOfPatientsFileExists = true;
numberOfPatientsFile.close();
}
doctorsFile.open(doctorsFilePath);
if (doctorsFile) {
doctorsFileExists = true;
doctorsFile.close();
}
numberOfDoctorsFile.open(numberOfDoctorsFilePath);
if (numberOfDoctorsFile) {
numberOfDoctorsFileExists = true;
numberOfDoctorsFile.close();
}
appointmentsFile.open(appointmentsFilePath);
if (appointmentsFile) {
appointmentsFileExists = true;
appointmentsFile.close();
}
if (patientsFileExists == false || numberOfPatientsFileExists == false || doctorsFileExists == false ||
numberOfDoctorsFileExists == false || appointmentsFileExists == false) {
cout << "Необхідних файлів не існує або файловій системі відбувся збій, всі данні будуть видалені" << endl;
CreateFile();
}
}
void File::CreateFile() {
ofstream patientsFile;
ofstream numberOfPatientsFile;
ofstream doctorsFile;
ofstream numberOfDoctorsFile;
ofstream appointmentsFile;
patientsFile.open(patientsFilePath.c_str(), ios::binary | ios::out);
patientsFile.close();
numberOfPatientsFile.open(numberOfPatientsFilePath.c_str(), ios::binary | ios::out);
numberOfPatientsFile.close();
doctorsFile.open(doctorsFilePath.c_str(), ios::binary | ios::out);
numberOfDoctorsFile.open(numberOfDoctorsFilePath.c_str(), ios::binary | ios::out);
appointmentsFile.open(appointmentsFilePath.c_str(), ios::binary | ios::out);
}
void File::DeleteFile() {
remove(patientsFilePath.c_str());
remove(numberOfPatientsFilePath.c_str());
remove(doctorsFilePath.c_str());
remove(numberOfDoctorsFilePath.c_str());
remove(appointmentsFilePath.c_str());
}
void File::ReadFile(Patient *patientsArray, int &numberOfPatients, Doctor *doctorsArray, int &numberOfDoctors,
Registry *appointmentsArray) {
ifstream patientsFile;
ifstream numberOfPatientsFile;
ifstream doctorsFile;
ifstream numberOfDoctorsFile;
ifstream appointmentsFile;
numberOfPatientsFile.open(numberOfPatientsFilePath.c_str());
if (!numberOfPatientsFile) {
cout << "Помилка при відкритті файлу numberOfPatients";
exit(1);
}
numberOfPatientsFile.read((char *) &numberOfPatients, sizeof(numberOfPatients));
numberOfPatientsFile.close();
patientsFile.open(patientsFilePath.c_str());
if (!patientsFile) {
cout << "Помилка при відкритті файлу patients";
exit(1);
}
for (int i = 0; i < numberOfPatients; i++) {
patientsFile >> patientsArray[i];
}
patientsFile.close();
numberOfDoctorsFile.open(numberOfDoctorsFilePath.c_str());
if (!numberOfDoctorsFile) {
cout << "Помилка при відкритті файлу numberOfDoctors";
exit(1);
}
numberOfDoctorsFile.read((char *) &numberOfDoctors, sizeof(numberOfDoctors));
numberOfDoctorsFile.close();
doctorsFile.open(doctorsFilePath.c_str());
if (!doctorsFile) {
cout << "Помилка при відкритті файлу doctors";
exit(1);
}
for (int i = 0; i < numberOfDoctors; i++) {
doctorsFile >> doctorsArray[i];
}
doctorsFile.close();
appointmentsFile.open(appointmentsFilePath.c_str());
if (!appointmentsFile) {
cout << "Помилка при відкритті файлу appointments";
exit(1);
}
for (int i = 0; i < numberOfPatients; i++) {
appointmentsFile >> appointmentsArray[i];
}
appointmentsFile.close();
}
void File::RewriteFile(Patient *patientsArray, int &numberOfPatients, Doctor *doctorsArray, int &numberOfDoctors,
Registry *appointmentsArray) {
ofstream patientsFile;
ofstream numberOfPatientsFile;
ofstream doctorsFile;
ofstream numberOfDoctorsFile;
ofstream appointmentsFile;
numberOfPatientsFile.open(numberOfPatientsFilePath.c_str());
if (!numberOfPatientsFile) {
cout << "Помилка при відкритті файлу numberOfPatients";
exit(1);
}
numberOfPatientsFile.write((char *) &numberOfPatients, sizeof(numberOfPatients));
numberOfPatientsFile.close();
patientsFile.open(patientsFilePath.c_str());
if (!patientsFile) {
cout << "Помилка при відкритті файлу patients";
exit(1);
}
for (int i = 0; i < numberOfPatients; i++) {
patientsFile << patientsArray[i];
}
patientsFile.close();
numberOfDoctorsFile.open(numberOfDoctorsFilePath.c_str());
if (!numberOfDoctorsFile) {
cout << "Помилка при відкритті файлу numberOfDoctors";
exit(1);
}
numberOfDoctorsFile.write((char *) &numberOfDoctors, sizeof(numberOfDoctors));
numberOfDoctorsFile.close();
doctorsFile.open(doctorsFilePath.c_str());
if (!doctorsFile) {
cout << "Помилка при відкритті файлу doctors";
exit(1);
}
for (int i = 0; i < numberOfDoctors; i++) {
doctorsFile << doctorsArray[i];
}
doctorsFile.close();
appointmentsFile.open(appointmentsFilePath.c_str());
if (!appointmentsFile) {
cout << "Помилка при відкритті файлу appointments";
exit(1);
}
for (int i = 0; i < numberOfPatients; i++) {
appointmentsFile << appointmentsArray[i];
}
appointmentsFile.close();
}
Hi all, I'm doing a kind of "Registrar's Office for the hospital". I can't read the file or write the file normally (I don't know exactly what the problem is).
When I add patients, at first everything seems to go fine, but then when I add 3 patients, and display all of them on the screen, there is just nothing in the name field, starting from 3 patients.
Please help me. I haven't understood what the problem is for a few days now.