哪位高手帮我写个C++程序?

2025-02-22 23:53:51
推荐回答(1个)
回答1:

今天兴致好,帮帮你O(∩_∩)O~
#include
using namespace std;
template
class MyArray
{
public:
MyArray(int s=0):size(s)
{
if(size>0)
P_head=new Type[s];
else P_head=NULL;
}
MyArray(MyArray &arr)
{
if(size)
delete [] P_head;
this->size=arr.GetSize();
P_head=new Type[size];
for(int i=0;i P_head[i]=arr[i];
}
MyArray& operator=(MyArray &arr)
{
if(size)
delete [] P_head;
this->size=arr.GetSize();
P_head=new Type[size];
for(int i=0;i P_head[i]=arr[i];
return *this;
}
~MyArray()
{
delete [] P_head;
}
int GetSize(){return size;}
Type& operator[](int i)
{
if(i return P_head[i];
else return *(new Type);
}
void ptr()
{
for(int i=0;i cout< cout< }
void SetAt(int nIndex , int newElement)
{
if((nIndex>=size)||(nIndex<0))
return;
P_head[nIndex]=newElement;
}
void MySort()
{
if(size>=2)
for(int i=0;i for(int j=i;j if(P_head[i]>P_head[j])
{
P_head[i]=P_head[i]+P_head[j];
P_head[j]=P_head[i]-P_head[j];
P_head[i]=P_head[i]-P_head[j];
}
}
private:
Type* P_head;
int size;
};
int main()
{
MyArray a(5),b(3);
a.SetAt(0,2);//a[0]=2;
a.SetAt(1,1);//a[1]=1;
a.SetAt(2,5);//a[2]=5;
a.SetAt(3,3);//a[3]=3;
a.SetAt(4,8);//a[4]=8;
a.ptr();
for(int i=0;i for(int j=i;j if(a[i]>a[j])
{
a[i]=a[i]+a[j];
a[j]=a[i]-a[j];
a[i]=a[i]-a[j];
}
a.ptr();
b[0]=3;
b[1]=2;
b[2]=5;
b.ptr();
b.MySort();
b.ptr();
b=a;
b.ptr();
return 0;
}
看不懂的地方再问我吧~或者问问其他人