标准IO(输入输出)

先编写以下结构体

struct Student{

char name[20];

double math;

double chinese;

double english;

double physical;

double chemical;

double biological; };

第一步:创建一个 struct Student 类型的链表,初始化该数组中3个学生的属性

第二步:编写一个叫做save的函数,功能为 将数组arr中的3个学生的所有信息,保存到文件中去,使用fprintf实现

第三步:编写一个叫做load的函数,功能为 将文件中保存的3个学生信息,读取后,写入到另一个链表 brr 中去

第四步:编写一个叫做 show的函数,功能为 遍历输出 arr 或者 brr 链表中的所有学生的信息

第五步:编写一个叫做 setMath 的函数,功能为 修改 文件中 所有学生的数学成绩

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Student {
    char name[20];
    double math;
    double chinese;
    double english;
    double physical;
    double chemical;
    double biological;
    struct Student *next;
} Student;

// 创建一个新的学生节点
Student* createStudent(const char *name, double math, double chinese, double english, double physical, double chemical, double biological) {
    Student *newStudent = (Student *)malloc(sizeof(Student));
    if (newStudent == NULL) {
        perror("内存分配失败");
        exit(EXIT_FAILURE);
    }
    strcpy(newStudent->name, name);
    newStudent->math = math;
    newStudent->chinese = chinese;
    newStudent->english = english;
    newStudent->physical = physical;
    newStudent->chemical = chemical;
    newStudent->biological = biological;
    newStudent->next = NULL;
    return newStudent;
}

// 保存学生信息到文件
void save(Student *head, const char *filename) {
    FILE *file = fopen(filename, "w");
    if (file == NULL) {
        perror("无法打开文件");
        return;
    }

    Student *current = head;
    while (current != NULL) {
        fprintf(file, "%s %.2f %.2f %.2f %.2f %.2f %.2f\n",
                current->name, current->math, current->chinese, current->english,
                current->physical, current->chemical, current->biological);
        current = current->next;
    }

    fclose(file);
    printf("学生信息已保存到文件:%s\n", filename);
}

// 从文件中加载学生信息
Student* load(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        perror("无法打开文件");
        return NULL;
    }

    Student *head = NULL;
    Student *tail = NULL;
    char name[20];
    double math, chinese, english, physical, chemical, biological;

    while (fscanf(file, "%s %lf %lf %lf %lf %lf %lf",
                  name, &math, &chinese, &english, &physical, &chemical, &biological) == 7) {
        Student *newStudent = createStudent(name, math, chinese, english, physical, chemical, biological);
        if (tail != NULL) {
            tail->next = newStudent;
        } else {
            head = newStudent;
        }
        tail = newStudent;
    }

    fclose(file);
    return head;
}

// 显示学生信息
void show(Student *head) {
    Student *current = head;
    while (current != NULL) {
        printf("%s %.2f %.2f %.2f %.2f %.2f %.2f\n",
               current->name, current->math, current->chinese, current->english,
               current->physical, current->chemical, current->biological);
        current = current->next;
    }
}

// 设置所有学生的数学分数
void setMath(const char *filename, double score) {
    Student *head = load(filename);
    Student *current = head;

    while (current != NULL) {
        current->math = score;
        current = current->next;
    }

    save(head, filename);
    free(head); // 释放链表内存
}

// 释放链表内存
void freeList(Student *head) {
    Student *current = head;
    while (current != NULL) {
        Student *next = current->next;
        free(current);
        current = next;
    }
}

int main() {
    const char *filename = "stu.txt";
    Student *head = createStudent("张志", 67, 67, 87, 56, 65, 43);
    head->next = createStudent("张嘴", 67, 67, 87, 96, 65, 93);
    head->next->next = createStudent("张振义", 67, 37, 97, 56, 75, 43);

    save(head, filename); // 保存学生信息到文件
    freeList(head); // 释放链表内存

    Student *loadedHead = load(filename); // 从文件加载学生信息
    show(loadedHead); // 显示学生信息
    printf("\n");

    setMath(filename, 99); // 设置所有学生的数学分数为99
    loadedHead = load(filename); // 重新加载更新后的学生信息
    show(loadedHead); // 显示更新后的学生信息
    freeList(loadedHead); // 释放链表内存

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值