2013-10-28 24 views
0

我正在尝试按照字母顺序按团队排序,但没有任何运气。如果有任何提示或建议,我会很感激。以下是我减去数据输入的程序。基本上我只想知道我将如何专门按字母顺序排序。如何在C++编程中按字母顺序排序

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; 

} 
+0

编辑和包括数据类或结构。我们需要知道成员的数据类型。 – hasan83

+0

请仅填写相关代码。您发布的内容与分类无关。 – interjay

+0

'std :: sort'与lambda? – crashmstr

回答

2
std::sort(std::begin(data), std::end(data), 
      [](const nflrecievers& a, const nflrecievers& b) { return a.team < b.team; }); 
0

我会使用std ::排序

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); 
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); 
+0

它仍然可以不要让它起作用,我想更多的信息会有所帮助,对不起。我现在想的是,我想在“主菜单”中添加第5个选项,所有接收者都可以按字母顺序查看。我理解你的连接器的概念只是不确定把它放在我的结构中。这是我迄今为止: – user2929498

+0

否则如果(a == 5) \t { \t \t cout <<“接受者按团队列出Alpabetically:”<< endl; \t \t的for(int i = 0;我数据[D + 1] .team) \t \t \t \t { \t \t \t \t \t nflrecievers温度=数据[J]; \t \t \t \t \t data [j] = data [j + 1]; \t \t \t \t \t data [j + 1] = temp; \t \t \t \t} \t \t \t \t而(C user2929498

+0

我的解决方案排序是否正确? – hasan83