【求助】把对象数组当作普通数组进行排序失败
程序代码:
#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 编辑 ]