C语言学生管理系统 函数版
时间: 2023-10-18 09:13:41 浏览: 63
以下是一个简单的 C 语言学生管理系统函数版的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[50];
int id;
int age;
float gpa;
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: Maximum number of students reached.\n");
return;
}
struct student new_student;
printf("Enter student name: ");
scanf("%s", new_student.name);
printf("Enter student ID: ");
scanf("%d", &new_student.id);
printf("Enter student age: ");
scanf("%d", &new_student.age);
printf("Enter student GPA: ");
scanf("%f", &new_student.gpa);
students[num_students] = new_student;
num_students++;
printf("Student added successfully.\n");
}
void print_student(struct student s) {
printf("Name: %s\n", s.name);
printf("ID: %d\n", s.id);
printf("Age: %d\n", s.age);
printf("GPA: %.2f\n", s.gpa);
}
void print_all_students() {
printf("List of all students:\n");
for (int i = 0; i < num_students; i++) {
printf("Student %d:\n", i + 1);
print_student(students[i]);
}
}
void find_student_by_id() {
int id;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Found student:\n");
print_student(students[i]);
return;
}
}
printf("Error: Student with ID %d not found.\n", id);
}
void menu() {
printf("\n=== Student Management System ===\n\n");
printf("1. Add new student\n");
printf("2. Print all students\n");
printf("3. Find student by ID\n");
printf("4. Exit\n\n");
}
int main() {
int choice;
do {
menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
print_all_students();
break;
case 3:
find_student_by_id();
break;
case 4:
printf("Goodbye!\n");
return 0;
default:
printf("Error: Invalid choice.\n");
break;
}
} while (1);
return 0;
}
```
该程序定义了一个结构体 `student`,用于表示每个学生的信息。使用一个静态数组 `students` 存储所有学生的信息,最大容量为 `MAX_STUDENTS`,默认为 100。也可以通过修改 `MAX_STUDENTS` 来改变最大容量。
程序提供了以下功能:
1. 添加新学生信息
2. 打印所有学生信息
3. 按 ID 查找学生信息
4. 退出程序
在主函数中,使用一个 `do-while` 循环来展示菜单,通过 `switch` 语句选择对应的操作函数。每个操作函数都有对应的功能,例如 `add_student` 函数用于添加新学生信息,`print_all_students` 函数用于打印所有学生信息,`find_student_by_id` 函数用于按 ID 查找学生信息。
此外,还定义了一些辅助函数,例如 `print_student` 函数用于打印单个学生信息,`menu` 函数用于打印菜单。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)