STL的排序准则两种排序方式.docx

上传人:b****3 文档编号:7027573 上传时间:2023-05-11 格式:DOCX 页数:8 大小:118.97KB
下载 相关 举报
STL的排序准则两种排序方式.docx_第1页
第1页 / 共8页
STL的排序准则两种排序方式.docx_第2页
第2页 / 共8页
STL的排序准则两种排序方式.docx_第3页
第3页 / 共8页
STL的排序准则两种排序方式.docx_第4页
第4页 / 共8页
STL的排序准则两种排序方式.docx_第5页
第5页 / 共8页
STL的排序准则两种排序方式.docx_第6页
第6页 / 共8页
STL的排序准则两种排序方式.docx_第7页
第7页 / 共8页
STL的排序准则两种排序方式.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

STL的排序准则两种排序方式.docx

《STL的排序准则两种排序方式.docx》由会员分享,可在线阅读,更多相关《STL的排序准则两种排序方式.docx(8页珍藏版)》请在冰点文库上搜索。

STL的排序准则两种排序方式.docx

STL的排序准则两种排序方式

题目描述:

学校要对某专业学生成绩设定成绩等级。

首先输入学生人数(人数不大于100),然后输入每个学生的学号和三门课成绩。

计算总分,按照总分设定成绩等级:

前20%为A级;其后的40%为B级;其后的30%为C级;最后的10%为D级。

输出:

1)按总分由高到低输出:

学生的学号、三门课成绩、总分和成绩等级;2)按输入顺序输出。

如图(16人)。

设定成绩等级依据四舍五入原则:

如果计算出A级为2.6人,即设置为3人;如果为2.4,则为2人

实验代码如下:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

usingnamespacestd;

classstudent

{

private:

doubleenglish;

doublemath;

doublechinse;

doubletotal;

intorder;

charlevel;

stringnum;

public:

student(into,stringn,chardd,vectortemp)

{

vector:

:

iteratorpos=temp.begin();

order=o;

num=n;

level=dd;

english=*pos;

math=*(++pos);

chinse=*(--temp.end());

total=english+chinse+math;

}

voidSet_level(chard){level=d;}

charGet_level(){returnlevel;}

doubleGet_total(){returntotal;}

intGet_order(){returnorder;}

voidouput()

{

cout<<"num:

"<

"<

"<

<<"chinse:

"<

"<

"<

}

//如果要求有多红排序准则,就可以使用友元函数。

friendclassord;

friendclasstot;

//如果只要求一种排序方式,可以直接在类里面进行定义。

如下所示

//public:

//booloperator<(conststudent&st)const//{

//returnthis->total

//}

/*booloperator<(conststudent&st)const

{

returnthis->order

}*/

};

//定义的排序准则,按照输入顺序排序

classord

{

public:

booloperator()(conststudent&st1,conststudent&st2)const

{

returnst1.order

}

};

//定义的排序准则,按照总成绩大小排序

classtot

{

public:

booloperator()(conststudent&st1,conststudent&st2)const

{

returnst1.total

}

};

主函数调用为:

#include"stdafx.h"

#include"example38.h"

intmain()

{

intn,i,k;

stringnum;

doubleeng,mat,chi,total;

vectortemp;

vectornum_temp;

multisetst;

multisetst3;

vector:

:

iteratornum_pos;

multimap>stu;

multimap>:

:

iteratorstu_pos;

multimaplevel;

multimap:

:

iteratorlevel_pos;

multimaporder;

multimap:

:

iteratorpos;

cout<<"Howmanystudent?

";

cin>>n;

for(i=0;i

{

cout<<"thestudentnum:

";

cin>>num;

num_temp.push_back(num);

cout<<"englishmathchinse:

"<

cin>>eng>>mat>>chi;

total=eng+mat+chi;

temp.push_back(eng);

temp.push_back(mat);

temp.push_back(chi);

stu.insert(make_pair(i,temp));

order.insert(make_pair(total,i));

temp.clear();

}

eng=n*0.2;

mat=n*0.6;

chi=n*0.9;

if(eng-floor(eng)>=0.5)

eng=(int)(eng+1.0);

else

eng=(int)eng;

if(mat-floor(mat)>=0.5)

mat=(int)(mat+1.0);

else

mat=(int)mat;

if(chi-floor(chi)>=0.5)

chi=(int)(chi+1.0);

else

chi=(int)chi;

for(i=0,pos=order.begin();pos!

=order.end(),i

{

if((i+1)<=eng)

{

k=pos->second;

level.insert(make_pair(k,'A'));

}

elseif((i+1)<=mat)

{

k=pos->second;

level.insert(make_pair(k,'B'));

}

elseif((i+1)<=chi)

{

k=pos->second;

level.insert(make_pair(k,'C'));

}

else

{

k=pos->second;

level.insert(make_pair(k,'D'));

}

}

//getthestudent

chardd;

num_pos=num_temp.begin();

stu_pos=stu.begin();

for(i=0;i

{

num=*num_pos;

temp=stu_pos->second;

for(level_pos=level.begin();level_pos!

=level.end();++level_pos)

{

if(level_pos->first==i)

{

dd=level_pos->second;

break;

}

}

studentst1(i,num,dd,temp);

st.insert(st1);

st3.insert(st1);

++num_pos;

++stu_pos;

}

cout<<"theorderis:

"<

multiset:

:

iteratorst_pos;

multiset:

:

iteratorst3_pos;

for(st_pos=st.begin();st_pos!

=st.end();++st_pos)

{

studentst2=*st_pos;

st2.ouput();

}

cout<

"<

for(st3_pos=st3.begin();st3_pos!

=st3.end();++st3_pos)

{

studentst3=*st3_pos;

st3.ouput();

}

return0;

}

实验结果展示:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2