2013-11-15 81 views
0

我有選擇排序的問題,我想按字母順序排列學生名稱。我編譯它,它顯示了我在VS中的整個一堆錯誤。結構數組選擇排序

我不認爲它與我的顯示所有學生的功能做的,我認爲這更多的是與SortByName和SortByScoreFunctions

任何幫助表示讚賞謝謝!

這裏是我的程序結構。

struct StudentType 
{ 
    string studentName; 
    int testScore; 
    char grade; 
}; 


    void SortStudentsByName(StudentType student[], int numStudents) 
    { 
     int startScan, minIndex, FirstInAlphabet; 
     for (startScan = 0; startScan < (NUM_STUDENTS-1); startScan++) 
     { 
      minIndex = startScan; 
      FirstInAlphabet = student[0]; 
      for(int index = startScan+1; index < NUM_STUDENTS; index++) 
      { 
       if (student[index] > FirstInAlphabet) 
       { 
        FirstInAlphabet = student[index]; 
        minIndex = index; 
       } 
      } 
     } 
    } 



     void SortStudentsByScore(StudentType student[], int numStudents) 
{ 
    int startScan, 
     minIndex, 
     lowest; 
    for (startScan = 0; startScan < (NUM_STUDENTS-1); startScan++) 
    { 
     minIndex = startScan; 
     lowest = student[0].testScore; 
     for (int index = startScan+1; index < NUM_STUDENTS; index++) 
     { 
      if(student[index].testScore < lowest) 
      { 
       lowest = student[index].testScore; 
       minIndex = index; 
      } 
     } 
     student[minIndex].testScore = student[startScan].testScore; 
     student[startScan].testScore = lowest; 
     cout <<"List of Students sorted by Score from Highest to Lowest" << endl; 
     DisplayAllStudents(student, numStudents); 
    } 
} 



void DisplayAllStudents(const StudentType student[], int numStudents) 
{ 
    cout << endl; 
    FormatNameScoreGrade(cout); 
    for(int i = 0; i < numStudents; i++) 
    { 
      cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl; 
    } 
    cout << endl; 
    EndOfList(cout); 
} 
當我編譯這裏

是輸出我收到

這些都是我的排序輸出結果按名稱

Fibonacci, Leonardo  63   D 
    Huffman, David  79   C 
     Augusta, Ada  91   A 
Goldbach, Christian  81   B 
     Venn, John  100   A 
    Church, Alonzo  72   C 
    Fermat, Pierre  84   B 
    Kruskal, Joseph  66   D 
     Cantor, Georg  67   D 
     Turing, Alan  85   B 
    Chebysheva, PL  100   A 
DeMorgan, Augustus  79   C 
    Karnaugh, Maurice  72   C 
    Babbage, Charles  98   A 
     Hooper, Grace  95   A 

它在這裏沒有工作

,這是輸出爲我排序最高級

Student Name   Test Score  Grade 
------------------------------------------ 
        -858993460   D 
    Huffman, David-858993460   C 
     Augusta, Ada-858993460   A 
Goldbach, Christian-858993460   B 
     Venn, John-858993460   A 
    Church, Alonzo-858993460   C 
    Fermat, Pierre-858993460   B 
    Kruskal, Joseph-858993460   D 
     Cantor, Georg-858993460   D 
     Turing, Alan-858993460   B 
    Chebysheva, PL-858993460   A 
DeMorgan, Augustus-858993460   C 
    Karnaugh, Maurice-858993460   C 
    Babbage, Charles-858993460   A 
     Hooper, Grace-858993460   A 
+0

對錯誤進行排序並向我們顯示第一個錯誤。 –

+0

「排序」都不會交換條目。按分數排序似乎會使分數變動,但不是名稱或分數。 –

+0

如果有的話,你做了什麼來調試你懷疑的'SortStudentsByX'函數*除了*在這裏發佈? – WhozCraig

回答

0

你應該使用std :: string類來比較這個。有關比較外觀的信息here。 另一個問題是,你沒有交換學生。 請訪問wiki:Selection Sort

這是我爲您的問題實施的。我沒有測試過,希望這會起作用。

void SortStudentsByName(StudentType student[], int numStudents) 
{ 
    for(int i = 0;i < numStudents - 1; ++i) 
    { 
     int min = i; 
     for(int j = i + 1;j < numStudents; ++j) 
     { 
      if(student[i].studentName.compare(student[j].studentName) < 0) 
      { 
       min = j; 
      } 
     } 
     if(min != i) 
     { 
      StudentType temp = student[i]; 
      student[i]  = student[min]; 
      student[min]  = temp; 
     } 
    } 
} 
+0

非常感謝,完美的工作,但一個問題。是選擇排序?因爲對我來說選擇排序和冒泡排序看起來非常相似 –

+0

是的!這是選擇排序。它們具有相同的複雜度O(n),但實現是不同的。這些算法在維基百科上描述得非常好。您只需觀看[selection.gif](http://en.wikipedia.org/wiki/File:Selection-Sort-Animation.gif)和[buble.gif](http://en.wikipedia .ORG /維基/文件:冒泡排序,例如-300px.gif)。 –

+0

我只想說很多,非常感謝你的幫助!沒有人想幫助我。 –

0

從你給我的代碼中改變一個不等號,並得到這個!

void SortStudentsByName(StudentType student[], int numStudents) 
    { 

      int startScan, 
      minIndex; 

     for (startScan = 0; startScan < (numStudents-1); startScan++) 
     { 
      minIndex = startScan; 
      for (int index = startScan; index < numStudents; index++) 
      { 
       if(student[index].studentName < student[minIndex].studentName) 
        minIndex = index; 
      } 
      if(minIndex!=startScan) 
      { 
       StudentType temp = student[minIndex]; 
       student[minIndex] = student[startScan]; 
       student[startScan] = temp; 
      } 
     } 

    cout << endl; 
    cout << "List of Students sorted Alphabetically "<< endl; 
    DisplayAllStudents(student, numStudents); 

    } 



    void SortStudentsByScore(StudentType student[], int numStudents) 
    { 

     int startScan, 
      minIndex; 

     for (startScan = 0; startScan < (numStudents-1); startScan++) 
     { 
      minIndex = startScan; 
      for (int index = startScan; index < numStudents; index++) 
      { 
       if(student[index].testScore>student[minIndex].testScore) 
        minIndex = index; 
      } 
      if(minIndex!=startScan) 
      { 
       StudentType temp = student[minIndex]; 
       student[minIndex] = student[startScan]; 
       student[startScan] = temp; 
      } 
     } 

     cout <<"List of Students sorted by Score from Highest to Lowest" << endl; 
     DisplayAllStudents(student, numStudents); 
    }