写一个c++的学生管理系统
时间: 2024-05-14 19:15:38 浏览: 3
以下是一个简单的C语言学生管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
int id;
char name[50];
int age;
float gpa;
};
struct Student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
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 GPA: ");
scanf("%f", &new_student.gpa);
students[num_students++] = new_student;
}
void list_students() {
for (int i = 0; i < num_students; i++) {
struct Student s = students[i];
printf("%d\t%s\t%d\t%.2f\n", s.id, s.name, s.age, s.gpa);
}
}
void search_student() {
int id;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
struct Student s = students[i];
if (s.id == id) {
printf("%d\t%s\t%d\t%.2f\n", s.id, s.name, s.age, s.gpa);
return;
}
}
printf("Student not found.\n");
}
void delete_student() {
int id;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
struct Student s = students[i];
if (s.id == id) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j+1];
}
num_students--;
printf("Student deleted.\n");
return;
}
}
printf("Student not found.\n");
}
int main() {
while (1) {
printf("Enter a command (add, list, search, delete, quit): ");
char command[10];
scanf("%s", command);
if (strcmp(command, "add") == 0) {
add_student();
} else if (strcmp(command, "list") == 0) {
list_students();
} else if (strcmp(command, "search") == 0) {
search_student();
} else if (strcmp(command, "delete") == 0) {
delete_student();
} else if (strcmp(command, "quit") == 0) {
return 0;
} else {
printf("Invalid command.\n");
}
}
}
```
这个学生管理系统可以添加、列出、搜索和删除学生。它使用了一个结构体来表示学生,以及一个数组来存储所有学生的信息。用户可以通过命令行界面来输入不同的命令来操作学生信息。