-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructures.c
269 lines (241 loc) · 8.48 KB
/
Structures.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
//enum of positions of players in the team
enum pos {
Forward=1,
Goalkeeper=2,
Defender=3,
Midfielder=4,
};
//structure where we can write all characteristics of a person
struct person {
int id;
char name[1000];
enum pos position;
int age;
int goals;
};
//transformation enum input data to string to put it in p[k].pos
enum pos transformation(char s[]){
if (strcmp(s,"Forward")==0)
return Forward;
if (strcmp(s,"Goalkeeper")==0)
return Goalkeeper;
if (strcmp(s,"Defender")==0)
return Defender;
if (strcmp(s,"Midfielder")==0)
return Midfielder;
return -1;
}
int main() {
FILE *a = fopen("input.txt", "r"); // a - to read, b - to write
FILE *b = fopen("output.txt", "w");
char alp[52] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char n[30]="a", s[100];
int k=0, h, f=0, flag=0, update_ind, delete_ind; // f and flag - flags in 2 other situations
struct person p[100];
while(strcmp(n, "Display")!=0) {
fscanf(a, "%s", n); // scan command "Add", "Delete", "Search" and "Update"
//ADD command
if(strcmp(n, "Add")==0) {
//ID SCAN - START
fscanf(a, "%d", &p[k].id);
if(k>0) {
for(int i=0; i<k; i++) /// e) check that new ID is unique
if(p[k].id==p[i].id) {
fprintf(b, "Invalid inputs\n");
return 0;
}
}
//ID SCAN - END
//NAME SCAN - START
fscanf(a, "%s", p[k].name);
for(int i=0; i<26; i++) { /// check a) name starts with a capital letter
if(p[k].name[0]==alp[i]) flag=1;
}
if(flag==0) {
fprintf(b, "Invalid inputs\n");
return 0;
}
flag=0;
for(int j=0; j<strlen(p[k].name); j++) { /// check c) name contains only English letters
for(int i=0; i<52; i++) {
if(p[k].name[j]==alp[i]) f=1;
}
if(f==0) {
fprintf( b, "Invalid inputs\n");
return 0;
}
f=0;
}
if(strlen(p[k].name)<2 || strlen(p[k].name)>15) { /// check b) 2 <= name length <= 15
fprintf(b, "Invalid inputs\n");
return 0;
}
//NAME SCAN - END
//POS SCAN - START
fscanf(a, "%s", s); //use enum type to write person's position data
p[k].position = transformation(s);
//POS SCAN - END
//AGE SCAN - START
fscanf(a, "%d", &p[k].age);
if(p[k].age<18 || p[k].age>100) { ///check f) 18 < age < 100
fprintf(b, "Invalid inputs\n");
return 0;
}
//AGE SCAN - END
//GOALS SCAN - START
fscanf(a, "%d", &p[k].goals);
if(p[k].goals<0 || p[k].goals>=1000) { /// check g) 0 <= goals <= 1000
fprintf(b, "Invalid inputs\n");
return 0;
}
//GOALS SCAN - END
k=k+1;
}
//UPDATE
else if(strcmp(n, "Update")==0) {
fscanf(a, "%d", &h);
for(int i=0; i<k; i++)
if(p[i].id==h){
f=1;
update_ind=i;
}
if(f==0) {
fprintf(b, "Invalid inputs\n"); //print error if we can't find existed id
return 0;
}
else {
//SCAN NAME FOR UPDATE - START
fscanf(a, "%s", p[update_ind].name);
for(int i=0; i<26; i++) {
if(p[update_ind].name[0]==alp[i]) flag=1;
}
if(flag==0) {
fprintf(b, "Invalid inputs\n");
return 0;
}
flag=0;
for(int j=0; j<strlen(p[update_ind].name); j++) {
for(int i=0; i<52; i++) {
if(p[update_ind].name[j]==alp[i]) f=1;
}
if(f==0) {
fprintf(b, "Invalid inputs\n");
return 0;
}
f=0;
}
if(strlen(p[update_ind].name)<2 || strlen(p[update_ind].name)>15) {
fprintf(b, "Invalid inputs\n");
return 0;
}
//SCAN NAME FOR UPDATE - END
//SCAN POS FOR UPDATE - START
fscanf(a, "%s", s);
p[update_ind].position = transformation(s);
//SCAN POS FOR UPDATE - END
//SCAN AGE FOR UPDATE - START
fscanf(a, "%d", &p[update_ind].age);
if(p[update_ind].age<18 || p[update_ind].age>100) {
fprintf( b, "Invalid inputs\n");
return 0;
}
//SCAN AGE FOR UPDATE - END
//SCAN GOALS FOR UPDATE - START
fscanf(a, "%d", &p[update_ind].goals);
if(p[update_ind].goals<0 || p[update_ind].goals>=1000) {
fprintf(b, "Invalid inputs\n");
return 0;
}
//SCAN GOALS FOR UPDATE - END
}
f=0;
}
//DELETE
else if(strcmp(n, "Delete")==0) {
//SCAN ID TO DELETE
fscanf(a, "%d", &h);
for(int i=0; i<k; i++)
if(p[i].id==h){
f=1;
delete_ind=i;
}
if(f==0) {
fprintf(b, "Impossible to delete\n");
}
else {
p[delete_ind].id=0;
p[delete_ind].age=0;
p[delete_ind].goals=0;
}
f=0;
}
//SEARCH
else if(strcmp(n, "Search")==0) {
//SCAN ID TO SEARCH
fscanf(a, "%d", &h);
for(int i=0; i<k; i++)
if(p[i].id==h)
f=1;
if(f==1)
fprintf(b, "Found\n");
else
fprintf(b, "Not found\n");
f=0;
}
else if(strcmp(n, "Display")!=0) {
fprintf(b, "Invalid inputs\n");
return 0;
}
/// the last line "Display" was entered, let`s show the received data:
}
if(k==0) {
fprintf(b, "Invalid inputs\n"); // check if input file contains inly command "Display"
return 0;
}
for(int i=0; i<k; i++) // check if we delete all added persons
if(p[i].id!=0)
f=1;
if(!f){
fprintf(b, "Invalid inputs\n");
return 0;
}
for(int i=0; i<k; i++) {
if(p[i].id!=0) {
switch(p[i].position) {
case 1:
fprintf(b, "ID: %d, ", p[i].id);
fprintf(b, "Name: %s, ", p[i].name);
fprintf(b, "Position: Forward, ");
break;
case 2:
fprintf(b, "ID: %d, ", p[i].id);
fprintf(b, "Name: %s, ", p[i].name);
fprintf(b, "Position: Goalkeeper, ");
break;
case 3:
fprintf(b, "ID: %d, ", p[i].id);
fprintf(b, "Name: %s, ", p[i].name);
fprintf(b, "Position: Defender, ");
break;
case 4:
fprintf(b, "ID: %d, ", p[i].id);
fprintf(b, "Name: %s, ", p[i].name);
fprintf(b, "Position: Midfielder, ");
break;
default:
fprintf(b, "Invalid inputs\n"); //invalid if position is another word
return 0;
break;
}
fprintf(b, "Age: %d, ", p[i].age); //age
fprintf(b, "Goals: %d\n", p[i].goals); //goals
}
}
fclose(a); //close files
fclose(b);
}