学生管理系统C语言
时间: 2023-07-09 17:06:53 浏览: 37
学生管理系统是一种常见的应用程序,用于管理学生的基本信息、课程信息、成绩信息等。下面是使用 C 语言实现一个简单的学生管理系统的示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
#define MAX_NAME_LEN 20
typedef struct student {
char name[MAX_NAME_LEN];
int age;
int score;
} Student;
Student students[MAX_STUDENT_NUM];
int student_num = 0;
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("The maximum number of students has been reached.\n");
return;
}
printf("Please enter the student's name:\n");
scanf("%s", students[student_num].name);
printf("Please enter the student's age:\n");
scanf("%d", &students[student_num].age);
printf("Please enter the student's score:\n");
scanf("%d", &students[student_num].score);
student_num++;
}
void show_students() {
printf("Name\tAge\tScore\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t%d\t%d\n", students[i].name, students[i].age, students[i].score);
}
}
void find_student() {
char name[MAX_NAME_LEN];
printf("Please enter the name of the student you want to find:\n");
scanf("%s", name);
for (int i = 0; i < student_num; i++) {
if (strcmp(name, students[i].name) == 0) {
printf("Name\tAge\tScore\n");
printf("%s\t%d\t%d\n", students[i].name, students[i].age, students[i].score);
return;
}
}
printf("The student named %s is not found.\n", name);
}
int main() {
int choice;
while (1) {
printf("Please select the operation you want to perform:\n");
printf("1. Add student\n");
printf("2. Show all students\n");
printf("3. Find student\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
show_students();
break;
case 3:
find_student();
break;
case 4:
return 0;
default:
printf("Invalid choice.\n");
}
}
}
```
该程序使用结构体存储学生信息,并提供了添加学生、查看所有学生和查找学生等操作。用户可以通过菜单选择要执行的操作,并按照提示输入相应的信息。