怎么把排序函数用一个函数整合一下,但还是可以实现按各数据成员排序

李荣强 2012-04-08 05:03:54
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
const int n=2;
const int m=10;
class student{
private:
int ID,english,math,computer,total;
string name;
string sex;
int chinese;
public:
student(){cout<<"构造函数在执行\n";}
~student(){cout<<"析构函数在执行\n";}
student(student &p);
student &operator=(student&);
void Getinfo();
friend ostream&operator<<(ostream&os,const student &a);
friend istream&operator>>(istream&is,student&a);
void Print(student st[])const;
int FindID(student st[])const;
void FindName(student st[])const;
void SortName(student st[]);
void SortID(student st[],int (*fun)(int a,int b));
void SortTotal(student st[],int (*fun)(int a,int b));
void SortChinese(student st[],int (*fun)(int a,int b));
void SortMath(student st[],int (*fun)(int a,int b));
void SortEnglish(student st[],int (*fun)(int a,int b));
void SortComputer(student st[],int (*fun)(int a,int b));
void swap(student &,student &);
friend bool checki(string str);
};
void student::SortTotal(student st[n],int (*fun)(int a,int b))
{
int i,j,k; student s;
for(i=0;i<n-1;i++)
{ k=i;
for(j=i+1;j<n;j++)
if(fun(st[k].total,st[j].total)) {k=j;}
if(k!=i){s.swap(st[i],st[k]);}
}
for(i=0;i<n;i++)
cout<<st[i];
}
void student::SortID(student st[n],int (*fun)(int a,int b)) //fun是一个函数指针,用来选择升序、降序!
{
int i,j,k;
student s;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(fun(st[k].ID,st[j].ID)) {k=j;}
if(k!=i) {s.swap(st[i],st[k]); }
}
for(i=0;i<n;i++)
cout<<st[i];
}//这只是两个排序函数,我想把这些排序函数整合到一个函数中,但还是能实现按各数据成员排序!求建议!求实现方法!
...全文
177 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattycat 2012-04-08
  • 打赏
  • 举报
回复
参考这个吧,用stl的sort,你只需要实现几个比较函数,按你的不同的类成员比较的函数,比如total, ID, math等.
http://www.cppblog.com/mzty/archive/2005/12/15/1770.html
李荣强 2012-04-08
  • 打赏
  • 举报
回复
void student::SortTotal(student st[n],int (*fun)(int a,int b))
{
int i,j,k; student s;
for(i=0;i<n-1;i++)
{ k=i;
for(j=i+1;j<n;j++)
if(fun(st[k].total,st[j].total)) {k=j;}//我的意思是能不能把这里的total用一个什么来代替,使之可以代替ID,math,chinese等,求高手
if(k!=i){s.swap(st[i],st[k]);}
}
for(i=0;i<n;i++)
cout<<st[i];
}
iamnobody 2012-04-08
  • 打赏
  • 举报
回复
http://en.cppreference.com/w/cpp/algorithm/sort

1.参考std::sort() 用函数对象.

2.给自己的sort 增加一个参数:成员指针.先 google :成员指针或 c++ primer-> 类->成员指针.
李荣强 2012-04-08
  • 打赏
  • 举报
回复
void print()
{
cout<<"欢迎使用荣强学生信息管理系统,请根据提示选择操作\n";
cout<<"温馨提示\n";
cout<<"输入1,进入输入数据界面\n";
cout<<"输入2,进入输出数据界面\n";
cout<<"输入3,进入数据按学号排序界面\n";
cout<<"输入4,进入数据按学号查找界面\n";
cout<<"输入5,进入数据按姓名排序界面\n";
cout<<"输入6,进入数据按姓名查找界面\n";
cout<<"输入7,进入数据按总分排序界面\n";
cout<<"输入8,进入数据按语文成绩排序界面\n";
cout<<"输入9,进入数据按数学成绩排序界面\n";
cout<<"输入10,进入数据按英语成绩排序界面\n";
cout<<"输入11,进入数据按计算机成绩排序界面\n";
cout<<"输入0,退出该系统\n";
}
int ascending(int a,int b){if(a>b) return 1;else return 0;}
int descending(int a,int b){if(a<b) return 1;else return 0;}
int main()
{ student a;
print();
student ST[n];
int i,choice=1;
while(choice!=0)
{
kk: cout<<"请输入你的选择\n";string str;
cin>>str;
if(!checki(str)) {cout<<"输入格式不正确,请重新输入\n";cin>>str;}
choice=atoi(str.c_str());
int scend;
switch(choice)
{
case 0:cout<<"退出系统,欢迎下次使用\n";break;
case 1:cout<<"进入输入数据界面\n";
for(i=0;i<n;i++) cin>>ST[i]; print();break;
case 2:cout<<"进入输出数据界面\n";for(i=0;i<n;i++) cout<<ST[i];print();
break;
case 3:cout<<"进入数据按学号排序界面\n";cout<<"升序请按1,否则降序\n";cin>>str;
if(!checki(str)) {cout<<"输入格式不正确,请重新输入\n";cin>>str;}
scend=atoi(str.c_str());
if(scend==1) {ST[0].SortID(ST,ascending);}
else {ST[0].SortID(ST,descending);}
print(); break;
case 4:cout<<"进入数据按学号查找界面\n";ST[0].FindID(ST);print();break;
case 5:cout<<"进入数据按姓名排序界面\n";ST[0].SortName(ST);print(); break;
case 6:cout<<"进入数据按姓名查找界面\n";ST[0].FindName(ST); print();break;
case 7:cout<<"进入数据按总分排序界面\n";cout<<"升序请按1,否则降序\n";
cin>>str;
if(!checki(str)) {cout<<"输入格式不正确,请重新输入\n";cin>>str;}
scend=atoi(str.c_str());
if(scend==1) {ST[0].SortTotal(ST,ascending);}
else {ST[0].SortTotal(ST,descending);}
print(); break;
case 8:cout<<"进入数据按语文排序界面\n";cout<<"升序请按1,否则降序\n";
cin>>str;
if(!checki(str)) {cout<<"输入格式不正确,请重新输入\n";cin>>str;}
scend=atoi(str.c_str());
if(scend==1) {ST[0].SortChinese(ST,ascending);}
else {ST[0].SortChinese(ST,descending);}
print(); break;
case 9:cout<<"进入数据按数学排序界面\n";cout<<"升序请按1,否则降序\n";
cin>>str;
if(!checki(str)) {cout<<"输入格式不正确,请重新输入\n";cin>>str;}
scend=atoi(str.c_str());
if(scend==1) {ST[0].SortMath(ST,ascending);}
else {ST[0].SortMath(ST,descending);}
print(); break;
case 10:cout<<"进入数据按英语排序界面\n";cout<<"升序请按1,否则降序\n";
cin>>str;
if(!checki(str)) {cout<<"输入格式不正确,请重新输入\n";cin>>str;}
scend=atoi(str.c_str());
if(scend==1) {ST[0].SortEnglish(ST,ascending);}
else {ST[0].SortEnglish(ST,descending);}
print(); break;
case 11:cout<<"进入数据按计算机排序界面\n";cout<<"升序请按1,否则降序\n";
cin>>str;
if(!checki(str)) {cout<<"输入格式不正确,请重新输入\n";cin>>str;}
scend=atoi(str.c_str());
if(scend==1) {ST[0].SortComputer(ST,ascending);}
else {ST[0].SortComputer(ST,descending);}
print(); break;
default:cout<<"输入有误,请重新输入\n";goto kk;break;
}
}
return 0;
}这是我的主函数!求高手,求建议!







65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧