c语言写个学生管理系统
时间: 2023-10-08 13:06:50 浏览: 120
C语言写的学生管理系统
好的,下面是一个简单的学生管理系统的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 50
struct student {
char name[20];
int age;
int score;
};
int num_students = 0;
struct student students[MAX_STUDENTS];
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
return;
}
struct student s;
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter age: ");
scanf("%d", &s.age);
printf("Enter score: ");
scanf("%d", &s.score);
students[num_students] = s;
num_students++;
}
void print_student(struct student s) {
printf("%s (age %d): %d\n", s.name, s.age, s.score);
}
void list_students() {
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
}
void find_student() {
char name[20];
printf("Enter name: ");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
print_student(students[i]);
return;
}
}
printf("Student not found\n");
}
int main() {
int choice;
while (1) {
printf("1. Add student\n");
printf("2. List students\n");
printf("3. Find student\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
list_students();
break;
case 3:
find_student();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
```
该程序使用结构体来存储学生的姓名、年龄和分数信息。用户可以选择添加学生、列出所有学生、查找学生或退出程序。程序可以存储最多50个学生的信息。
阅读全文