zoukankan      html  css  js  c++  java
  • 自考新教材-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;
    }

    运行结果:

  • 相关阅读:
    Spring static 静态属性注入
    大众点评Cat--架构分析
    rxjava
    TCP/IP协议三次握手与四次握手流程解析
    [SDOI2014]数数
    CF-GYM101741K. Consistent Occurrences
    [JSOI2012]玄武密码
    [POI2000]病毒
    [JSOI2007]文本生成器
    [HNOI2006]最短母串问题
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12263413.html
Copyright © 2011-2022 走看看