| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 314 人关注过本帖
标题:【求助】把对象数组当作普通数组进行排序失败
只看楼主 加入收藏
lyj123
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:60
专家分:152
注 册:2010-11-15
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:2 
【求助】把对象数组当作普通数组进行排序失败
程序代码:
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<string>
using namespace std;
const string NAME[12]={"Sam","John","Peter","Lucy","Pierre","Antonio","Lillian","Fred","Mark","Hemingway","Carmen","Dan Dervish"};
double random(){
    return double(rand())/RAND_MAX;//生成0-1之间的随机数
}//rand()%n
int random(int x){
    return int(random()*(x-1)+0.5);
}
class Student{
private:
/*
    学生资料,应包含
    语文、数学、英语、科学、体育成绩、姓名、
    历史成绩、是否曾评过三好生
*/
    int ChineseScore,MathScore,EnglishScore,ScienceScore,PE;
    int AllScore;
    int HistoryScore;
    string name;
    bool is_Great;
public:
    Student(){}
    ~Student(){cout<<"deleteing ..."<<name<<endl;}
    //Student(const string& N,const int PE,const int& C,const int& M,const int& E,const int& S,const int &H,const bool Great):ChineseScore(C),MathScore(M),EnglishScore(E),ScienceScore(S),HistoryScore(H),is_Great(Great),name(N),PE(PE){AllScore=C+M+E+S+PE;}
    void setup(const string& N,const int PE,const int& C,const int& M,const int& E,const int& S,const int &H,const bool Great);
    const int Get_C(){return ChineseScore;}
    const int Get_M(){return MathScore;}
    const int Get_E(){return EnglishScore;}
    const int Get_S(){return ScienceScore;}
    const int Get_H(){return HistoryScore;}
    const int Get_A(){return AllScore;}
    const int Get_P(){return PE;}
    string& Get_Name(){return name;}
    bool Get_Great()const{return is_Great;}
    void display();
};
void Student::display(){
    cout<<"Name: "<<name<<" TotalPoints:"<<AllScore<<" Chinese:"<<ChineseScore<<" Math:"<<MathScore<<" English:"<<EnglishScore<<" Science:"<<ScienceScore<<endl
        <<" PE:"<<PE<<" History:"<<HistoryScore<<" Is_Great:"<<is_Great<<endl;
    cout<<endl;
}
//调用顺序:姓名、体育、语文、数学、英语、科学、历史、三好生
void Student::setup(const string& N,const int PE,const int& C,const int& M,const int& E,const int& S,const int &H,const bool Great){
    ChineseScore=C;
    MathScore=M;
    EnglishScore=E;
    ScienceScore=S;
    HistoryScore=H;
    is_Great=Great;
    name=N;
    this->PE=PE;
    AllScore=C+M+E+S+PE;
}
class Manager{
    Student* stu;
    int Num;
    int pass;
    int size;
public:
    Manager(int sz):Num(0),size(sz){buildup(sz);pass=Num/sz;}
    ~Manager();
    int qsort(int,int);
    void sort(int,int);
    void exchange(int,int);
    void buildup(int sz);
    void work();
};
Manager::~Manager(){
    delete[] stu;
}
//------------------------------注意下面这个函数----------------------------------------
void Manager::work(){
    sort(0,size-1);
    for(int i=0;i<size;i++){
        stu[i].display();
    }
}
//---------------------------以上的排序过程中调用了析构函数-----------------------------
void Manager::exchange(int x,int y){
    Student t=stu[x];stu[x]=stu[y];stu[y]=t;    //我已经知道了,是这里的t在函数结束时调用析构函数,但是排序还是失败,不知道为什么
}
int Manager::qsort(int p,int r){
    int i=p-1,j;
    for(j=p;j<r;j++){
        if(stu[j].Get_A()>stu[r].Get_A()){
            exchange(++i,j);
        }
    }
    exchange(i+1,r);
    return i+1;
}
void Manager::sort(int p,int r){

 if(p<r){
    int q=qsort(p,r);
    sort(p,q);
    sort(q+1,r);}
}
void Manager::buildup(int sz){
    stu=new Student[sz];
    int ChineseScore,MathScore,EnglishScore,ScienceScore,PE;
    int HistoryScore;
    string name;
    bool is_Great;
//调用顺序:姓名、体育、语文、数学、英语、科学、历史、三好生
    int x=70,y=40;
    for(int i=0;i<sz;i++)
        {
        if(i%2==0){x=90;y=30;}else{x=70;y=40;}
        ChineseScore=random(y)+x;
        MathScore=random(y)+x;
        EnglishScore=random(y)+x;
        ScienceScore=random(90)+x;
        PE=random(20)+10;
        HistoryScore=random(80)+20;
        name=NAME[random(10)];
        if(random(10)>7)is_Great=true;
        else is_Great=false;
        stu[i].setup(name,PE,ChineseScore,MathScore,EnglishScore,ScienceScore,HistoryScore,is_Great);
        stu[i].display();
        Num+=stu[i].Get_A();
        }
        //测试方便,随机建立学生档案
}
int main(){
    srand(time(NULL));
    Manager manage(10);
    //manage.work();
   
return 0;
}


[ 本帖最后由 lyj123 于 2012-12-9 12:49 编辑 ]
2012-12-08 20:01
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:20 
楼主厉害

有心者,千方百计;无心者,千难万难。
2012-12-09 01:07
lyj123
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:60
专家分:152
注 册:2010-11-15
收藏
得分:0 
这个程序并没有什么高级的技术含量的
现在为了保护资源,使所有事物流对象,但排序过程中失败,请高手来看看,
修改后的代码如下
程序代码:
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<string>
#include<cstddef>
using namespace std;
const string NAME[12]={"Sam","John","Peter","Lucy","Pierre","Antonio","Lillian","Fred","Mark","Hemingway","Carmen","Dan Dervish"};
double random(){
    return double(rand())/RAND_MAX;//生成0-1之间的随机数
}//rand()%n
int random(int x){
    return int(random()*(x-1)+0.5);
}
template<typename T,const size_t sz=1>
class Pwrap{
public:
    T p[sz];
    Pwrap(){
        //p=new T[sz];
        cout<<"allocting Pwrap!"<<endl;
    }
    ~Pwrap(){
        //delete[]p;
        cout<<"deleteing Pwrap!"<<endl;
    }
};
class Student{
private:
/*
    学生资料,应包含
    语文、数学、英语、科学、体育成绩、姓名、
    历史成绩、是否曾评过三好生
*/
    int ChineseScore,MathScore,EnglishScore,ScienceScore,PE;
    int AllScore;
    int HistoryScore;
    string name;
    bool is_Great;
public:
    Student(){}
    ~Student(){/*cout<<"deleteing ..."<<name<<endl;*/}
    //Student(const string& N,const int PE,const int& C,const int& M,const int& E,const int& S,const int &H,const bool Great):ChineseScore(C),MathScore(M),EnglishScore(E),ScienceScore(S),HistoryScore(H),is_Great(Great),name(N),PE(PE){AllScore=C+M+E+S+PE;}
    void setup(const string& N,const int PE,const int& C,const int& M,const int& E,const int& S,const int &H,const bool Great);
    const int Get_C(){return ChineseScore;}
    const int Get_M(){return MathScore;}
    const int Get_E(){return EnglishScore;}
    const int Get_S(){return ScienceScore;}
    const int Get_H(){return HistoryScore;}
    const int Get_A(){return AllScore;}
    const int Get_P(){return PE;}
    string& Get_Name(){return name;}
    bool Get_Great()const{return is_Great;}
    void display();
};
void Student::display(){
    cout<<"Name: "<<name<<" TotalPoints:"<<AllScore<<" Chinese:"<<ChineseScore<<" Math:"<<MathScore<<" English:"<<EnglishScore<<" Science:"<<ScienceScore<<endl
        <<" PE:"<<PE<<" History:"<<HistoryScore<<" Is_Great:"<<is_Great<<endl;
    cout<<endl;
}
//调用顺序:姓名、体育、语文、数学、英语、科学、历史、三好生
void Student::setup(const string& N,const int PE,const int& C,const int& M,const int& E,const int& S,const int &H,const bool Great){
    ChineseScore=C;
    MathScore=M;
    EnglishScore=E;
    ScienceScore=S;
    HistoryScore=H;
    is_Great=Great;
    name=N;
    this->PE=PE;
    AllScore=C+M+E+S+PE;
}
class Manager{
    //Student* stu;
    Pwrap<Student,10> stu;
    int Num;
    int pass;
    int size;
public:
    Manager(int sz):Num(0),size(sz){
    buildup(sz);pass=Num/sz;
    //throw 47;
    }
    ~Manager();
    int qsort(int,int);
    void sort(int,int);
    void exchange(int,int);
    void buildup(int sz);
    void work();
};
Manager::~Manager(){
    cout<<"~Manager()"<<endl;
}
void Manager::work(){
    sort(0,size);
    cout<<"---------------------------"<<endl;
    for(int i=0;i<size;i++){
        stu.p[i].display();
    }
}
void Manager::exchange(int x,int y){
    //cout<<"exchange!"<<endl;
    Student t=stu.p[x];stu.p[x]=stu.p[y];stu.p[y]=t;
    //cout<<"change end!"<<endl;   
}
int Manager::qsort(int p,int r){
    int i=p-1,j;
    for(j=p;j<r;j++){
        if(stu.p[j].Get_A()>stu.p[r].Get_A()){
            //system("PAUSE");
            exchange(++i,j);
        }
    }
    exchange(i+1,r);
    for(int i=p;i<r;i++)cout<<stu.p[i].Get_A()<<" ";cout<<endl;
    return i+1;
}
void Manager::sort(int p,int r){

 if(p<r){
    int q=qsort(p,r);
    sort(p,q);
    sort(q+1,r);}
}
void Manager::buildup(int sz){
    //stu=new Student[sz];
    int ChineseScore,MathScore,EnglishScore,ScienceScore,PE;
    int HistoryScore;
    string name;
    bool is_Great;
//调用顺序:姓名、体育、语文、数学、英语、科学、历史、三好生
    int x=70,y=40;
    for(int i=0;i<sz;i++)
        {
        if(i%2==0){x=90;y=30;}else{x=70;y=40;}
        ChineseScore=random(y)+x;
        MathScore=random(y)+x;
        EnglishScore=random(y)+x;
        ScienceScore=random(90)+x;
        PE=random(20)+10;
        HistoryScore=random(80)+20;
        name=NAME[random(10)];
        if(random(10)>7)is_Great=true;
        else is_Great=false;
        stu.p[i].setup(name,PE,ChineseScore,MathScore,EnglishScore,ScienceScore,HistoryScore,is_Great);
        stu.p[i].display();
        Num+=stu.p[i].Get_A();
        }
        //测试方便,随机建立学生档案
}
int main(){
    srand(time(NULL));
    try{
    Manager manage(10);
   
    cout<<"!!!!!!!!!!!!!!!!!!!"<<endl;
    manage.work();
    cout<<"new now!"<<endl;
    Student p[2];
    Student t1=p[1];p[1]=p[2];p[2]=t1;
    cout<<"the end!"<<endl;
    }catch(...){
        cout<<"error has been caught!"<<endl;
    }
   
   
return 0;
}

相互帮助

2012-12-09 07:07
快速回复:【求助】把对象数组当作普通数组进行排序失败
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038552 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved