Fixed print student data and read data from file

This commit is contained in:
Sambo Chea 2020-08-02 19:20:50 +07:00
parent 63966b9e34
commit 9aa3dc2919
2 changed files with 67 additions and 49 deletions

View File

@ -6,29 +6,3 @@ Computer
98 98
52 52
95 95
sambo
222
c
1
1
Sambo
728728
CS
4
38
32
34
56
CS2
382S
SNS
2
1
3
SHSj
43434
SHS
3
2
5
2

View File

@ -41,7 +41,7 @@ void read_file();
void quite(); void quite();
// Linked list functions // Linked list functions
void add_student(char *student_name, char *student_id, char *course_name, int no_of_units, int *marks); void add_student(char student_name[20], char student_id[10], char course_name[20], int no_of_units, int marks[4]);
int count(); int count();
// util functions // util functions
@ -97,7 +97,42 @@ void print_welcome()
void display_students() void display_students()
{ {
printf("\n"); printf("\n");
read_student_to_data(); read_student_to_data();
STUDENT *student;
student = head;
if (student == NULL)
{
printf("No student!");
return;
}
print_student(student);
while (student->next != NULL)
{
student = student->next;
print_student(student);
}
}
void print_student(STUDENT *student)
{
printf("Student Name: %s\n", student->student_info.name);
printf("Student ID: %s\n", student->student_info.id);
printf("Course Name: %s\n", student->course_info.course_name);
printf("No of units: %d\n", student->course_info.no_of_units);
printf("Marks: [ ");
for (int i = 0; i < student->course_info.no_of_units; i++)
{
printf("%d ", student->course_info.marks[i]);
}
printf("]\n");
printf("Avg: %.2f\n", student->course_info.avg);
} }
void display_menu() void display_menu()
@ -110,20 +145,29 @@ void display_menu()
printf("(6) Quit program\n\n"); printf("(6) Quit program\n\n");
} }
void add_student(char *student_name, char *student_id, char *course_name, int no_of_units, int *marks) void add_student(char student_name[20], char student_id[10], char course_name[20], int no_of_units, int marks[4])
{ {
STUDENT *temp, *iterator; STUDENT *temp, *iterator;
temp = (struct student_tag *)malloc(sizeof(struct student_tag)); temp = (struct student_tag *)malloc(sizeof(struct student_tag));
PERSON info = {student_name, student_id}; PERSON info;
COURSE course = {course_name, no_of_units, marks}; strncpy(info.name, student_name, 20);
int sum = 0; strncpy(info.id, student_id, 10);
COURSE course;
strncpy(course.course_name, course_name, 20);
course.no_of_units = no_of_units;
// memcpy(course.marks, marks);
float sum = 0;
for (int i = 0; i < no_of_units; i++) for (int i = 0; i < no_of_units; i++)
{ {
course.marks[i] = marks[i];
sum += marks[i]; sum += marks[i];
} }
course.avg = sum / no_of_units; course.avg = sum / no_of_units;
temp->student_info = info; temp->student_info = info;
temp->course_info = course; temp->course_info = course;
// reference in head // reference in head
iterator = head; iterator = head;
@ -157,9 +201,9 @@ void read_student_to_data()
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
char student_name[BUFFER_SIZE]; char student_name[20];
char student_id[BUFFER_SIZE]; char student_id[10];
char course_name[BUFFER_SIZE]; char course_name[20];
int no_of_units; int no_of_units;
int marks[4]; int marks[4];
@ -197,26 +241,26 @@ void read_student_to_data()
course_name[i] = '\0'; course_name[i] = '\0';
printf("Student Name: %s\n", student_name); // printf("Student Name: %s\n", student_name);
printf("Student ID: %s\n", student_id); // printf("Student ID: %s\n", student_id);
printf("Course Name: %s\n", course_name); // printf("Course Name: %s\n", course_name);
printf("No of units: %s", no); // printf("No of units: %s", no);
no_of_units = atoi(no); no_of_units = atoi(no);
for (int j = 0; j < no_of_units; j++) for (int j = 0; j < no_of_units; j++)
{ {
char d[BUFFER_SIZE]; char mark[BUFFER_SIZE];
fgets(d, sizeof d, file); fgets(mark, sizeof mark, file);
sscanf(d, "%d", &marks[j]); sscanf(mark, "%d", &marks[j]);
} }
printf("Marks: [ "); // printf("Marks: [ ");
for (int x = 0; x < no_of_units; x++) // for (int x = 0; x < no_of_units; x++)
{ // {
printf("%d ", marks[x]); // printf("%d ", marks[x]);
} // }
printf("]\n\n"); // printf("]\n\n");
// add into linked list // add into linked list
add_student(student_name, student_id, course_name, no_of_units, marks); add_student(student_name, student_id, course_name, no_of_units, marks);