吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C++ 中的结构排序

C++ 中的结构排序

来源:网络 | 更新时间:2022-01-21 19:59:07
所有学生都有不同科目(物理、化学和数学)的名称和分数。任务是计算所有学生的总分和排名。最后显示所有学生按等级排序的情况。 使用以下规则计算学生的排名。 
  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()进行结构排序。在结构排序中,结构对象所具有的所有相应属性都根据对象的一个​​(或多个)属性进行排序。 在这个例子中,不同科目的学生分数由用户提供。将各个科目的这些分数相加以计算学生的总分,然后根据他们的排名对不同的学生进行排序(如上所述)。
// 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 << " "
<< "Totaln";

// 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
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com