0% found this document useful (0 votes)
5 views

The_array_function[1]

The document defines data structures for managing patients, appointments, and doctors in a healthcare system. It includes functions to add, update, search, delete, and sort records for patients, appointments, and doctors, utilizing binary file operations for data storage. The main function provides a menu-driven interface for user interaction with these functionalities.

Uploaded by

gideynegasi3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

The_array_function[1]

The document defines data structures for managing patients, appointments, and doctors in a healthcare system. It includes functions to add, update, search, delete, and sort records for patients, appointments, and doctors, utilizing binary file operations for data storage. The main function provides a menu-driven interface for user interaction with these functionalities.

Uploaded by

gideynegasi3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

struct Patient {

int id;

string name;

string address;

string phone;

string email;

string medical_history;

};

struct Appointment {

int id;

int patient_id;

string doctor_name;

string date;

string time;

string reason;

};

struct Doctor {

int id;

string name;

string specialty;

string availability;

};
void add_patient(Patient patient) {

// Open the database file

fstream file("patients.dat", ios::in | ios::out | ios::binary);

// Move to the end of the file

file.seekg(0, ios::end);

// Get the current position in the file

int position = file.tellg();

// Write the patient's information to the file

file.write((char*)&patient, sizeof(patient));

// Close the database file

file.close();

void update_patient(Patient patient) {

// Open the database file

fstream file("patients.dat", ios::in | ios::out | ios::binary);

// Move to the patient's record in the file

file.seekg((patient.id - 1) * sizeof(patient), ios::beg);

// Write the patient's information to the file


file.write((char*)&patient, sizeof(patient));

// Close the database file

file.close();

Patient search_patient_by_id(int id) {

// Open the database file

fstream file("patients.dat", ios::in | ios::out | ios::binary);

// Move to the patient's record in the file

file.seekg((id - 1) * sizeof(Patient), ios::beg);

// Read the patient's information from the file

Patient patient;

file.read((char*)&patient, sizeof(patient));

// Close the database file

file.close();

// Return the patient's information

return patient;

void delete_patient(int id) {

// Open the database file

fstream file("patients.dat", ios::in | ios::out | ios::binary);


// Move to the patient's record in the file

file.seekg((id - 1) * sizeof(Patient), ios::beg);

// Read the patient's information from the file

Patient patient;

file.read((char*)&patient, sizeof(patient));

// Overwrite the patient's record with empty data

file.seekp((id - 1) * sizeof(Patient), ios::beg);

file.write((char*)&patient, sizeof(patient));

// Close the database file

file.close();

vector<Patient> sort_patients_by_name() {

// Open the database file

fstream file("patients.dat", ios::in | ios::out | ios::binary);

// Read all the patients from the file into a vector

vector<Patient> patients;

Patient patient;

while (file.read((char*)&patient, sizeof(patient))) {

patients.push_back(patient);

}
// Close the database file

file.close();

// Sort the patients by name

std::sort(patients.begin(), patients.end(), [](const Patient& a, const Patient& b) {

return a.name < b.name;

});

// Return the sorted patients

return patients;

void add_appointment(Appointment appointment) {

// Open the database file

fstream file("appointments.dat", ios::in | ios::out | ios::binary);

// Move to the end of the file

file.seekg(0, ios::end);

// Get the current position in the file

int position = file.tellg();

// Write the appointment's information to the file

file.write((char*)&appointment, sizeof(appointment));
// Close the database file

file.close();

void update_appointment(Appointment appointment) {

// Open the database file

fstream file("appointments.dat", ios::in | ios::out | ios::binary);

// Move to the appointment's record in the file

file.seekg((appointment.id - 1) * sizeof(appointment), ios::beg);

// Write the appointment's information to the file

file.write((char*)&appointment, sizeof(appointment));

// Close the database file

file.close();

Appointment search_appointment_by_id(int id) {

// Open the database file

fstream file("appointments.dat", ios::in | ios::out | ios::binary);

// Move to the appointment's record in the file

file.seekg((id - 1) * sizeof(Appointment), ios::beg);

// Read the appointment's information from the file


Appointment appointment;

file.read((char*)&appointment, sizeof(appointment));

// Close the database file

file.close();

// Return the appointment's information

return appointment;

void delete_appointment(int id) {

// Open the database file

fstream file("appointments.dat", ios::in | ios::out | ios::binary);

// Move to the appointment's record in the file

file.seekg((id - 1) * sizeof(Appointment), ios::beg);

// Read the appointment's information from the file

Appointment appointment;

file.read((char*)&appointment, sizeof(appointment));

// Overwrite the appointment's record with empty data

file.seekp((id - 1) * sizeof(Appointment), ios::beg);

file.write((char*)&appointment, sizeof(appointment));

// Close the database file


file.close();

vector<Appointment> sort_appointments_by_date() {

// Open the database file

fstream file("appointments.dat", ios::in | ios::out | ios::binary);

// Read all the appointments from the file into a vector

vector<Appointment> appointments;

Appointment appointment;

while (file.read((char*)&appointment, sizeof(appointment))) {

appointments.push_back(appointment);

// Close the database file

file.close();

// Sort the appointments by date

std::sort(appointments.begin(), appointments.end(), [](const Appointment& a, const Appointment&


b) {

return a.date < b.date;

});

// Return the sorted appointments

return appointments;

void add_doctor(Doctor doctor) {


// Open the database file

fstream file("doctors.dat", ios::in | ios::out | ios::binary);

// Move to the end of the file

file.seekg(0, ios::end);

// Get the current position in the file

int position = file.tellg();

// Write the doctor's information to the file

file.write((char*)&doctor, sizeof(doctor));

// Close the database file

file.close();

void update_doctor(Doctor doctor) {

// Open the database file

fstream file("doctors.dat", ios::in | ios::out | ios::binary);

// Move to the doctor's record in the file

file.seekg((doctor.id - 1) * sizeof(doctor), ios::beg);

// Write the doctor's information to the file

file.write((char*)&doctor, sizeof(doctor));
// Close the database file

file.close();

Doctor search_doctor_by_id(int id) {

// Open the database file

fstream file("doctors.dat", ios::in | ios::out | ios::binary);

// Move to the doctor's record in the file

file.seekg((id - 1) * sizeof(Doctor), ios::beg);

// Read the doctor's information from the file

Doctor doctor;

file.read((char*)&doctor, sizeof(doctor));

// Close the database file

file.close();

// Return the doctor's information

return doctor;

void delete_doctor(int id) {

// Open the database file

fstream file("doctors.dat", ios::in | ios::out | ios::binary);

// Move to the doctor's record in the file


file.seekg((id - 1) * sizeof(Doctor), ios::beg);

// Read the doctor's information from the file

Doctor doctor;

file.read((char*)&doctor, sizeof(doctor));

// Overwrite the doctor's record with empty data

file.seekp((id - 1) * sizeof(Doctor), ios::beg);

file.write((char*)&doctor, sizeof(doctor));

// Close the database file

file.close();

vector<Doctor> sort_doctors_by_name() {

// Open the database file

fstream file("doctors.dat", ios::in | ios::out | ios::binary);

// Read all the doctors from the file into a vector

vector<Doctor> doctors;

Doctor doctor;

while (file.read((char*)&doctor, sizeof(doctor))) {

doctors.push_back(doctor);

}
// Close the database file

file.close();

// Sort the doctors by name

std::sort(doctors.begin(), doctors.end(), [](const Doctor& a, const Doctor& b) {

return a.name < b.name;

});

// Return the sorted doctors

return doctors;

// Array of patients

Patient patients[100];

// Array of appointments

Appointment appointments[100];

// Array of doctors

Doctor doctors[100];

int main() {

// Display a menu of options to the user

while (true) {

cout << "1. Add a new patient" << endl;

cout << "2. Update a patient's information" << endl;

cout << "3. Search for a patient by ID" << endl;

cout << "4. Delete a patient from the database" << endl;

cout << "5. Sort patients by name" << endl;


cout << "6. Add a new appointment" << endl;

cout << "7. Update an appointment's information" << endl;

cout << "8. Search for an appointment by ID" << endl;

cout << "9. Delete an appointment from the database" << endl;

cout << "10. Sort appointments by date" << endl;

cout << "11. Add a new doctor" << endl;

cout << "12. Update a doctor's information" << endl;

cout << "13. Search for a doctor by ID" << endl;

cout << "14. Delete a doctor from the database" << endl;

cout << "15. Sort doctors by name" << endl;

cout << "16. Quit" << endl;

// Get the user's choice

int choice;

cin >> choice;

// Call the appropriate function based on the user's choice

switch (choice) {

case 1:

add_patient();

break;

case 2:

update_patient();

break;

case 3:
search_patient_by_id();

break;

case 4:

delete_patient();

break;

case 5:

sort_patients_by_name();

break;

case 6:

add_appointment();

break;

case 7:

update_appointment();

break;

case 8:

search_appointment_by_id();

break;

case 9:

delete_appointment();

break;

case 10:

sort_appointments_by_date();

break;

case 11:
add_doctor();

break;

case 12:

update_doctor();

break;

case 13:

search_doctor_by_id();

break;

case 14:

delete_doctor();

break;

case 15:

sort_doctors_by_name();

break;

case 16:

quit();

break;

default:

cout << "Invalid choice" << endl;

return 0;

You might also like