题目:学生基本信息管理c语言代码 内容:给出n个学生的m门考试的成绩表,每个学生的信息由学号、姓名以及各科成绩组成。对学生的考试成绩进行有关统计,并打印统计表。具体功能如下: 主要功能: (1)具有对学生信息的增、删(按学号)、查(按学号、姓名)、改(按学号)功 能。 (2)按总分高低次序,打印出名次表,总分相同的为同一名次; (3)按名次打印出每个学生的学号、姓名、总分以及各科成缋。 (4)输出每科课程的平均成绩。
时间: 2024-02-15 07:06:12 浏览: 146
以下是学生基本信息管理的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUMBER 100
#define NAME_LENGTH 20
typedef struct {
int id;
char name[NAME_LENGTH];
int score[3];
int totalScore;
int rank;
} Student;
Student students[MAX_STUDENT_NUMBER];
int studentCount = 0;
void addStudent() {
if (studentCount >= MAX_STUDENT_NUMBER) {
printf("无法添加更多学生\n");
return;
}
printf("请输入学生信息:\n");
Student student;
printf("学号:");
scanf("%d", &student.id);
printf("姓名:");
scanf("%s", student.name);
printf("三门课程成绩:");
scanf("%d %d %d", &student.score[0], &student.score[1], &student.score[2]);
student.totalScore = student.score[0] + student.score[1] + student.score[2];
student.rank = 1;
students[studentCount] = student;
studentCount++;
printf("添加成功\n");
}
void deleteStudent() {
int id;
printf("请输入要删除学生的学号:");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到对应学生\n");
return;
}
for (int i = index; i < studentCount - 1; i++) {
students[i] = students[i + 1];
}
studentCount--;
printf("删除成功\n");
}
void searchStudentById() {
int id;
printf("请输入要查询学生的学号:");
scanf("%d", &id);
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
printf("学号:%d,姓名:%s,三门课程成绩:%d %d %d,总分:%d\n",
students[i].id, students[i].name, students[i].score[0],
students[i].score[1], students[i].score[2], students[i].totalScore);
return;
}
}
printf("未找到对应学生\n");
}
void searchStudentByName() {
char name[NAME_LENGTH];
printf("请输入要查询学生的姓名:");
scanf("%s", name);
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学号:%d,姓名:%s,三门课程成绩:%d %d %d,总分:%d\n",
students[i].id, students[i].name, students[i].score[0],
students[i].score[1], students[i].score[2], students[i].totalScore);
}
}
}
void updateStudent() {
int id;
printf("请输入要修改学生的学号:");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到对应学生\n");
return;
}
printf("请输入学生信息:\n");
Student student;
printf("学号:");
scanf("%d", &student.id);
printf("姓名:");
scanf("%s", student.name);
printf("三门课程成绩:");
scanf("%d %d %d", &student.score[0], &student.score[1], &student.score[2]);
student.totalScore = student.score[0] + student.score[1] + student.score[2];
student.rank = 1;
students[index] = student;
printf("修改成功\n");
}
void printRank() {
for (int i = 0; i < studentCount; i++) {
students[i].rank = 1;
for (int j = 0; j < studentCount; j++) {
if (students[j].totalScore > students[i].totalScore) {
students[i].rank++;
}
}
}
printf("名次 学号 姓名 总分\n");
for (int i = 0; i < studentCount; i++) {
printf("%d %d %s %d\n", students[i].rank, students[i].id,
students[i].name, students[i].totalScore);
}
}
void printStudent() {
printf("请输入要查询的名次:");
int rank;
scanf("%d", &rank);
printf("学号:%d,姓名:%s,三门课程成绩:%d %d %d,总分:%d\n",
students[rank - 1].id, students[rank - 1].name, students[rank - 1].score[0],
students[rank - 1].score[1], students[rank - 1].score[2], students[rank - 1].totalScore);
}
void printAverageScore() {
float averageScore[3] = {0};
for (int i = 0; i < studentCount; i++) {
for (int j = 0; j < 3; j++) {
averageScore[j] += students[i].score[j];
}
}
for (int i = 0; i < 3; i++) {
printf("第%d门课程平均成绩:%.2f\n", i + 1, averageScore[i] / studentCount);
}
}
int main() {
int choice;
while (1) {
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");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
searchStudentById();
break;
case 4:
searchStudentByName();
break;
case 5:
updateStudent();
break;
case 6:
printRank();
break;
case 7:
printStudent();
break;
case 8:
printAverageScore();
break;
case 9:
exit(0);
default:
printf("无效的操作\n");
break;
}
}
return 0;
}
```
这个程序实现了对学生信息的增、删、查、改、打印名次表、打印学生信息和打印课程平均成绩等功能。具体实现细节可以参考代码注释。
阅读全文