将数据从文件读入结构体,对数据进行排序并写入文件

Reading data from a file into a struct, sorting the data and writing to a file

本文关键字:数据 排序 文件 从文件读 结构体      更新时间:2023-10-16

我几乎什么都试过了。只是想找点建议。

该项目是从文件["racers2011.txt"]中读取数据到一个结构体中,并对男性和女性的比赛时间进行排序。将雄性和雌性分开分组,并输出它们的等级和比赛时间,作为它们最好的蓝色种族和它们最好的红色种族的总和。我已经读取文件并输出到新文件,但不知道如何对文件进行排序。

如果有人能帮我一点忙,我将不胜感激。

这是我到目前为止的代码(我有一些代码不编译,所以我已经把它注释掉了):

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
struct Racer_struct
{
    int bib;
    char sex;
    char fname[30];
    char lname[30];
    double b1, b2, r1, r2;
};
bool connectInFile(ifstream& fin, char infilename[]);
bool connectOutFile(ofstream& fout, char outfilename[]);
void readData(ifstream& fin, Racer_struct racers[], const int& MAX);
//void racerGender(ostream& fout, Racer_struct racers[], const int& MAX);
//double calcTotalTime(Racer_struct racers[], double total[], const int& MAX);
void writeData(ostream& fout, Racer_struct racers[], const int& MAX);
int main()
{
    const int MAX = 38;
    Racer_struct racers[MAX];
//    double total[MAX];
    ifstream fin;
    ofstream fout;
    char in_file[30], out_file[30];
    bool opened;
    char title[79];
    opened = connectInFile(fin, in_file);
    cout << opened << endl;
    opened = connectOutFile(fout, out_file);
    cout << opened << endl;
    if(opened)
    {
        cout << "CONNECTED to: " << in_file << endl;
        cout << "WRITING to: " << out_file << endl;
        for(int i=0; i<=3; i++)
        {
            fin.getline(title, 80);
            fout << title << "n";
        }
    }
    readData(fin, racers, MAX);
    writeData(fout, racers, MAX);
    fin.close();
    fout.close();
    cout << endl;
    return 0;
}
bool connectInFile(ifstream& fin, char infilename[])
{
    bool success = true;
    cout << "Enter input filename: ";
    cin >> infilename;
    fin.open(infilename);
    if(fin.fail())
        success = false;
    return success;
}
bool connectOutFile(ofstream& fout, char outfilename[])
{
    bool opened = true;
    cout << "Enter the filename you wish to write to: ";
    cin >> outfilename;
    fout.open(outfilename);
    if(fout.fail())
        opened = false;
    return opened;
}
void readData(ifstream& fin, Racer_struct racers[], const int& MAX)
{
    char ws;
    for(int i=0; i<MAX && fin.peek()!= EOF; i++)
    {
        fin >> racers[i].bib >> racers[i].sex >> racers[i].fname >> racers[i].lname
            >> racers[i].b1 >> racers[i].b2 >> racers[i].r1 >> racers[i].r2;
        fin.get(ws);
    }
}
/*
void racerGender(ostream& fout, Racer_struct racers[], const int& MAX)
{
    for(int i=0; i<MAX; i++)
        if(racers[i].sex == 'M')
        {
            calcTotalTime(racers, total, MAX);
            writeData(fout, racers, MAX);
        }
        else
        {
            calcTotalTime(racers, total, MAX);
            writeData(fout, racers, MAX);
        }
}
double calcTotalTime(Racer_struct racers[], double total[], const int& MAX)
{
    double total[MAX];
    for(int i=0; i<MAX; i++)
        if(racers[i].r1 > racers[i].r2 && racers[i].b1 > racers[i].b2)
            total[i] = racers[i].r2 + racers[i].b2;
        else if(racers[i].r2 > racers[i].r1 && racers[i].b2 > racers[i].b1)
            total[i] = racers[i].r1 + racers[i].b1;
        else if(racers[i].r1 > racers[i].r2 && racers[i].b2 > racers[i].b1)
            total[i] = racers[i].r2 + racers[i].b1;
        else
            total[i] = racers[i].b2 + racers[i].r1;
    return total[i];
}
*/
void writeData(ostream& fout, Racer_struct racers[], const int& MAX)
{
    for(int i=0; i<MAX; i++)
    {
        fout << racers[i].bib << "t" << racers[i].sex << "t" << racers[i].fname
             << "t" << racers[i].lname << "t" << racers[i].b1 << "t" << racers[i].b2
             << "t" << racers[i].r1 << "t" << racers[i].r2 /*<< "t" << total[i]*/ << endl;
/*      if((i+1)%5)
            fout << "t";
        else
            fout << endl;
*/
    }
}

使用std::sort。说得太多会暴露的,我想这是作业。

std::sort是一个非常有效的排序函数,它是c++标准的一部分,在标题algorithm中。

std::sort使用了"迭代器"的概念。这是一个比较难的主题,所以我将在这里大致总结一下。在c++中,任何序列都可以表示为一对迭代器:一个指向第一个元素,第二个指向最后一个元素之后的一个位置(因此,[begin, end[)。这在数组中很容易看到:对于大小为N的数组a, a[N]不是数组的一部分。数组的迭代器类型为指针。

那么,让我们看看如何在你的例子中使用std::sort:
std::sort(racers, racers + MAX);

上面这行可以理解为"对racersracers + MAX所分隔的序列中的元素进行排序"。对于数组,数组的名称指向第一个元素,并将该地址的大小添加到"end"迭代器(如上所述)。如果使用标准容器,如std::vector,则使用vector的begin()end()方法来获得相应的迭代器。

现在,std::sort使用比较函数逐个比较每个元素。默认情况下,这是<操作符(因此元素按升序排序)。重载允许您在需要时提供自己的函数。在我们的例子中,为Racer_struct重载<就足够了:

// This should be defined after Racer_struct and before the first call to std::sort
bool operator<(const Racer_struct &left, const Racer_struct &right)
{
    // return true if left should get before right
}

正如John Zwinck所说,您可能希望使用std::sort来进行排序。就我个人而言,我将operator>>operator<<过载为读写。我还会重载operator<来进行比较。

有了这些,你的顶层代码可能看起来像这样:

typedef std::istream_iterator<Racer_struct> reader;
std::vector<Racer_struct> racers((reader(fin)), reader());
std::sort(racers.begin(), racers.end());
std::copy(racers.begin(), racers.end(), 
          std::ostream_iterator<Racer_struct>(std::cout, "n"));

考虑到你的标准(把男性和女性分开),你可能想把性别作为主要领域,然后是时间。这将把所有的雄性和所有的雌性组合在一起(按照你选择的顺序)。