2013-10-28 3 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'와 람다? – crashmstr

답변

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

나는 표준을 사용합니다 :: 종류의

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

여전히 수 있습니다 작동시키지 않으면 더 많은 정보가 도움이 될 것 같아요, 미안 해요. 지금 생각하고있는 것은 "메인 메뉴"에 다섯 번째 옵션을 추가하고 싶습니다. 모든 수신기가 팀에 의해 알파벳 순서로 볼 수있는 곳입니다. 나는 당신의 상인들의 개념을 이해할 수 없다. 이것은 내가 지금까지 가지고있는 것입니다 : – user2929498

+0

else if (a == 5) \t { \t \t cout << "팀별로 알파벳순으로 나열된 수신기 :"<< endl; \t \t 찾는 (; 전 C <- 1, I = 0을 int로 난 ++) 용 \t \t \t (INT의 J = 0; J 데이터 [J + 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 데이터 [j + 1] = 임시; \t \t \t \t \t} \t \t \t하면서 (c user2929498

+0

내 솔루션이 올바르게 정렬 되나요? – hasan83

관련 문제