用c语言制作一个学生信息管理系统,其中包括学号,姓名,性别,年龄,生源地,成绩,系统与菜单方式与用户进行交互,系统具备打印输出所有学生信息的功能,添加学生信息,显示学生信息,修改学生信息,删除学生信息,查找学生信息,按学生总成绩进行高到低排序。
时间: 2024-06-14 16:06:16 浏览: 207
学生信息管理系统,用于c语言实现
以下是用C语言制作的学生信息管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
int studentID;
char name[50];
char gender[10];
int age;
char hometown[50];
float score;
};
struct Student students[MAX_STUDENTS];
int numStudents = 0;
void printMenu() {
printf("\n-- 学生信息管理系统 --\n");
printf("1. 添加学生信息\n"); printf("2. 显示所有学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 删除学生信息\n");
printf("5. 查找学生信息\n");
printf("6. 按学生总成绩排序\n");
printf("0. 退出系统\n");
printf("--\n");
printf("请输入选项:");
}
void addStudent() {
if (numStudents >= MAX_STUDENTS) {
printf("学生信息已满,无法添加新学生。\n");
return;
}
struct Student newStudent;
printf("请输入学生学号:");
scanf("%d", &newStudent.studentID);
printf("请输入学生姓名:");
scanf("%s", newStudent.name);
printf("请输入学生性别:");
scanf("%s", newStudent.gender);
printf("请输入学生年龄:");
scanf("%d", &newStudent.age);
printf("请输入学生生源地:");
scanf("%s", newStudent.hometown);
printf("请输入学生成绩:");
scanf("%f", &newStudent.score);
students[numStudents] = newStudent;
numStudents++;
printf("学生信息添加成功。\n");
}
void displayStudents() {
if (numStudents == 0) {
printf("暂无学生信息。\n");
return;
}
printf("\n-- 所有学生信息 --\n");
for (int i = 0; i < numStudents; i++) {
printf("学号:%d\n", students[i].studentID);
printf("姓名:%s\n", students[i].name);
printf("性别:%s\n", students[i].gender);
printf("年龄:%d\n", students[i].age);
printf("生源地:%s\n", students[i].hometown);
printf("成绩:%.2f\n", students[i].score);
printf("--\n");
}
}
void modifyStudent() {
int studentID;
printf("请输入要修改的学生学号:");
scanf("%d", &studentID);
int foundIndex = -1;
for (int i = 0; i < numStudents; i++) {
if (students[i].studentID == studentID) {
foundIndex = i;
break;
}
}
if (foundIndex == -1) {
printf("未找到该学生学号的信息。\n");
return;
}
struct Student modifiedStudent;
printf("请输入学生姓名:");
scanf("%s", modifiedStudent.name);
printf("请输入学生性别:");
scanf("%s", modifiedStudent.gender);
printf("请输入学生年龄:");
scanf("%d", &modifiedStudent.age);
printf("请输入学生生源地:");
scanf("%s", modifiedStudent.hometown);
printf("请输入学生成绩:");
scanf("%f", &modifiedStudent.score);
students[foundIndex] = modifiedStudent;
printf("学生信息修改成功。\n");
}
void deleteStudent() {
int studentID;
printf("请输入要删除的学生学号:");
scanf("%d", &studentID);
int foundIndex = -1;
for (int i = 0; i < numStudents; i++) {
if (students[i].studentID == studentID) {
foundIndex = i;
break;
}
}
if (foundIndex == -1) {
printf("未找到该学生学号的信息。\n");
return;
}
for (int i = foundIndex; i < numStudents - 1; i++) {
students[i] = students[i + 1];
}
numStudents--;
printf("学生信息删除成功。\n");
}
void searchStudent() {
char name[50];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
int foundIndex = -1;
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].name, name) == 0) {
foundIndex = i;
break;
}
}
if (foundIndex == -1) {
printf("未找到该学生姓名的信息。\n");
return;
}
printf("\n-- 学生信息 --\n");
printf("学号:%d\n", students[foundIndex].studentID);
printf("姓名:%s\n", students[foundIndex].name);
printf("性别:%s\n", students[foundIndex].gender);
printf("年龄:%d\n", students[foundIndex].age);
printf("生源地:%s\n", students[foundIndex].hometown);
printf("成绩:%.2f\n", students[foundIndex].score);
printf("--\n");
}
void sortByScore() {
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");
}
int main() {
int option;
do {
printMenu();
scanf("%d", &option);
switch (option) {
case 1:
addStudent();
break;
case 2:
displayStudents();
break;
case 3:
modifyStudent();
break;
case 4:
deleteStudent();
break;
case 5:
searchStudent();
break;
case 6:
sortByScore();
break;
case 0:
printf("感谢使用学生信息管理系统,再见!\n");
break;
default:
printf("无效的选项,请重新输入。\n");
break;
}
} while (option != 0);
return 0;
}
```
阅读全文