-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·158 lines (144 loc) · 5.28 KB
/
main.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
#include <stdio.h>
#include <getopt.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define all_string_maxsize 30
#define child_num 5
struct home_cure {
char surname_distr_doc[all_string_maxsize];
};
struct hosp_cure {
char hosp_num[all_string_maxsize];
char hosp_address[all_string_maxsize];
char surname_hosp_doc[all_string_maxsize];
};
struct info {
char child_surname[all_string_maxsize];
int hospital_cure;
char last_illness[all_string_maxsize];
union patient_or_no {
struct home_cure home;
struct hosp_cure hospital;
} ch_union;
};
int name_cmp(const void *a, const void *b)
{
return strcmp((*(struct info *) a).child_surname,
(*(struct info *) b).child_surname);
}
void readme(void)
{
puts("Usage: ./main [mode]");
puts(" -p print data from database");
puts(" -l <illness> alphabetically sort list of patients with");
puts(" this illness");
puts(" -h help");
}
void print_alphabet(struct info *child, char *illness, int child_num_typed)
{
int i;
printf("Patients with %s:\n", illness);
for (i = 0; i < child_num_typed; i++)
if (!strcmp(illness, child[i].last_illness)) {
if (child[i].hospital_cure)
printf
("%s, hospital #%s, address: %s, doctor: %s, illness: %s\n",
child[i].child_surname,
child[i].ch_union.hospital.hosp_num,
child[i].ch_union.hospital.hosp_address,
child[i].ch_union.hospital.surname_hosp_doc,
child[i].last_illness);
else
printf("%s, doctor: %s, illness: %s\n",
child[i].child_surname,
child[i].ch_union.home.surname_distr_doc,
child[i].last_illness);
}
}
void print_database(struct info *child, int child_num_typed)
{
int i;
puts("Database:");
for (i = 0; i < child_num_typed; i++)
if (child[i].hospital_cure)
printf
("%d: %s, hospital #%s, address: %s, doctor: %s, illness: %s\n",
i, child[i].child_surname,
child[i].ch_union.hospital.hosp_num,
child[i].ch_union.hospital.hosp_address,
child[i].ch_union.hospital.surname_hosp_doc,
child[i].last_illness);
else
printf("%d: %s, district doctor: %s, illness: %s\n", i,
child[i].child_surname,
child[i].ch_union.home.surname_distr_doc,
child[i].last_illness);
}
int init_database(struct info *child)
{
int child_num_typed = 5;
strncpy(child[0].child_surname, "Alehin", all_string_maxsize);
strncpy(child[0].last_illness, "anemia", all_string_maxsize);
child[0].hospital_cure = 1;
strncpy(child[0].ch_union.hospital.hosp_num, "12", all_string_maxsize);
strncpy(child[0].ch_union.hospital.hosp_address, "Kalinowski street",
all_string_maxsize);
strncpy(child[0].ch_union.hospital.surname_hosp_doc, "Bobr",
all_string_maxsize);
strncpy(child[1].child_surname, "Ivanov", all_string_maxsize);
strncpy(child[1].last_illness, "pneumonia", all_string_maxsize);
child[1].hospital_cure = 0;
strncpy(child[1].ch_union.home.surname_distr_doc, "Kashliak",
all_string_maxsize);
strncpy(child[2].child_surname, "Sidorov", all_string_maxsize);
strncpy(child[2].last_illness, "tetanus", all_string_maxsize);
child[2].hospital_cure = 1;
strncpy(child[2].ch_union.hospital.hosp_num, "5", all_string_maxsize);
strncpy(child[2].ch_union.hospital.hosp_address, "Zapadnaya street",
all_string_maxsize);
strncpy(child[2].ch_union.hospital.surname_hosp_doc, "Maksimov",
all_string_maxsize);
strncpy(child[3].child_surname, "Petrov", all_string_maxsize);
strncpy(child[3].last_illness, "anemia", all_string_maxsize);
child[3].hospital_cure = 0;
strncpy(child[3].ch_union.home.surname_distr_doc, "Maksimova",
all_string_maxsize);
strncpy(child[4].child_surname, "Aleain", all_string_maxsize);
strncpy(child[4].last_illness, "tetanus", all_string_maxsize);
child[4].hospital_cure = 1;
strncpy(child[4].ch_union.hospital.hosp_num, "4", all_string_maxsize);
strncpy(child[4].ch_union.hospital.hosp_address, "West street",
all_string_maxsize);
strncpy(child[4].ch_union.hospital.surname_hosp_doc, "Melnova",
all_string_maxsize);
return child_num_typed;
}
int main(int argc, char *argv[])
{
const char *options = "pl:h";
int opt = 0, child_num_typed;
struct info child[child_num];
child_num_typed = init_database(child);
qsort(child, child_num_typed, sizeof(struct info), name_cmp);
opt = getopt(argc, argv, options);
if (opt == -1)
readme();
while (opt != -1) {
switch (opt) {
case 'p':
print_database(child, child_num_typed);
break;
case 'l':
print_alphabet(child, optarg, child_num_typed);
break;
case 'h':
readme();
break;
default:
break;
}
opt = getopt(argc, argv, options);
}
return 0;
}