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
4

2 回答 2

0

为此,您应该使用类 std::string 中的比较。关于比较的信息看这里。另一个问题,你不是交换学生。请访问 wiki 进行选择排序

这是我对您的问题的实现。我还没有测试它希望这会奏效。

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;
        }
    }
}
于 2013-11-15T07:49:30.830 回答
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);
    }
于 2013-11-16T05:13:03.093 回答