编写一个c++的学生信息管理系统
时间: 2023-05-20 14:02:07 浏览: 92
好的,我可以回答这个问题。学生信息管理系统通常包括学生信息的录入、查询、修改和删除等功能。在C语言中,可以使用结构体来存储学生信息,使用文件来保存和读取数据。以下是一个简单的学生信息管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_STUDENT_NUM 100
typedef struct {
char name[MAX_NAME_LEN];
int age;
float score;
} Student;
void add_student(Student students[], int *num_students);
void search_student(Student students[], int num_students);
void modify_student(Student students[], int num_students);
void delete_student(Student students[], int *num_students);
void print_all_students(Student students[], int num_students);
int main() {
Student students[MAX_STUDENT_NUM];
int num_students = 0;
int choice;
while (1) {
printf("1. Add student\n");
printf("2. Search student\n");
printf("3. Modify student\n");
printf("4. Delete student\n");
printf("5. Print all students\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
search_student(students, num_students);
break;
case 3:
modify_student(students, num_students);
break;
case 4:
delete_student(students, &num_students);
break;
case 5:
print_all_students(students, num_students);
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
void add_student(Student students[], int *num_students) {
if (*num_students >= MAX_STUDENT_NUM) {
printf("Too many students!\n");
return;
}
Student student;
printf("Enter name: ");
scanf("%s", student.name);
printf("Enter age: ");
scanf("%d", &student.age);
printf("Enter score: ");
scanf("%f", &student.score);
students[*num_students] = student;
(*num_students)++;
printf("Student added successfully!\n");
}
void search_student(Student students[], int num_students) {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Name: %s, Age: %d, Score: %.2f\n", students[i].name, students[i].age, students[i].score);
return;
}
}
printf("Student not found!\n");
}
void modify_student(Student students[], int num_students) {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Enter new age: ");
scanf("%d", &students[i].age);
printf("Enter new score: ");
scanf("%f", &students[i].score);
printf("Student modified successfully!\n");
return;
}
}
printf("Student not found!\n");
}
void delete_student(Student students[], int *num_students) {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
for (int i = 0; i < *num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
for (int j = i; j < *num_students - 1; j++) {
students[j] = students[j + 1];
}
(*num_students)--;
printf("Student deleted successfully!\n");
return;
}
}
printf("Student not found!\n");
}
void print_all_students(Student students[], int num_students) {
for (int i = 0; i < num_students; i++) {
printf("Name: %s, Age: %d, Score: %.2f\n", students[i].name, students[i].age, students[i].score);
}
}
```
这个程序可以实现基本的学生信息管理功能,包括添加、查询、修改和删除学生信息,以及打印所有学生信息。当然,这只是一个简单的示例,实际的学生信息管理系统可能需要更多的功能和更复杂的实现。
阅读全文