请用C语言写一个学生成绩管理系统,必须包含增加、删除、查找、修改的功能,且利用链表的数据结构,必须有人机互动
时间: 2023-05-28 10:06:46 浏览: 60
抱歉,作为人工智能语言模型,我无法提供本地编译和运行能力。以下是一个简单的C语言学生成绩管理系统的代码示例,仅供参考。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
// 学生信息结构体
typedef struct student_info {
char name[MAX_NAME_LEN];
int score;
struct student_info* next;
} StudentInfo;
// 全局变量,指向学生信息链表的头结点
StudentInfo* g_head = NULL;
// 增加学生信息
void add_student() {
StudentInfo *new_node = (StudentInfo*) malloc(sizeof(StudentInfo));
if (new_node == NULL) {
printf("Error: memory allocation failed.\n");
return;
}
printf("Please enter the student's name: ");
scanf("%s", new_node->name);
printf("Please enter the student's score: ");
scanf("%d", &new_node->score);
new_node->next = g_head;
g_head = new_node;
printf("Student information added successfully.\n");
}
// 删除学生信息
void delete_student() {
char name[MAX_NAME_LEN];
printf("Please enter the name of the student you want to delete: ");
scanf("%s", name);
StudentInfo* prev = NULL;
StudentInfo* curr = g_head;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
if (prev == NULL) {
g_head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
printf("Student information deleted successfully.\n");
return;
}
prev = curr;
curr = curr->next;
}
printf("Error: student information not found.\n");
}
// 查找学生信息
void find_student() {
char name[MAX_NAME_LEN];
printf("Please enter the name of the student you want to find: ");
scanf("%s", name);
StudentInfo* curr = g_head;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
printf("Name: %s Score: %d\n", curr->name, curr->score);
return;
}
curr = curr->next;
}
printf("Error: student information not found.\n");
}
// 修改学生信息
void modify_student() {
char name[MAX_NAME_LEN];
printf("Please enter the name of the student you want to modify: ");
scanf("%s", name);
StudentInfo* curr = g_head;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
printf("Please enter the new score: ");
scanf("%d", &curr->score);
printf("Student information modified successfully.\n");
return;
}
curr = curr->next;
}
printf("Error: student information not found.\n");
}
// 打印学生信息
void print_student() {
StudentInfo* curr = g_head;
while (curr != NULL) {
printf("Name: %s Score: %d\n", curr->name, curr->score);
curr = curr->next;
}
}
// 释放学生信息链表占用的内存
void free_student_list() {
StudentInfo* curr = g_head;
while (curr != NULL) {
StudentInfo* next = curr->next;
free(curr);
curr = next;
}
}
// 主函数
int main() {
int choice = 0;
while (1) {
printf("\nPlease choose from the following options:\n");
printf("1. Add student information\n");
printf("2. Delete student information\n");
printf("3. Find student information\n");
printf("4. Modify student information\n");
printf("5. Print all student information\n");
printf("6. Exit\n");
printf("Your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
find_student();
break;
case 4:
modify_student();
break;
case 5:
print_student();
break;
case 6:
free_student_list();
exit(0);
default:
printf("Error: invalid choice.\n");
break;
}
}
return 0;
}
```