读书人

如何改才能让这个程序能实现按平均成绩

发布时间: 2012-03-31 13:13:26 作者: rapoo

怎么改才能让这个程序能实现按平均成绩降序排序!求解决
[size=24px][/size]
#include<iomanip>
#include<iostream>
using namespace std;
#include<string>
class student{
public:
void getinfo();
void display();
void sort(student *p);
private:
char name[9];
int english,computer,mathematics;
double average;
};
void student::getinfo ()
{
cout<<"姓名";cin>>name;
cout<<"英语成绩";cin>>english;
cout<<"计算机成绩";cin>>computer;
cout<<"数学成绩";cin>>mathematics;
average=(english+computer+mathematics)/3.0;
}
void student::display ()
{ cout<<setw(8)<<name<<":英语="<<setw(3)<<english<<",计算机="
<<setw(3)<<computer<<",数学="<<setw(3)<<mathematics
<<"平均成绩="<<setw(5)<<setprecision(4)<<average<<endl;
}
void student::sort (student *p)
{
int tmp;
double temp;
char tname[9];
if(average>p->average )
{
strcpy(tname,name);strcpy(name,p->name );strcpy(p->name ,tname);
tmp=english;english=p->english ;p->english =tmp;
tmp=computer;computer=p->computer ;p->computer=tmp;
tmp=mathematics;mathematics=p->mathematics ;p->mathematics =tmp;
temp=average;average=p->average ;p->average=temp;
}
}
const int n=3;
void main()
{
student *ST[n];
for(int i=0;i<n;i++)
{
ST[i]=new student;
cout<<"学生"<<i+1<<endl;
ST[i]->getinfo();
}
int j;
for(j=0;j<n-1;j++)
for(i<0;i<n-1-j;i++)
ST[i]->sort(ST[i+1]);
cout<<"\n排序结果\n";
for(i=0;i<n;i++)
{ST[i]->display();delete ST[i];}
}

怎么改才能让这个程序能实现按平均成绩降序排序!


[解决办法]
你没实现效果是 因为你for(i<0;i<n-1-j;i++)应写为 for(i=0;i<n-1-j;i++),在你得到学生信息时,i已经变为了3,所以根本就不会调用你的排序函数.
[解决办法]
楼主顺手结帖吧 即是对别人答案的认可 也帮助了自己

C/C++ code
#include<iomanip>#include<iostream>#include<string>using namespace std;class student{public:    void getinfo();    void display();    void sort(student *p);private:    void swap(student *p)    {        int tmp;        double temp;        char tname[9];        strcpy(tname,name);strcpy(name,p->name );strcpy(p->name ,tname);        tmp=english;english=p->english ;p->english =tmp;        tmp=computer;computer=p->computer ;p->computer=tmp;        tmp=mathematics;mathematics=p->mathematics ;p->mathematics =tmp;        temp=average;average=p->average ;p->average=temp;    }private:    char name[9];    int english,computer,mathematics;    double average;};void student::getinfo (){    cout<<"姓名";cin>>name;    cout<<"英语成绩";cin>>english;    cout<<"计算机成绩";cin>>computer;    cout<<"数学成绩";cin>>mathematics;    average=(english+computer+mathematics)/3.0;}void student::display (){    cout<<setw(8)<<name<<":英语="<<setw(3)<<english<<",计算机="    <<setw(3)<<computer<<",数学="<<setw(3)<<mathematics    <<"平均成绩="<<setw(5)<<setprecision(4)<<average<<endl;}void student::sort (student *p){    /*    int tmp;    double temp;    char tname[9];    */    //要降序改下这里就可以了    if(average < p->average )    {        /*这样的代码最好专门交给一个swap函数去处理 或者重载赋值操作符         strcpy(tname,name);strcpy(name,p->name );strcpy(p->name ,tname);        tmp=english;english=p->english ;p->english =tmp;        tmp=computer;computer=p->computer ;p->computer=tmp;        tmp=mathematics;mathematics=p->mathematics ;p->mathematics =tmp;        temp=average;average=p->average ;p->average=temp;        */        //p->swap(this);        swap(p);    }}const int n=3;int main(){      student *ST[n];    int i= 0;    //for(int i=0;i<n;i++)    for(i=0;i<n;i++)    {        ST[i]=new student;        cout<<"学生"<<i+1<<endl;        ST[i]->getinfo();    }    int j;    for(j=0;j<n-1;j++)    {        //楼主这个i<0害人不浅        for(i = 0;i<n-1-j;i++)            ST[i]->sort(ST[i+1]);    }    cout<<"\n排序结果\n";    //i 无声明  上面的i在for循环结束后就死亡了    for(i=0;i<n;i++)    {        ST[i]->display();        delete ST[i];    }    system("pause");    return 0;} 

读书人网 >C++

热点推荐