给我一个C++写的学生成绩管理系统
时间: 2023-05-26 15:01:30 浏览: 63
A:以下是一个简单的 C 语言学生成绩管理系统,包括添加学生信息、删除学生信息、查询学生信息以及修改学生信息等功能:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 50
struct Student {
char name[20];
int age;
float score;
};
struct Student students[MAX_STUDENTS];
int num_students = 0;
void add_student()
{
struct Student new_student;
printf("Input student name: ");
scanf("%s", new_student.name);
printf("Input student age: ");
scanf("%d", &new_student.age);
printf("Input student score: ");
scanf("%f", &new_student.score);
students[num_students] = new_student;
num_students++;
printf("New student added successfully.\n");
}
void delete_student()
{
char name[20];
int i, j;
printf("Input the name of the student you want to delete: ");
scanf("%s", name);
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
for (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 search_student()
{
char name[20];
int i;
printf("Input the name of the student you want to search: ");
scanf("%s", name);
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Score: %.2f\n", students[i].score);
return;
}
}
printf("Student not found.\n");
}
void modify_student()
{
char name[20];
int i;
printf("Input the name of the student you want to modify: ");
scanf("%s", name);
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Input student new score: ");
scanf("%f", &students[i].score);
printf("Student information modified successfully.\n");
return;
}
}
printf("Student not found.\n");
}
void print_menu()
{
printf("1 - Add student\n");
printf("2 - Delete student\n");
printf("3 - Search student\n");
printf("4 - Modify student\n");
printf("5 - Exit\n");
}
int main()
{
int option;
do {
print_menu();
printf("Input your option: ");
scanf("%d", &option);
switch(option) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
search_student();
break;
case 4:
modify_student();
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Invalid option.\n");
}
} while(option != 5);
return 0;
}
```
在该程序中,定义了一个名为 `Student` 的结构体,用于存储学生的名字、年龄和分数等信息。程序最多可以管理 `MAX_STUDENTS` 个学生信息。在 `main()` 函数中,用一个循环不断显示菜单并等待用户输入选项,然后调用相应的函数来执行对应的操作。
添加学生信息的函数是 `add_student()`,该函数要求用户输入学生姓名、年龄和分数等信息,然后将该学生的信息添加到存储学生信息的数组中。
删除学生信息的函数是 `delete_student()`,该函数要求用户输入要删除的学生的姓名,然后在数组中查找该学生的信息,并将其从数组中删除。
查询学生信息的函数是 `search_student()`,该函数要求用户输入要查询的学生的姓名,然后在数组中查找该学生的信息,并将其打印出来。
修改学生信息的函数是 `modify_student()`,该函数要求用户输入要修改的学生的姓名,然后在数组中查找该学生的信息,并要求用户输入新的分数,然后将该学生的分数更新为输入的新分数。
在 `main()` 函数中,用一个循环不断显示菜单并等待用户输入选项,然后调用相应的函数来执行对应的操作。当用户选择“5 - 退出”时,程序结束。