• 自考新教材-p311


    源程序:

    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    using namespace std;
    const int MAX_NUM = 1000;
    class CStudent
    {
    public:
    char id[11];
    char name[21];
    int score;
    }stu[MAX_NUM];
    int MyCompare(const void *el, const void *e2)
    {
    return(*(CStudent *)el).score - (*(CStudent *)e2).score;
    }
    int main()
    {
    ifstream srcFile("c:\tmp\score.txt", ios::in);
    if (!srcFile)
    {
    cout << "opening source file error." << endl;
    return 0;
    }
    ofstream destFile("c:\tmp\out.txt", ios::out);
    if (!destFile)
    {
    srcFile.close();
    cout << "opening destination file error." << endl;
    return 0;
    }
    int n = 0;
    cout << "排序前: ";
    while (srcFile >> stu[n].id >> stu[n].name >> stu[n].score)
    {
    cout << stu[n].id << "" << stu[n].name << "" << stu[n].score << endl;
    n++;
    }
    qsort(stu, n, sizeof(CStudent), MyCompare);
    cout << "排序后: ";
    for (int i = 0; i<n; ++i)
    {
    cout << stu[i].id << "" << stu[i].name << "" << stu[i].score << endl;
    destFile << stu[i].id << "" << stu[i].name << "" << stu[i].score << endl;
    }
    destFile.close();
    srcFile.close();
    system("pause");
    return 0;
    }

    运行结果:

  • 相关阅读:
    PKU 1860 Currency Exchange 最短路 bellman
    PKU 3259 Wormholes 最短路 bellman
    bzoj3514
    bzoj2594
    bzoj3901
    bzoj2843&&1180
    bzoj2631
    bzoj2049
    bzoj2002
    bzoj1146
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12263413.html
Copyright © 2020-2023  润新知