-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqn1.c
128 lines (115 loc) · 5.12 KB
/
qn1.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
/*This source code was prepared by:
- Emmanuel Limisi
- Kristian Onjala
- Joshua Ndirangu
- George Williams
- Peter Ngatia
And is under the MIT license */
#include <stdio.h> // used for printf, scanf
#include <stdlib.h> // used for exit, realloc, calloc and free
#include <string.h>// used for strlen, strcpy
struct Patient{
char *name;
int id;
char *medical_history;
};
void add_patient(struct Patient *patient_array[]){
static unsigned int patient_count = 0;
char medical_history_buffer[10000];
char name_buffer[100];
patient_array[patient_count] = calloc(1, sizeof(struct Patient));//allocate memory for new patient
if (patient_array[patient_count] == NULL) exit(EXIT_FAILURE);// Error handling, checking for failed allocation
printf("Enter patient's name:\n");
scanf(" %[^\n]",name_buffer);
patient_array[patient_count]->name = calloc(1, strlen(name_buffer) + 1); //allocate memory for patient's name
if (patient_array[patient_count]->name == NULL) exit(EXIT_FAILURE);// Error handling by exiting the program if memory allocation failed
strcpy(patient_array[patient_count]->name, name_buffer);
printf("Enter patient's ID:\n");
if (scanf("%d", &(patient_array[patient_count]->id)) == 0) exit(EXIT_FAILURE);// Error handling by checking for failed allocation
printf("Enter patient's medical history:\n");
scanf(" %[^\n]",medical_history_buffer);
patient_array[patient_count]->medical_history = calloc(1, strlen(medical_history_buffer) + 1); //allocate memory for patient's medical history
if (patient_array[patient_count]->medical_history == NULL) exit(EXIT_FAILURE);//Program exits if memory allocation fails
strcpy(patient_array[patient_count]->medical_history, medical_history_buffer);
printf("\n");
patient_count++;
}
void print_all_records(struct Patient *patient_array[]){
for(int i = 0; patient_array[i] != NULL; i++){
printf("Patient %d\n", i + 1);
if(patient_array[i]->id == 0) printf("Discharged\n");
else {
printf("Name: %s\n", patient_array[i]->name);
printf("ID: %d\n", patient_array[i]->id);
printf("Medical History: %s\n", patient_array[i]->medical_history);
}
printf("=================================\n");
}
}
void discharge_patient(struct Patient *patient_array[]){
int found = 0;
int patient_id;
printf("Enter patient's ID\n");
if (scanf("%d", &patient_id) == 0) exit(EXIT_FAILURE);
for(int i = 0; patient_array[i] != NULL; i++){
if (patient_array[i]->id == patient_id){
found = 1;
patient_array[i]->id = 0;
free(patient_array[i]->name);//free memory previously used for patient's name
free(patient_array[i]->medical_history);//free memory previously used for patient's medical history
printf("Patient successfully discharged\n");
}
}
if (found == 0) printf("The patient's ID does not exist\n");
printf("\n");
}
void update_patient_details(struct Patient *patient_array[]){
int patient_id;
int found = 0;
char medical_history_buffer[10000];
printf("Enter patient's ID\n");
if (scanf("%d", &patient_id) == 0) exit(EXIT_FAILURE);
for(int i = 0; patient_array[i] != NULL; i++){
if (patient_array[i]->id == patient_id){
found = 1;
printf("Enter new medical history\n");
scanf(" %[^\n]",medical_history_buffer);
patient_array[i]->medical_history = realloc(patient_array[i]->medical_history, strlen(medical_history_buffer) + 1);//reallocate memory for new size of patient's medical history
if (patient_array[i]->medical_history == NULL) exit(EXIT_FAILURE);//program exits if realloc fails
strcpy(patient_array[i]->medical_history, medical_history_buffer);
printf("Medical history successfully updated\n");
}
}
if (found == 0) printf("The patient's ID does not exist\n");
printf("\n");
}
void free_patient_array_data(struct Patient *patient_array[]){
for(int i = 0; patient_array[i] != NULL && patient_array[i]->id != 0; i++){ // The && is used to prevent a double free when the patient is discharged
free(patient_array[i]->name);
free(patient_array[i]->medical_history);
}
}
int main(void){
int answer;
struct Patient **patient_array = calloc(100, sizeof(struct Patient *));//calloc is used to ensure zero initialization
printf("Welcome to the Health Management System\n");
printf("Created by EL - 0698, KO - 0704, JN - 1268, GW - 0744, PN - 0729\n");
while(answer != 5){
printf("What do you want to do ?\n");
printf("(1) View all patient's records\t (2) Add a patient\n");
printf("(3) Update a patient's history\t (4) Discharge a patient\n");
printf("(5) Exit\n");
if (scanf("%d", &answer) == 0) answer = 5;
printf("\n");
switch (answer){
case 1: print_all_records(patient_array); break;
case 2: add_patient(patient_array); break;
case 3: update_patient_details(patient_array); break;
case 4: discharge_patient(patient_array); break;
case 5: answer = 5; break;
}
}
free_patient_array_data(patient_array);//clean up to ensure there is no memory leak when the program exits successfully
free(patient_array);//clean up to ensure there is no memory leak when the program exits successfully
return 0;
}