当前位置: 首页 > article >正文

C语言练习(32)

(1)有5个学生,每个学生有3门课程的成绩,从键盘输入以上数据(包括学号、姓名、3门课程成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。

设5名学生的学号、姓名和3门课程成绩如下:

99101   Wang   89,98,67.5

99103   Li     60,80,90

99106   Fun    75.5,91.5,99

99110   Ling   100,50,62.5

99113   Yuan   58,68,71

在向文件stud写入数据后,应检查验证stud文件中的内容是否正确。

(2)将(1)题stud文件中的学生数据按平均分进行排序处理,将已排序的学生数据存入一个新文件stu_sort中。

在向文件stu_sort写人数据后,应检查验证stu_sort文件中的内容是否正确。

ps:第一题见31

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

// 学生结构体
typedef struct Student {
    char id[10];
    char name[20];
    double scores[3];
    double average;
} Student;

// 计算平均成绩
void calculateAverage(Student* s) {
    s->average = (s->scores[0] + s->scores[1] + s->scores[2]) / 3;
}

// 交换两个学生结构体
void swap(Student* a, Student* b) {
    Student temp = *a;
    *a = *b;
    *b = temp;
}

// 冒泡排序
void bubbleSort(Student* students, int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (students[j].average > students[j + 1].average) {
                swap(&students[j], &students[j + 1]);
            }
        }
    }
}

// 将学生数据写入文件
void writeToFile(Student* students, int n, char* filename) {
    FILE* fp = fopen(filename, "w");
    if (fp == NULL) {
        perror("Error opening file");
        exit(1);
    }
    for (int i = 0; i < n; i++) {
        fprintf(fp, "%s %s %.2lf %.2lf %.2lf %.2lf\n", students[i].id, students[i].name,
            students[i].scores[0], students[i].scores[1], students[i].scores[2], students[i].average);
    }
    fclose(fp);
}

// 从文件读取学生数据
int readStudentsFromFile(Student* students, char* filename) {
    FILE* fp = fopen(filename, "r");
    if (fp == NULL) {
        perror("Error opening file");
        exit(1);
    }
    int count = 0;
    while (fscanf(fp, "%s %s %lf %lf %lf", students[count].id, students[count].name,
        &students[count].scores[0], &students[count].scores[1], &students[count].scores[2]) == 5) {
        calculateAverage(&students[count]);
        count++;
    }
    fclose(fp);
    return count;
}

// 插入新学生数据并保持顺序
void insertStudent(Student* students, int* n, Student newStudent) {
    int i;
    for (i = 0; i < *n; i++) {
        if (newStudent.average > students[i].average) {
            break;
        }
    }
    for (int j = *n; j > i; j--) {
        students[j] = students[j - 1];
    }
    students[i] = newStudent;
    (*n)++;
}

int main() {
    // (1)
    Student students[5];
    students[0].scores[0] = 89;
    students[0].scores[1] = 98;
    students[0].scores[2] = 67.5;
    strcpy(students[0].id, "99101");
    strcpy(students[0].name, "Wang");

    students[1].scores[0] = 60;
    students[1].scores[1] = 80;
    students[1].scores[2] = 90;
    strcpy(students[1].id, "99103");
    strcpy(students[1].name, "Li");

    students[2].scores[0] = 75.5;
    students[2].scores[1] = 91.5;
    students[2].scores[2] = 99;
    strcpy(students[2].id, "99106");
    strcpy(students[2].name, "Fun");

    students[3].scores[0] = 100;
    students[3].scores[1] = 50;
    students[3].scores[2] = 62.5;
    strcpy(students[3].id, "99110");
    strcpy(students[3].name, "Ling");

    students[4].scores[0] = 58;
    students[4].scores[1] = 68;
    students[4].scores[2] = 71;
    strcpy(students[4].id, "99113");
    strcpy(students[4].name, "Yuan");

    for (int i = 0; i < 5; i++) {
        calculateAverage(&students[i]);
    }
    writeToFile(students, 5, "stud");

    // (2)
    Student sortedStudents[5];
    int count = readStudentsFromFile(sortedStudents, "stud");
    bubbleSort(sortedStudents, count);
    writeToFile(sortedStudents, count, "stu_sort");

    // (3)
    Student newStudent;
    newStudent.scores[0] = 90;
    newStudent.scores[1] = 95;
    newStudent.scores[2] = 60;
    strcpy(newStudent.id, "99108");
    strcpy(newStudent.name, "Xin");
    calculateAverage(&newStudent);
    Student stuNew[6];
    count = readStudentsFromFile(stuNew, "stu_sort");
    insertStudent(stuNew, &count, newStudent);
    writeToFile(stuNew, count, "stu_new");

    // (4)
    // 这里直接将stu_new中的数据复制到stu_sort(实际操作可优化)
    count = readStudentsFromFile(stuNew, "stu_new");
    writeToFile(stuNew, count, "stu_sort");

    return 0;
}


http://www.kler.cn/a/525932.html

相关文章:

  • C动态库的生成与在Python和QT中的调用方法
  • 【安全测试】测开方向学习遇到的问题记录
  • 上位机知识篇---GitGitHub
  • 解锁豆瓣高清海报:深度爬虫与requests进阶之路
  • 21.2-工程中添加FreeRTOS(掌握) 用STM32CubeMX添加FreeRTOS
  • FreeRTOS从入门到精通 第十四章(队列集)
  • C++,STL,【目录篇】
  • Formality:黑盒(black box)
  • 基于RAG方案构专属私有知识库(开源|高效|可定制)
  • oracle中使用in 和 not in 查询效率分析
  • 控件【QT】
  • 对于RocksDB和LSM Tree的一些理解
  • 【MySQL】数据类型与表约束
  • 设想中的计算机语言:可执行对象的构造函数和析构函数
  • Vue.js路由管理与自定义指令深度剖析
  • Python | Pytorch | Tensor知识点总结
  • 智能汽车网络安全威胁报告
  • k8s--部署k8s集群--控制平面节点
  • 春节期间,景区和酒店如何合理用工?
  • DOM操作中childNodes与children的差异及封装方案
  • 算法随笔_30: 去除重复字母
  • 显示当前绑定变量
  • 【Elasticsearch】内置分词器和IK分词器
  • 【VASP】AIMD计算总结
  • 《千朵桃花一世开》浅谈
  • WGCLOUD服务器资源监控软件使用笔记 - Token is error是什么错误