c语言学生管理系统
时间: 2023-07-03 13:10:06 浏览: 45
好的,下面是一个简单的 C 语言学生管理系统示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 50
#define MAX_NAME_LEN 20
struct Student {
int id;
char name[MAX_NAME_LEN];
int age;
float gpa;
};
void printStudent(struct Student s) {
printf("ID: %d\n", s.id);
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
printf("GPA: %.2f\n", s.gpa);
}
int main() {
struct Student students[MAX_STUDENTS];
int numStudents = 0;
int choice = 0;
while (choice != 4) {
printf("\n--- Student Management System ---\n");
printf("1. Add Student\n");
printf("2. View Student\n");
printf("3. List All Students\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (numStudents >= MAX_STUDENTS) {
printf("Error: Maximum number of students reached.\n");
} else {
printf("\nEnter student ID: ");
scanf("%d", &students[numStudents].id);
printf("Enter student name: ");
scanf("%s", students[numStudents].name);
printf("Enter student age: ");
scanf("%d", &students[numStudents].age);
printf("Enter student GPA: ");
scanf("%f", &students[numStudents].gpa);
numStudents++;
}
break;
case 2:
if (numStudents == 0) {
printf("Error: No students added yet.\n");
} else {
int id;
printf("\nEnter student ID: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (students[i].id == id) {
printStudent(students[i]);
found = 1;
break;
}
}
if (!found) {
printf("Error: Student not found.\n");
}
}
break;
case 3:
if (numStudents == 0) {
printf("Error: No students added yet.\n");
} else {
printf("\n--- List of Students ---\n");
for (int i = 0; i < numStudents; i++) {
printStudent(students[i]);
}
}
break;
case 4:
printf("Exiting program...\n");
break;
default:
printf("Error: Invalid choice.\n");
break;
}
}
return 0;
}
```
该程序使用 `struct` 来存储学生的信息,可以添加、查看和列出所有学生。需要注意的是,该程序仅作为示例,实际应用中可能需要根据具体需求进行修改和扩展。
相关推荐
![](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)