选择使用结构数组进行排序
Selection sort with arrays of structs
我在选择排序时遇到问题,我正在尝试按字母顺序对学生姓名进行排序。我编译了它,它向我展示了 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
你应该使用 compare from class std::string 来解决这个问题。有关比较的信息请看这里。另一个问题,你不是交换学生。请访问维基进行选择排序。
这是我对您的问题的实现。我还没有测试过它,希望这会起作用。
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;
}
}
}
从您给我的代码中更改一个不等式符号并得到这个!
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);
}
相关文章:
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 合并排序不排序自创建数组类 c++
- 带有枚举方向/类型的气泡排序结构数组
- 为什么指针在对二维数组进行排序时无法正常工作?
- 排序并行数组
- 如何在数组 c++ 中对字符串进行排序
- 为什么在此排序算法实现中,向量明显比数组慢?
- 如何对不在数组中的变量进行排序?
- C++数组与向量排序(在我的情况下,向量比数组慢~2.5倍(无优化))
- 按字母顺序对结构内数组变量中的名称进行排序
- 使用排序函数 c++ 对字符数组进行排序
- 使用 std::sort 对二维 c 数组进行排序
- 使用STL对用户输入数组进行排序的错误有什么解决方案吗?
- 使用 std::sort 对 C 样式的 2D 数组进行部分排序
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 对 0 、1 和 2 的数组进行排序