用c语言写:设计和实现一个简单的学生管理系统,具有以下功能:1. 学生信息录入:可以录入学生的基本信息,包括学号、姓名、性别、年龄、所在院系等信息。要求支持多名学生信息的录入。2. 学生信息查询:可以按学号或姓名进行学生信息的查询,找到相应学生的基本信息。3. 学生成绩录入:可以为每名学生录入多门课程的成绩,包括课程名称、学分、成绩等。4. 学生成绩查询:可以按学号和课程名称查询某名学生的指定课程成绩。5. 课程平均分查询:可以查询每门课程的平均成绩。6. 学生总分统计:可以统计每名学生的总学分和总成绩,以及根据总成绩计算学生的平均分。该系统需要提供一个菜单界面,让用户选择不同的功能;需要定义学生信息结构体和成绩结构体、考虑利用数据文件等存储数据。
时间: 2024-03-16 07:43:04 浏览: 62
下面是一个简单的学生管理系统的代码实现,使用了C语言和文件操作:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
#define MAX_COURSE_NUM 10
#define MAX_NAME_LEN 20
#define FILE_NAME "student.dat"
// 学生信息结构体
struct Student {
int id;
char name[MAX_NAME_LEN];
char gender;
int age;
char department[MAX_NAME_LEN];
int courses[MAX_COURSE_NUM];
};
// 成绩结构体
struct Score {
char course[MAX_NAME_LEN];
int credit;
int score;
};
// 学生信息数组
struct Student students[MAX_STUDENT_NUM];
int studentNum = 0;
// 成绩数组
struct Score scores[MAX_COURSE_NUM];
int courseNum = 0;
// 读取数据文件
void readData() {
FILE* fp = fopen(FILE_NAME, "rb");
if (fp == NULL) {
printf("Data file not found!\n");
return;
}
fread(&studentNum, sizeof(int), 1, fp);
fread(students, sizeof(struct Student), studentNum, fp);
fread(&courseNum, sizeof(int), 1, fp);
fread(scores, sizeof(struct Score), courseNum, fp);
fclose(fp);
}
// 写入数据文件
void writeData() {
FILE* fp = fopen(FILE_NAME, "wb");
if (fp == NULL) {
printf("Cannot create data file!\n");
return;
}
fwrite(&studentNum, sizeof(int), 1, fp);
fwrite(students, sizeof(struct Student), studentNum, fp);
fwrite(&courseNum, sizeof(int), 1, fp);
fwrite(scores, sizeof(struct Score), courseNum, fp);
fclose(fp);
}
// 添加学生信息
void addStudent() {
if (studentNum >= MAX_STUDENT_NUM) {
printf("Cannot add more students!\n");
return;
}
struct Student student;
printf("Please input student's ID: ");
scanf("%d", &student.id);
printf("Please input student's name: ");
scanf("%s", student.name);
printf("Please input student's gender (M/F): ");
scanf(" %c", &student.gender);
printf("Please input student's age: ");
scanf("%d", &student.age);
printf("Please input student's department: ");
scanf("%s", student.department);
students[studentNum++] = student;
printf("Student added successfully!\n");
}
// 查询学生信息
void queryStudent() {
int choice;
printf("Please choose search criteria:\n");
printf("1. ID\n");
printf("2. Name\n");
scanf("%d", &choice);
if (choice == 1) {
int id;
printf("Please input student's ID: ");
scanf("%d", &id);
for (int i = 0; i < studentNum; i++) {
if (students[i].id == id) {
printf("ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Gender: %c\n", students[i].gender);
printf("Age: %d\n", students[i].age);
printf("Department: %s\n", students[i].department);
return;
}
}
} else if (choice == 2) {
char name[MAX_NAME_LEN];
printf("Please input student's name: ");
scanf("%s", name);
for (int i = 0; i < studentNum; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Gender: %c\n", students[i].gender);
printf("Age: %d\n", students[i].age);
printf("Department: %s\n", students[i].department);
return;
}
}
}
printf("Student not found!\n");
}
// 添加成绩
void addScore() {
if (courseNum >= MAX_COURSE_NUM) {
printf("Cannot add more courses!\n");
return;
}
struct Score score;
printf("Please input course name: ");
scanf("%s", score.course);
printf("Please input course credit: ");
scanf("%d", &score.credit);
for (int i = 0; i < studentNum; i++) {
printf("Please input score for %s (ID: %d): ", students[i].name, students[i].id);
scanf("%d", &students[i].courses[courseNum]);
}
scores[courseNum++] = score;
printf("Score added successfully!\n");
}
// 查询成绩
void queryScore() {
int id;
printf("Please input student's ID: ");
scanf("%d", &id);
for (int i = 0; i < studentNum; i++) {
if (students[i].id == id) {
printf("Student: %s\n", students[i].name);
printf("Course\tCredit\tScore\n");
for (int j = 0; j < courseNum; j++) {
printf("%s\t%d\t%d\n", scores[j].course, scores[j].credit, students[i].courses[j]);
}
return;
}
}
printf("Student not found!\n");
}
// 查询课程平均分
void queryAverageScore() {
for (int i = 0; i < courseNum; i++) {
int sum = 0;
for (int j = 0; j < studentNum; j++) {
sum += students[j].courses[i];
}
printf("%s: %.2f\n", scores[i].course, (float)sum / studentNum);
}
}
// 查询学生总分统计
void queryTotalScore() {
for (int i = 0; i < studentNum; i++) {
int sum = 0;
for (int j = 0; j < courseNum; j++) {
sum += students[i].courses[j] * scores[j].credit;
}
printf("%s: Total %d credits, total score %d, average %.2f\n", students[i].name,
sum, sum / 10, (float)sum / (10 * courseNum));
}
}
// 主菜单
void showMenu() {
printf("Welcome to student management system!\n");
printf("1. Add student\n");
printf("2. Query student\n");
printf("3. Add score\n");
printf("4. Query score\n");
printf("5. Query average score\n");
printf("6. Query total score\n");
printf("0. Exit\n");
}
// 主函数
int main() {
readData();
while (1) {
showMenu();
int choice;
scanf("%d", &choice);
switch (choice) {
case 0:
writeData();
return 0;
case 1:
addStudent();
break;
case 2:
queryStudent();
break;
case 3:
addScore();
break;
case 4:
queryScore();
break;
case 5:
queryAverageScore();
break;
case 6:
queryTotalScore();
break;
default:
printf("Invalid choice!\n");
break;
}
}
}
```
这个学生管理系统可以录入学生信息和成绩,并支持查询功能,包括按学号或姓名查询学生信息,按学号和课程名称查询学生成绩,查询每门课程的平均成绩,统计每名学生的总学分和总成绩。数据存储在文件中,程序启动时从文件中读取数据,程序结束时将数据保存到文件中。
阅读全文