📄 cam.h
字号:
/************************************************
Simple&Easy!
This header file are coded for the convenience of
CArray&CMatrix operations.
It contains two classes,namely CArray & CMatrix.
Expect bugs!
Please use and enjoy, and let me know of any bugs/mods/improvements
that you have found/implemented and I will fix/incorporate them into
this file.
Written by hujinshan@2002.12.23 Airforce Engineering University.
************************************************/
/*** AM.h ***/
#ifndef AM_H_
#define AM_H_
#include <stdlib.h>
#include <time.h>
#include <string.h>
class CArray
{
protected:
double* pdata;
unsigned int length;
public:
CArray();
CArray(unsigned int);
CArray(CArray const&);
virtual ~CArray();
void operator = (CArray&);
CArray& operator + (CArray&);
CArray& operator - (CArray&);
double const& operator [] (unsigned int)const;
double& operator [](unsigned int);
double const* GetData() const;
unsigned int GetLenght();
void SetLength(unsigned int,bool=true);
};
////////////////////////////////////////////////////////////
//缺省构造函数
CArray::CArray():pdata(new double[1]),length(1)
{
}
//数组构造函数
CArray::CArray(unsigned int n):pdata(new double[n]),length(n)
{
}
//拷贝构造函数
CArray::CArray(CArray const& array):pdata(new double[array.length]),length(array.length)
{
for(unsigned int i=0;i<length;i++)
pdata[i]=array.pdata[i];
}
//析构函数
CArray::~CArray()
{
delete []pdata;
}
//得到指向数组的指针变量
double const* CArray::GetData()const
{
return pdata;
}
//得到数组长度
unsigned int CArray::GetLenght()
{
return length;
}
//重载操作符
CArray& CArray::operator + (CArray& array)
{
if(array.length!=length)
throw("Error: CArray do not match !");
for(unsigned int i=0;i<length;i++)
pdata[i]+=array[i];
return *this;
}
CArray& CArray::operator - (CArray& array)
{
if(array.length!=length)
throw("Error: CArray do not match !");
for(unsigned int i=0;i<length;i++)
pdata[i]-=array[i];
return *this;
}
void CArray::operator = (CArray& array)
{
if(array.length!=length)
throw;
for(unsigned int i=0;i<length;i++)
pdata[i]=array[i];
return;
}
//下标操作
double const& CArray::operator[](unsigned int position)const
{
if(position>=length)
throw("Error: CArray--Out_of_range!");
return pdata[position];
}
double& CArray::operator[](unsigned int position)
{
if(position>=length)
throw("Error: CArray--Out_of_range!");
return pdata[position];
}
//数组大小重新调整
void CArray::SetLength(unsigned int newLength,bool bCopyData)
{
double* const pnewData=new double[newLength];
unsigned int const min=length<newLength?length:newLength;
if(bCopyData)
for(unsigned int i=0;i<min;i++)
pnewData[i]=pdata[i];
delete []pdata;
pdata=pnewData;
length=newLength;
}
//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//
class CMatrix
{
protected:
unsigned int numberOfRows;
unsigned int numberOfColumns;
CArray array;
public:
class Row
{
CMatrix& matrix;
unsigned int const row;
public:
Row (CMatrix& _matrix,unsigned int _row):matrix(_matrix),row(_row){}
double& operator [](unsigned int column)const
{return matrix.Select(row,column);}
};
CMatrix(unsigned int, unsigned int);
CMatrix(CMatrix& mat);
CMatrix();
virtual ~CMatrix();
double& Select(unsigned int, unsigned int);
Row operator[](unsigned int);
CMatrix operator + (CMatrix& mat);
CMatrix operator - (CMatrix& mat);
CMatrix operator * (CMatrix& mat);
bool operator == (CMatrix& mat);
CArray& GetData();
unsigned int GetNumberOfRows();
unsigned int GetNumberOfColumns();
bool LoadFromArray(double [],unsigned int,unsigned int);
bool LoadFromString(char*,char,char);
bool ResetMatrix(unsigned int, unsigned int);
bool ReverseMatrix();
void ZeroMatrix();
void RandomMatrix(int max);
};
//-----------------------------------------
//构造函数
CMatrix::CMatrix(unsigned int rows,unsigned int cols):
numberOfRows(rows),numberOfColumns(cols),array(rows*cols)
{//ZeroMatrix();
}
//拷贝构造函数
CMatrix::CMatrix(CMatrix& mat):
numberOfRows(mat.numberOfRows),numberOfColumns(mat.numberOfColumns),
array(mat.numberOfRows*mat.numberOfColumns)
{*this=mat;
}
CMatrix::CMatrix():
numberOfRows(1),numberOfColumns(1),
array(1)
{//array[0]=0.0;
}
CMatrix::~CMatrix()
{
}
//重新调整矩阵大小
bool CMatrix::ResetMatrix(unsigned int rows, unsigned int cols)
{
try{
CMatrix mat(*this);
array.SetLength(rows*cols,false);
if(numberOfRows<=rows&&numberOfColumns<=cols)
for(unsigned int i=0;i<numberOfRows;i++)
for(unsigned int j=0;j<numberOfColumns;j++)
array[i*cols+j]=mat.Select(i,j);
numberOfRows=rows;
numberOfColumns=cols;//ZeroMatrix();
}
catch(...){return false;}
return true;
}
//从数组导入数据
bool CMatrix::LoadFromArray(double a[],unsigned int rows, unsigned int cols)
{
if(!ResetMatrix(rows,cols))
return false;
for(unsigned int i=0;i<rows;i++)
for(unsigned int j=0;j<cols;j++)
Select(i,j)=a[i*numberOfColumns+j];
return true;
}
//从字符串导入数据
bool CMatrix::LoadFromString(char* str,char chSepOne=',',char chSepRow=';')
{
try{
char buf[50];
buf[0]='\0';
if(chSepRow==chSepOne)
throw("Sorry, the dividing symbols cannot be the same!");
unsigned int i,j,nRows=0,nCols=0,nIndex=0;
int pos,nTmp=-1;//pos为字符在字符串中的位置
for(i=0;i<strlen(str);i++)
{
if((str[i]==chSepRow) || (i==strlen(str)-1))
{
nRows++;nCols++;
if(nTmp==-1)
nTmp=nCols;
else if(nTmp!=nCols)
throw("Sorry, the string is invalid!");
if(i!=strlen(str)-1)
nCols=0;
}
else if(str[i]==chSepOne)
nCols++;
}
if(nTmp!=nCols)
throw("Sorry, the string is invalid!");
CArray arrayTmp(nRows*nCols);
unsigned int iA=0;
ResetMatrix(nRows,nCols);
char ch[2];
for(i=0;i<strlen(str);i++)
if(str[i]==chSepRow)
{
for(j=nIndex;j<i;j++)
ch[0]=str[j],ch[1]='\0',strcat(buf,ch);
pos=strcspn(buf,&chSepOne);
if(pos<strlen(buf))
strncpy(buf+pos," ",1);
pos=strcspn(buf,&chSepRow);
if(pos<strlen(buf))
strncpy(buf+pos," ",1);
arrayTmp[iA]=atof(buf);
iA++;
nIndex=i;buf[0]='\0';
}
else
{
if(i==strlen(str)-1)//last char is not chSepRow
{
for(j=nIndex;j<i+1;j++)
ch[0]=str[j],ch[1]='\0',strcat(buf,ch);
pos=strcspn(buf,&chSepOne);
if(pos<strlen(buf))
strncpy(buf+pos, " ",1);
pos=strcspn(buf,&chSepRow);
if(pos<strlen(buf))
strncpy(buf+pos," ",1);
arrayTmp[iA]=atof(buf);
iA++;
buf[0]='\0';
nRows++;nIndex=i;
}
else if(str[i]==chSepOne)
{
for(j=nIndex;j<i;j++)
ch[0]=str[j],ch[1]='\0',strcat(buf,ch);
pos=strcspn(buf,&chSepOne);
if(pos<strlen(buf))
strncpy(buf+pos, " ",1);
pos=strcspn(buf,&chSepRow);
if(pos<strlen(buf))
strncpy(buf+pos," ",1);
arrayTmp[iA]=atof(buf);
iA++;
nIndex=i;buf[0]='\0';
}
}
for(i=0;i<numberOfRows;i++)
for(j=0;j<numberOfColumns;j++)
Select(i,j)=arrayTmp[i*numberOfColumns+j];
}
catch(...)
{throw("Sorry, cannot load matrix from string! ");}
return true;
}
//选取元素
double& CMatrix::Select(unsigned int i, unsigned int j)
{
char ch[50];
if(i>=numberOfRows)
{
sprintf(ch,"Invalid row: %d",i);
throw (ch);
}
if(j>=numberOfColumns)
{
sprintf(ch,"Invalid colum: %d",j);
throw (ch);
}
return array[i*numberOfColumns+j];
}
CArray& CMatrix::GetData()
{
return array;
}
//得到行数
unsigned int CMatrix::GetNumberOfRows()
{
return numberOfRows;
}
//得到列数
unsigned int CMatrix::GetNumberOfColumns()
{
return numberOfColumns;
}
//下标操作
CMatrix::Row CMatrix::operator [] (unsigned int row)//返回Row对象
{
return Row(*this,row);
}
//重载操作符
CMatrix CMatrix::operator + (CMatrix& mat)
{
CMatrix mt=*this;
if( (numberOfRows!=mat.numberOfRows)||
(mat.numberOfColumns!=numberOfColumns) )
throw("The matrices are not match!");
for(unsigned int i=0;i<numberOfRows;i++)
for(unsigned int j=0;j<numberOfColumns;j++)
mt[i][j]+=mat[i][j];
return mt;
}
CMatrix CMatrix::operator - (CMatrix& mat)
{
CMatrix mt=*this;
if( (numberOfRows!=mat.numberOfRows)||
(mat.numberOfColumns!=numberOfColumns) )
throw("The matrices do not match!");
for(unsigned int i=0;i<numberOfRows;i++)
for(unsigned int j=0;j<numberOfColumns;j++)
mt[i][j]-=mat[i][j];
return mt;
}
CMatrix CMatrix::operator * (CMatrix& mat)
{
if(mat.numberOfRows!=numberOfColumns)
throw("The matrices do not match!");
CMatrix matRet(numberOfRows,mat.numberOfColumns);
unsigned int i,j,l;
double tmp;
for (i=0; i<numberOfRows; i++)
for (j=0; j<mat.numberOfColumns; j++)
{
tmp=0.0;
for (l=0; l<numberOfColumns; l++)
tmp=tmp+array[i*numberOfColumns+l]*(mat.Select(l,j));
matRet[i][j]=tmp;
}
return matRet;
}
bool CMatrix::operator == (CMatrix& mat)
{
if( (numberOfRows!=mat.numberOfRows)||
(mat.numberOfColumns!=numberOfColumns) )
return false;
for(unsigned int i=0;i<numberOfRows;i++)
for(unsigned int j=0;j<numberOfColumns;j++)
if(Select(i,j)!=mat[i][j])
return false;
return true;
}
//矩阵元素清零
void CMatrix::ZeroMatrix()
{
for(unsigned int i=0;i<numberOfRows;i++)
for(unsigned int j=0;j<numberOfColumns;j++)
Select(i,j)=0;
}
void CMatrix::RandomMatrix(int max)
{
srand( (unsigned)time( NULL ) );
for(unsigned int i=0;i<numberOfRows;i++)
for(unsigned int j=0;j<numberOfColumns;j++)
Select(i,j)=rand()%max+1;
}
//实矩阵转置
bool CMatrix::ReverseMatrix()
{
double tmp;
try{
if(numberOfRows!=numberOfColumns)
{
CArray tmpArray(array);
ResetMatrix(numberOfColumns,numberOfRows);
for(unsigned int i=0;i<numberOfRows;i++)
for(unsigned int j=0;j<numberOfColumns;j++)
array[i*numberOfColumns+j]=tmpArray[j*numberOfRows+i];
}
else
{
for(unsigned int i=0;i<numberOfRows;i++)
for(unsigned int j=0;j<numberOfColumns;j++)
if(i>j)
{
tmp=array[i*numberOfColumns+j];
array[i*numberOfColumns+j]=array[j*numberOfColumns+i];
array[j*numberOfColumns+i]=tmp;
}
}return true;
}
catch(...)
{return false;}
}
//------------------------------------------------------
#endif
//end of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -