This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrating.c
178 lines (148 loc) · 4.17 KB
/
rating.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
/*
* @file rating.c
* @author Литвиненко А.В., 515а
* @date 09 липня 2022
* @brief
* За умовою задачі потрібно створити програму, яка дозволить впорядковувати дані про оцінки здобувачів кафедри комп’ютерних систем, мереж і кібербезпеки сортуванням.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <iso646.h>
#include "structs.h"
#include "statuses.h"
#include "rating.h"
#include "sorting.h"
#include "database.h"
int main(int argc, const char* argv[]){
lineString("Welcome to \"Rating Cheker\"");
int fd, fd_tmp;
int size = 0;
char *datafile, *buffer;
datafile = (char*) ec_malloc(1024);
buffer = (char*) ec_malloc(1024);
memset(datafile, 0, 1024);
memset(buffer, 0, 1024);
strcpy(datafile, "records.txt");
record records[1024], sorted[1024];
if((fd = open(datafile, O_RDONLY)) == -1){
Info("File didn't exists! Creating new one...");
if((fd = open(datafile, O_WRONLY|O_APPEND|O_CREAT, S_IRUSR|S_IWUSR)) == -1){
CritError("In function main() during creating a new file");
}
}
else{
Info("Opened successfully, starting reading!");
Read_records(fd, records);
size = Records_count(records);
if(close(fd) == -1){
CritError("In function main() during closing the file");
}
}
for(int i=0;i<size;++i){
if(records[i].course == 0) break;
sorted[i] = records[i];
}
unsigned int user_choice = 0;
char* input = (char*) ec_malloc(128);
while(1){
switch(user_choice){
case 0:
Menu_of_actions();
break;
case 1:
Show_table(records, size);
break;
case 2:
record ex;
printf(">>>><<<<\n");
while(not(ex.course > 0 and ex.course <= 5)){
printf("Course: ");
scanf("%u", &ex.course);
}
do{
printf("Group (6 symbols): ");
scanf("%s", ex.group);
}while(strlen(ex.group) > 6);
do{
printf("Surname (25 symbols): ");
scanf("%s", ex.surname);
}while(strlen(ex.surname) > 25);
printf("Rating (5 4 3 2): ");
scanf("%u%u%u%u", &ex.rating[0], &ex.rating[1], &ex.rating[2], &ex.rating[3]);
printf(">>>><<<<\n");
Add_record(ex, records, &size);
for(int i=0;i<size;++i){
if(records[i].course == 0) break;
sorted[i] = records[i];
}
ex.course = 0;
memset(ex.group, 0, 6);
memset(ex.surname, 0, 25);
break;
case 3:
Sort_by_course(sorted, size);
Show_table(sorted, size);
Info("Sorted successfully!");
printf("Would you like to export it? (y/N): ");
scanf("%s", input);
if(not(strcmp(input, "y")) || not(strcmp(input, "Y"))){
printf("Good! Enter filename: ");
scanf("%s", input);
fd_tmp = open(input, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
Write_records(fd_tmp, sorted, size, input);
}
memset(input, 0, 128);
break;
case 4:
Sort_by_group(sorted, size);
Show_table(sorted, size);
Info("Sorted successfully!");
printf("Would you like to export it? (y/N): ");
scanf("%s", input);
if(not(strcmp(input, "y")) || not(strcmp(input, "Y"))){
printf("Good! Enter filename: ");
scanf("%s", input);
fd_tmp = open(input, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
Write_records(fd_tmp, sorted, size, input);
}
memset(input, 0, 128);
break;
case 5:
Sort_by_surname(sorted, size);
Show_table(sorted, size);
Info("Sorted successfully!");
printf("Would you like to export it? (y/N): ");
scanf("%s", input);
if(not(strcmp(input, "y")) || not(strcmp(input, "Y"))){
printf("Good! Enter filename: ");
scanf("%s", input);
fd_tmp = open(input, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
Write_records(fd_tmp, sorted, size, input);
}
memset(input, 0, 128);
break;
case 6:
About();
break;
case 7:
fd_tmp = open("records.txt", O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
Write_records(fd_tmp, records, size, "records.txt");
close(fd_tmp);
Exit();
default:
Warning("Bad option!");
break;
}
line("Menu");
printf(">> ");
scanf("%u", &user_choice);
}
free(datafile);
free(buffer);
return 0;
}