7. 编写程序,对第5题中stu2.dat按总分进行排序,结果存入文件:stu3.dat。

该程序使用C语言实现读取 stu2.dat 文件中的学生数据,然后根据学生的总分进行升序排序,并将排序后的结果写入 stu3.dat 文件。程序采用选择排序算法进行排序操作。

编写程序,对第5题中stu2.dat按总分进行排序,结果存入文件:stu3.dat。


```c
```c
#include <stdio.h>
#include <stdlib.h>
#define N 5
struct student
{
    int number;
    char name[10];
    char sex;
    int chinese;
    int math;
    int pe;
    double num;
    double ave;

};
int main()
{
    FILE *fp1,*fp2;
    int i, u ;
    double ave,num;
    struct student stu[N];
    struct student temp;
    if((fp1=fopen("stu3.dat","w"))==NULL)
    {
        printf("File open er1ror");
        exit(0);
    }
    if((fp2=fopen("D:\\codeblock\\shiyanyiwenjian6\\stu2.dat","r"))==NULL)
    {
        printf("File open error");
        exit(0);
    }
    for(i=0;i<N;i++)
        {
            fscanf(fp2,"%d",&stu[i].number);
            fgetc(fp2);// fgetchar吞空格,避免把空格当成%s%c
            fscanf(fp2,"%s",stu[i].name);
            fgetc(fp2);
            fscanf(fp2,"%c",&stu[i].sex);
            fscanf(fp2,"%d",&stu[i].chinese);
            fscanf(fp2,"%d",&stu[i].math);
            fscanf(fp2,"%d",&stu[i].pe);
            fscanf(fp2,"%lf",&stu[i].num);
            fscanf(fp2,"%lf",&stu[i].ave);
            }
     for(i=0;i<N-1;i++)//选择排序
     {

         for(u=i+1;u<N;u++)
         {
             if(stu[u].num>stu[i].num)
             {temp=stu[u];
             stu[u]=stu[i];
             stu[i]=temp;

             }

         }
     }

    for(i=0; i<N; i++)
    {
        fprintf(fp1,"%d\t%s\t%c\t%d\t%d\t%d\t%lf\t%lf\n",stu[i].number,stu[i].name,stu[i].sex,stu[i].chinese,stu[i].math,stu[i].pe,stu[i].num,stu[i].ave);
    }
    if(fclose(fp1))
    {
        printf("Can not close file");
    }
    if(fclose(fp2))
    {
        printf("Can not close file");
    }

    return 0;
}`

在这里插入图片描述在这里插入图片描述

#include <iostream> #include <iomanip> #include <fstream> using namespace std; // 定义学生结构体 class Student { int id; string name; int chinese; int math; int english; int total; public: bool operator>(Student& s) { return total > s.total; }// 按总成绩从高到低排序要用到 void calculateTotal() { total = chinese + math + english; } friend ostream& operator<<(ostream& out, Student& stu); friend istream& operator>>(istream& in, Student& stu); friend ifstream& operator>>(ifstream& input, Student& stu); friend void sort(Student* stus, int n); }; /***********begin************/ ifstream& operator>>(ifstream& input, Student& stu) { input >> stu.id >> stu.name >> stu.chinese >> stu.math >> stu.english; return input; } istream& operator>>(istream& in, Student& stu) { in >> stu.id>>stu.name>>stu.chinese>>stu.math>>stu.english; return in; } ostream& operator<<(ostream& out, Student& stu) { out << stu.id <<&#39; &#39;<< stu.name << &#39; &#39; << stu.chinese << &#39; &#39; << stu.math << &#39; &#39; << stu.english <<&#39; &#39;<<stu.total<< endl; return out; } void sort(Student* stus, int n) { for (int a = 0;a < n;a++) { for (int b = a + 1;b < n;b++) { if (stus[a].total < stus[b].total) { Student temp = stus[a]; stus[a] = stus[b]; stus[b] = temp; } } } } /***********end************/ int main() { // 1. 输入学生信息并写入文件 result.dat ofstream outFile("result.txt"); if (!outFile) { cerr << "无法打开文件 result.txt" << endl; return 1; } int n; cin >> n; /***********创建动态数组stus************/ Student* stus = new Student[n]; for (int i = 0; i < n; i++) { cin >> stus[i]; stus[i].calculateTotal(); outFile << stus[i]; /***********保存学生信息到文件result.txt************/ } outFile.close(); // 2. 从 result.txt 中读取数据并排序 ifstream inFile("result.txt"); if (!inFile) { cerr << "无法打开文件 result.txt" << endl; return 1; } int i = 0; while ( inFile>>stus[i] /***********从文件result.txt读取学生信息并存入stus[i]************/) { i++; } inFile.close(); /***********调用前面定义的sort函数对数组stus的信息按总分排序************/ sort(stus,n); // 3.排序后的数据写入文件 sort.txt,并输出到屏幕 ofstream sortedFile("sort.txt"); if (!sortedFile) { cerr << "无法打开文件 sort.txt" << endl; return 1; } for (i = 0; i < n;i++) { sortedFile << stus[i]; cout << stus[i]; } sortedFile.close(); delete[]stus; return 0; }
03-30
#include <iostream> #include <iomanip> #include <fstream> using namespace std; // 定义学生结构体 class Student { int id; string name; int chinese; int math; int english; int total; public: bool operator>(Student& s) { return total > s.total; }// 按总成绩从高到低排序要用到 void calculateTotal() { total = chinese + math + english; } friend ostream& operator<<(ostream& out, Student& stu); friend istream& operator>>(istream& in, Student& stu); friend void sort(Student* stus, int n); }; /***********begin************/ /***********end************/ int main() { // 1. 输入学生信息并写入文件 result.dat ofstream outFile("result.txt"); if (!outFile) { cerr << "无法打开文件 result.txt" << endl; return 1; } int n; cin >> n; /***********创建动态数组stus************/ for (int i = 0; i < n; i++) { cin >> stus[i]; stus[i].calculateTotal(); /***********保存学生信息到文件result.txt************/ } outFile.close(); // 2. 从 result.txt 中读取数据并排序 ifstream inFile("result.txt"); if (!inFile) { cerr << "无法打开文件 result.txt" << endl; return 1; } int i = 0; while ( /***********从文件result.txt读取学生信息并存入stus[i]************/ ) { i++; } inFile.close(); /***********调用前面定义的sort函数对数组stus的信息按总分排序************/ // 3.排序后的数据写入文件 sort.txt,并输出到屏幕 ofstream sortedFile("sort.txt"); if (!sortedFile) { cerr << "无法打开文件 sort.txt" << endl; return 1; } for (i = 0; i < n;i++) { sortedFile<<stus[i]; cout << stus[i]; } sortedFile.close(); delete[]stus; return 0; }
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值