C++模板编程完善程序

完善程序

#include  
#include  
using  namespace  std;
template  
class  myArrayList
{
private:
int  mSize;
int  mLen;
ElemType  *mpArr;
public:
myArrayList(int  n);
myArrayList(ElemType  *a,  int  n);
void  show();
ElemType  getMax();
void  sort();
//*************************1
myArrayList(myArrayList  &other); 

};
//*****************************2
template  
void  myArrayList::sort()
{
for  (int  i  =  0;  i  mpArr[j+1])
		{ElemType temp;
		temp=mpArr[j];
		mpArr[j]=mpArr[j+1];
		mpArr[j+1]=temp;
		}
	}
}
}
template
myArrayList::myArrayList(ElemType  *a,  int  n)

{
this->mSize  =  n;
this->mLen=n;this->mpArr  =  new  ElemType[this->mLen];
for  (int  i  =  0;  i  < n;  i++)
this->mpArr[i]  =  a[i];
}

template
myArrayList::myArrayList(myArrayList  &other)

{

this->mLen  =  other.mLen;

this->mSize  =  other.mSize;

this->mpArr  =  new  ElemType[this->mLen];

for  (int  i  =  0;  i  <  this->mLen;  i++)

this->mpArr[i]  =  other.mpArr[i];

}

template

myArrayList::myArrayList(int  n)

{

this->mSize  =  n;

this->mLen  =  0;

this->mpArr  =  new  ElemType[mSize];

}
//*************************************

template  

void  myArrayList::show()

{

for  (int  i  =  0;  i  <  mLen;  i++)

cout  <<  mpArr[i]<<"      ";//三个空格

cout  <<  endl;

}

template  
ElemType  myArrayList::getMax()
{
ElemType    max;
max  =  mpArr[0];
for  (int  i  =  1;  i  <  mLen;  i++)
if  (max  <  mpArr[i])
max  =  mpArr[i];
return  max;
}



//Student.h

class  Student
{
private:
int  mId;
float  height;
int  score;
public:
Student(int  id  =  0,  float  h  =  0,  int  s  =  0)  :height(h),  mId(id),  score(s)
{
}

//********************************3
bool operator<(Student& a2){return this->score < a2.score; }
bool operator>(Student& a2){return this->score > a2.score; }
friend ostream &operator <<(ostream &out,const Student &s)//重载输出运算符 
	{
		out << "ID:" << s.mId << "  Height:" << s.height << " Score:" << s.score << endl;
		return out;
	}
};



//主程序

int  main()
{
int  a[]  =  {  1,  2,  3,  5,  7,  9,  12,  8  };
double    b[]  =  {  1,  2.5,  3.6,  5,  7,  9,  12.8,  8  };
myArrayList    list1(a,  8);
list1.sort();
list1.show();
cout  <<  "max="  <<  list1.getMax()  <<  endl;
myArrayList    list2(b,  8);
list2.sort();
list2.show();
cout  <<  "max="  <<  list2.getMax()  <<  endl;
Student  s[3]  =  {  Student(1,  175,  80),  Student(2,  178,  90),  Student(3,  195,  83)  },  s1;
myArrayList    list3(s,  3);
list3.sort();
list3.show();
cout  <<  "max="<

你可能感兴趣的:(C++,c++,算法,数据结构)