c语言编写一个学生信息管理系统
时间: 2023-05-21 19:05:16 浏览: 114
学生信息管理系统C语言版本
可以使用结构体来保存每个学生的信息,如下所示:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_COUNT 100
typedef struct {
char name[50];
int id;
int age;
float score;
} Student;
int main() {
Student students[MAX_STUDENT_COUNT];
int student_count = 0;
int choice = 0;
while (1) {
printf("1. 添加学生信息\n");
printf("2. 查看学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 删除学生信息\n");
printf("5. 退出\n");
printf("请输入你的选择: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
if (student_count >= MAX_STUDENT_COUNT) {
printf("学生信息已满,无法添加新的学生!\n");
break;
}
printf("请输入学生姓名: ");
scanf("%s", students[student_count].name);
printf("请输入学生编号: ");
scanf("%d", &students[student_count].id);
printf("请输入学生年龄: ");
scanf("%d", &students[student_count].age);
printf("请输入学生成绩: ");
scanf("%f", &students[student_count].score);
student_count++;
printf("学生信息已添加成功!\n");
break;
}
case 2: {
if (student_count == 0) {
printf("学生信息为空,无法查看学生信息!\n");
break;
}
printf("编号\t姓名\t年龄\t成绩\n");
for (int i = 0; i < student_count; i++) {
printf("%d\t%s\t%d\t%.2f\n", students[i].id, students[i].name,
students[i].age, students[i].score);
}
break;
}
case 3: {
if (student_count == 0) {
printf("学生信息为空,无法修改学生信息!\n");
break;
}
int id = 0;
printf("请输入要修改的学生编号: ");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < student_count; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到学生信息,无法修改!\n");
break;
}
printf("当前信息为: %s\t%d\t%d\t%.2f\n", students[index].name,
students[index].id, students[index].age, students[index].score);
printf("请输入新的学生姓名: ");
scanf("%s", students[index].name);
printf("请输入新的学生年龄: ");
scanf("%d", &students[index].age);
printf("请输入新的学生成绩: ");
scanf("%f", &students[index].score);
printf("学生信息已修改成功!\n");
break;
}
case 4: {
if (student_count == 0) {
printf("学生信息为空,无法删除学生信息!\n");
break;
}
int id = 0;
printf("请输入要删除的学生编号: ");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < student_count; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到学生信息,无法删除!\n");
break;
}
for (int i = index; i < student_count - 1; i++) {
students[i] = students[i + 1];
}
student_count--;
printf("学生信息已删除成功!\n");
break;
}
case 5: {
printf("感谢使用学生信息管理系统!\n");
exit(0);
}
default: {
printf("非法选择,请重新输入!\n");
break;
}
}
}
return 0;
}
```
阅读全文