-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHospital.c
299 lines (273 loc) · 8.33 KB
/
Hospital.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 100
#define NAME_LENGTH 50
typedef struct {
int id;
char name[NAME_LENGTH];
int age;
char gender;
char disease[NAME_LENGTH];
} Patient;
typedef struct {
int id;
char name[NAME_LENGTH];
char specialization[NAME_LENGTH];
int experience; // in years
} Doctor;
Patient patients[MAX_RECORDS];
Doctor doctors[MAX_RECORDS];
int patientCount = 0;
int doctorCount = 0;
// Function declarations
void addPatient();
void viewPatients();
void deletePatient();
void addDoctor();
void viewDoctors();
void deleteDoctor();
void searchPatient();
void searchDoctor();
void saveRecords();
void loadRecords();
int main() {
loadRecords();
int choice;
do {
printf("\nPES University Hospital Management System\n");
printf("1. Add Patient\n");
printf("2. View Patients\n");
printf("3. Delete Patient\n");
printf("4. Add Doctor\n");
printf("5. View Doctors\n");
printf("6. Delete Doctor\n");
printf("7. Search Patient\n");
printf("8. Search Doctor\n");
printf("9. Save Records\n");
printf("10. Load Records\n");
printf("11. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: addPatient(); break;
case 2: viewPatients(); break;
case 3: deletePatient(); break;
case 4: addDoctor(); break;
case 5: viewDoctors(); break;
case 6: deleteDoctor(); break;
case 7: searchPatient(); break;
case 8: searchDoctor(); break;
case 9: saveRecords(); break;
case 10: loadRecords(); break;
case 11: printf("Exiting...\n"); break;
default: printf("Invalid choice! Please try again.\n");
}
} while(choice != 11);
return 0;
}
void addPatient() {
if (patientCount >= MAX_RECORDS) {
printf("Cannot add more patients. Storage full.\n");
return;
}
Patient p;
p.id = patientCount + 1;
printf("Enter patient name: ");
scanf(" %[^\n]%*c", p.name);
printf("Enter age: ");
scanf("%d", &p.age);
printf("Enter gender (M/F): ");
scanf(" %c", &p.gender);
printf("Enter disease: ");
scanf(" %[^\n]%*c", p.disease);
patients[patientCount++] = p;
printf("Patient added successfully!\n");
}
void viewPatients() {
if (patientCount == 0) {
printf("No patient records found.\n");
return;
}
for (int i = 0; i < patientCount; i++) {
printf("\nPatient ID: %d\nName: %s\nAge: %d\nGender: %c\nDisease: %s\n", patients[i].id, patients[i].name, patients[i].age, patients[i].gender, patients[i].disease);
}
}
void deletePatient() {
int id;
printf("Enter patient ID to delete: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < patientCount; i++) {
if (patients[i].id == id) {
found = 1;
for (int j = i; j < patientCount - 1; j++) {
patients[j] = patients[j + 1];
}
patientCount--;
printf("Patient record deleted successfully.\n");
break;
}
}
if (!found) {
printf("Patient ID not found.\n");
}
}
void addDoctor() {
if (doctorCount >= MAX_RECORDS) {
printf("Cannot add more doctors. Storage full.\n");
return;
}
Doctor d;
d.id = doctorCount + 1;
printf("Enter doctor name: ");
scanf(" %[^\n]%*c", d.name);
printf("Enter specialization: ");
scanf(" %[^\n]%*c", d.specialization);
printf("Enter experience (in years): ");
scanf("%d", &d.experience);
doctors[doctorCount++] = d;
printf("Doctor added successfully!\n");
}
void viewDoctors() {
if (doctorCount == 0) {
printf("No doctor records found.\n");
return;
}
for (int i = 0; i < doctorCount; i++) {
printf("\nDoctor ID: %d\nName: %s\nSpecialization: %s\nExperience: %d years\n", doctors[i].id, doctors[i].name, doctors[i].specialization, doctors[i].experience);
}
}
void deleteDoctor() {
int id;
printf("Enter doctor ID to delete: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < doctorCount; i++) {
if (doctors[i].id == id) {
found = 1;
for (int j = i; j < doctorCount - 1; j++) {
doctors[j] = doctors[j + 1];
}
doctorCount--;
printf("Doctor record deleted successfully.\n");
break;
}
}
if (!found) {
printf("Doctor ID not found.\n");
}
}
void searchPatient() {
int choice;
printf("Search patient by:\n1. Name\n2. ID\nEnter choice: ");
scanf("%d", &choice);
if (choice == 1) {
char name[NAME_LENGTH];
printf("Enter patient name: ");
scanf(" %[^\n]%*c", name);
int found = 0;
for (int i = 0; i < patientCount; i++) {
if (strcmp(patients[i].name, name) == 0) {
printf("\nPatient ID: %d\nName: %s\nAge: %d\nGender: %c\nDisease: %s\n", patients[i].id, patients[i].name, patients[i].age, patients[i].gender, patients[i].disease);
found = 1;
}
}
if (!found) {
printf("Patient not found.\n");
}
} else if (choice == 2) {
int id;
printf("Enter patient ID: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < patientCount; i++) {
if (patients[i].id == id) {
printf("\nPatient ID: %d\nName: %s\nAge: %d\nGender: %c\nDisease: %s\n", patients[i].id, patients[i].name, patients[i].age, patients[i].gender, patients[i].disease);
found = 1;
}
}
if (!found) {
printf("Patient not found.\n");
}
} else {
printf("Invalid choice.\n");
}
}
void searchDoctor() {
int choice;
printf("Search doctor by:\n1. Name\n2. ID\nEnter choice: ");
scanf("%d", &choice);
if (choice == 1) {
char name[NAME_LENGTH];
printf("Enter doctor name: ");
scanf(" %[^\n]%*c", name);
int found = 0;
for (int i = 0; i < doctorCount; i++) {
if (strcmp(doctors[i].name, name) == 0) {
printf("\nDoctor ID: %d\nName: %s\nSpecialization: %s\nExperience: %d years\n", doctors[i].id, doctors[i].name, doctors[i].specialization, doctors[i].experience);
found = 1;
}
}
if (!found) {
printf("Doctor not found.\n");
}
} else if (choice == 2) {
int id;
printf("Enter doctor ID: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < doctorCount; i++) {
if (doctors[i].id == id) {
printf("\nDoctor ID: %d\nName: %s\nSpecialization: %s\nExperience: %d years\n", doctors[i].id, doctors[i].name, doctors[i].specialization, doctors[i].experience);
found = 1;
}
}
if (!found) {
printf("Doctor not found.\n");
}
} else {
printf("Invalid choice.\n");
}
}
void saveRecords() {
FILE *fp;
fp = fopen("patients.dat", "wb");
if (fp == NULL) {
printf("Error saving patient records.\n");
return;
}
fwrite(&patientCount, sizeof(int), 1, fp);
fwrite(patients, sizeof(Patient), patientCount, fp);
fclose(fp);
fp = fopen("doctors.dat", "wb");
if (fp == NULL) {
printf("Error saving doctor records.\n");
return;
}
fwrite(&doctorCount, sizeof(int), 1, fp);
fwrite(doctors, sizeof(Doctor), doctorCount, fp);
fclose(fp);
printf("Records saved successfully.\n");
}
void loadRecords() {
FILE *fp;
fp = fopen("patients.dat", "rb");
if (fp != NULL) {
fread(&patientCount, sizeof(int), 1, fp);
fread(patients, sizeof(Patient), patientCount, fp);
fclose(fp);
printf("Patient records loaded successfully.\n");
} else {
printf("Error: Unable to load patient records.\n");
}
fp = fopen("doctors.dat", "rb");
if (fp != NULL) {
fread(&doctorCount, sizeof(int), 1, fp);
fread(doctors, sizeof(Doctor), doctorCount, fp);
fclose(fp);
printf("Doctor records loaded successfully.\n");
} else {
printf("Error: Unable to load doctor records.\n");
}
}