如何用C语言做简单的学生管理系统
时间: 2024-01-21 18:20:00 浏览: 16
下面是一个简单的学生管理系统的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
int id;
char name[20];
int age;
float score;
};
int main() {
struct student students[MAX_STUDENTS];
int num_students = 0;
int choice;
do {
printf("1. Add student\n");
printf("2. Print all students\n");
printf("3. Search student by ID\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (num_students == MAX_STUDENTS) {
printf("Error: maximum number of students reached.\n");
break;
}
struct student new_student;
printf("Enter student ID: ");
scanf("%d", &new_student.id);
printf("Enter student name: ");
scanf("%s", new_student.name);
printf("Enter student age: ");
scanf("%d", &new_student.age);
printf("Enter student score: ");
scanf("%f", &new_student.score);
students[num_students++] = new_student;
printf("Student added successfully.\n");
break;
case 2:
if (num_students == 0) {
printf("No students found.\n");
break;
}
printf("ID\tName\tAge\tScore\n");
for (int i = 0; i < num_students; 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 (num_students == 0) {
printf("No students found.\n");
break;
}
int search_id;
printf("Enter student ID: ");
scanf("%d", &search_id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == search_id) {
printf("ID\tName\tAge\tScore\n");
printf("%d\t%s\t%d\t%.2f\n",
students[i].id,
students[i].name,
students[i].age,
students[i].score);
break;
}
}
printf("Student not found.\n");
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 4);
return 0;
}
```
这个程序使用结构体来存储学生信息,并且可以添加新学生、打印所有学生和按 ID 搜索学生。你可以根据自己的需要对它进行修改和扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)