c语言撰写学生成绩管理系统
时间: 2023-12-31 16:24:11 浏览: 77
根据提供的引用内容,设计一个C语言学生成绩管理系统可以包括以下功能:学生成绩的输入、输出、查询、排序、删除、统计和退出等。下面是一个简单的示例代码,演示了如何使用结构体和指针来实现学生成绩管理系统的基本功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
// 定义学生结构体
typedef struct {
char id[20]; // 学号
char name[50]; // 姓名
float scores[3]; // 三门课程的成绩
float average; // 平均分
} Student;
// 全局变量,用于存储学生信息
Student students[MAX_STUDENTS];
int numStudents = 0;
// 函数声明
void addStudent();
void displayStudents();
void searchStudent();
void sortStudents();
void deleteStudent();
void calculateAverage();
void exitProgram();
int main() {
int choice;
do {
printf("学生成绩管理系统\n");
printf("1. 添加学生\n");
printf("2. 显示学生信息\n"); printf("3. 查询学生\n");
printf("4. 排序学生\n");
printf("5. 删除学生\n");
printf("6. 统计平均分\n");
printf("7. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayStudents();
break;
case 3:
searchStudent();
break;
case 4:
sortStudents();
break;
case 5:
deleteStudent();
break;
case 6:
calculateAverage();
break;
case 7:
exitProgram();
break;
default:
printf("无效的选择,请重新输入。\n");
break;
}
} while (choice != 7);
return 0;
}
// 添加学生
void addStudent() {
if (numStudents >= MAX_STUDENTS) {
printf("学生信息已满,无法添加新学生。\n");
return;
}
Student newStudent;
printf("请输入学号:");
scanf("%s", newStudent.id);
printf("请输入姓名:");
scanf("%s", newStudent.name);
printf("请输入三门课程的成绩:");
scanf("%f %f %f", &newStudent.scores[0], &newStudent.scores[1], &newStudent.scores[2]);
// 计算平均分
newStudent.average = (newStudent.scores[0] + newStudent.scores[1] + newStudent.scores[2]) / 3;
students[numStudents] = newStudent;
numStudents++;
printf("学生信息添加成功。\n");
}
// 显示学生信息
void displayStudents() {
if (numStudents == 0) {
printf("暂无学生信息。\n");
return;
}
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t平均分\n");
for (int i = 0; i < numStudents; i++) {
printf("%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].id, students[i].name,
students[i].scores[0], students[i].scores[1], students[i].scores[2], students[i].average);
}
}
// 查询学生
void searchStudent() {
if (numStudents == 0) {
printf("暂无学生信息。\n");
return;
}
char searchId[20];
printf("请输入要查询的学号:");
scanf("%s", searchId);
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].id, searchId) == 0) {
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t平均分\n");
printf("%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].id, students[i].name,
students[i].scores[0], students[i].scores[1], students[i].scores[2], students[i].average);
return;
}
}
printf("未找到该学号对应的学生。\n");
}
// 排序学生
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].average < students[j + 1].average) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t平均分\n");
for (int i = 0; i < numStudents; i++) {
printf("%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].id, students[i].name,
students[i].scores[0], students[i].scores[1], students[i].scores[2], students[i].average);
}
}
// 删除学生
void deleteStudent() {
if (numStudents == 0) {
printf("暂无学生信息。\n");
return;
}
char deleteId[20];
printf("请输入要删除的学号:");
scanf("%s", deleteId);
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].id, deleteId) == 0) {
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
numStudents--;
printf("学生信息删除成功。\n");
return;
}
}
printf("未找到该学号对应的学生。\n");
}
// 统计平均分
void calculateAverage() {
if (numStudents == 0) {
printf("暂无学生信息。\n");
return;
}
float sum = 0;
for (int i = 0; i < numStudents; i++) {
sum += students[i].average;
}
float average = sum / numStudents;
printf("学生平均分为:%.2f\n", average);
}
// 退出程序
void exitProgram() {
printf("感谢使用学生成绩管理系统,再见!\n");
}
```
阅读全文