-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent db.c
401 lines (363 loc) · 10.8 KB
/
student db.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <time.h>
struct student_node{
char* surname;
char* name;
char* patronym;
char* facility;
char* department ;
} ;
struct node{
char* number;
struct student_node* content;
struct node* next;
};
typedef struct node xnode;
xnode* root = NULL;
char* fdynstring(FILE* fp, char ch){
int l = 0;
char buf;
for(;(buf = fgetc(fp)) != ch && buf != EOF; ++l);
if (buf == EOF)
fseek(fp, -1*(l), SEEK_CUR);
else if (buf =='\n')
fseek(fp, -1*(l+2), SEEK_CUR);
else
fseek(fp, -1*(l+1), SEEK_CUR);
char* a = (char*)malloc((l + 1) * sizeof(char));
for (int i = 0; i < l; ++i)
a[i]=fgetc(fp);
a[l] = '\0';
if (buf != EOF)
fgetc(fp);
else
fseek(fp, -1, 2);
return a;
}
char* dynstring(char c) {
int l = 0;
int size = 1;
char *string = (char*) malloc(size * sizeof(char));
char ch = getchar();
while (ch != c) {
string[(l)++] = ch;
if (l >= size) {
size *= 2;
string = (char*) realloc(string, size * sizeof(char));
}
ch = getchar();
}
string[l] = '\0';
return string;
}
void clear_xnode_st(xnode* node){
free(node->content->surname);
free(node->content->name);
free(node->content->patronym);
free(node->content->facility);
free(node->content->department);
free(node->number);
free(node->content);
free(node);
}
char* timeinf(){
const time_t timer = time(NULL);
char* tm =(char*)malloc(21*sizeof(char));
struct tm* timeinf = localtime(&timer);
strftime (tm,21,"%d_%m_%Y_%H.%M.%S.",timeinf);
//printf("%s\n",buffer);
return tm;
}
void add(xnode* current,char* number,struct student_node* content){
if (current->next == NULL ){
current->next = (xnode*)malloc(sizeof(xnode));
current->next->number = number;
current->next->next = NULL;
current->next->content = content;
} else if(strcmp(number,current->next->number)<0){
xnode* temp = (xnode *)malloc(sizeof(xnode));
temp->number=number;
temp->content = content;
temp->next = current->next;
current->next = temp;
} else {
if (strcmp(number,current->next->number)){
add(current->next, number, content);
} else {
current->content = content;
}
}
}
void add_first(xnode* current,char* number, struct student_node* content){
if (current == NULL){
root = (xnode*)malloc(sizeof(xnode));
root->number = number;
root->next = NULL;
root->content = content;
} else if (strcmp(number,current->number) < 0){
xnode* nde = (xnode*)malloc(sizeof(xnode));
nde->number = number;
nde->next = root;
nde->content = content;
root = nde;
} else if (strcmp(number,current->number)){
add(current, number, content);
} else {
current->content = content;
}
}
void add_student(){
struct student_node* temp = (struct student_node*)malloc(sizeof(struct student_node));
printf("Type number of student's book:");
char* number = dynstring('\n');
printf("Type surname:");
temp->surname = dynstring('\n');
printf("Type name:");
temp->name = dynstring('\n');
printf("Type patronym:");
temp->patronym = dynstring('\n');
printf("Type facility:");
temp->facility = dynstring('\n');
printf("Type name of department:");
temp->department = dynstring('\n');
putchar('\n');
add_first(root,number,temp);
}
void del(xnode* current,char* number){
if (strcmp(number,current->next->number)){
del(current->next,number);
}
xnode* temp = current->next;
current->next = current->next->next;
free(temp);
}
void del_first(xnode* current,char* number){
if(strcmp(current->number,number)){
del(current,number);
} else {
xnode* temp = current->next;
free(current);
root = temp;
}
}
void delfull(xnode* current){
if (current == NULL){
return;
}
xnode* next = current->next;
clear_xnode_st(current);
delfull(next);
}
void delfullfirst(xnode* current){
delfull(current);
root = NULL;
}
void base_out(xnode* current,FILE* fth){
fprintf(fth,"%s;",current->number);
fprintf(fth,"%s;",current->content->surname);
fprintf(fth,"%s;",current->content->name);
fprintf(fth,"%s;",current->content->patronym);
fprintf(fth,"%s;",current->content->facility);
fprintf(fth,"%s\n",current->content->department);
};
void list(xnode* current,FILE* fth){
if (current == NULL){
return;
}
if (fth == NULL){
return; //error
}
//base_out(current,fth);
fprintf(fth,"%s;",current->number);
fprintf(fth,"%s;",current->content->surname);
fprintf(fth,"%s;",current->content->name);
fprintf(fth,"%s;",current->content->patronym);
fprintf(fth,"%s;",current->content->facility);
fprintf(fth,"%s\n",current->content->department);
list(current->next, fth);
}
xnode* find(xnode* current,char* number){
if (current == NULL){
return NULL; //error
}
if (!strcmp(number,current->number)){
base_out(current,stdout);
}
return find(current->next,number);
}
xnode* find_edit(xnode* current,char* number){
if (current == NULL){
return NULL; //error
}
if (!strcmp(number,current->number)){
return current;
}
return find_edit(current->next,number);
}
xnode* find_by_surname(xnode* current,char* surname){
if (current == NULL){
return NULL; //error
}
if (!(strcmp(surname ,current->content->surname))){
base_out(current,stdout);
}
return find_by_surname(current->next, surname);
}
void edit_student(){
printf("Welcome to Edit Menu\n");
printf("write:\n");
printf("number\n");
char* number= dynstring('\n');
printf("name of column\n");
char* cell = dynstring('\n');
printf("value\n");
char* value = dynstring('\n');
xnode* curr = find_edit(root,number);
if (!strcmp("name", cell)){
free(curr->content->name);
curr->content->name = value;
} else if (!strcmp("surname", cell)) {
free(curr->content->surname);
curr->content->surname = value;
} else if (!strcmp("patronym", cell)) {
free(curr->content->patronym);
curr->content->patronym = value;
} else if (!strcmp("facility", cell)) {
free(curr->content->facility);
curr->content->facility = value;
} else if (!strcmp("department", cell)) {
free(curr->content->department);
curr->content->department = value;
} else {
// 10/5 chertovikh errorov
}
}
void backup(){
char* tmt = timeinf();
char* book = "students_";
char* flnm = "C:\\Users\\Timur\\Desktop\\Database\\Backups\\";
char* filename = calloc(strlen(tmt) + strlen(book) + 1, 1);
strcat(filename,flnm);
strcat(filename,book);
strcat(filename,tmt);
strcat(filename,"csv");
FILE* fth1 = fopen(filename,"w");
list(root,fth1);
FILE* fth2 = fopen("C:\\Users\\Timur\\Desktop\\Database\\Backups\\BackupsStudents.txt","a+"); //C:\Users\Timur\Desktop\Database\Backups
fputs(filename,fth2);
fputc('\n',fth2);
fclose(fth1);
fclose(fth2);
printf("Backup saved\n");
}
void backup_out(){
delfullfirst(root);
printf("Choose file of backup\n");
FILE* fth = fopen("C:\\Users\\Timur\\Desktop\\Database\\Backups\\BackupsStudents.txt","r");
if (fth == NULL){
//error
}
char ch;
while((ch = getc(fth)) != EOF){
putchar(ch);
}
fclose(fth);
char* string = dynstring('\n');
char* way = "C:\\Users\\Timur\\Desktop\\Database\\Backups\\";
char* str_bck = calloc(strlen(way) + strlen(string) + 1, 1);
strcat(str_bck,way);
strcat(str_bck,string);
FILE *fth1, *fth2;
// Open one file for reading
fth1 = fopen(str_bck, "r");
if (fth1 == NULL){
printf("Cannot open file %s \n", str_bck);
//error(0);
}
// Open another file for writing
fth2 = fopen("C:\\Users\\Timur\\Desktop\\Database\\Databases\\Students.csv", "w");
if (fth2 == NULL)
{
printf("Cannot open file %s \n", "Students.csv");
//error();
}
// Read contents from file
while ((ch = fgetc(fth1)) != EOF) { //(ch = fgetc(fth1)) != EOF
fputc(ch, fth2);
}
//fputc(EOF,fth2);
//fseek(fth1,0,0);
printf("\nBackup successful recover\n");
fclose(fth1);
fclose(fth2);
}
void base_to_stack(){
FILE* fth1 = fopen("C:\\Users\\Timur\\Desktop\\Database\\Databases\\Students.csv","r");
while(fgetc(fth1)!= EOF){
fseek(fth1,-1,1);
struct student_node* cont = (struct student_node*)malloc(sizeof(struct student_node));
char* number = fdynstring(fth1,';');
cont->surname = fdynstring(fth1,';');
//printf("\n%s\n",cont->surname);
cont->name = fdynstring(fth1,';');
//printf("\n%s\n",cont->name);
cont->patronym = fdynstring(fth1,';');
//printf("\n%s\n",cont->patronym);
cont->facility = fdynstring(fth1,';');
//printf("\n%s\n",cont->facility);
cont->department = fdynstring(fth1, '\n');
//printf("\n%s\n",cont->department);
add_first(root,number,cont);
}
}
int main(){
// backup_out();
/*struct student_node* cont = (struct student_node*) malloc(sizeof(struct student_node));
cont -> name= "timo" ;
cont -> surname="mahmudov";
cont -> patronym = "naz";
cont -> department="IU";
cont -> facility="IU4";
add_first(root,"19U254",cont);
cont = (struct student_node*) malloc(sizeof(struct student_node));
cont -> name= "ktimo" ;
cont -> surname="mahmudov";
cont -> patronym = "naz";
cont -> department="IU";
cont -> facility="IU4";
cont = (struct student_node*) malloc(sizeof(struct student_node));
add_first(root,"19U251",cont);
cont -> name = "ktimo" ;
cont -> surname="mahmudov";
cont -> patronym = "naz";
cont -> department="IU";
cont -> facility="IU4";
add_first(root,"19U256",cont);
cont = (struct student_node*) malloc(sizeof(struct student_node));
cont -> name= "ktimo" ;
cont -> surname="mahmudov";
cont -> patronym = "naz";
cont -> department="IU";
cont -> facility="IU4";
add_first(root,"19U255",cont);*/
//add_student();
//backup();
//list(root,stdout);
//backup_out();
//list(root,stdout);
//edit_student();
//list(root,stdout);
//list(root,stdout);
//delfullfirst(root);
//del_first(root,"19U251");
//list(root,stdout);
//backup();
//putchar('\n');
//find(root,"19U256");
//putchar('\n');
//find_by_surname(root,"mahmudov");
//backup();
}