用c语言设计一个学生成绩管理系统,实现对信息进行排序,取平均数,取总分,修改,删除,查询,添加新的学生信息的功能
时间: 2024-06-14 12:06:07 浏览: 103
以下是一个用C语言设计的学生成绩管理系统的示例,实现了对信息进行排序、取平均数、取总分、修改、删除、查询和添加新的学生信息的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
char name[50];
int id;
float score;
};
struct Student students[MAX_STUDENTS];
int numStudents = 0;
void addStudent() {
if (numStudents >= MAX_STUDENTS) {
printf("学生信息已满,无法添加新的学生。\n");
return;
}
struct Student newStudent;
printf("请输入学生姓名:");
scanf("%s", newStudent.name);
printf("请输入学生学号:");
scanf("%d", &newStudent.id);
printf("请输入学生成绩:");
scanf("%f", &newStudent.score);
students[numStudents] = newStudent;
numStudents++;
printf("学生信息添加成功。\n");
}
void printStudents() {
if (numStudents == 0) {
printf("学生信息为空。\n");
return;
}
printf("学生信息如下:\n");
printf("姓名\t学号\t成绩\n");
for (int i = 0; i < numStudents; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].score);
}
}
void sortStudents() {
if (numStudents == 0) {
printf("学生信息为空,无法排序。\n");
return;
}
for (int i = 0; i < numStudents - 1; i++) {
for (int j = 0; j < numStudents - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
struct Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("学生信息已按成绩降序排序。\n");
}
float calculateAverageScore() {
if (numStudents == 0) {
printf("学生信息为空,无法计算平均分。\n");
return 0;
}
float totalScore = 0;
for (int i = 0; i < numStudents; i++) {
totalScore += students[i].score;
}
float averageScore = totalScore / numStudents;
return averageScore;
}
float calculateTotalScore() {
if (numStudents == 0) {
printf("学生信息为空,无法计算总分。\n");
return 0;
}
float totalScore = 0;
for (int i = 0; i < numStudents; i++) {
totalScore += students[i].score;
}
return totalScore;
}
void modifyStudent() {
if (numStudents == 0) {
printf("学生信息为空,无法修改。\n");
return;
}
int studentId;
printf("请输入要修改的学生学号:");
scanf("%d", &studentId);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (students[i].id == studentId) {
printf("请输入新的学生姓名:");
scanf("%s", students[i].name);
printf("请输入新的学生成绩:");
scanf("%f", &students[i].score);
found = 1;
break;
}
}
if (found) {
printf("学生信息修改成功。\n");
} else {
printf("未找到该学生学号的信息。\n");
}
}
void deleteStudent() {
if (numStudents == 0) {
printf("学生信息为空,无法删除。\n");
return;
}
int studentId;
printf("请输入要删除的学生学号:");
scanf("%d", &studentId);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (students[i].id == studentId) {
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
numStudents--;
found = 1;
break;
}
}
if (found) {
printf("学生信息删除成功。\n");
} else {
printf("未找到该学生学号的信息。\n");
}
}
void searchStudent() {
if (numStudents == 0) {
printf("学生信息为空,无法查询。\n");
return;
}
int choice;
printf("请选择查询方式:\n");
printf("1.按学号查询\n");
printf("2.按姓名查询\n");
printf("请输入选项:");
scanf("%d", &choice);
if (choice == 1) {
int studentId;
printf("请输入要查询的学生学号:");
scanf("%d", &studentId);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (students[i].id == studentId) {
printf("学生信息如下:\n");
printf("姓名\t学号\t成绩\n");
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].score);
found = 1;
break;
}
}
if (!found) {
printf("未找到该学生学号的信息。\n");
}
} else if (choice == 2) {
char studentName[50];
printf("请输入要查询的学生姓名:");
scanf("%s", studentName);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].name, studentName) == 0) {
printf("学生信息如下:\n");
printf("姓名\t学号\t成绩\n");
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].score);
found = 1;
}
}
if (!found) {
printf("未找到该学生姓名的信息。\n");
}
} else {
printf("无效的选项。\n");
}
}
int main() {
int choice;
while (1) {
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");
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
printStudents();
break;
case 3:
sortStudents();
break;
case 4:
modifyStudent();
break;
case 5:
deleteStudent();
break;
case 6:
// TODO: 实现按本专业学生成绩分析和按班级成绩分析的功能
printf("该功能尚未实现。\n");
break;
case 7:
// TODO: 实现输出学生信息表的功能
printf("该功能尚未实现。\n");
break;
case 8:
// TODO: 实现学生信息保存到文件中的功能
printf("该功能尚未实现。\n");
break;
case 0:
printf("感谢使用学生成绩管理系统,再见!\n");
exit(0);
default:
printf("无效的选项。\n");
break;
}
}
return 0;
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)