学生管理系统使用的是C语言控制台完成的
有以下功能:
======================
学生管理系统
[1] 添加学生
[2] 添加学生成绩
[3] 修改学生成绩
[4] 显示学生列表
[5] 导出学生信息到文件
[6] 从文件导入学生信息
====================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Grade {
char subject[50]; // 科目名称
float score; // 成绩
struct Grade* next; // 下一个科目节点指针
};
struct Student {
char studentID[10]; // 学号
char name[50]; // 姓名
int age; // 年龄
float gpa; // GPA
struct Grade* grades; // 科目链表头指针
struct Student* next; // 下一个学生节点指针
};
struct Student* head = NULL; // 学生链表头指针
int studentCount = 0; // 学生数量
// 检查学号是否存在
int isStudentIDExists(char* studentID) {
struct Student* current = head;
while (current != NULL) {
if (strcmp(current->studentID, studentID) == 0) {
return 1; // 学号存在
}
current = current->next;
}
return 0; // 学号不存在
}
// 自动生成学生学号
void generateStudentID(char* studentID) {
static int id = 1;
do {
sprintf(studentID, "S%04d", id);
id++;
} while (isStudentIDExists(studentID));
}
// 添加学生
void addStudent() {
struct Student* newStudent = (struct Student*)malloc(sizeof(struct Student));
generateStudentID(newStudent->studentID); // 自动生成学号
printf("自动生成的学号:%s\n", newStudent->studentID);
printf("请输入学生姓名:");
scanf("%s", newStudent->name);
printf("请输入学生年龄:");
scanf("%d", &(newStudent->age));
printf("请输入学生GPA:");
scanf("%f", &(newStudent->gpa));
newStudent->grades = NULL;
newStudent->next = NULL;
if (head == NULL) {
head = newStudent;
} else {
struct Student* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
}
studentCount++;
printf("学生添加成功!\n");
}
// 添加学生成绩
void addGrade(struct Student* student) {
char choice;
do {
struct Grade* newGrade = (struct Grade*)malloc(sizeof(struct Grade));
printf("请输入科目名称:");
scanf("%s", newGrade->subject);
printf("请输入成绩:");
scanf("%f", &(newGrade->score));
newGrade->next = NULL;
if (student->grades == NULL) {
student->grades = newGrade;
} else {
struct Grade* current = student->grades;
while (current->next != NULL) {
current = current->next;
}
current->next = newGrade;
}
printf("成绩添加成功!\n");
printf("是否继续添加成绩?(Y/N):");
scanf(" %c", &choice); // 注意这里的空格,用于消除换行符的影响
} while (choice == 'Y' || choice == 'y');
}
// 修改学生成绩
void modifyGrade(struct Student* student) {
char choice;
do {
char subject[50];
printf("请输入要修改成绩的科目:");
scanf("%s", subject);
struct Grade* current = student->grades;
while (current != NULL) {
if (strcmp(current->subject, subject) == 0) {
printf("当前科目:%s\t当前成绩:%.2f\n", current->subject, current->score);
printf("请输入新的成绩:");
scanf("%f", &(current->score));
printf("成绩修改成功!\n");
printf("是否继续修改成绩?(Y/N):");
scanf(" %c", &choice); // 注意这里的空格,用于消除换行符的影响
break;
}
current = current->next;
}
if (current == NULL) {
printf("未找到指定科目的成绩。\n");
break;
}
} while (choice == 'Y' || choice == 'y');
}
// 显示学生列表
void displayStudents() {
struct Student* current = head;
while (current != NULL) {
printf("学号:%s \t", current->studentID);
printf("姓名:%s \t", current->name);
printf("年龄:%d \t", current->age);
printf("GPA:%.2f \n", current->gpa);
printf("成绩列表:\n");
struct Grade* grade = current->grades;
while (grade != NULL) {
printf("科目:%s\t成绩:%.2f ", grade->subject, grade->score);
grade = grade->next;
}
printf("\n");
current = current->next;
}
}
// 导出学生信息到文件
void exportStudents() {
FILE* file = fopen("students.txt", "w");
if (file == NULL) {
printf("无法打开文件。\n");
return;
}
struct Student* current = head;
while (current != NULL) {
fprintf(file, "%s,%s,%d,%.2f", current->studentID, current->name, current->age, current->gpa);
struct Grade* grade = current->grades;
while (grade != NULL) {
fprintf(file, ",%s,%.2f", grade->subject, grade->score);
grade = grade->next;
}
fprintf(file, "\n");
current = current->next;
}
fclose(file);
printf("学生信息导出成功!\n");
}
// 从文件导入学生信息
void importStudents() {
FILE* file = fopen("students.txt", "r");
if (file == NULL) {
printf("无法打开文件。\n");
return;
}
// 清空之前的数据链表
while (head != NULL) {
struct Student* temp = head;
head = head->next;
struct Grade* grade = temp->grades;
while (grade != NULL) {
struct Grade* tempGrade = grade;
grade = grade->next;
free(tempGrade);
}
free(temp);
}
char line[256];
while (fgets(line, sizeof(line), file)) {
struct Student* newStudent = (struct Student*)malloc(sizeof(struct Student));
char* token = strtok(line, ",");
strcpy(newStudent->studentID, token);
token = strtok(NULL, ",");
strcpy(newStudent->name, token);
token = strtok(NULL, ",");
newStudent->age = atoi(token);
token = strtok(NULL, ",");
newStudent->gpa = atof(token);
newStudent->grades = NULL;
newStudent->next = NULL;
token = strtok(NULL, ",");
while (token != NULL) {
struct Grade* newGrade = (struct Grade*)malloc(sizeof(struct Grade));
strcpy(newGrade->subject, token);
token = strtok(NULL, ",");
newGrade->score = atof(token);
newGrade->next = NULL;
if (newStudent->grades == NULL) {
newStudent->grades = newGrade;
} else {
struct Grade* current = newStudent->grades;
while (current->next != NULL) {
current = current->next;
}
current->next = newGrade;
}
token = strtok(NULL, ",");
}
if (head == NULL) {
head = newStudent;
} else {
struct Student* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
}
studentCount++;
}
fclose(file);
printf("学生信息导入成功!\n");
}
// 打印居中对齐的字符串
void printCenterAligned(const char* str, int width) {
int len = strlen(str);
int padding = (width - len) / 2;
printf("%*s%s%*s", padding, "", str, padding, (padding * 2 + len == width) ? "" : " ");
}
// 主函数
int main() {
int choice;
do {
printf("======================\n");
printCenterAligned("学生管理系统", 22);
printf("\n======================\n");
printf("[1] 添加学生\n");
printf("[2] 添加学生成绩\n");
printf("[3] 修改学生成绩\n");
printf("[4] 显示学生列表\n");
printf("[5] 导出学生信息到文件\n");
printf("[6] 从文件导入学生信息\n");
printf("[0] 退出\n");
printf("======================\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2: {
char studentID[10];
printf("请输入学生学号:");
scanf("%s", studentID);
struct Student* current = head;
while (current != NULL) {
if (strcmp(current->studentID, studentID) == 0) {
addGrade(current);
break;
}
current = current->next;
}
if (current == NULL) {
printf("未找到指定学号的学生。\n");
}
break;
}
case 3: {
char studentID[10];
printf("请输入学生学号:");
scanf("%s", studentID);
struct Student* current = head;
while (current != NULL) {
if (strcmp(current->studentID, studentID) == 0) {
modifyGrade(current);
break;
}
current = current->next;
}
if (current == NULL) {
printf("未找到指定学号的学生。\n");
}
break;
}
case 4:
displayStudents();
break;
case 5:
exportStudents();
break;
case 6:
importStudents();
break;
case 0:
printf("再见!\n");
break;
default:
printf("无效的选择。\n");
break;
}
printf("\n");
} while (choice != 0);
return 0;
}