C++源码:动态数组实例(1维)
\\ C++源码:动态数组实例(1维)useage: Example for dyn ARRAY Class (1D)
*******************/
// ARRAY
#include <iostream>
using namespace std;
const int defau_m=9,defau_n=9;
class IntArray
{
protected: //
int size,m,n; //Array size,
int *pia;
public:
IntArray (int mm=defau_m,int nn=defau_n);//Constructor1 构造函数1
IntArray (const IntArray& ); //Constructor2 构造函数(COPY CONSTRUCTOR)
~IntArray (); //析构函数
IntArray & operator= (const IntArray &); //=use &: 实现左值
IntArray operator+ (const IntArray &);
IntArray operator- (const IntArray &);
IntArray operator* (const IntArray &);
// int& operator[] (int,int);
int getsize () {return size;}
void assign (int value,int index1,int index2);
void chang_s(int mm,int nn);
void show1 (int index1,int index2);
void show ();
};
IntArray::IntArray(int mm,int nn) // constuctor 1
{
m=mm; n=nn;
size=m*n;
pia=new int[size];
for (int i=0;i<size;i++)
pia[i]=0;
}
IntArray::IntArray(const IntArray &PA) // constuctor 2 (COPY constructor)
{
m=PA.m; n=PA.n;
size=m*n;
// delete pia;
pia=new (int[size]);
for (int i=0;i<(size);i++)
pia[i]=PA.pia[i];
}
IntArray& IntArray::operator=(const IntArray &PA)
{
delete pia;
m=PA.m; n=PA.n;
size=PA.size;
delete pia;
pia=new (int [size]);
for (int i=0;i<size;i++)
pia[i]=PA.pia[i];
return *this;
}
//
IntArray::~IntArray()
{
delete pia;
}
// Overload "+"
IntArray IntArray::operator+(const IntArray &PA)
{
IntArray tempA;
tempA.m=m; tempA.n=n; tempA.size=size; // initalize
if (size!=PA.size)
{
cout<<"Can not add !\n";
return 1;
}
for (int i=0;i<(size);i++)
tempA.pia[i]=pia[i]+PA.pia[i];
return tempA;
}
// Overload "-"
IntArray IntArray::operator-(const IntArray &PA)
{
IntArray tempA;
tempA.m=PA.m; tempA.n=PA.n; // initalize !!
tempA.size=PA.size; // initalize !!
if (size!=PA.size)
{
cout<<"Can not sub !\n";
return 1;
}
for (int i=0;i<(size);i++)
tempA.pia[i]=pia[i]-PA.pia[i];
return tempA;
}
// Overload "*"
IntArray IntArray::operator*(const IntArray &PA)
{
IntArray tempA;
int tempI=0;
if (n!=PA.m)
{
cout<<"Can not plus !\n";
return 1;
}
for (int i=0;i<(m-1);i++)
for (int j=0;j<(PA.n-1);++j)
{
tempI=0;
for (int k=0;k<(n-1);++k)
tempI=tempI+pia[i*n+k]*PA.pia[k*n+j];
tempA.pia[i*n+j]=tempI;
}
return tempA;
}
/*
// Overload "[]" and left value
int& IntArray::operator[] (int index1, int index2)
{
int index;
index=index1*n+index2;
if ((index1<0).or.(index2<0).or.(index1>m).or.(index2>n))
{
cout<<"ARRAY index over !\n";
return;
}
else
return pia[index];
}
*/
// Assign ARRAY element a value
void IntArray::assign(int value,int index1,int index2)
{
int index;
index=(index1-1)*n+(index2-1);
pia[index]=value;
}
void IntArray::chang_s(int mm,int nn)
{
m=mm; n=nn;
size=m*n;
delete pia;
pia=new (int[size]);
for (int l=0;l<size;++l)
pia[l]=0; // initalize
}
void IntArray::show1(int index1,int index2)
{
cout<<pia[((index1-1)*n)+(index2-1)]<<" ";
}
void IntArray::show()
{
int counter=0;
// cout<<"\nNow in IntAray::show()\n";
cout<<"size="<<size<<"\n";
for (int i=0;i<size;i++)
{
// cout<<"\nNow in loop for OF IntArray::show()\n";
counter=counter+1;
cout<<pia[i]<<" ";
if (counter==n) { cout<<"\n";counter=0;} //MATIX line RETURN
}
cout<<"\n\n"; // END MATIX SHOW
}
main()
{
int m1=1,n1=1,value1,ii,jj;
IntArray A1,A2,A3,A4,A5;
while (m1!=0)
{
// Beginning of (Input a ARRAY, and display it:)
cout<<"\007"<<"Please input the ARRAY1 size:"<<flush;
cin>>m1>>n1;
// PROCESS USER WANT TO EXIT (1)
if ((m1==0)||(n1==0))
{
cout<<" \n\n You want to EXIT !\n BeCAUSE YOU INPUT 0 0";
return 1;
}
A1.chang_s(m1,n1); //Creat a ARRAY, and define it's size = m1*n1
m1=m1+1; n1=n1+1;
for (int i=1;i<m1;i++)
{
for (int j=1;j<n1;j++)
{
cout<<" ARRAY["<<i<<","<<j<<"]="<<flush;
cin>>value1;
ii=i;jj=j;
A1.assign(value1,ii,jj);
}
cout<<"\n";
}
cout<<"\n Call A1.show()\n";
A1.show();
// Beginning of (Input the other ARRAY, and display it:)
cout<<"\007"<<"Please input the ARRAY2 size:"<<flush;
cin>>m1>>n1;
if ((m1==0)||(n1==0))
{
cout<<" \n\n You want to EXIT !\n BeCAUSE YOU INPUT 0 0";
return 1;
}
A2.chang_s(m1,n1); //Creat a ARRAY, and define it's size
m1=m1+1; n1=n1+1;
for (int i=1;i<m1;++i)
{
for (int j=1;j<n1;++j)
{
cout<<" ARRAY["<<i<<","<<j<<"]="<<flush;
cin>>value1;
ii=i; jj=j;
A2.assign(value1,ii,jj);
}
cout<<"\n";
}
A2.show();
//END OF (Input a ARRAY, and display it:)
A3=A1+A2;
A4=A2-A1;
A5=A1*A2;
A3.show();
A4.show();
// A5.show();
} //END OF WHILE
return 0;
} // end of main()