这三个要求很简单
但是这个考核,要求课程结束后,自动完成考核要求有点模糊
#include <stdio.h> #include <string.h> #define MAX_STUDENTS 10 #define MAX_CLASSES 4 #define MAX_NAME_LENGTH 50typedef struct {char name[MAX_NAME_LENGTH];char gender[10];int studentID;int classID; // 班级IDint attendance; // 出勤次数 } Student;Student classes[MAX_CLASSES][MAX_STUDENTS];void loadStudents() {FILE *file = fopen("C:\\Users\\32761\\Desktop\\students.txt", "r");if (file == NULL) {// 如果文件不存在,创建一个空文件file = fopen("C:\\Users\\32761\\Desktop\\students.txt", "w");if (file == NULL) {perror("无法创建文件");return;}fclose(file);printf("文件不存在,已创建空文件。\n");return;}for (int i = 0; i < MAX_CLASSES; i++) {for (int j = 0; j < MAX_STUDENTS; j++) {// 使用宽度限定符防止缓冲区溢出if (fscanf(file, "%49s %9s %d %d %d", classes[i][j].name, classes[i][j].gender, &classes[i][j].studentID, &classes[i][j].classID, &classes[i][j].attendance) != 5) {// 遇到读取错误或文件结束,停止读取break;}}// 如果内层循环因为读取错误而跳出,则也应跳出外层循环if (feof(file)) {break;}}fclose(file); }void saveStudents() {FILE *file = fopen("C:\\Users\\32761\\Desktop\\students.txt", "w");if (file == NULL) {perror("无法打开或创建文件");return;}for (int i = 0; i < MAX_CLASSES; i++) {for (int j = 0; j < MAX_STUDENTS; j++) {if (classes[i][j].studentID != 0) {fprintf(file, "%s %s %d %d %d\n", classes[i][j].name, classes[i][j].gender, classes[i][j].studentID, classes[i][j].classID, classes[i][j].attendance);}}}fclose(file); }void addStudent() {int classID;printf("请输入班级ID(1-4):");scanf("%d", &classID);getchar(); // 清除输入缓冲区中的换行符if (classID < 1 || classID > MAX_CLASSES) {printf("无效的班级ID。\n");return;}for (int j = 0; j < MAX_STUDENTS; j++) {if (classes[classID - 1][j].studentID == 0) {printf("请输入学生姓名、性别、学号:");scanf("%s %s %d", classes[classID - 1][j].name, classes[classID - 1][j].gender, &classes[classID - 1][j].studentID);getchar(); // 清除输入缓冲区中的换行符classes[classID - 1][j].classID = classID;classes[classID - 1][j].attendance = 0;printf("学生添加成功!\n");saveStudents(); // 保存到文件return;}}printf("班级已满,无法添加更多学生。\n"); }void deleteStudentByID(int studentID) {for (int i = 0; i < MAX_CLASSES; i++) {for (int j = 0; j < MAX_STUDENTS; j++) {if (classes[i][j].studentID == studentID) {classes[i][j].studentID = 0; // 将学号设为0表示删除printf("学生删除成功!\n");saveStudents(); // 保存到文件return;}}}printf("未找到该学号的学生。\n"); }void modifyStudentByID(int studentID) {for (int i = 0; i < MAX_CLASSES; i++) {for (int j = 0; j < MAX_STUDENTS; j++) {if (classes[i][j].studentID == studentID) {printf("请输入新的学生姓名、性别、学号、班级ID:");scanf("%s %s %d %d", classes[i][j].name, classes[i][j].gender, &classes[i][j].studentID, &classes[i][j].classID);getchar(); // 清除输入缓冲区中的换行符printf("学生信息修改成功!\n");saveStudents(); // 保存到文件return;}}}printf("未找到该学号的学生。\n"); }void queryStudentByID(int studentID) {for (int i = 0; i < MAX_CLASSES; i++) {for (int j = 0; j < MAX_STUDENTS; j++) {if (classes[i][j].studentID == studentID) {printf("姓名: %s, 性别: %s, 学号: %d, 班级ID: %d, 出勤次数: %d\n",classes[i][j].name,classes[i][j].gender,classes[i][j].studentID,classes[i][j].classID,classes[i][j].attendance);return;}}}printf("未找到该学号的学生。\n"); }void recordAttendanceByID(int studentID) {for (int i = 0; i < MAX_CLASSES; i++) {for (int j = 0; j < MAX_STUDENTS; j++) {if (classes[i][j].studentID == studentID) {classes[i][j].attendance++;printf("考勤记录成功!\n");saveStudents(); // 保存到文件return;}}}printf("未找到该学号的学生。\n"); }void calculateScores() {FILE *file = fopen("C:\\Users\\32761\\Desktop\\scores.txt", "a");if (file == NULL) {printf("无法打开文件。\n");return;}for (int i = 0; i < MAX_CLASSES; i++) {for (int j = 0; j < MAX_STUDENTS; j++) {if (classes[i][j].studentID != 0) { // 只计算已存在的学生int score = classes[i][j].attendance * 10;printf("%s 的考核分数为:%d\n", classes[i][j].name, score);fprintf(file, "%s 的考核分数为:%d\n", classes[i][j].name, score);}}}fclose(file); }void menu() {printf("学生考勤系统\n");printf("1. 添加学生\n");printf("2. 删除学生(通过学号)\n");printf("3. 修改学生信息(通过学号)\n");printf("4. 查询学生信息(通过学号)\n");printf("5. 记录考勤(通过学号)\n");printf("6. 查看考核分数\n");printf("7. 退出\n"); }int main() {loadStudents();int choice, studentID;while (1) {menu();printf("请选择操作:");scanf("%d", &choice);getchar(); // 清除输入缓冲区中的换行符switch (choice) {case 1:addStudent();break;case 2:printf("请输入学生学号:");scanf("%d", &studentID);getchar(); // 清除输入缓冲区中的换行符deleteStudentByID(studentID);break;case 3:printf("请输入学生学号:");scanf("%d", &studentID);getchar(); // 清除输入缓冲区中的换行符modifyStudentByID(studentID);break;case 4:printf("请输入学生学号:");scanf("%d", &studentID);getchar(); // 清除输入缓冲区中的换行符queryStudentByID(studentID);break;case 5:printf("请输入学生学号:");scanf("%d", &studentID);getchar(); // 清除输入缓冲区中的换行符recordAttendanceByID(studentID);break;case 6:calculateScores();break;case 7:saveStudents();printf("退出系统。\n");return 0;default:printf("无效的选择,请重试。\n");}} }