大一一年,挫败与心累,动摇与迷茫。
盛夏再临,唯余梦碎的声音。
大一即将结束,期末要求用c语言做一个学生成绩管理系统实践项目
任务书
可以实现的功能
代码部分
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最多学生人数
struct student {
char name[30]; // 姓名
int id; // 学号
char gender[10]; // 性别
int computer_score; // 计算机分数
int math_score; // 数学分数
int english_score; // 英语分数
int score[3];
int total_score;//总分
};
struct student students[MAX_STUDENTS]; // 学生数组
int num_students = 0; // 当前学生人数
// 显示主菜单
void menu() {
printf("--------------------------------------\n");
printf("学生成绩管理系统\n");
printf("--------------------------------------\n");
printf("设计者:惨绿少年\n");
printf("--------------------------------------\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("9. 删除学生信息\n");
printf("10. 添加学生信息\n");
printf("11.学生信息导出到文件\n");
printf("0. 退出程序\n");
printf("请选择:");
}
// 录入学生信息1
void input_student() {
int n;
printf("请输入学生人数:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
struct student s;
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", s.name);
printf("请输入第%d个学生的学号:", i + 1);
scanf("%d", &s.id);
printf("请输入第%d个学生的性别(男/女):", i + 1);
scanf(" %s", s.gender);
printf("请输入第%d个学生的计算机分数:", i + 1);
scanf("%d", &s.computer_score);
printf("请输入第%d个学生的数学分数:", i + 1);
scanf("%d", &s.math_score);
printf("请输入第%d个学生的英语分数:", i + 1);
scanf("%d", &s.english_score);
s.total_score = s.computer_score + s.math_score + s.english_score;
students[num_students++] = s;
}
}
//从文件录入2
void load_students() {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
while (!feof(fp)) {
struct student s;
fscanf(fp, "%s %d %s %d %d %d", s.name, &s.id, s.gender, &s.computer_score, &s.math_score, &s.english_score);
s.score[0] = s.computer_score;
s.score[1] = s.math_score;
s.score[2] = s.english_score;
s.total_score = s.computer_score + s.math_score + s.english_score;
students[num_students++] = s;
}
fclose(fp);
printf("从文件录入完成。\n");
}
//按学号排序3
void sort_by_id() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].id > students[j].id) {
struct student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("姓名\t学号\t性别\t计算机分数\t数学分数\t英语分数\n");
for (int i = 0; i < num_students; i++) {
struct student s = students[i];
printf("%s\t%d\t%s\t%d\t\t%d\t\t%d\n",
s.name, s.id, s.gender, s.computer_score, s.math_score, s.english_score);
}
}
// 按姓名查找学生信息4
void search_student_by_name() {
char name[30];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_students; i++) {
struct student s = students[i];
if (strcmp(s.name, name) == 0) {
printf("姓名\t学号\t性别\t计算机分数\t数学分数\t英语分数\n");
printf("%s\t%d\t%s\t%d\t\t%d\t\t%d\n",
s.name, s.id, s.gender, s.computer_score, s.math_score, s.english_score);
found = 1;
}
}
if (!found) {
printf("没有找到该学生的信息\n");
}
}
// 按学号查找学生信息5
void search_student_by_id() {
int id;
printf("请输入要查找的学生学号:");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_students; i++) {
struct student s = students[i];
if (s.id == id) {
printf("姓名\t学号\t性别\t计算机分数\t数学分数\t英语分数\n");
printf("%s\t%d\t%s\t%d\t\t%d\t\t%d\n",
s.name, s.id, s.gender, s.computer_score, s.math_score, s.english_score);
found = 1;
}
}
if (!found) {
printf("没有找到该学生的信息\n");
}
}
// 浏览学生信息按总分排序6
void sort_by_total_score() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].total_score < students[j].total_score) {
struct student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("按总分排序完成。\n");
printf("姓名\t学号\t性别\t计算机分数\t数学分数\t英语分数\n");
for (int i = 0; i < num_students; i++) {
struct student s = students[i];
printf("%s\t%d\t%s\t%d\t\t%d\t\t%d\n",
s.name, s.id, s.gender, s.computer_score, s.math_score, s.english_score);
}
}
//统计信息7
void statistics(struct student students[],int num_students) {
int computer_max = 0, math_max = 0, english_max = 0;
int computer_min = 100, math_min = 100, english_min = 100;
int computer_total = 0, math_total = 0, english_total = 0;
for (int i = 0; i < num_students; i++) {
if (students[i].computer_score > computer_max) {
computer_max = students[i].computer_score;
}
if (students[i].math_score > math_max) {
math_max = students[i].math_score;
}
if (students[i].english_score > english_max) {
english_max = students[i].english_score;
}
if (students[i].computer_score < computer_min) {
computer_min = students[i].computer_score;
}
if (students[i].math_score < math_min) {
math_min = students[i].math_score;
}
if (students[i].english_score < english_min) {
english_min = students[i].english_score;
}
computer_total += students[i].computer_score;
math_total += students[i].math_score;
english_total += students[i].english_score;
}
printf("计算机:最高分%d,最低分%d,平均分%d\n", computer_max, computer_min, computer_total / num_students);
printf("数学:最高分%d,最低分%d,平均分%d\n", math_max, math_min, math_total / num_students);
printf("英语:最高分%d,最低分%d,平均分%d\n", english_max, english_min, english_total / num_students);
}
//更新学生信息8
void update_student() {
int id;
printf("请输入要更新的学生学号:");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_students; i++) {
struct student *s = &students[i];
if (s->id == id) {
printf("请输入新的计算机分数:");
scanf("%d", &s->computer_score);
printf("请输入新的数学分数:");
scanf("%d", &s->math_score);
printf("请输入新的英语分数:");
scanf("%d", &s->english_score);
found = 1;
break;
}
}
if (!found) {
printf("没有找到该学生的信息\n");
} else {
printf("更新成功\n");
}
}
//删除学生信息9
void delete_student() {
int id;
printf("请输入要删除的学生学号:");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
found = 1;
break;
}
}
if (!found) {
printf("没有找到该学生的信息\n");
} else {
printf("删除成功\n");
}
}
//添加学生信息10
void add_student() {
struct student s;
printf("请输入要添加的学生姓名:");
scanf("%s", s.name);
printf("请输入要添加的学生学号:");
scanf("%d", &s.id);
printf("请输入要添加的学生性别(男/女):");
scanf(" %s", s.gender);
printf("请输入要添加的学生计算机分数:");
scanf("%d", &s.computer_score);
printf("请输入要添加的学生数学分数:");
scanf("%d", &s.math_score);
printf("请输入要添加的学生英语分数:");
scanf("%d", &s.english_score);
s.total_score = s.computer_score + s.math_score + s.english_score;
students[num_students++] = s;
printf("添加成功\n");
}
//保存到文件11
void save_students() {
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
for (int i = 0; i < num_students; i++) {
fprintf(fp, "%s %d %s %d %d %d\n", students[i].name, students[i].id, students[i].gender,
students[i].computer_score, students[i].math_score, students[i].english_score);
}
fclose(fp);
printf("保存到文件完成。\n");
}
//主函数
int main() {
while (1) {
menu();
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
input_student();
break;
case 2:
load_students();
break;
case 3:
sort_by_id();
break;
case 4:
search_student_by_name();
break;
case 5:
search_student_by_id();
break;
case 6:
sort_by_total_score();
break;
case 7:
statistics(students, num_students);
break;
case 8:
update_student();
break;
case 9:
delete_student();
break;
case 10:
add_student();
break;
case 11:
save_students();
break;
case 0:
printf("程序已退出,谢谢使用\n");
return 0;
default:
printf("无效的选项,请重新输入\n");
break;
}
}
}
//结束
项目总结
在本次实践项目中,我制作了一个学生成绩管理系统。这个系统的目的是帮助学校管理员、教师和学生管理、查询和分析学生成绩。这个系统包括了多个功能,如添加、修改、删除学生成绩、查询学生成绩等
在制作这个系统的过程中,我遇到了一些问题。例如,我需要考虑如何存储学生的基本信息和成绩。在实现系统的各个功能时,我需要考虑如何处理异常情况,如输入错误或系统故障
通过这次学生成绩管理系统的实践项目,我收获了很多宝贵的经验和知识。在这个项目中,我学习了如何使用编程语言和工具来开发一个实用的程序。在编写代码的过程中,我不断测试和调试我的代码,以确保它能够正确地处理各种情况。为了编写高质量的代码,我需要不断学习新的编程技巧和技术,并遵循最佳实践。这些经验将对我未来产生积极的影响。
如果发现程序存在问题,欢迎留言指正。