码迷,mamicode.com
首页 > 编程语言 > 详细

C++实现多级排序

时间:2016-06-27 21:44:22      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

stl中sort详细说明

实现功能:期末开始4位同学的成绩,按多级排序,排序规则为:按数学从小到大,如果数学相等,则按语文从大到小排列,如果语文相等,则按英语从小到大排列,如果英语相等,则按历史从大到小排烈

技术分享
  1 #include "stdafx.h"
  2 
  3 #include
  4 
  5 #include
  6 
  7 #include
  8 
  9 typedef struct stu
 10 
 11 {
 12 
 13     char name[24];
 14 
 15     int math;//数学
 16 
 17     int chinese;//语文
 18 
 19     int english;//英语
 20 
 21     int history;//历史
 22 
 23 }Student;
 24 
 25 //按照你想排序的方法排序
 26 
 27 bool cmp(stu l, stu r)
 28 
 29 {
 30 
 31     if (l.math < r.math)//数学从小到大
 32 
 33     {
 34 
 35         return true;
 36 
 37     }
 38 
 39     else if (l.math == r.math)//如果数学相等
 40 
 41     {
 42 
 43         if (l.chinese > r.chinese)//按语文从大到小
 44 
 45         {
 46 
 47             return true;
 48 
 49         }
 50 
 51         else if (l.chinese == r.chinese)//如果语文相等
 52 
 53         {
 54 
 55             if (l.english < r.english)//按英语从小到大
 56 
 57             {
 58 
 59                 return true;
 60 
 61             }
 62 
 63             else if (l.english == r.chinese)//如果英语相等
 64 
 65             {
 66 
 67                 if (l.history > r.history)//按历史从大到小
 68 
 69                 {
 70 
 71                     return true;
 72 
 73                 }
 74 
 75                 else if (l.history == r.history)
 76 
 77                 {
 78 
 79                     return false;
 80 
 81                 }
 82 
 83             }
 84 
 85         }
 86 
 87     }
 88 
 89     return false;
 90 
 91 }
 92 
 93 using namespace std;
 94 
 95 //  小明 73, 80, 86, 79
 96 
 97 //小花 77, 78, 60, 90
 98 
 99 //  小刚 73, 79, 90, 90
100 
101 //  小白 77, 78, 40, 89
102 
103 //排序后
104 
105 //  小明 73, 80, 86, 79
106 
107 //  小刚 73, 79, 90, 90
108 
109 //  小白 77, 78, 40, 89
110 
111 //小花 77, 78, 60, 90
112 
113 Student students[4] = { { "小明", 73, 80, 86, 79 }
114 
115 , { "小花", 77, 78, 60, 90 }
116 
117 , { "小刚", 73, 79, 90, 90 }
118 
119 , { "小白", 77, 78, 40, 89 } };
120 
121 int _tmain(int argc, _TCHAR* argv[])
122 
123 {
124 
125     cout << "排序前:===========================================" << endl;
126 
127     for (Student s : students)
128 
129     {
130 
131         cout << s.name << ""
132 
133             << s.math << ""
134 
135             << s.chinese << ""
136 
137             << s.english << ""
138 
139             << s.history << endl;
140 
141     }
142 
143     sort(students, students + 4, cmp);
144 
145     cout << "排序后:===========================================" << endl;
146 
147     for (Student s : students)
148 
149     {
150 
151         cout << s.name << ""
152 
153             << s.math << ""
154 
155             << s.chinese << ""
156 
157             << s.english << ""
158 
159             << s.history << endl;
160 
161     }
162 
163     getchar();
164 
165     return 0;
166 
167 }
View Code

效果如:

技术分享多级排序

C++实现多级排序

标签:

原文地址:http://www.cnblogs.com/swarmbees/p/5621590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!