0

我正在尝试按团队的字母顺序为我的程序排序,但没有任何运气。如果有任何提示或建议,我将不胜感激。下面是我减去数据输入的程序。基本上我只想知道我将如何仅按字母顺序按团队进行专门排序。

nflrecievers data[100];
ifstream fin;
fin.open("data.txt");
ofstream fout;
fout.open("validationReport.txt");
int i = 0;

while(!fin.eof())
{
    fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
    i = i + 1;

}

int a;
int b;
cout << " Select NFL Receivers Statistics. Input 1-4 " << endl;
cout << " 1) Receivers with 25+ Rec and 300+ Yards. " << endl;
cout << " 2) Recievers with 3+ TDs and 3+ Rec over 20 Yards. " << endl;
cout << " 3) Recievers with 100+ Yards per game and 15+ First Downs. " << endl;
cout << " 4) Veiw Total Recievers Statistics. " << endl;
cin >> a;


int c = 0;
if (a==1)
{

    cout << " Receivers with 25+ Rec and 300+ Yards. " << endl;
    while( c < i-1)
    {
        if(data[c].rec > 25 && data[c].yards > 300)
        {
            cout << endl;
            cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
            cout << " Rec: " << data[c].rec << " Yards: " << data[c].yards << endl;
            cout << endl;
        }
        c++;
    }
}

else if(a==2)
{

    cout << " Recievers with 3+ TDs and 3+ Receptions past 20 Yards. " << endl;
    while( c < i-1)
    {
        if(data[c].tds > 3 && data[c].recpasttwenty_yrds > 3)
        {
            cout << endl;
            cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
            cout << " TDs: " << data[c].tds << " Receptions past 20 Yards: " << data[c].recpasttwenty_yrds << endl;
            cout << endl;

        }
        c++;
    }

}
else if(a==3)
{
    cout << " Recievers who average over 100+ yards per game and 15+ First Downs. " << endl;
    while( c < i-1)
    {
        if(data[c].yrds_pergame > 100 && data[c].first_dwns > 15)
        {
            cout << endl;
            cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
            cout << " Average Yards per game: " << data[c].yrds_pergame << " First Downs: " << data[c].first_dwns << endl;
            cout << endl;

        }
        c++;
    }
}
else if(a==4)
{
    cout << " Select a Reciever: "  << endl;
    while( c < i-1)
    {
        cout << c << ") " << data[c].fname << " " << data[c].lname << endl;
        c++;
    }
    cout << " Total NFL Receivers Statistics. " << endl;
    cin >> b;
    cout << data[b].fname << " " << data[b].lname << endl;
    cout << " Team: " << data[b].team << endl;
    cout << " Receptions: " << data[b].rec << endl;
    cout << " Yards: " << data[b].yards << endl;
    cout << " Average Yards Per Catch: " << data[b].avgyrds_percatch << endl;
    cout << " Longest Reception: " << data[b].longest_rec << endl;
    cout << " Receptions over 20 Yards: " << data[b].recpasttwenty_yrds << endl;
    cout << " Yards per game " << data[b].yrds_pergame << endl;
    cout << " Fumbles: " << data[b].fumbles << endl;
    cout << " Average Yards After Catch " << data[b].yac << endl;
    cout << " Total First Downs: " << data[b].first_dwns << endl;
}

return 0;

}
4

3 回答 3

2
std::sort(std::begin(data), std::end(data), 
          [](const nflrecievers& a, const nflrecievers& b) { return a.team < b.team; });
于 2013-10-28T23:07:39.023 回答
0

我会使用 std::sort

bool compare_teams(const nflrecievers &a, const nflrecievers &b) {
  return a.team < b.team;
}

int i = 0;
while(!fin.eof())
{
    fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
    i = i + 1;
}

std::sort(data, data+i, compare_teams);
于 2013-10-28T23:09:43.517 回答
0
for (int i=0;i<c-1;i++)
   for (int j=0;j<c-1;j++)
      if (data[j].team>data[j+1].team)
      {
           nflrecievers temp = data[j];
           data[j] = data[j+1];
           data[j+1] = temp;
      }

另一种解决方案:

int compare(const void *v1, const void *v2)
{
    nflrecievers p1 = *(nflrecievers *)v1, p2 = *(nflrecievers *)v2;
    return strcmp(p1.team,p2.team);
}

qsort(data,c,sizeof(data),compare);
于 2013-10-28T23:09:53.343 回答