-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.c
More file actions
154 lines (131 loc) · 3.52 KB
/
database.c
File metadata and controls
154 lines (131 loc) · 3.52 KB
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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "database.h"
#include "inout.h"
void p_init(struct aPatient *table, int *number){ //we introduced in the current table the past patients
char line[256];
FILE *file;
file=fopen("patients.txt","r");
if (file!=NULL){
while ((fgets (line, sizeof(line), file)) != NULL){ //we pass the content of the file to the string line
sscanf(line,"%s %s %d %d %d %c",
table[*number].name,table[*number].dni,&table[*number].age,&table[*number].fever,&table[*number].cough,&table[*number].symptom);
(*number)++;
}
fclose (file);
}
}
void p_register(struct aPatient *table, int *number){
char name[25];
char dni[10];
printf("\nRegister\n");
get_string("\nName",name,1,24);
strcpy(table[*number].name,name);//save the name in table
get_string("\nDNI",dni,9,9);
while(verify_DNI(dni)==0){
printf("\nInvalid DNI\n");
get_string("\nDNI",dni,9,9);
}
strcpy(table[*number].dni,dni);//save the DNI in table
table[*number].age=get_integer(1900,2020,"\nDate");
table[*number].fever=yes_or_no("\nFever (y/n): ");
table[*number].cough=yes_or_no("\nCough (y/n): ");
table[*number].symptom=get_character("FSTMN","\nSymptom");
printf("\nNew patient:\n");
display_patient(table[*number]);// number initialized to 0 in divoc
(*number)++;//next patient
}
void p_search(struct aPatient *table, int *number){
int i,eq=0;
char dni[10];//dni introduced by the user
printf("\nSearch\n");
if(*number==0)
printf("\nNo patients yet\n");
else{
get_string("\nDNI",dni,9,9);
for(i=0;i<(*number);i++){
if(strcmp(dni,table[i].dni)==0){//0 if they are equals
eq=1;
break;
}
}
if(eq==0)
printf("\nUnknown patient\n");
else{
printf("\nPatient data:\n");
display_patient(table[i]);
}
}
}
void p_discharge(struct aPatient *table, int *number){
int i,k,eq=0;
char dni[10];//dni enter by the user
printf("\nDischarge\n");
if(*number==0)
printf("\nNo patients yet\n");
else{
get_string("\nDNI",dni,9,9);
for(i=0;i<(*number);i++){
if(strcmp(dni,table[i].dni)==0){//0 if they are equals
for(k=i;k<(*number)-1;k++){
table[k]=table[k+1];
}
(*number)--;
eq=1;
break;
}
}
if(eq==0)
printf("\nUnknown patient\n");
else
printf("\nDischarged patient\n");
}
}
void p_list(struct aPatient *table, int *number){
int i,year;
printf("\nList\n");
if(*number==0){
printf("\nNo patients yet\n");
}
else{
year=get_integer(1900,2020,"\nDate");
printf("\nPatients born before %d:\n",year);
for(i=0;i<(*number);i++){
if(table[i].age<=year){
display_patient(table[i]);
}
}
}
return;
}
void p_mark(struct aPatient *table, int *number){
int i;
printf("\nPositives\n");
if(*number==0)
printf("\nNo patients yet\n");
else{
printf("\nPositive patients:\n");
for(i=0;i<*number;i++){
if ((table[i].fever==1)&&(table[i].cough==1)&&(table[i].symptom!='N'))
display_patient(table[i]);
}
}
}
int p_exit(struct aPatient *table, int *number){
printf("\nExit\n");
if (yes_or_no("\nAre you sure you want to exit the program? (y/n): ")==1){
int i=0;
FILE *file;
file=fopen("patients.txt","w");
while (i<*number) {//pass to the file all the patients
fprintf(file, "%s %s %d %d %d %c\n",table[i].name, table[i].dni, table[i].age, table[i].fever, table[i].cough, table[i].symptom);
i++;
}
fclose(file);
return 1;
}
else
return 0;
}