用c++语音写一个班级成绩管理系统的代码,要求 (1)利用结构体和简单的链表,实现一个班学生成绩的管理。 (2)成绩管理功能包括:输入、输出,利用new和delete运算符添加/删除学生信息。
时间: 2023-03-30 17:04:29 浏览: 150
好的,以下是班级成绩管理系统的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_LEN 20
// 学生信息结构体
typedef struct student {
char name[MAX_NAME_LEN]; // 学生姓名
int score; // 学生成绩
struct student *next; // 指向下一个学生的指针
} Student;
// 添加学生信息
void add_student(Student **head) {
Student *new_student = (Student *) malloc(sizeof(Student));
printf("请输入学生姓名:");
scanf("%s", new_student->name);
printf("请输入学生成绩:");
scanf("%d", &new_student->score);
new_student->next = *head;
*head = new_student;
}
// 删除学生信息
void delete_student(Student **head) {
char name[MAX_NAME_LEN];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
Student *prev = NULL;
Student *curr = *head;
while (curr != NULL) {
if (strcmp(curr->name, name) == ) {
if (prev == NULL) {
*head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
printf("删除成功!\n");
return;
}
prev = curr;
curr = curr->next;
}
printf("未找到该学生!\n");
}
// 输出学生信息
void print_student(Student *student) {
printf("姓名:%s,成绩:%d\n", student->name, student->score);
}
// 输出所有学生信息
void print_all_students(Student *head) {
printf("所有学生信息如下:\n");
Student *curr = head;
while (curr != NULL) {
print_student(curr);
curr = curr->next;
}
}
int main() {
Student *head = NULL;
int choice;
while (1) {
printf("请选择操作:\n");
printf("1. 添加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 输出所有学生信息\n");
printf("4. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(&head);
break;
case 2:
delete_student(&head);
break;
case 3:
print_all_students(head);
break;
case 4:
return ;
default:
printf("无效的选择!\n");
}
}
return ;
}
阅读全文