-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_engine.c
69 lines (53 loc) · 2.58 KB
/
query_engine.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
#include <stdio.h>
#include <stdlib.h>
typedef struct Student {
char name[30];
char surname[30];
int course; // year of study
double average; // average grade
int load; // number of courses
char courses[10][30]; // course names
int grades[10]; // course grades
char languages[100]; // spoken languages
} Student;
int main(int argc, char *argv[]) {
FILE *db = NULL;
// open database file for reading, provide a parameter or use default "db.bin"
if (argc > 1)
db = fopen(argv[1], "rb");
else
db = fopen("db.bin", "rb");
if (db){
Student students[1000]; // all the data goes here
int size = 0; // how many students in database
// reading data from file
fread(&size, sizeof(int), 1, db);
for (int i = 0; i < size; i++){
fread(&students[i], sizeof(Student), 1, db);
}
printf("%d records loaded succesfully\n", size);
// MODIFY CODE BELOW
int counterDemo = 0; // for counting students
for (int i = 0; i < size; ++i){ // process all the student records in database
Student s = students[i]; // store data for each student in s
if(1){ // *** first filter, conditions on the student
printf("full name: %s %s\nyear of study: %d\naverage grade: %f\nnumber of courses: %d\n", s.name, s.surname, s.course, s.average, s.load); // print student data
int anotherDemo = 0; // for counting courses/grades
for (int i = 0; i < s.load; ++i){ // process each course taken by the student
if(1){ // *** second filter, conditions on the course/grade
++anotherDemo; // counting courses
printf("%s %d\n", s.courses[i], s.grades[i]);
}
}
printf("%s\n\n", s.languages);
if (anotherDemo == s.load) // *** third filter, various other conditions
++counterDemo; // counting studfents
}
}
printf("Filter applied, %d students found\n", counterDemo); // how many passed the filters
fclose(db);
} else {
printf("File db.bin not found, check current folder\n");
}
return 0;
}