forked from Hantorism/2024-Advanced-C-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
64 lines (52 loc) · 1.94 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
#include "student.h"
#include "sort.h"
#include "utils.h"
int main(int argc, char* argv[]) { // [TODO] Fill in the parameters
FILE *input, *output;
char *filename;
if (!strstr(argv[1], "_sort.in")) { // [TODO] Fill in the condition
fprintf(stderr, "Command should be format like below\n");
fprintf(stderr, "%s try/*_sort.in\n", argv[0]); // [TODO] Fill in the arguments
exit(EXIT_FAILURE);
}
filename = strdup(argv[1]); // [TODO] Fill in the arguments
input = fopen(filename, "r");
if (input == NULL) {
fprintf(stderr, "Input file error\n");
exit(EXIT_FAILURE);
}
strcpy(strstr(filename, "in"), "out");
output = fopen(filename, "w");
if (output == NULL) {
fprintf(stderr, "Output file error\n");
exit(EXIT_FAILURE);
}
char line[MAX_BUFFER_SIZE];
int length = countLines(input);
int index = 0;
Student *students = createStudents(length); // [TODO] Fill in the arguments
while (fgets(line, MAX_BUFFER_SIZE, input) != NULL) {
char name[MAX_BUFFER_SIZE];
int id;
sscanf(line, "name: %s %*s %*s id: %d", name, &id); // [TODO] Fill in the arguments
appendStudent(students, index++, newStudent(name, id)); // [TODO] Fill in the arguments
}
SortBy sortBy = setSortBy(filename); // [TODO] Fill in the arguments
switch (sortBy) {
case NAME:
quickSort(students, length, compareByName); // [TODO] Fill in the arguments
printStudents(output, students, length); // [TODO] Fill in the arguments
break;
case ID:
quickSort(students, length, compareById); // [TODO] Fill in the arguments
printStudents(output, students, length); // [TODO] Fill in the arguments
break;
default:
exit(EXIT_FAILURE);
}
freeStudents(students, length); // [TODO] Fill in the arguments
free(filename);
fclose(input);
fclose(output);
return 0;
}