学生信息管理系统C语言
时间: 2023-07-09 08:07:11 浏览: 90
学生信息管理系统C语言版本
学生信息管理系统是一个常见的项目,可以用 C 语来实现。以下是一个简单的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[50];
int age;
float gpa;
};
int main() {
struct student students[MAX_STUDENTS];
int num_students = 0;
int choice;
while (1) {
printf("\n1. Add student\n");
printf("2. View all students\n");
printf("3. Search by name\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 s;
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student age: ");
scanf("%d", &s.age);
printf("Enter student GPA: ");
scanf("%f", &s.gpa);
students[num_students++] = s;
printf("Student added successfully.\n");
break;
}
case 2: {
if (num_students == 0) {
printf("No students to display.\n");
break;
}
printf("All students:\n");
for (int i = 0; i < num_students; i++) {
printf("%s, %d years old, GPA %.2f\n", students[i].name, students[i].age, students[i].gpa);
}
break;
}
case 3: {
if (num_students == 0) {
printf("No students to search.\n");
break;
}
char name[50];
printf("Enter student name to search: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("%s, %d years old, GPA %.2f\n", students[i].name, students[i].age, students[i].gpa);
found = 1;
}
}
if (!found) {
printf("No students found with name %s.\n", name);
}
break;
}
case 4: {
printf("Exiting...\n");
exit(0);
}
default: {
printf("Invalid choice. Try again.\n");
break;
}
}
}
return 0;
}
```
这个程序使用一个结构体 `student` 来存储每个学生的信息,包括姓名、年龄和 GPA。主函数中定义了一个 `students` 数组来存储所有学生的信息。程序包含一个菜单,可以让用户选择添加、查看或搜索学生信息,或者退出程序。每次添加学生信息时,程序会检查是否已经达到了最大学生数。每次搜索学生信息时,程序会遍历 `students` 数组,查找与搜索关键字匹配的学生信息。
阅读全文