帮我写一段c语言程序,使其能够在devc++上不下载其他插件也能正常运行并满足:建立一个班级成绩管理系统:(1)要求利用链表指针来操作完成、动态内存分配。(2)姓名、性别、程序设计训练课成绩。(3)对成绩排序,同时可以插入学生、删除学生。(4)按一定的格式进行文件保存、读取(5)使用fprintf()与fscanf()
时间: 2024-04-12 10:31:25 浏览: 202
用c++编写的班级成绩管理系统
5星 · 资源好评率100%
当然可以!以下是一个基本的班级成绩管理系统的C语言程序,满足你的要求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生信息结构体
typedef struct Student {
char name[50];
char gender[10];
float score;
struct Student* next;
} Student;
// 创建新的学生节点
Student* createStudent(char name[], char gender[], float score) {
Student* newStudent = (Student*)malloc(sizeof(Student));
strcpy(newStudent->name, name);
strcpy(newStudent->gender, gender);
newStudent->score = score;
newStudent->next = NULL;
return newStudent;
}
// 插入学生节点到链表
void insertStudent(Student** head, Student* newStudent) {
if (*head == NULL) {
*head = newStudent;
} else {
Student* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newStudent;
}
}
// 删除学生节点
void deleteStudent(Student** head, char name[]) {
if (*head == NULL) {
printf("链表为空,无法删除学生。\n");
return;
}
Student* temp = *head;
Student* prev = NULL;
// 找到待删除的学生节点
while (temp != NULL && strcmp(temp->name, name) != 0) {
prev = temp;
temp = temp->next;
}
// 若找到了待删除的学生节点,则删除之
if (temp != NULL) {
if (prev != NULL) {
prev->next = temp->next;
} else {
*head = temp->next;
}
free(temp);
printf("学生 %s 已被删除。\n", name);
} else {
printf("找不到学生 %s ,无法删除。\n", name);
}
}
// 对学生链表按成绩排序
void sortStudents(Student** head) {
if (*head == NULL || (*head)->next == NULL) {
return;
}
int swapped;
Student* ptr1;
Student* lptr = NULL;
do {
swapped = 0;
ptr1 = *head;
while (ptr1->next != lptr) {
if (ptr1->score < ptr1->next->score) {
// 交换节点
Student* temp = createStudent("", "", 0);
strcpy(temp->name, ptr1->name);
strcpy(temp->gender, ptr1->gender);
temp->score = ptr1->score;
strcpy(ptr1->name, ptr1->next->name);
strcpy(ptr1->gender, ptr1->next->gender);
ptr1->score = ptr1->next->score;
strcpy(ptr1->next->name, temp->name);
strcpy(ptr1->next->gender, temp->gender);
ptr1->next->score = temp->score;
free(temp);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
// 保存学生链表到文件
void saveToFile(Student* head, char filename[]) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("无法打开文件 %s 用于保存。\n", filename);
return;
}
Student* temp = head;
while (temp != NULL) {
fprintf(file, "%s %s %.2f\n", temp->name, temp->gender, temp->score);
temp = temp->next;
}
fclose(file);
printf("学生信息已保存到文件 %s 。\n", filename);
}
// 从文件中加载学生链表
Student* loadFromFile(char filename[]) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("无法打开文件 %s 用于读取。\n", filename);
return NULL;
}
Student* head = NULL;
char name[50];
char gender[10];
float score;
while (fscanf(file, "%s %s %f", name, gender, &score) == 3) {
Student* newStudent = createStudent(name, gender, score);
insertStudent(&head, newStudent);
}
fclose(file);
printf("从文件 %s 中加载学生信息成功。\n", filename);
return head;
}
// 打印学生链表
void printStudents(Student* head) {
if (head == NULL) {
printf("链表为空。\n");
return;
}
printf("姓名\t性别\t成绩\n");
Student* temp = head;
while (temp != NULL) {
printf("%s\t%s\t%.2f\n", temp->name, temp->gender, temp->score);
temp = temp->next;
}
}
int main() {
Student* head = NULL;
char filename[] = "students.txt";
// 操作示例
insertStudent(&head, createStudent("张三", "男", 85.5));
insertStudent(&head, createStudent("李四", "女", 92.0));
insertStudent(&head, createStudent("王五", "男", 78.5));
printf("初始学生信息:\n");
printStudents(head);
printf("\n");
sortStudents(&head);
printf("按成绩排序后的学生信息:\n");
printStudents(head);
printf("\n");
deleteStudent(&head, "李四");
printf("删除学生后的学生信息:\n");
printStudents(head);
printf("\n");
saveToFile(head, filename);
printf("\n");
Student* loadedHead = loadFromFile(filename);
printf("从文件中加载的学生信息:\n");
printStudents(loadedHead);
// 释放内存
Student* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
return 0;
}
```
这段程序使用链表指针来操作学生信息,实现了动态内存分配。可以插入、删除学生节点,并按成绩排序。使用 `fprintf()` 和 `fscanf()` 函数来进行文件保存和读取。
你可以在DevC++中运行这段代码,它将创建一个包含学生信息的链表,并对其进行一些操作和展示。你可以根据自己的需求对其进行修改和扩展。希望能对你有所帮助!
阅读全文