⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dzqarray.h

📁 一个非常完整的动态数组类,包含一维数组,二维数组,3维数组. 具有常规构造函数,拷贝构造函数,付值函数,不存在内存泄露.
💻 H
字号:
#include <iostream.h>

#ifndef _dzqArray_
#define _dzqArray_

//Array1D's interface.
template<class T>
class Array1D
 {
	public:
		Array1D(int size = 0);
		Array1D(const Array1D<T>& v); // 复制构造函数
		~Array1D() 
		{
			delete [] element;
			cout<<"destructor of Array1D!"<<endl;
		}
		T& operator[](int i) const;
		int Size() {return size;}

		Array1D<T>& operator=(const Array1D<T>& v);
		Array1D<T> operator+() const; // 一元加法操作符
		Array1D<T> operator+(const Array1D<T>& v) const;
		Array1D<T> operator-() const; // 一元减法操作符
		Array1D<T> operator-(const Array1D<T>& v) const;
		Array1D<T> operator*(const T& v)const;
		Array1D<T> operator*(const Array1D<T>& v)const;

		
		Array1D<T>& operator+=(const Array1D<T>& x);
		Array1D<T>& operator-=(const Array1D<T>& x);
		Array1D<T>& operator*=(const T& x);
		Array1D<T>& operator*=(const Array1D<T>& x);

		Array1D<T>& InitArray1D();
		Array1D<T>  Resize(int sz);

		friend ostream& operator<<(ostream& out,Array1D<T>& m);
		friend istream& operator>>(istream& in,Array1D<T>& m);
	private:
		int size;
		T *element; 
};






//Array2D's interface.
template<class T>
class Array2D
 {
public:
	Array2D(int r = 0, int c = 0);
	Array2D(const Array2D<T>& m); // 复制构造函数
	Array2D<T>& operator=(const Array2D<T>& m);
	~Array2D()
	{
		delete [] row;
		cout<<"destructor of Array2D!"<<endl;
	}

	Array2D<T> Resize(int r,int c);
	int Rows() const {return rows;}
	int Columns() const {return cols;}
	Array1D<T>& operator[](int i) const;

	Array2D<T> operator+() const; // 一元加法操作符
	Array2D<T> operator+(const Array2D<T>& m) const;
	Array2D<T> operator-() const; // 一元减法操作符
	Array2D<T> operator-(const Array2D<T>& m) const;
	Array2D<T> operator*(const Array2D<T>& m) const;

	
	Array2D<T>& operator+=(const T& x);
		
	friend ostream& operator<<(ostream& out,Array2D<T>& m);
	friend istream& operator>>(istream& in,Array2D<T>& m);
	
private:
	int rows, cols; // 数组维数
	Array1D<T> *row; // 一维数组的数组
};





//Array3D's interface.
template<class T>
class Array3D
 {
public:
	Array3D(int r = 0, int c = 0,int v=0);
	Array3D(const Array3D<T>& m); // 复制构造函数
	Array3D<T>& operator=(const Array3D<T>& m);
	Array2D<T>& operator[](int i) const;

	Array3D<T> Resize(int v,int r,int c);

	friend ostream& operator<<(ostream& out,Array3D<T>& m);
	friend istream& operator>>(istream& in,Array3D<T>& m);


	~Array3D() 
	{
		delete [] vol;
		cout<<"destructor of Array3D!"<<endl;
	}	
	

	
private:
	int rows, cols,vols;  
	Array2D<T> *vol; 
};

#endif






//*****************************************************************
//*********************Implementations*****************************
//*****************************************************************


void SizeMismatch()
{
	cout<<"Error! Array's size mismatch!"<<endl;
	exit(0);
}


void BadInitializers()
{
	cout<<"Error! bad subscipt!"<<endl;
	exit(0);
}

void OutOfBounds()
{
	cout<<"Error! addressing out of array's bound!";
	exit(0);
}








//Array1D.cpp
template<class T>
Array1D<T>::Array1D(int sz)
{
	if(sz<0) throw BadInitializers();
	size=sz;
	element=new T[sz];
}

template<class T>
Array1D<T>:: Array1D(const Array1D<T>& v)
{
	size=v.size;
	element=new T[size];
	for(int i=0; i<size; i++)
	{
		element[i]=v.element[i];
	}
}


template<class T>
T& Array1D<T>::operator[](int i) const
{
	if(i<0 || i>=size) throw OutOfBounds();
	return element[i];
}

template<class T>
Array1D<T>& Array1D<T>::operator =(const Array1D<T>& v)
{
	if(this!=&v) //avoid assigment byonsslef.
	{
		size=v.size;
		delete[] element;
		element=new T[size];
		for(int i=0; i<size; i++)
		{
			element[i]=v.element[i];
		}
	}
	return *this;
}



template<class T>
Array1D<T> Array1D<T>::operator-(const Array1D<T>& v) const
{
	if(size!=v.size) throw SizeMismatch();
	Array1D<T> w(size);
	for(int i=0; i<size; i++)
	{
		w.element[i]=element[i]-v.element[i];
	}
	return w;
}




template<class T>
Array1D<T> Array1D<T>::operator+(const Array1D<T>& v) const
{
	if(size!=v.size) throw SizeMismatch();
	Array1D<T> w(size);
	for(int i=0; i<size; i++)
	{
		w.element[i]=element[i]+v.element[i];
	}
	return w;
}



template<class T>
Array1D<T>& Array1D<T>::operator-=(const Array1D<T>& x)
{
	if(size!=x.size) throw SizeMismatch();
	for(int i=0; i<size; i++)
	{
		element[i]-=x.element[i];
	}
	return *this;
}

template<class T>
Array1D<T> Array1D<T>:: operator-()const
{
	Array1D<T> w(size);
	for(int i=0; i<size; i++)
	{
		w.element[i]=-element[i];
	}
	return w;
}



template<class T>
Array1D<T>& Array1D<T>::operator+=(const Array1D<T>& x)
{
	if(size!=x.size) throw SizeMismatch();
	for(int i=0; i<size; i++)
	{
		element[i]+=x.element[i];
	}
	return *this;
}



template<class T>
Array1D<T> Array1D<T>::operator*(const T& v)const
{
	Array1D<T> w(size);
	for(int i=0; i<size; i++)
	{
		w.element[i]=element[i]*v;
	}
	return w;
}


template<class T>
Array1D<T> Array1D<T>::operator*(const Array1D<T>& v)const
{	
	Array1D<T> w(size);
	for(int i=0; i<size; i++)
	{
		w.element[i]=element[i]*v.element[i];
	}
	return w;
}





template<class T>
Array1D<T>& Array1D<T>::operator*=(const T& x)
{
	for(int i=0; i<size; i++)
	{
		element[i]*=x;
	}
	return *this;
}


template<class T>
Array1D<T>& Array1D<T>::operator*=(const Array1D<T>& x)
{
	if(size!=x.size)
	{
		throw SizeMismatch();
	}
	for(int i=0; i<size; i++)
	{
		element[i]*=x.element[i];
	}
	return *this;
}



template<class T>
Array1D<T> Array1D<T>::Resize(int sz)
{
	if(sz<0) 
		throw OutOfBounds();
	delete [] element;
	size=sz;
	element=new T[size];
	return *this;
}





template<class T>
Array1D<T>&  Array1D<T>::InitArray1D()
{ 
	int tempsize;
	cout<<"You array's size=";
	
	cin>>tempsize;
	if(tempsize!=size)
	{
		cout<<"Warning! your array's size has changed!"<<endl;
	}
	Resize(tempsize);
	T el;
	cout<<"Input your datas:"<<endl;
	for(int i=0; i<size; i++)
	{

		cin>>el;
		element[i]=el;
	}
	cout<<"Array init finished!"<<endl;
	return *this;
}




template<class T>
ostream& operator<<(ostream& out,Array1D<T>& m)
{
	for(int i=0; i<m.size; i++)
	{
		out<<m.element[i]<<"  "<<endl;
	}
	return out;
}

template<class T>
istream& operator>>(istream& in,Array1D<T>& m)
{
	char temp;
	int sz;
	cout<<"Array1D's size="<<m.size<<"\n"
		<<"if you wanna change size,please enter 'Y' key:"<<endl;
	
	cin>>temp;
	if('y'==temp || 'Y'==temp)
	{
		cout<<"Please enter you array's new size:"<<endl;
		cin>>sz;
		if(sz!=m.size)
			m.Resize(sz);
	}
	if(0==m.size)
		throw BadInitializers();
	

	for(int i=0; i<m.size; i++)
	{
		cout<<"Enter your "<<i+1<<"th"<<" value"<<endl;
		in>>m.element[i];
	}
	return in;
}







//Below Array2D.cpp

template<class T>
Array2D<T>::Array2D(int r,int c)
{
	if(r<0 || c<0) 
		throw BadInitializers();
	if( (!r || !c) && (r||c) )
		throw BadInitializers();
	rows=r;
	cols=c;
	row=new Array1D<T> [r];
	for(int i=0; i<r; i++)
	{
		row[i].Resize(c);
	}
}


template<class T>
Array2D<T>::Array2D(const Array2D<T>& m)
{
	rows=m.rows;
	cols=m.cols;
	row=new Array1D<T>[rows];
	for(int i=0; i<rows; i++)
	{
		row[i]=m.row[i];
	}
}


template<class T>
Array2D<T>& Array2D<T>:: operator=(const Array2D<T>& m)
{
	if(this!=&m)
	{
		rows=m.rows;
		cols=m.cols;
		delete []row;
		row=new Array1D<T> [rows];
		for(int i=0; i<cols; i++)
		{
			row[i]=m.row[i];
		}
	}
	return *this;
}





template<class T>
Array1D<T>& Array2D<T>::operator[](int i) const
{
	if(i<0 || i>=rows)
	{
		throw OutOfBounds();
	}
	return row[i];
}


template<class T>
Array2D<T> Array2D<T>::operator-(const Array2D<T>& m)const
{
	if( (rows!=m.rows) || (cols!=m.cols) )
	{
		throw SizeMissMatch();
	}
	Array2D<T> w(rows,cols);
	for(int i=0; i<rols; i++)
	{
		w.row[i]=row[i]-m.row[i];
	}
	return w;
}







template<class T>
Array2D<T> Array2D<T>::operator*(const Array2D<T>& m) const
{
	if(cols!=m.rows)
	{
		throw SizeMissMatch();
	}
	Array2D w(rows,m.cols);
	for(i=0; i<rows; i++)
	{
		for(j=0; j<m.cols; j++)
		{
			T sum=(*this)[i][0]*m[0][j];  //smart initialization of sum.
			for(int k=1; k<cols; k++)     //k begin at 1.
			{
				sum+=(*this)[i][k]*m[k][j];
			}
			w[i][j]=sum;
		}
	}
	return w;
}


template<class T>
Array2D<T> Array2D<T>::Resize(int r,int c)
{
	if(r<0 || c<0) 
		throw OutOfBounds();
	delete [] row;
	rows=r;
	cols=c;
	row=new Array1D<T>[rows];
	for(int i=0; i<rows; i++)
	{
		row[i].Resize(cols);
	}
	return *this;
}


template<class T>
ostream& operator<<(ostream& out,Array2D<T>& m)
{
	if(0==m.rows || 0==m.cols)
		throw OutOfBounds();
	for(int i=0; i<m.rows; i++)
	{
		out<<m.row[i];
	}
	return out;
}


template<class T>
istream& operator>>(istream& in,Array2D<T>& m)
{
	char temp;
	int rows;
	int cols;
	cout<<"Array2D's rows= "<<m.rows<<",cols="<<m.cols<<"\n"
		<<"if you wanna change size,please enter 'Y' key:"<<endl;
	
	cin>>temp;
	if('y'==temp || 'Y'==temp)
	{
		cout<<"Please enter you array's new rows and cols:"<<endl;
		cin>>rows>>cols;
		if( (rows!=m.rows) || (cols!=m.cols) )
			m.Resize(rows,cols);
	}

	if(0==m.rows || 0==m.cols)
		throw BadInitializers();
	

	for(int i=0; i<m.rows; i++)
	{
		cout<<"Enter your "<<i+1<<"th rows's"<<" value"<<endl;
		in>>m.row[i];
	}
	return in;
}







//The implementation of Array3D.
template<class T>
Array3D<T>::Array3D(int r, int c,int v)
{
	if(r<0 || c<0  || v<0)
		throw BadInitializers();
	rows=r;
	cols=c;
	vols=v;
	vol=new Array2D<T> [v];
	for(int i=0; i<v; i++)
	{
		vol[i].Resize(r,c);
	}
}


template<class T>
Array3D<T>::Array3D(const Array3D<T>& m)
{
	rows=m.rows;
	cols=m.cols;
	vols=m.vols;
	vol=new Array2D<T> [vols];
	for(int i=0; i<vols; i++)
	{
		vol[i]=m.vol[i];
	}
}



template<class T>
Array3D<T>& Array3D<T>::operator=(const Array3D<T>& m)
{
	if(this!=&m)
	{
		delete [] vol;
		vol=new Array2D<T> [m.vols];
		vols=m.vols;
		rows=m.rows;
		cols=m.cols;

		for(int i=0; i<vols; i++)
		{
			vol[i]=m.vol[i];
		}
	}
	return *this;
}



template<class T>
Array2D<T>& Array3D<T>::operator[](int i) const
{
	if(i<0 || i>=vols)
	{
		throw OutOfBounds();
	}
	return vol[i];
}



template<class T>
ostream& operator<<(ostream& out,Array3D<T>& m)
{
	if(0==m.vols || 0==m.rows || 0==m.cols )
		throw OutOfBounds();
	for(int i=0; i<m.vols; i++)
	{
		out<<m.vol[i];
	}
	return out;
}


template<class T>
istream& operator>>(istream& in,Array3D<T>& m)
{
	char temp;
	int rows;
	int cols;
	int vols;
	cout<<"Array3D's vols="<<m.vols<<",rows="<<m.rows<<",cols="<<m.cols<<"\n"
		<<"if you wanna change size,please enter 'Y' key:"<<endl;
	
	cin>>temp;
	if('y'==temp || 'Y'==temp)
	{
		cout<<"Please enter you array's new vols ,rows and cols:"<<endl;
		cin>>vols>>rows>>cols;

		if(vols<=0 || rows<=0 || cols<=0)
			throw BadInitializers();
		if( (vols!=m.vols) || (rows!=m.rows) || (cols!=m.cols) )
			m.Resize(vols,rows,cols);
	}

	
	

	for(int i=0; i<m.vols; i++)
	{
		cout<<"Enter your "<<i+1<<"th vol's"<<" value"<<endl;
		in>>m.vol[i];
	}
	return in;
}




template<class T>
Array3D<T> Array3D<T>::Resize(int v,int r,int c)
{
	if(vols<0 || rows<0 || cols<0) 
		throw OutOfBounds();
	vols=v;
	rows=r;
	cols=c;
	delete [] vol;
	vol=new Array2D<T> [vols];
	for(int i=0; i<cols; i++)
	{
		vol[i].Resize(rows,cols);
	}
	return *this;
}










⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -