Updated function

This commit is contained in:
Sambo Chea 2020-08-02 19:36:32 +07:00
parent 632b863ff9
commit 909f55f621

View File

@ -32,6 +32,11 @@ typedef struct person_tag PERSON;
typedef struct course_tag COURSE;
typedef struct student_tag STUDENT;
// util functions
void display_menu();
void print_welcome();
void print_student(STUDENT *student);
// core functions
void display_students();
void search_student();
@ -45,11 +50,6 @@ void quite();
void add_student(char student_name[20], char student_id[10], char course_name[20], int no_of_units, int marks[MAX_NO_OF_UNITS]);
int count();
// util functions
void display_menu();
void read_student_to_data();
void print_welcome();
int main(void)
{
// variable use for menu selected
@ -57,6 +57,9 @@ int main(void)
print_welcome();
// init the read data from the file
read_file();
while (1)
{
display_menu();
@ -99,7 +102,7 @@ void display_students()
{
printf("\n");
read_student_to_data();
read_file();
STUDENT *student;
@ -191,7 +194,102 @@ void add_student(char student_name[20], char student_id[10], char course_name[20
}
}
void read_student_to_data()
int count()
{
int n = 1;
STUDENT *temp;
temp = head;
if (head == NULL)
{
return 0;
}
while (temp->next != NULL)
{
n++;
temp = temp->next;
}
return n;
}
void search_student()
{
printf("\nNot implement yet!");
}
void find_maximum()
{
printf("\nNot implement yet!");
}
void find_failed()
{
printf("\nNot implement yet!");
}
void update_file()
{
FILE *file;
file = fopen(FILE_STUDENT_DATA_PATH, "a");
char name[20];
char id[10];
char course_name[20];
int no_of_units;
int marks[MAX_NO_OF_UNITS];
printf("Enter student name: ");
scanf("%s", &name);
printf("Enter student id: ");
scanf("%s", &id);
printf("Enter course name: ");
scanf("%s", &course_name);
again:
printf("Enter no of units: ");
scanf("%d", &no_of_units);
if (no_of_units > MAX_NO_OF_UNITS)
{
printf("you cannot input the units bigger than %d\n", MAX_NO_OF_UNITS);
getchar();
goto again;
}
for (int i = 0; i < no_of_units; i++)
{
printf("Enter mark[%d]: ", i + 1);
scanf("%d", &marks[i]);
}
if (count() > 0)
{
fputs("\n", file);
}
fputs(name, file);
fputs("\n", file);
fputs(id, file);
fputs("\n", file);
fputs(course_name, file);
fputs("\n", file);
fprintf(file, "%d", no_of_units);
for (int i = 0; i < no_of_units; i++)
{
fputs("\n", file);
fprintf(file, "%d", marks[i]);
}
fclose(file);
printf("\nSaved");
}
void read_file()
{
// init head to null
head = NULL;
@ -261,105 +359,6 @@ void read_student_to_data()
fclose(file);
}
int count()
{
int n = 1;
STUDENT *temp;
temp = head;
if (head == NULL)
{
return 0;
}
while (temp->next != NULL)
{
n++;
temp = temp->next;
}
return n;
}
void search_student()
{
printf("\nNot implement yet!");
}
void find_maximum()
{
printf("\nNot implement yet!");
}
void find_failed()
{
printf("\nNot implement yet!");
}
void update_file()
{
FILE *file;
file = fopen(FILE_STUDENT_DATA_PATH, "a");
char name[20];
char id[10];
char course_name[20];
int no_of_units;
int marks[MAX_NO_OF_UNITS];
printf("Enter student name: ");
scanf("%s", &name);
printf("Enter student id: ");
scanf("%s", &id);
printf("Enter course name: ");
scanf("%s", &course_name);
again:
printf("Enter no of units: ");
scanf("%d", &no_of_units);
if (no_of_units > MAX_NO_OF_UNITS) {
printf("you cannot input the units bigger than %d\n", MAX_NO_OF_UNITS);
getchar();
goto again;
}
for (int i = 0; i < no_of_units; i++)
{
printf("Enter mark[%d]: ", i + 1);
scanf("%d", &marks[i]);
}
if (count() > 0)
{
fputs("\n", file);
}
fputs(name, file);
fputs("\n", file);
fputs(id, file);
fputs("\n", file);
fputs(course_name, file);
fputs("\n", file);
fprintf(file, "%d", no_of_units);
for (int i = 0; i < no_of_units; i++)
{
fputs("\n", file);
fprintf(file, "%d", marks[i]);
}
fclose(file);
printf("\nSaved");
}
void read_file()
{
printf("\nNot implement yet!");
}
void quite()
{
printf("\nGoodbye!");