楼主你好,你看看下面这个代码吧 和你的比较相似 :
#include
#include
#include
using namespace std;
class School{
private:
int id;//学校编号
string name;//学校名称
int total;//学校总分
int male;//男子总分
int female;//女子总分
public:
School(){//初始化
id=0;
male=0;
female=0;
total=0;
}
void setName(string s){
name=s;
}
string getName(){
return name;
}
void setId(int i){
id=i;
}
int getId(){
return id;
}
void setTotal(int a){
total=a;
}
int getTotal(){
return total;
}
void setMale(int a){
male=a;
}
void setFemale(int a){
female=a;
}
int getMale(){
return male;
}
int getFemale(){
return female;
}
};
class Item{
private:
School sc[20];
string name;
public:
School getSchool(int i){
return sc[i];
}
School* getSchools(){
return sc;
}
void setSchool(School sch,int i){
sc[i]=sch;
}
void setName(string s){
name=s;
}
string getName(){
return name;
}
};
class Count{
private:
School sc[20];
Item item[20];
int integral[5];
int n;
public:
Count(){
n=0;
integral[0]=7;
integral[1]=5;
integral[2]=3;
integral[3]=2;
integral[4]=1;
}
void input();//输入
void print(int);//打印
void statisticsForTotal();//按总分排序
void statisticsForId();//按编号排序
void statisticsForMale();//按男子总分排序
void statisticsForFemale();//按女子总分排序
void schoolInfo();//显示学校信息
void itemInfo();//显示项目信息
void menu();//显示菜单
void sort(School*,string,int);//排序操作
};
void Count::input(){
int i,j,num,m,f,id;
School *sch;
Item *it;
string s;
cout<
cin>>n;
for(i=0;isch=new School;
cout<
cin>>s;
sch->setName(s);
sch->setId(i+1);
sc[i]=*sch;
}
cout<
cin>>m;
cin>>f;
while(m>20||m<1||f>20||f<1){
cout<
cin>>m;
cin>>f;
}
for(i=0;icout<
cin>>s;
it=new Item;
it->setName(s);
item[i]=*it;
cout<
for(j=0;j<5;j++){
cin>>id;
if(isc[id-1].setMale(sc[id-1].getMale()+integral[j]);
else if(isc[id-1].setFemale(sc[id-1].getFemale()+integral[j]);
sc[id-1].setTotal(sc[id-1].getTotal()+integral[j]);
item[i].setSchool(sc[id-1],j);
}
}
}
void Count::print(int i){
cout<
void Count::sort(School* sch,string mode,int num){//排序函数
int i,j,exchange;
School t;
if(mode=="total"){
exchange=0;
for(i=0;ifor(j=num-2;j>=0;j--)
if(sch[j+1].getTotal()>sch[j].getTotal()){
exchange=1;
t=sc[j+1];
sch[j+1]=sch[j];
sch[j]=t;
}
if(!exchange)
return;
}
return;
}
if(mode=="id"){
for(i=0;ifor(j=i+1;jif(sch[i].getId()>sch[j].getId()){
t=sch[i];
sch[i]=sch[j];
sch[j]=t;
}
}
return;
}
if(mode=="male"){
exchange=0;
for(i=0;ifor(j=num-2;j>=0;j--)
if(sch[j+1].getMale()>sch[j].getMale()){
exchange=1;
t=sc[j+1];
sch[j+1]=sch[j];
sch[j]=t;
}
if(!exchange)
return;
}
return;
}
if(mode=="female"){
exchange=0;
for(i=0;ifor(j=num-2;j>=0;j--)
if(sch[j+1].getFemale()>sch[j].getFemale()){
exchange=1;
t=sc[j+1];
sch[j+1]=sch[j];
sch[j]=t;
}
if(!exchange)
return;
}
return;
}
}
void Count::statisticsForTotal(){
int i;
sort(sc,"total",n);
cout<
cout<
for(i=0;iprint(i);
}
void Count::statisticsForId(){
int i;
sort(sc,"id",n);
cout<
cout<
for(i=0;iprint(i);
}
void Count::statisticsForMale(){
int i;
sort(sc,"male",n);
cout<
cout<
for(i=0;iprint(i);
}
void Count::statisticsForFemale(){
int i;
sort(sc,"female",n);
cout<
cout<
for(i=0;iprint(i);
}
void Count::schoolInfo(){ //查询学校的情况
int i,s;
cout<
while(1){
cin>>s;
if(s<1||s>n){
cout<
continue;
}
break;
}
printf("该学校的信息信息:");
printf("编号 学校名称 总分 男子总分 女子总分");
for(i=0;iif(sc[i].getId()==s){
print(i);
break;
}
}
cout
void Count::itemInfo(){ //查询项目的情况
int i,s;
cout<
while(1){
cin>>s;
if(s<1||s>n){
cout<
continue;
}
break;
}
cout
cout<
for(i=0;i<5;i++)
cout<
<
}
void Count::menu(){ //程序菜单
int i,flag=1;
while(flag){
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cin>>i;
while(i<1||i>7){
cout<
cin>>i;
}
switch(i){
case 1:
statisticsForId();
break;
case 2:
statisticsForTotal();
break;
case 3:
statisticsForMale();
break;
case 4:
statisticsForFemale();
break;
case 5:
schoolInfo();
break;
case 6:
itemInfo();
break;
case 7:
flag=0;
break;
}
}
}
void main(){
Count c;
c.input();
c.menu();
}
希望能够帮助到你!