-
Notifications
You must be signed in to change notification settings - Fork 0
/
school.c
executable file
·375 lines (353 loc) · 7.72 KB
/
school.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "school.h"
int create_num() {
char temp;
int num = 0;
flushall();
while ((temp = getchar()) != ' ' && temp != '\n' && temp != '\r') {
if (temp >= '0' && temp <= '9') {
num *= 10;
num += temp - '0';
}
}
return num;
}
char yOn() {
char answer = 0;
while (!(answer == 'y' || answer == 'Y')) {
flushall();
scanf(" %c", &answer);
if (answer == 'n' || answer == 'N')
return 'n';
}
return 'y';
}
char again() {
printf("Again (y/n)?\n");
return yOn();
}
int is_str_equall(char * name, char * string) {
char nm, st;
if (!string)
return 0;
while(*name && *string) {
nm = *name++;
st = *string++;
if (nm >= 'a' && nm <= 'z')
nm -= 32;
if (st >= 'a' && st <= 'z')
st -= 32;
if (nm != st)
return 0;
}
if (*name == *string && *string == 0)
return 1;
return 0;
}
void name_print(char * name) {
HANDLE hConsole;
CONSOLE_SCREEN_BUFFER_INFO csbi;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
SetConsoleTextAttribute(hConsole, CYAN);
printf("'%s'", name);
SetConsoleTextAttribute(hConsole, csbi.wAttributes);
}
pCls_t make_cls(data_item data) {
pCls_t nNode = NULL;
nNode = (pCls_t)calloc(1, sizeof(Cls_t));
if (!nNode) {
printf("Failed allocation");
return NULL;
}
nNode->type_t = CLS;
nNode->clsNum = data;
return nNode;
}
pStn_t make_student(char name[]) {
pStn_t nNode = NULL;
nNode = (pStn_t)calloc(1, sizeof(Stn_t));
if (!nNode) {
printf("Failed allocation \n");
return NULL;
}
nNode->name = (char*)calloc(strlen(name) + 1, sizeof(char));
strcpy(nNode->name, name);
return nNode;
}
pGrd_t make_grade(int gradeId, int grd ) {
pGrd_t nNode = NULL;
nNode = (pGrd_t)calloc(1, sizeof(Grd_t));
if (!nNode) {
printf("Failed allocation \n");
return NULL;
}
nNode->lsn = gradeId;
nNode->grade = grd;
return nNode;
}
int add_first_student(pStn_t *head, char name[]) {
pStn_t nNode = make_student(name);
if (!nNode) {
printf("Failed allocation \n");
return 1;
}
if (!head) {
printf("Missing pointer \n");
return 2;
}
nNode->next = *head;
*head = nNode;
return 0;
}
int add_first_grade(pGrd_t *head, int gradeId, int grd) {
pGrd_t nNode = make_grade(gradeId, grd);
if (!nNode) {
printf("Failed allocation \n");
return 1;
}
if (!head) {
printf("Missing pointer \n");
return 2;
}
nNode->next = *head;
*head = nNode;
return 0;
}
int add_last_grade(pGrd_t *head, pGrd_t last, int gradeId, int grd) {
pGrd_t nNode = make_grade(gradeId, grd);
if (!nNode) {
printf("Failed allocation \n");
return 1;
}
if (!last) {
*head = nNode;
return 0;
}
last->next = nNode;
return 0;
}
int find_stn(pStn_t * stn) {
char name[NAME];
if (!(stn && *stn)) {
printf("No students \n");
return 2;
}
printf("Student name: ");
flushall();
gets(name);
while (!(is_str_equall(name, (*stn)->name))) {
if (!(*stn = (*stn)->next)) {
printf("Name not found \n");
return 1;
}
}
return 0;
}
int add_student(pStn_t *head) {
pStn_t ptr;
char name[100];
if (!head) {
printf("No students \n");
return 2;
}
ptr = *head;
printf("Student name: ");
flushall();
gets(name);
while (ptr) {
if (is_str_equall(name, ptr->name)) {
printf("The name exsist \n");
return 1;
}
ptr = ptr->next;
}
return add_first_student(head, name);
}
int change_grd(pStn_t stn, pGrd_t * last, int lsn, int grd) {
pGrd_t prv = stn->to_grade, ptr = stn->to_grade;
while (ptr && ptr->lsn != lsn) {
prv = ptr;
ptr = ptr->next;
}
if (ptr) {
printf("The lesson exist \n");
printf("Would you like to change it (y/n)? ");
if (yOn() == 'y') ptr->grade = grd;
return 1;
}
else *last = prv;
return 0;
}
int add_lsn(pStn_t stn) {
int lsn, grd, func;
pGrd_t noUseVar = NULL;
if (!stn) {
printf("No students \n");
return 2;
}
printf("Enter lesson ID: ");
lsn = create_num();
while (stn) {
printf("Enter grade for ");
name_print(stn->name);
printf(" (15 - 110): ");
do { grd = create_num(); } while (grd < 15 || grd > 110);
if (!(change_grd(stn, &noUseVar, lsn, grd))) {
if (func = add_first_grade(&stn->to_grade, lsn, grd))
return func;
}
stn = stn->next;
}
return 0;
}
int add_stn_lsn(pStn_t stn) {
int lsn, grd, func;
pGrd_t last = NULL;
if (!stn) {
printf("No students \n");
return 2;
}
if (func = find_stn(&stn)) return func;
printf("Enter lesson ID: ");
lsn = create_num();
printf("Enter grade (15 - 110): ");
do { grd = create_num(); } while (grd < 15 || grd > 110);
if (!(change_grd(stn, &last, lsn, grd)))
return add_last_grade(&stn->to_grade, last, lsn, grd);
return 0;
}
int del_stn(pStn_t * head) {
pStn_t ptr, prv;
char name[100];
if (!(head && *head)) {
printf("No students \n");
return 2;
}
printf("Student name: ");
flushall();
gets(name);
ptr = prv = *head;
if (is_str_equall(name, (*head)->name)) {
*head = (*head)->next;
free(ptr->name);
free(ptr);
return 0;
}
while (ptr && !is_str_equall(name, ptr->name)) {
prv = ptr;
ptr = ptr->next;
}
if (ptr) {
prv->next = ptr->next;
free(ptr->name);
free(ptr);
return 0;
}
printf("Name not found \n");
return 1;
}
int del_lsn(pStn_t stn) {
pGrd_t ptr, prv;
int lsn, c = 0;
if (!stn) {
printf("No students \n");
return 2;
}
printf("Enter lesson ID: ");
lsn = create_num();
while (stn) {
if ((ptr = stn->to_grade) && ptr->lsn == lsn) {
stn->to_grade = ptr->next;
free(ptr);
c++;
}
else {
while (ptr && lsn != ptr->lsn) {
prv = ptr;
ptr = ptr->next;
}
if (ptr) {
prv->next = ptr->next;
free(ptr);
c++;
}
}
stn = stn->next;
}
if (c)
return 0;
printf("Lesson not found \n");
return 1;
}
int del_stn_lsn(pStn_t stn) {
pGrd_t ptr, prv;
int lsn, func;
if (!stn) {
printf("No students \n");
return 2;
}
if (func = find_stn(&stn))
return func;
if (!stn->to_grade) {
printf("Result sheet is empty \n");
return 2;
}
while (1) {
printf("Enter lesson ID: ");
lsn = create_num();
if ((ptr = stn->to_grade) && ptr->lsn == lsn) {
stn->to_grade = ptr->next;
free(ptr);
return 0;
}
while (ptr) {
if (ptr->lsn == lsn) {
prv->next = ptr->next;
free(ptr);
return 0;
}
prv = ptr;
ptr = ptr->next;
}
printf("Lesson ID not found \n");
if(again() == 'n') return 2;
}
return 2;
}
void lprnt_cls(void * Cls, void * null) {
pStn_t stn; pGrd_t ptr; pCls_t cls = (pCls_t)Cls;
if (!cls) { printf("Create school first \n"); return; }
if (cls->type_t != CLS) { printf("Init error \n"); return; }
if (!(stn = cls->student)) { printf("No students \n"); return; }
printf("\t Class %d, students %d: \n", cls->clsNum, cls->amountOfStudents);
while (stn) {
printf("\t");
name_print(stn->name);
printf(" grades: \n");
if (!(ptr = stn->to_grade)) printf("\t Results sheet is empty ");
while (ptr) {
printf("Lesson ID - %d: %d\t", ptr->lsn, ptr->grade);
ptr = ptr->next;
}
printf("\n");
stn = stn->next;
}
}
void lprnt_stn_grds(void * Cls, void * null) {
pGrd_t ptr; pStn_t stn;
if (!Cls) { printf("Create school first \n"); return; }
if (((pCls_t)Cls)->type_t != CLS) { printf("Init error \n"); return; }
stn = ((pCls_t)Cls)->student;
if (find_stn(&stn)) return;
if (!(ptr = stn->to_grade)) { printf("Results sheet is empty "); return; }
while (ptr) {
printf("Lesson ID - %d: %d\t", ptr->lsn, ptr->grade);
ptr = ptr->next;
}
printf("\n");
}