📄 array.h
字号:
/*********************************************************
array.h
二维数组一维动态化+矩阵
x[m][n] to x[long] ,最大方阵为5792x5792,一维为33547264
2004.11.14 14.28
*********************************************************/
#ifndef NULL
#define NULL 0
#endif
#ifndef ARRAY_H
#define ARRAY_H
#define TYPE_V long
template <class T>
class Array
{
public :
//构造函数
Array()
{
IsEmpty=1;
type=false;
singlesize=0;
}
Array(Array <T> &matrix);
Array(TYPE_V m,TYPE_V n); //二维数组定义(构造函数)
Array<T>& operator =(Array<T> &matrix); //=号重载
//基本操作
void Initalize(TYPE_V m,TYPE_V n); //array()后使用,二维数组构造 Initalize(2,2)=构造一个2x2的二维数组
void InsertDataEX(TYPE_V m,TYPE_V n,T data); //二维化赋值 如InsertDataEX(0,2,10)=array[0][2]=10
void InsertDataNormal(TYPE_V m,T data); //一维化赋值 如InsertDataNormal(2,10)=array[2]=10
T& GetDataEX(TYPE_V m,TYPE_V n); //得到数据(二维方式)
T& GetDataNormal(TYPE_V m); //得到数据(一维方式)
T* GetArray(){return array;}
int IsArrayEmpty(){return IsEmpty;}
long GetVerticalDim(){ return VLine;}
long GetHorizonDim(){return HLine;}
long GetSingleSize(){return singlesize;}
bool GetType(){return type;}
//矩阵运算
//得到二维数组指定列中行范围的最大值
//vpos指定列,hstartpos指定行开始,hendpos指定行结束
//&pos返回为最大值的行数
//列主消元时取主用
T GetMaxVertical(TYPE_V vpos,TYPE_V hstartpos,TYPE_V hendpos,TYPE_V &pos);
T HorMulVer(TYPE_V m,Array<T> &sMatrix,TYPE_V n); //m行乘sMartix的n列
void HSwap(TYPE_V m,TYPE_V n);//行交换
void HAdd(TYPE_V m,TYPE_V n,double dataflag);//行相加 n行加m行*dataflag放入n行
bool GaussMethod(); //高斯消元函数,对增广矩阵使用
//计算高斯消元后的x值,result为列为1的矩阵
//放x值用,可以使用Array<int(double...)> 数组名(行数(x的数量),1)来定义
bool GetGaussResult(Array<T> &result);
void MatrixMulData(double data); //矩阵乘以常数
bool MatrixMulMatrix(Array<T> &sMatrix); //矩阵相乘,返回为相乘矩阵
bool MatrixAddMatrix(Array<T> &sMatrix,bool flag); //矩阵相加,返回为相加矩阵,lflag为true时加否则减
bool MatrixPow(Array <T> &sMatrix,TYPE_V time); //矩阵幂运算
protected:
T *array;
long VLine,HLine;
int IsEmpty;
long singlesize;
bool type;//二维或一维,false 1,true 2
};
template <class T>
Array <T>::Array(TYPE_V m,TYPE_V n)
{
if (m>=0 && n>=0)
{
array=new T[m*n];
VLine=n;
HLine=m;
IsEmpty=1;
type=true;
}
}
template <class T>
Array <T>::Array(Array<T> &matrix)
{
TYPE_V i,j;
IsEmpty=matrix.IsArrayEmpty();
type=matrix.GetType();
if (type)
{
HLine=matrix.GetHorizonDim();
VLine=matrix.GetVerticalDim();
array=new T[HLine*VLine];
if (!IsEmpty)
{
for (i=0;i<HLine;i++)
for(j=0;j<VLine;j++)
GetDataEX(i,j)=matrix.GetDataEX(i,j);
}
}
else
{
singlesize=matrix.GetSingleSize();
array=new T[singlesize];
if (!IsEmpty)
for (i=0;i<singlesize;i++)
GetDataNormal(i)=matrix.GetDataNormal(i);
}
}
template <class T>
Array<T>& Array<T>::operator =(Array<T> &matrix) //=号重载
{
TYPE_V i,j;
type=matrix.GetType();
IsEmpty=matrix.IsArrayEmpty();
if(type)
{
HLine=matrix.GetHorizonDim();
VLine=matrix.GetVerticalDim();
array=new T[HLine*VLine];
if (!IsEmpty)
{
for (i=0;i<HLine;i++)
for(j=0;j<VLine;j++)
GetDataEX(i,j)=matrix.GetDataEX(i,j);
}
}
else
{
singlesize=matrix.GetSingleSize();
array=new T[singlesize];
if (!IsEmpty)
for (i=0;i<singlesize;i++)
GetDataNormal(i)=matrix.GetDataNormal(i);
}
return *this;
}
template <class T>
T& Array <T>::GetDataEX(TYPE_V m,TYPE_V n)
{
return array[m*VLine+n];
}
template <class T>
T& Array <T>::GetDataNormal(TYPE_V m)
{
return array[m];
}
template <class T>
void Array <T>::InsertDataEX(TYPE_V m,TYPE_V n,T data)
{
if (m>=0 && n>=0 && array!=NULL && type)
{
array[VLine*m+n]=data;
IsEmpty=0;
type=true;
}
}
template <class T>
void Array <T>::InsertDataNormal(TYPE_V m,T data)
{
if (m>=0 && array!=NULL)
{
array[m]=data;
IsEmpty=0;
singlesize++;
}
}
template <class T>
void Array<T>::Initalize(TYPE_V m,TYPE_V n)
{
if (m>=0 && n>=0)
{
array=new T[m*n];
VLine=n;
HLine=m;
IsEmpty=1;
type=true;
}
}
template <class T>
T Array<T>::GetMaxVertical(TYPE_V vpos,TYPE_V hstartpos,TYPE_V hendpos,TYPE_V &pos) //得到二维数组列中指定地方的最大值,hpos指定列,vstartpos指定行开始,,vstartpos指定行结束,i返回为最大值的行数
{
TYPE_V n;//行数
T temp;
temp=GetDataEX(hstartpos,vpos);
pos=hstartpos;
for (n=hstartpos;n<=hendpos;n++)
{
if(GetDataEX(n,vpos)>=temp)
{
temp=GetDataEX(n,vpos);
pos=n;
}
}
return temp;
}
template <class T>
void Array<T>::HSwap(TYPE_V m,TYPE_V n)//行交换
{
TYPE_V i;
T temp;
for(i=0;i<=VLine-1;i++)
{
temp=GetDataEX(m,i);
GetDataEX(m,i)=GetDataEX(n,i);
GetDataEX(n,i)=temp;
}
}
template <class T>
void Array<T>::HAdd(TYPE_V m,TYPE_V n,double dataflag)//行相加 n行加m行*dataflag放入n行
{
TYPE_V i;
for(i=0;i<=VLine-1;i++)
GetDataEX(n,i)=GetDataEX(m,i)*dataflag+GetDataEX(n,i);
}
template <class T>
bool Array<T>::GaussMethod() //高斯消元函数
{
bool returnvalue(false);
if(type)
{
//vvalue 列数,hvalue行数 0.....n-1
TYPE_V i,j,pos;
T temp,mainelement;
for (i=0;i<VLine;i++)
{
//选取主元
mainelement=GetMaxVertical(i,i,HLine-1,pos);
//行交换
HSwap(i,pos);
//减法运算
for (j=i+1;j<HLine;j++)
{
if (mainelement==0) return false;
temp=(-1.0)*GetDataEX(j,i)/mainelement;
HAdd(i,j,temp);//行相加 j行加i行*比例系数放入j行
}
}
returnvalue=true;
}
return returnvalue;
}
template <class T>
bool Array<T>::GetGaussResult(Array<T> &result)
{
bool returnvalue(false);
if(type)
{
//vvalue 列数,hvalue行数 0.....n-1
TYPE_V i,j;
T temp;
temp=GetDataEX(HLine-1,VLine-1)/GetDataEX(HLine-1,VLine-2);
result.InsertDataNormal(HLine-1,temp);
for(i=HLine-2;i>=0;i--)
{
temp=0;
for (j=i+1;j<=VLine-2;j++)
temp=temp+GetDataEX(i,j)*result.GetDataNormal(j);
temp=(GetDataEX(i,VLine-1)-temp)/GetDataEX(i,i);
if (i>=0) result.InsertDataNormal(i,temp);
}
returnvalue=true;
}
return returnvalue;
}
template <class T>
void Array<T>::MatrixMulData(double data) //矩阵乘以常数
{
TYPE_V i,j;
if (!IsEmpty && type)
for (i=0;i<=HLine-1;i++)
for (j=0;j<=VLine-1;j++)
GetDataEX(i,j)=GetDataEX(i,j)*data;
}
template <class T>
T Array<T>::HorMulVer(TYPE_V m,Array<T> &sMatrix,TYPE_V n) //m行乘sMartix的n列
{
T sum(0);
TYPE_V i;
for(i=0;i<VLine;i++)
sum=sum+GetDataEX(m,i)*sMatrix.GetDataEX(i,n);
return sum;
}
template <class T>
bool Array<T>::MatrixMulMatrix(Array<T> &sMatrix) //矩阵相乘
{
bool returnvalue(false);
if (!IsEmpty && !sMatrix.IsArrayEmpty() && type && sMatrix.GetType())
if (VLine==sMatrix.GetHorizonDim())
{
TYPE_V i,j;
Array<T> temp(HLine,sMatrix.GetVerticalDim());
for (i=0;i<HLine;i++)
for (j=0;j<sMatrix.GetVerticalDim();j++)
temp.GetDataEX(i,j)=HorMulVer(i,sMatrix,j);
VLine=sMatrix.GetVerticalDim();
for (i=0;i<HLine;i++)
for (j=0;j<sMatrix.GetVerticalDim();j++)
GetDataEX(i,j)=temp.GetDataEX(i,j);
returnvalue=true;
}
return returnvalue;
}
template <class T>
bool Array<T>::MatrixAddMatrix(Array<T> &sMatrix,bool flag) //矩阵相加,返回为相加矩阵
{
bool returnvalue(false);
if (!IsEmpty && !sMatrix.IsArrayEmpty() && type && sMatrix.GetType())
{
if (VLine==sMatrix.GetVerticalDim() && HLine==sMatrix.GetHorizonDim())
{
TYPE_V i,j;
for (i=0;i<HLine;i++)
for (j=0;j<VLine;j++)
{
if (flag)
GetDataEX(i,j)=GetDataEX(i,j)+sMatrix.GetDataEX(i,j);
else
GetDataEX(i,j)=GetDataEX(i,j)-sMatrix.GetDataEX(i,j);
}
returnvalue=true;
}
}
return returnvalue;
}
template <class T>
bool Array<T>::MatrixPow(Array <T> &sMatrix,TYPE_V time) //矩阵幂运算
{
bool returnvalue(false)
if (!IsEmpty && type)
{
TYPE_V i;
Array<T> temp(sMatrix);
for(i=1;i<time;i++)
if (!MatrixMulMatrix(temp)) return false;
returnvalue=true;
}
return returnvalue;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -