📜  C++中的结构排序(按多个规则)

📅  最后修改于: 2021-05-26 02:44:37             🧑  作者: Mango

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

  1. 如果总分数不同,则分数较高的学生将获得更高的排名。
  2. 如果总成绩相同,则数学成绩较高的学生将获得更高的排名。
  3. 如果总分数相同,而数学中的分数也相同,则物理分数较高的学生将获得更高的排名。
  4. 如果总分数相同,而数学和物理分数均相同,则化学分数较高的学生将获得更高的排名。
  5. 如果所有分数(总分,数学,物理和化学)都相同,那么任何学生都可以被分配床位。

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

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 
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\n";
 
    // 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 << "\n";
    }
 
    return 0;
}


输出
Rank Name     Maths Physics Chemistry Total
1    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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”