C++中的多结构排序

先决条件: C语言中的结构 为所有学生提供不同科目(物理、化学和数学)的名称和分数。任务是计算所有学生的总分和等级。最后显示按等级排序的所有学生。 学生的排名使用以下规则计算。

null
  1. 如果总分不同,那么分数越高的学生排名越好。
  2. 如果总分相同,那么数学分数越高的学生排名就越好。
  3. 如果总成绩相同,数学成绩也相同,那么物理成绩好的学生排名就更好。
  4. 如果总分相同,数学和物理的分数也相同,那么化学成绩好的学生排名就更好。
  5. 如果所有分数(总分、数学、物理和化学)相同,那么任何学生都可以被分配到Bedter等级。

我们使用下面的结构来存储学生的详细信息。

struct Student {    string name; // Given    int math;  // Marks in math (Given)    int phy;   // Marks in Physics (Given)    int che;   // Marks in Chemistry (Given)    int total; // Total marks (To be filled)    int rank;  // Rank of student (To be filled)};  

我们使用 std::sort() 对于 结构排序 .在结构排序中,结构对象拥有的所有相应属性都是基于该对象的一个(或多个)属性进行排序的。 在本例中,不同科目的学生分数由用户提供。将各个科目的分数相加,计算学生的总分数,然后根据学生的等级对不同的学生进行排序(如上所述)。

CPP

// C++ program to demonstrate structure sorting in C++
#include <bits/stdc++.h>
using namespace std;
struct Student
{
string name; // Given
int math; // Marks in math (Given)
int phy; // Marks in Physics (Given)
int che; // Marks in Chemistry (Given)
int total; // Total marks (To be filled)
int rank; // Rank of student (To be filled)
};
// Function for comparing two students according
// to given rules
bool compareTwoStudents(Student a, Student b)
{
// If total marks are not same then
// returns true for higher total
if (a.total != b.total)
return a.total > b.total;
// If marks in Maths are same then
// returns true for higher marks
if (a.math != b.math)
return a.math > b.math;
if (a.phy != b.phy)
return a.phy > b.phy;
return (a.che > b.che);
}
// Fills total marks and ranks of all Students
void computeRanks(Student a[], int n)
{
// To calculate total marks for all Students
for ( int i = 0; i < n; i++)
a[i].total = a[i].math + a[i].phy + a[i].che;
// Sort structure array using user defined
// function compareTwoStudents()
sort(a, a + 5, compareTwoStudents);
// Assigning ranks after sorting
for ( int i = 0; i < n; i++)
a[i].rank = i + 1;
}
// Driver code
int main()
{
int n = 5;
// array of structure objects
Student a[n];
// Details of Student 1
a[0].name = "Bryan" ;
a[0].math = 80;
a[0].phy = 95;
a[0].che = 85;
// Details of Student 2
a[1].name = "Kevin" ;
a[1].math = 95;
a[1].phy = 85;
a[1].che = 99;
// Details of Student 3
a[2].name = "Nicky" ;
a[2].math = 95;
a[2].phy = 85;
a[2].che = 80;
// Details of Student 4
a[3].name = "Steve" ;
a[3].math = 80;
a[3].phy = 70;
a[3].che = 90;
// Details of Student 5
a[4].name = "Rohan" ;
a[4].math = 80;
a[4].phy = 80;
a[4].che = 80;
computeRanks(a, n);
// Column names for displaying data
cout << "Rank"
<< " "
<< "Name"
<< "     " ;
cout << "Maths"
<< " "
<< "Physics"
<< " "
<< "Chemistry" ;
cout << " "
<< "Total" ;
// Display details of Students
for ( int i = 0; i < n; i++) {
cout << a[i].rank << "    " ;
cout << a[i].name << "      " ;
cout << a[i].math << "     " << a[i].phy << "     "
<< a[i].che << "       " ;
cout << a[i].total << " " ;
cout << "" ;
}
return 0;
}


输出

Rank Name     Maths Physics Chemistry Total1    Kevin      95     85     99       279 2    Nicky      95     85     80       260 3    Bryan      80     95     85       260 4    Rohan      80     80     80       240 5    Steve      80     70     90       240 

相关文章: C++中的Stutter() C中qsort()的比较函数 C qSORT()与C++的SO() 根据设置位的计数对数组进行排序 本文由 阿比纳夫·蒂瓦里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享