-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
163 lines (153 loc) · 4.27 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
159
160
161
162
163
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdio_ext.h>
#define max_length 30
#define max_base 20
struct kindergarden
{
char *surname, *name;
int hosp_identify;
union
{
struct
{
char *illness, *doc;
} nonhosp;
struct
{
char *illness, *hospnum, *hospplace, *hospdoc;
} hosp;
} hospital;
};
int init_array(struct kindergarden*);
char* check_string(char*);
void sort_array(struct kindergarden*, int);
void search_illness(struct kindergarden*, int, char**);
int atoi_change();
void check_help(int, char**);
int main (int argc, char **argv)
{
int array_size;
struct kindergarden *children;
children = (struct kindergarden*)malloc(max_base*sizeof(struct kindergarden));
check_help(argc, argv);
array_size = init_array(children);
sort_array(children, array_size);
search_illness(children, array_size, argv);
free(children);
return 0;
}
int init_array(struct kindergarden* new_kids_base)
{
int n=0, identifier;
do
{
new_kids_base[n].surname = check_string("Enter a child's surname: ");
new_kids_base[n].name = check_string("Enter a child's name: ");
printf("Was a child hospitalized?");
identifier = atoi_change();
if(identifier)
{
new_kids_base[n].hosp_identify = 1;
new_kids_base[n].hospital.hosp.illness = check_string("Enter an illness: ");
new_kids_base[n].hospital.hosp.hospnum = check_string("Enter the number of hospital: ");
new_kids_base[n].hospital.hosp.hospplace = check_string("Enter the situation of hospital: ");
new_kids_base[n].hospital.hosp.hospdoc = check_string("Enter a hospital doc's surname: ");
}
else
{
new_kids_base[n].hosp_identify = 0;
new_kids_base[n].hospital.nonhosp.illness = check_string("Enter an illness: ");
new_kids_base[n].hospital.nonhosp.doc = check_string("Enter a doc's surname: ");
};
identifier = 0;
if(n<max_base-1)
{
printf("Want to continue?");
identifier = atoi_change();
if (!identifier) { break; }
}
n++;
} while (n<max_base);
return n;
}
int atoi_change()
{
int identifier;
char string[128];
while(1)
{
printf("(yes - 1/no - 0) ");
fgets(string, 128, stdin);
identifier = atoi(string);
if(identifier == 1 || identifier == 0) { break; }
puts("It's not correct. Please, input data again.");
}
return identifier;
}
char* check_string(char* comment)
{
char *text;
text = (char*)malloc(max_length*sizeof(char));
do
{
printf("%s", comment);
__fpurge(stdin);
gets(text);
if(text[0] == '\n') { continue; }
} while (0);
return text;
}
void sort_array(struct kindergarden* kids_base, int array_size)
{
struct kindergarden temp;
int i, j;
for(i=0; i<array_size-1; i++)
for(j=i+1; j<array_size; j++)
if(strcmp(kids_base[i].surname, kids_base[j].surname)>0)
{
temp = kids_base[i];
kids_base[i] = kids_base[j];
kids_base[j] = temp;
}
}
void search_illness(struct kindergarden* kids_base, int array_size, char **argv)
{
int i;
printf("This is the list of kids illed with %s:\n", argv[1]);
for(i=0; i<=array_size; i++)
{
if (kids_base[i].hosp_identify == 0)
{
if(!strcmp(kids_base[i].hospital.nonhosp.illness, argv[1]))
{
printf("The child %s %s was searched by Doc.%s\n", kids_base[i].surname, kids_base[i].name, kids_base[i].hospital.nonhosp.doc);
}
}
else if(!strcmp(kids_base[i].hospital.hosp.illness, argv[1]))
{
printf("The child %s %s was hospitalized in Hospital %s in %s, where was searched by Doc.%s\n", kids_base[i].surname, kids_base[i].name, kids_base[i].hospital.hosp.hospnum, kids_base[i].hospital.hosp.hospplace, kids_base[i].hospital.hosp.hospdoc);
}
}
}
void check_help(int argc, char **argv)
{
if(argc > 1 && !strcmp(argv[1], "-h"))
{
puts("\n=============================================\n"
"Manual\n\n"
"1) send an interested illness by command line\n"
"2) A: Enter an information about children:\n"
"- surname\n"
"- name\n"
"B: Choose if he/she was hospitalized and then put the information about illness, hospital and doctor"
"- illness\n"
"- doctor\n"
"- hospital number\n"
"- hospital situation\n"
"3) Output data about interested illness\n\n"
"=================================================\n");
exit(0);
}
}