湖大CG满分教程:学生成绩管理(C++语言二进制文件读写)

该程序定义了一个学生结构体,用于存储学号、姓名、性别、年龄及两门课程成绩。它能读取二进制文件中的学生信息,显示数据,然后按总成绩降序排序。用户可以选择继续录入新数据,新数据会整合到已排序的列表中,最后将排序后的数据重新写入文件。
摘要由CSDN通过智能技术生成

【问题描述】

在以下问题中,定义学生的结构体student,录入学生信息,并存入到二进制文件grade.bin中,要求存入的是按照总成绩从大到小的顺序存储,数据可以分多次录入,每次录入前,都将显示文件中已有的数据,其具体形式如下:

在提示Input continue(y/n)?下输入'y',则可以继续输入,否则将退出。继续输入的内容将与现有的数据进行重新按总分从大到小的顺序排列后重新写入文件grade.bin中。

【输入形式】

【输出形式】文件

调试时的测试文件grade.bin压缩文件为

grade.zip

请将这个文件下载到与源代码相同的文件夹下并解压。

#include <iostream>
#include <fstream>
using namespace std;

/*
    瀹氫箟缁撴瀯浣搒tudent鐢ㄤ簬琛ㄧず瀛︾敓鐨勫熀鏈俊鎭紝閲囩敤閾捐〃缁撴瀯
*/
struct student
{
    int no;                       // 瀛﹀彿
    char name[20], sex;           // 濮撳悕浠ュ強鎬у埆, 璇锋€濊€冨鏋滃皢name[20]淇敼涓?name锛岀粨鏋滃皢浼氭€庢牱锛岃繕閫傚悎鏈鐨勫鐞嗘柟寮忓悧锛?
    int age;                      // 骞撮緞
    float chinese, computer;      // 琛ㄧず涓ら棬璇剧▼鐨勬垚缁?
    student *next;
    bool operator<(const student &another) const      // 閲嶈浇杩愮畻绗?< (灏忎簬)锛岀敤浜庣粨鏋勪綋瀵硅薄涔嬮棿鐨勬瘮杈冿紝鍦↙inkSort涓瘮杈冮摼琛ㄧ殑涓や釜鑺傜偣澶у皬
    {
        return (chinese + computer) < (another.chinese + another.computer);
    }
};

student* ReadFile(ifstream& infile, student *head, int &n);  // 浠庝簩杩涘埗鏂囦欢(鏂囦欢鎸囬拡涓篺p)涓皢鎵€鏈夋暟鎹鍏ワ紝瀛樺偍浜庝互head涓哄ご鎸囬拡鐨勯摼琛ㄤ腑
void display(student *head);                         // 鐢ㄤ簬鏄剧ず閾捐〃鐨勫唴瀹?
void WriteFile(ofstream& outfile, student *head);             // 灏嗕互head涓哄ご鎸囬拡鐨勯摼琛ㄤ腑鐨勬暟鎹啓鍏ヤ簩杩涘埗鏂囦欢(鏂囦欢鎸囬拡涓篺p)
student* input(student *head, int &n);               // 杈撳叆鏂扮殑鏁版嵁锛屽皢鏂版暟鎹繛鎺ュ埌閾捐〃鏈熬
student* LinkSort(student *head, const int &n);      // 瀹炵幇瀵归摼琛ㄧ殑鎺掑簭锛屾寜鎬绘垚缁╀粠澶у埌灏忔帓鍒楋紝鍐掓场鎺掑簭
void swapNode(student *s, student *q);               // 瀹炵幇瀵归摼琛ㄨ妭鐐圭殑鏁版嵁浜ゆ崲锛岀敤浜庢帓搴? 鍦ㄥ嚱鏁癓inkSort涓皟鐢?

int main()
{
    int n = 0;
    student *head = NULL;

    ifstream getf;
getf.open("grade.bin",ios::in|ios::binary); 
    head = ReadFile(getf, head, n);
    display(head);
    getf.close();

    head = input(head, n);
    head = LinkSort(head, n);

    ofstream putf;
putf.open("grade.bin",ios::in|ios::binary);
    WriteFile(putf, head);
    putf.close();

    return 0;
}

student* ReadFile(ifstream& infile, student* head, int &n)
{
student *q = head;
    if (head)
        while(q->next) q = q->next;
    while(!infile.eof())
    {
        student *p = new student;                                     
        if (!infile.read((char*)p,sizeof(student))) 
        {
             delete p;
            break;
        }   
        p->next = NULL;
        if (!head)
            head = p;
        else
            q->next = p;

        q = p;
        n++;
    }
    return head;
}

void display(student* head)
{
    student *p = head;
    while(p)
    {
        printf("No: %d\n", p->no);
        printf("Name: %s\n", p->name);
        printf("Sex: %c\n", p->sex);
        printf("Age: %d\n", p->age);
        printf("Scores: %f %f\n\n", p->chinese, p->computer);
        p = p->next;
    }
}

student* input(student* head, int &n)
{

    student *q = head;

    if (head)
        while(q->next) q = q->next;

    while(1)
    {
        printf("Input continue(y/n)?");
        if (getchar() != 'y')
            break;

        student *p = new student;

        printf("No: ");
        scanf("%d", &p->no);
        getchar();
        printf("Name: ");
        gets(p->name);
        printf("Sex(M/F): ");
        p->sex = getchar();
        printf("Age: ");
        scanf("%d", &p->age);
        printf("Chinese score: ");
        scanf("%f", &p->chinese);
        printf("Computer score: ");
        scanf("%f", &p->computer);
        p->next = NULL;
        getchar();

        if (!head)
            head = p;
        else
            q->next = p;

        q = p;
        n++;
    }

    return head;
}

student* LinkSort(student* head, const int &n)
{
    student *p;
    for(int i = 1; i <= n - 1; i++)
    {
        p = head;
        for(int j = 1; j <= n - i; j++)
        {
            if (*p < *p->next)
            {
                swapNode(p,p->next);
            }
            p = p->next;
        }
    }

    return head;
}

void swapNode(student* s, student* q)
{
    student *snext = s->next, *qnext = q->next, tmp;
    tmp = *s;
    *s = *q;
    *q = tmp;
    s->next = snext;
    q->next = qnext;
}

void WriteFile(ofstream& outfile, student* head)
{
while(head)
    {
        outfile.write((char*)head,sizeof(student)); 
        head = head->next;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值