2015-11-13 92 views
0

我的任務是與名,得分爲文件的遊戲讀取(文件看起來像)排序結構和數組

5 
John Doe 200 
Chris Brown 340 
Chris Brown 320 
John Smith 300 
John Doe 600 

,並打印出的字母順序最高的名字(姓)來自那個人的分數。因此,輸出將類似於:

Chris Brown 340 
John Doe 600 
John Smith 300 

我想通了,如何排序和打印從最高到最低,但我失去了對如何從每個人打印出剛中得分最高的分數......任何幫助將不勝感激!

#include <iostream> 
#include <fstream> 
using namespace std; 

struct playerscore 
{ 
    string first, last; 
    int score; 
}; 

bool score(playerscore a, playerscore b); 
void selectionsort(playerscore *A, int n); 

int main() 
{ 
    string file; 
    int n; 
    cin >> file; 
    ifstream fin(file.c_str()); 
    fin >> n; 

    // read in the names and the scores in the form of struct playerscore 
    playerscore *A = new playerscore[n]; 
    for(int i = 0; i < n; i++) 
     fin >> A[i].first >> A[i].last >> A[i].score; 

    // sort the data 
    selectionsort(A, n); 

    // print in sorted order 
    for(int i = 0; i < n; i++) 
     cout << A[i].score << " "; 
    cout << endl; 

    return 0; 
} 

bool before(playerscore a, playerscore b) 
{ 
    return a.score > b.score; 
} 
void selectionsort(playerscore *A, int n) 
{ 
    for(int length = n; length > 1; length--) 
    { 
     //find imax, index of largest 
     int imax = 0, i; 
     for(i = 1; i < length; i++) 
      if(before(A[imax], A[i])) 
       imax = i; 
     // swap A[imax] and the last element 
     playerscore temp = A[imax]; 
     A[imax] = A[length-1]; 
     A[length-1] = temp; 
    } 
} 

回答

0

正如之前指出的那樣,由於這是一項任務,我決定重寫代碼,現在應該看起來像這樣。

  1. 排序基於所述分數

  2. 排序陣列基於字母順序

  3. 打印唯一條目

    #include <iostream> 
    #include <fstream> 
    using namespace std; 
    
    struct playerscore 
    { 
        string first, last; 
        int score; 
    }; 
    
    bool score(playerscore a, playerscore b); 
    void selectionsort(playerscore *A, int n); 
    
    int main() 
    { 
        string file; 
        int n; 
        cin >> file; 
        ifstream fin(file.c_str()); 
        fin >> n; 
    
        // read in the names and the scores in the form of struct playerscore 
        playerscore *A = new playerscore[n]; 
        for (int i = 0; i < n; i++) 
         fin >> A[i].first >> A[i].last >> A[i].score; 
    
        // sort the data 
        selectionsort(A, n); 
        // sort alphabetically and print unique entries 
        for (int i=0; i<n-1; i++) 
        { 
         for (int j=i+1; j<n; j++) 
         { 
          if (A[j].last <= A[i].last && A[j].first < A[i].first) 
          { 
           playerscore tmp = A[i]; 
           A[i] = A[j], A[j] = tmp; 
          } 
        } 
    
        playerscore max; 
        int j = 0; 
        for (int i=0; j+i<n; i++) 
        { 
         max.score = -1; 
    
         while (j+i<n && A[j+i].last == A[i].last && A[j+i].first == A[i].first) 
         { 
          if (A[j+i].score >= A[i].score) 
           max = A[j+i]; 
          j++; 
         } 
         j--; 
    
         if (max.score >= 0) 
          cout << max.last << " " << max.first << " " << max.score << endl; 
        } 
        cout << endl; 
    
        return 0; 
    } 
    
    bool before(playerscore a, playerscore b) 
    { 
         return a.score > b.score; 
    } 
    
    void selectionsort(playerscore *A, int n) 
    { 
        for(int length = n; length > 1; length--) 
        { 
         //find imax, index of largest 
         int imax = 0, i; 
         for(i = 1; i < length; i++) 
          if(before(A[imax], A[i])) 
           imax = i; 
         // swap A[imax] and the last element 
         playerscore temp = A[imax]; 
         A[imax] = A[length-1]; 
         A[length-1] = temp; 
        } 
    } 
    
+0

因爲這個問題與「我的任務」開始,那麼我的假設是,這是家庭作業,他們都應該是學習如何排序和不準使用內置的排序功能。 – crashmstr

+0

好點。但是,我從不傾向於做出假設。 – NTAuthority

+0

那麼你應該也建議他們使用'std :: vector'而不是原始數組:) – crashmstr

0
  1. 排序以升序對數組的名字。 如果比較的名稱相同,則得分較高的元素應該到達數組的較早位置。
  2. 打印名稱與前一個元素不同的第一個元素和元素。

樣品實施:

#include <iostream> 
#include <fstream> 
using namespace std; 

struct playerscore 
{ 
    string first, last; 
    int score; 
}; 

bool score(playerscore a, playerscore b); 
void selectionsort(playerscore *A, int n); 

int main() 
{ 
    string file; 
    int n; 
#if 0 
    cin >> file; 
    ifstream fin(file.c_str()); 
#else 
#define fin cin // to test with online compiler 
#endif 
    fin >> n; 

    // read in the names and the scores in the form of struct playerscore 
    playerscore *A = new playerscore[n]; 
    for(int i = 0; i < n; i++) 
     fin >> A[i].first >> A[i].last >> A[i].score; 

    // sort the data 
    selectionsort(A, n); 

    // print in sorted order 
    for(int i = 0; i < n; i++) 
     if (i == 0 || A[i].first != A[i - 1].first || A[i].last != A[i - 1].last) 
      cout << A[i].first << " " << A[i].last << " " << A[i].score << "\n"; 
    cout << endl; 

    return 0; 
} 

bool before(playerscore a, playerscore b) 
{ 
    return a.first == b.first ? (a.last == b.last ? a.score > b.score : a.last < b.last) : a.first < b.first; 
} 
void selectionsort(playerscore *A, int n) 
{ 
    for(int length = n; length > 1; length--) 
    { 
     //find imax, index of largest 
     int imax = 0, i; 
     for(i = 1; i < length; i++) 
      if(before(A[imax], A[i])) 
       imax = i; 
     // swap A[imax] and the last element 
     playerscore temp = A[imax]; 
     A[imax] = A[length-1]; 
     A[length-1] = temp; 
    } 
} 
0

你已經做了困難的部分。

假設((first, last), score)對已經排序,每個球員得分最高的是這位玩家的第一個記錄,所以只要保持目前的球員的名字作爲curr_name當您訪問數組:

  1. 如果下一個名稱與curr_name相同,您可以放心地忽略此項目。
  2. 如果不是,請打印此記錄並更新curr_name

的代碼是一樣自爆:

#include <iostream> 
#include <fstream> 
#include <algorithm> 
#include <vector> 
#include <string> 
#include <utility> 
using namespace std; 
bool mycmp (pair<pair<string, string>, int> a, pair<pair<string, string>, int> b) { 
    return a.first < b.first || (a.first == b.first && a.second > b.second); 
} 
int main(int argc, char *argv[]) 
{ 
    ifstream fin("data.txt"); 
    int n; 
    fin>>n; 
    vector<pair<pair<string, string>, int>> A(n); 
    for(int i = 0; i < n; ++i){ 
     fin>>A[i].first.first>>A[i].first.second>>A[i].second; 
    } 
    sort(A.begin(), A.end(), mycmp); 
    pair<string, string> cur_name; 
    for(int i = 0; i < n; ++i){ 
     if(cur_name != A[i].first){ 
      cout<<A[i].first.first<<" "<<A[i].first.second<<" "<<A[i].second<<endl; 
      cur_name = A[i].first; 
     } 
    } 
    return 0; 
}