/
main.c
155 lines (148 loc) · 4.39 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students
{
struct person
{
char surname[20],name[20],patronymic[20];
}fullname;
union characteristics
{
struct info_1
{
int height,weight;
}characteristics_1;
struct info_2
{
int height,weight,age,pulse;
}characteristics_2;
}characteristics;
int union_num;
};
void view_help(void)
{
printf("\nEnter students' names and characteristics\n");
}
char *delete_space(char *string)
{
int i = 0, j, k = 0;
while (string[i++] == ' ' && string[i]);
if (i < 30) {
for (j = i - 1; j < 30; j++)
string[k++]=string[j];
}
return string;
}
char* check_input(void)
{
char *string=(char*)calloc(20,sizeof(char));
do {
fgets(string, 20, stdin);
if(strlen(string)==19)
while(getchar()!='\n');
} while (string[0] == '\n');
return string;
}
void input(struct students *person, int records)
{
char *string;
do{
string=(char*)calloc(20,sizeof(char));
}while(!string);
int number=0,num;
puts("enter surname ");
string = check_input();
strcpy(person[records].fullname.surname, delete_space(string));
puts("enter name ");
string = check_input();
strcpy(person[records].fullname.name, delete_space(string));
puts("enter patronymic ");
string = check_input();
strcpy(person[records].fullname.patronymic, delete_space(string));
puts("1-height,weight 2-height,weight,other characteristics");
num=atoi(check_input());
if(num==1)
{
person[records].union_num=1;
puts("enter height");
number=atoi(check_input());
person[records].characteristics.characteristics_1.height=number;
puts("enter weight");
number=atoi(check_input());
person[records].characteristics.characteristics_1.weight=number;
}
else
{
person[records].union_num=2;
puts("enter height");
number=atoi(check_input());
person[records].characteristics.characteristics_2.height=number;
puts("enter weight");
number=atoi(check_input());
person[records].characteristics.characteristics_2.weight=number;
puts("enter age");
number=atoi(check_input());
person[records].characteristics.characteristics_2.age=number;
puts("enter pulse");
number=atoi(check_input());
person[records].characteristics.characteristics_2.pulse=number;
}
}
show_students( struct students *person, int records )
{
int i,num;
puts("1-show people with height,weight/2-show people with other characteristics");
num=atoi(check_input());
if(num==1)
{
for(i=0;i<records;i++)
{
if(person[i].union_num==1)
{
printf("\n%s%s%s",person[i].fullname.surname, person[i].fullname.name, person[i].fullname.patronymic);
printf("\n%dsm %dkg",person[i].characteristics.characteristics_1.height, person[i].characteristics.characteristics_1.weight);
}
}
}
else if(num==2)
{
for(i=0;i<records;i++)
{
if(person[i].union_num==2)
{
printf("\n%s%s%s",person[i].fullname.surname, person[i].fullname.name, person[i].fullname.patronymic);
printf("\n%dsm %dkg",person[i].characteristics.characteristics_2.height, person[i].characteristics.characteristics_2.weight);
printf("\n%dyears \npulse: %d ",person[i].characteristics.characteristics_2.age, person[i].characteristics.characteristics_2.pulse);
}
}
}
}
int main(int argc,char *argv[])
{
if(argc>1)
{
if(!strcmp(argv[1],"-h"))
view_help();
}
int records = 0, i;
char *string;
struct students *person;
do {
string = (char *) calloc(5, sizeof(char));
person = (struct students*) calloc(5, sizeof(struct students));
} while (!person && !string);
puts("ATTENCION: maximum length of record cannot exceed 19 symbols");
while (records < 5) {
input(person, records);
records++;
puts("enter stop to quit");
fgets(string,5,stdin);
if(strcmp(string,"stop")==0)
break;
}
if (records == 5)
puts("you can enter only 5 persons");
show_students(person,records);
return 0;
}