#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996).
#pragma warning(disable:6031).
#define MAX_NAME_LEN 50
#define MAX_ID_LEN 20
#define MAX_STUDENTS 100
#define MIN_GRADE 0
#define MAX_GRADE 100
// Student Information Structures
typedef struct {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
int grade[3]; // grade in three subjects
double avg; // average score
int temp;
} Student;
// Array of student information
Student students[MAX_STUDENTS];
int num_students = 0; // number of current students
// function declaration
void add_student();
void load_students();
void save_students();
void display_students();
void search_student();
void list_students();
void list_failed_students();
void sort_students();
void insert_grade();
void delete_student();
// Add student information
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("The maximum number of student information has been reached and no new student information can be added. \n");
return;
}
Student new_student;
printf("Please enter student's name: ");
scanf("%s", new_student.name);
printf("Please enter the student's number:");
scanf("%s", new_student.id);
printf("Please enter %s's three subject grades (separated by spaces):", new_student.name);
scanf("%d %d %d", &new_student.grade[0], &new_student.grade[1], &new_student.grade[2]);
for (int i = 0; i < 3; i++)
{
if (new_student.grade[i] < MIN_GRADE || new_student.grade[i] > MAX_GRADE) {
printf("Invalid grade, please re-enter. \n");
i--;
}
}
new_student.avg = (new_student.grade[0] + new_student.grade[1] + new_student.grade[2]) / 3.0;
students[num_students] = new_student;
num_students++;
printf("Student information has been added successfully. \n");
}
// Load student information from a file
void load_students() {
printf("Please enter the path to the student information file:");
char filename[100];
scanf("%s", filename);
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Failed to open file, please check if the file path is correct. \n");
return;
}
int count = 0;
while (1) {
Student new_student;
int ret = fscanf(file, "%s %s %d %d %d", new_student.name, new_student.id,
&new_student.grade[0], &new_student.grade[1], &new_student.grade[2]);
if (ret == EOF) {
break;
}
new_student.avg = (new_student.grade[0] + new_student.grade[1] + new_student.grade[2]) / 3.0;