Compare commits

...

6 Commits

2 changed files with 128 additions and 5 deletions

View File

@ -5,4 +5,16 @@ Computer
88
98
52
95
95
Sambo
12345
CS90
2
50
40
Chea
6789
CS78
2
50
20

View File

@ -34,12 +34,17 @@ typedef struct person_tag PERSON;
typedef struct course_tag COURSE;
typedef struct student_tag STUDENT;
// file function
void read_file();
// util functions
void display_menu();
void print_welcome();
void print_student(STUDENT *student);
void release(STUDENT *data);
int find_min_in_array(int *array);
STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE]);
STUDENT *find_maximum_avg();
// core functions
void display_students();
@ -47,7 +52,6 @@ void search_student();
void find_maximum();
void find_failed();
void update_file();
void read_file();
void quite();
// Linked list functions
@ -234,6 +238,9 @@ void search_student()
STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE])
{
// refresh data from file first
read_file();
STUDENT *temp = head;
while (temp != NULL)
@ -249,14 +256,118 @@ STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE])
return NULL;
}
STUDENT *find_maximum_avg()
{
// refresh data from file first
read_file();
STUDENT *temp = head, *max;
max = temp->next;
while (temp != NULL)
{
if (max == NULL)
{
return temp;
}
if (max->course_info.avg < temp->course_info.avg)
{
max = temp;
}
temp = temp->next;
}
// release the max next record
// set max next element to NULL
// because we use only one record
release(max->next);
max->next = NULL;
return max;
}
int find_min_in_array(int *array)
{
if (array == NULL)
{
return -1;
}
int min = array[0];
size_t size = sizeof(array) / sizeof(array[0]);
for (int i = 1; i < size; i++)
{
if (array[i] < min)
{
min = array[i];
}
}
return min;
}
STUDENT *find_failed_mark(int upper_mark)
{
// refresh data from file first
read_file();
STUDENT *temp = head, *failed_students = NULL;
while (temp != NULL)
{
int min = find_min_in_array(temp->course_info.marks);
if (min < upper_mark)
{
if (failed_students == NULL)
{
failed_students = temp;
failed_students->next = NULL;
}
else
{
while (failed_students->next != NULL)
{
failed_students = failed_students->next;
}
failed_students->next = temp;
}
}
temp = temp->next;
}
return failed_students;
}
void find_maximum()
{
printf("\nNot implement yet!");
// refresh data from file first
read_file();
STUDENT *max_student = find_maximum_avg();
if (max_student == NULL)
{
printf("\nNo maximum student found!\n");
return;
}
printf("\nFind maximum avg was found with: %0.2f\n", max_student->course_info.avg);
print_student(max_student);
}
void find_failed()
{
printf("\nNot implement yet!");
int upper_mark = 50;
STUDENT *failed_students = find_failed_mark(upper_mark);
if (failed_students == NULL)
{
printf("\nNo failed student found!\n");
return;
}
printf("\nFind the failed students that at least one mark less than %d\n", upper_mark);
print_student(failed_students);
}
void update_file()
@ -317,7 +428,7 @@ again:
fclose(file);
printf("\nSaved");
printf("\nRecord saved successfully!");
// reload data into linked list again
read_file();