首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-04-21 
怎么改才能让这个程序能实现按平均成绩降序排序怎么改才能让这个程序能实现按平均成绩降序排序#includeio

怎么改才能让这个程序能实现按平均成绩降序排序
怎么改才能让这个程序能实现按平均成绩降序排序
#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];}
}[/size]

[解决办法]

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/C++ code
class Student{public:    void getinfo();    void display();       private:    string name;    int english,computer,mathematics;    double average;    friend void SwapStudent(Student & obj1, Student &obj2); // 交换函数    friend void BubbleSort(Student stu[], int count); //冒泡排序};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 BubbleSort(Student stu[], int n) //冒泡排序 {     //每一趟交换,把最大的哪项交换到数组的最后面     bool flag=false;            //交换标志,为true 的时候,表示,发生交换;一趟下来,如果flag值为false,表示所有都排序好.     int  i,j;     for( i=0; i< n-1;  i++)    {        flag=false;        for(j=0; j< n-1- i; j++)  //扫描每一趟        {            //进行比较            if( stu[j].average<stu[j].average )            {                SwapStudent(stu[j],stu[j+1]);            //发生交换                flag=true;            }                }        if( flag ==false)            break;    } }
[解决办法]
我觉得LZ有不好写代码的习惯,我一贴到我的IDE上,看到挺乱的,建议养成好的代码习惯

热点排行