文章目录
- 前言
- 功能
- 运行截图
- 所有代码
前言
无套路,均已上机通过,求个关注求个赞,提供答疑解惑服务。
功能
本课题要求用C语言编写某班级学生学情管理系统。对一个有若干个学生的学院,每个学生有N门课程,系统实现对学生情况的管理。学生信息包括:学号、姓名、班级、身高、体重、身体健康,各门课程的成绩、家庭住址、本人联系电话、紧急联系人及联系电话、本人QQ号、本人特长、本人智育排名、任课教师姓名及手机号等信息。系统要求能够实现以下功能:
- 1、 信息录入:建立学生情况记录数据库;
- 2、 显示:按照学号顺序显示所有学生信息记录;
- 3、 增加记录:增加学生的信息记录;
- 4、 修改(要求先输入密码):可修改某个学生的基本信息(学号不可改变)。
- 5、 删除:删除一个或多个学生的记录;
- 6、 检索:可按“学号”、“姓名”或者“手机号”查询学生的记录,可提供模糊检索;
- 7、 成绩统计:可显示某门课程最高成绩及其名单、最差成绩及其名单、平均成绩、及格人数、不及格人数、及格率;
- 8、 智育排名序:按照智育排名排序,并将学生信息按顺序显示。
运行截图
所有代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char studentID[20];
char name[20];
float height;
float weight;
char address[50];
char phoneNumber[15];
char emergencyContact[20];
char emergencyPhoneNumber[15];
char qqNumber[15];
char specialSkills[50];
int intelligenceRank;
char teacherName[20];
char teacherPhoneNumber[15];
} Student;
typedef struct {
char courseName[20];
float score;
} Course;
typedef struct StudentNode {
Student data;
Course *courses; // 课程成绩数组
struct StudentNode *next;
} StudentNode;
// Function Declarations
void displayMenu();
void loadStudentsFromFile(StudentNode **head);
void saveStudentsToFile(StudentNode *head);
void addStudentRecord(StudentNode **head);
void displayAllStudents(StudentNode *head);
void modifyStudentRecord(StudentNode *head);
void deleteStudentRecord(StudentNode **head);
void searchStudentRecord(StudentNode *head);
void gradeStatistics(StudentNode *head);
void rankByIntelligence(StudentNode *head);
void searchByStudentID(StudentNode *head, const char *targetID);
void searchByName(StudentNode *head, const char *targetName);
void freeMemory(StudentNode *head);
void searchByPhoneNumber(StudentNode *head, const char *targetPhoneNumber);
void gradeStatistics(StudentNode *head);
void gradeStatistics(StudentNode *head);
void searchByName(StudentNode *head, const char *targetName);
void gradeStatistics(StudentNode *head);
int main() {
StudentNode *head = NULL;
loadStudentsFromFile(&head);
int choice;
do {
displayMenu();
printf("请输入你的选择: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudentRecord(&head);
break;
case 2:
displayAllStudents(head);
break;
case 3:
modifyStudentRecord(head);
break;
case 4:
deleteStudentRecord(&head);
break;
case 5:
searchStudentRecord(head);
break;
case 6:
gradeStatistics(head);
break;
case 7:
rankByIntelligence(head);
break;
case 8:
saveStudentsToFile(head);
break;
case 0:
printf("程序退出。再见!\n");
break;
default:
printf("无效的选择,请重试。\n");
}
} while (choice != 0);
freeMemory(head);
return 0;
}
void displayMenu() {
printf("\n===== 学生信息管理系统 =====\n");
printf("1. 添加学生记录\n");
printf("2. 显示所有学生\n");
printf("3. 修改学生记录\n");
printf("4. 删除学生记录\n");
printf("5. 搜索学生记录\n");
printf("6. 成绩统计\n");
printf("7. 智育排名\n");
printf("8. 将学生信息保存到文件\n");
printf("0. 退出\n");
}
void loadStudentsFromFile(StudentNode **head) {
FILE *file = fopen("students.txt", "r");
if (file == NULL) {
printf("打开文件读取错误。\n");
return;
}
while (!feof(file)) {
StudentNode *newStudent = (StudentNode *) malloc(sizeof(StudentNode));
if (newStudent == NULL) {
printf("内存分配错误。\n");
fclose(file);
return;
}
fscanf(file, "学号: %s\n", newStudent->data.studentID);
if (feof(file)) {
free(newStudent);
break; // 避免读取到文件末尾多分配一个节点
}
fscanf(file, "姓名: %s\n", newStudent->data.name);
fscanf(file, "身高: %f\n", &newStudent->data.height);
fscanf(file, "体重: %f\n", &newStudent->data.weight);
fscanf(file, "地址: %s\n", newStudent->data.address);
fscanf(file, "联系电话: %s\n", newStudent->data.phoneNumber);
fscanf(file, "紧急联系人: %s\n", newStudent->data.emergencyContact);
fscanf(file, "紧急联系电话: %s\n", newStudent->data.emergencyPhoneNumber);
fscanf(file, "QQ号: %s\n", newStudent->data.qqNumber);
fscanf(file, "特长: %s\n", newStudent->data.specialSkills);
fscanf(file, "智育排名: %d\n", &newStudent->data.intelligenceRank);
fscanf(file, "任课教师姓名: %s\n", newStudent->data.teacherName);
fscanf(file, "任课教师联系电话: %s\n", newStudent->data.teacherPhoneNumber);
newStudent->courses = (Course *) malloc(3 * sizeof(Course)); // 假设每个学生有三门课程
if (newStudent->courses == NULL) {
printf("内存分配错误。\n");
free(newStudent);
fclose(file);
return;
}
for (int i = 0; i < 3; i++) { // 假设每个学生有三门课程
fscanf(file, "课程: %s, 成绩: %f\n", newStudent->courses[i].courseName, &newStudent->courses[i].score);
}
newStudent->next = *head;
*head = newStudent;
}
fclose(file);
}
void saveStudentsToFile(StudentNode *head) {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("打开文件写入错误。\n");
return;
}
StudentNode *current = head;
while (current != NULL) {
fprintf(file, "学号: %s\n", current->data.studentID);
fprintf(file, "姓名: %s\n", current->data.name);
fprintf(file, "身高: %.2f\n", current->data.height);
fprintf(file, "体重: %.2f\n", current->data.weight);
fprintf(file, "地址: %s\n", current->data.address);
fprintf(file, "联系电话: %s\n", current->data.phoneNumber);
fprintf(file, "紧急联系人: %s\n", current->data.emergencyContact);
fprintf(file, "紧急联系电话: %s\n", current->data.emergencyPhoneNumber);
fprintf(file, "QQ号: %s\n", current->data.qqNumber);
fprintf(file, "特长: %s\n", current->data.specialSkills);
fprintf(file, "智育排名: %d\n", current->data.intelligenceRank);
fprintf(file, "任课教师姓名: %s\n", current->data.teacherName);
fprintf(file, "任课教师联系电话: %s\n", current->data.teacherPhoneNumber);
for (int i = 0; i < 3; i++) { // 假设每个学生有三门课程
fprintf(file, "课程: %s, 成绩: %.2f\n", current->courses[i].courseName, current->courses[i].score);
}
fprintf(file, "\n");
current = current->next;
}
fclose(file);
}
void addStudentRecord(StudentNode **head) {
StudentNode *newStudent = (StudentNode *) malloc(sizeof(StudentNode));
if (newStudent == NULL) {
printf("内存分配错误。\n");
return;
}
printf("输入学号: ");
scanf("%s", newStudent->data.studentID);
// 检查是否已存在相同学号的记录,避免重复
StudentNode *current = *head;
while (current != NULL) {
if (strcmp(current->data.studentID, newStudent->data.studentID) == 0) {
printf("错误: 学号已存在。\n");
free(newStudent);
return;
}
current = current->next;
}
printf("输入姓名: ");
scanf("%s", newStudent->data.name);
printf("输入身高: ");
scanf("%f", &newStudent->data.height);
printf("输入体重: ");
scanf("%f", &newStudent->data.weight);
printf("输入地址: ");
scanf("%s", newStudent->data.address);
printf("输入联系电话: ");
scanf("%s", newStudent->data.phoneNumber);
printf("输入紧急联系人: ");
scanf("%s", newStudent->data.emergencyContact);
printf("输入紧急联系电话: ");
scanf("%s", newStudent->data.emergencyPhoneNumber);
printf("输入QQ号: ");
scanf("%s", newStudent->data.qqNumber);
printf("输入特长: ");
scanf("%s", newStudent->data.specialSkills);
printf("输入智育排名: ");
scanf("%d", &newStudent->data.intelligenceRank);
printf("输入任课教师姓名: ");
scanf("%s", newStudent->data.teacherName);
printf("输入任课教师联系电话: ");
scanf("%s", newStudent->data.teacherPhoneNumber);
newStudent->courses = (Course *) malloc(3 * sizeof(Course)); // 假设每个学生有三门课程
if (newStudent->courses == NULL) {
printf("内存分配错误。\n");
free(newStudent);
return;
}
for (int i = 0; i < 3; i++) { // 假设每个学生有三门课程
printf("输入第 %d 门课程名称: ", i + 1);
scanf("%s", newStudent->courses[i].courseName);
printf("输入第 %d 门课程成绩: ", i + 1);
scanf("%f", &newStudent->courses[i].score);
}
newStudent->next = *head;
*head = newStudent;
printf("学生记录添加成功。\n");
}
void displayAllStudents(StudentNode *head) {
if (head == NULL) {
printf("无可用学生记录。\n");
return;
}
printf("\n===== 所有学生 =====\n");
StudentNode *current = head;
while (current != NULL) {
printf("学号: %s\n", current->data.studentID);
printf("姓名: %s\n", current->data.name);
printf("身高: %.2f\n", current->data.height);
printf("体重: %.2f\n", current->data.weight);
printf("地址: %s\n", current->data.address);
printf("联系电话: %s\n", current->data.phoneNumber);
printf("紧急联系人: %s\n", current->data.emergencyContact);
printf("紧急联系电话: %s\n", current->data.emergencyPhoneNumber);
printf("QQ号: %s\n", current->data.qqNumber);
printf("特长: %s\n", current->data.specialSkills);
printf("智育排名: %d\n", current->data.intelligenceRank);
printf("任课教师姓名: %s\n", current->data.teacherName);
printf("任课教师联系电话: %s\n", current->data.teacherPhoneNumber);
for (int i = 0; i < 3; i++) { // 假设每个学生有三门课程
printf("课程: %s, 成绩: %.2f\n", current->courses[i].courseName, current->courses[i].score);
}
printf("\n");
current = current->next;
}
}
void modifyStudentRecord(StudentNode *head) {
if (head == NULL) {
printf("无可用学生记录。\n");
return;
}
char targetID[20];
printf("请输入要修改的学生学号: ");
scanf("%s", targetID);
StudentNode *current = head;
while (current != NULL) {
if (strcmp(current->data.studentID, targetID) == 0) {
printf("学生找到,现在可以进行修改。\n");
printf("请输入新的姓名: ");
scanf("%s", current->data.name);
printf("请输入新的身高: ");
scanf("%f", ¤t->data.height);
printf("请输入新的体重: ");
scanf("%f", ¤t->data.weight);
printf("请输入新的地址: ");
scanf("%s", current->data.address);
printf("请输入新的联系电话: ");
scanf("%s", current->data.phoneNumber);
printf("请输入新的紧急联系人: ");
scanf("%s", current->data.emergencyContact);
printf("请输入新的紧急联系电话: ");
scanf("%s", current->data.emergencyPhoneNumber);
printf("请输入新的QQ号: ");
scanf("%s", current->data.qqNumber);
printf("请输入新的特长: ");
scanf("%s", current->data.specialSkills);
printf("请输入新的智育排名: ");
scanf("%d", ¤t->data.intelligenceRank);
printf("请输入新的任课教师姓名: ");
scanf("%s", current->data.teacherName);
printf("请输入新的任课教师联系电话: ");
scanf("%s", current->data.teacherPhoneNumber);
for (int i = 0; i < 3; i++) {
printf("请输入新的第 %d 门课程名称: ", i + 1);
scanf("%s", current->courses[i].courseName);
printf("请输入新的第 %d 门课程成绩: ", i + 1);
scanf("%f", ¤t->courses[i].score);
}
printf("学生信息修改成功。\n");
return;
}
current = current->next;
}
printf("未找到学生学号为 %s 的记录。\n", targetID);
}
void deleteStudentRecord(StudentNode **head) {
if (*head == NULL) {
printf("无可用学生记录。\n");
return;
}
char targetID[20];
printf("请输入要删除的学生学号: ");
scanf("%s", targetID);
StudentNode *current = *head;
StudentNode *prev = NULL;
while (current != NULL) {
if (strcmp(current->data.studentID, targetID) == 0) {
if (prev == NULL) {
*head = current->next;
} else {
prev->next = current->next;
}
free(current->courses);
free(current);
printf("学生信息删除成功。\n");
return;
}
prev = current;
current = current->next;
}
printf("未找到学生学号为 %s 的记录。\n", targetID);
}
void searchStudentRecord(StudentNode *head) {
if (head == NULL) {
printf("无可用学生记录。\n");
return;
}
int choice;
printf("请选择搜索方式:\n");
printf("1. 按学号搜索\n");
printf("2. 按姓名搜索\n");
printf("3. 按手机号搜索\n");
printf("请输入你的选择: ");
scanf("%d", &choice);
char target[20];
switch (choice) {
case 1:
printf("请输入要搜索的学生学号: ");
scanf("%s", target);
searchByStudentID(head, target);
break;
case 2:
printf("请输入要搜索的学生姓名: ");
scanf("%s", target);
searchByName(head, target);
break;
case 3:
printf("请输入要搜索的学生手机号: ");
scanf("%s", target);
searchByPhoneNumber(head, target);
break;
default:
printf("无效的选择。\n");
break;
}
}
void searchByPhoneNumber(StudentNode *head, const char *targetPhoneNumber) {
if (head == NULL) {
printf("无可用学生记录。\n");
return;
}
int found = 0;
StudentNode *current = head;
while (current != NULL) {
if (strcmp(current->data.phoneNumber, targetPhoneNumber) == 0 ||
strcmp(current->data.emergencyPhoneNumber, targetPhoneNumber) == 0) {
printf("\n===== 学生信息 =====\n");
printf("学号: %s\n", current->data.studentID);
printf("姓名: %s\n", current->data.name);
printf("身高: %.2f\n", current->data.height);
printf("体重: %.2f\n", current->data.weight);
printf("地址: %s\n", current->data.address);
printf("联系电话: %s\n", current->data.phoneNumber);
printf("紧急联系人: %s\n", current->data.emergencyContact);
printf("紧急联系电话: %s\n", current->data.emergencyPhoneNumber);
printf("QQ号: %s\n", current->data.qqNumber);
printf("特长: %s\n", current->data.specialSkills);
printf("智育排名: %d\n", current->data.intelligenceRank);
printf("任课教师姓名: %s\n", current->data.teacherName);
printf("任课教师联系电话: %s\n", current->data.teacherPhoneNumber);
for (int i = 0; i < 3; i++) {
printf("课程: %s, 成绩: %.2f\n", current->courses[i].courseName, current->courses[i].score);
}
found = 1;
break;
}
current = current->next;
}
if (!found) {
printf("未找到联系电话为 %s 的学生记录。\n", targetPhoneNumber);
}
}
void gradeStatistics(StudentNode *head) {
if (head == NULL) {
printf("无可用学生记录。\n");
return;
}
char courseName[20];
printf("请输入要统计的课程名称: ");
scanf("%s", courseName);
int countPass = 0;
int countFail = 0;
float sum = 0.0;
float maxScore = -1.0;
float minScore = 101.0;
StudentNode *current = head;
while (current != NULL) {
for (int i = 0; i < 3; i++) {
if (strcmp(current->courses[i].courseName, courseName) == 0) {
float score = current->courses[i].score;
sum += score;
if (score >= 60.0) {
countPass++;
} else {
countFail++;
}
if (score > maxScore) {
maxScore = score;
}
if (score < minScore) {
minScore = score;
}
}
}
current = current->next;
}
if (countPass + countFail == 0) {
printf("该课程暂无学生成绩。\n");
} else {
float average = sum / (countPass + countFail);
float passRate = (float)countPass / (countPass + countFail) * 100.0;
printf("\n===== 成绩统计 =====\n");
printf("最高分: %.2f\n", maxScore);
printf("最低分: %.2f\n", minScore);
printf("平均分: %.2f\n", average);
printf("及格人数: %d\n", countPass);
printf("不及格人数: %d\n", countFail);
printf("及格率: %.2f%%\n", passRate);
}
}
void rankByIntelligence(StudentNode *head) {
if (head == NULL) {
printf("无可用学生记录。\n");
return;
}
// 计算链表长度
int count = 0;
StudentNode *current = head;
while (current != NULL) {
count++;
current = current->next;
}
// 将链表数据提取到数组中以便排序
Student *students = (Student *)malloc(count * sizeof(Student));
if (students == NULL) {
printf("内存分配错误。\n");
return;
}
current = head;
for (int i = 0; i < count; i++) {
students[i] = current->data;
current = current->next;
}
// 使用冒泡排序按照智育排名排序
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (students[j].intelligenceRank < students[j + 1].intelligenceRank) {
// 交换两个学生的位置
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
// 显示排序结果
printf("\n===== 智育排名 =====\n");
for (int i = 0; i < count; i++) {
printf("排名 %d:\n", i + 1);
printf("学号: %s\n", students[i].studentID);
printf("姓名: %s\n", students[i].name);
printf("智育排名: %d\n", students[i].intelligenceRank);
printf("\n");
}
free(students);
}
void freeMemory(StudentNode *head) {
while (head != NULL) {
StudentNode *temp = head;
head = head->next;
free(temp->courses);
free(temp);
}
}
void searchByStudentID(StudentNode *head, const char *targetID) {
if (head == NULL) {
printf("无可用学生记录。\n");
return;
}
int found = 0;
StudentNode *current = head;
while (current != NULL) {
if (strcmp(current->data.studentID, targetID) == 0) {
printf("\n===== 学生信息 =====\n");
printf("学号: %s\n", current->data.studentID);
printf("姓名: %s\n", current->data.name);
printf("身高: %.2f\n", current->data.height);
printf("体重: %.2f\n", current->data.weight);
printf("地址: %s\n", current->data.address);
printf("联系电话: %s\n", current->data.phoneNumber);
printf("紧急联系人: %s\n", current->data.emergencyContact);
printf("紧急联系电话: %s\n", current->data.emergencyPhoneNumber);
printf("QQ号: %s\n", current->data.qqNumber);
printf("特长: %s\n", current->data.specialSkills);
printf("智育排名: %d\n", current->data.intelligenceRank);
printf("任课教师姓名: %s\n", current->data.teacherName);
printf("任课教师联系电话: %s\n", current->data.teacherPhoneNumber);
for (int i = 0; i < 3; i++) {
printf("课程: %s, 成绩: %.2f\n", current->courses[i].courseName, current->courses[i].score);
}
found = 1;
break;
}
current = current->next;
}
if (!found) {
printf("未找到学号为 %s 的学生记录。\n", targetID);
}
}
void searchByName(StudentNode *head, const char *targetName) {
if (head == NULL) {
printf("无可用学生记录。\n");
return;
}
int found = 0;
StudentNode *current = head;
while (current != NULL) {
if (strcmp(current->data.name, targetName) == 0) {
printf("\n===== 学生信息 =====\n");
printf("学号: %s\n", current->data.studentID);
printf("姓名: %s\n", current->data.name);
printf("身高: %.2f\n", current->data.height);
printf("体重: %.2f\n", current->data.weight);
printf("地址: %s\n", current->data.address);
printf("联系电话: %s\n", current->data.phoneNumber);
printf("紧急联系人: %s\n", current->data.emergencyContact);
printf("紧急联系电话: %s\n", current->data.emergencyPhoneNumber);
printf("QQ号: %s\n", current->data.qqNumber);
printf("特长: %s\n", current->data.specialSkills);
printf("智育排名: %d\n", current->data.intelligenceRank);
printf("任课教师姓名: %s\n", current->data.teacherName);
printf("任课教师联系电话: %s\n", current->data.teacherPhoneNumber);
for (int i = 0; i < 3; i++) {
printf("课程: %s, 成绩: %.2f\n", current->courses[i].courseName, current->courses[i].score);
}
found = 1;
break;
}
current = current->next;
}
if (!found) {
printf("未找到姓名为 %s 的学生记录。\n", targetName);
}
}