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

📄 am.h

📁 将C语言的常用程序集移植到VC开发环境的源代码
💻 H
字号:
/************************************************
 Simple&Easy!
 This header file are coded for the convenience of
 Array&Matrix operations.
 It contains two classes,namely Array & Matrix.
 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>

template <class T>
class Array
{
protected:
	T* pdata;
	unsigned int length;
public:
	Array();
	Array(unsigned int);
	Array(Array const&);

	virtual ~Array();

	void operator = (Array&);
	Array<T>& operator + (Array&);
	Array<T>& operator - (Array&);
	
	T const& operator [] (unsigned int)const;
	T& operator [](unsigned int);

	T const* GetData() const;
	unsigned int GetLenght();
	void SetLength(unsigned int,bool=true);

};
////////////////////////////////////////////////////////////
//缺省构造函数
template <class T>
Array<T>::Array():pdata(new T[1]),length(1)
{
}
//数组构造函数
template <class T>
Array<T>::Array(unsigned int n):pdata(new T[n]),length(n)
{
}
//拷贝构造函数
template <class T>
Array<T>::Array(Array<T> const& array):pdata(new T[array.length]),length(array.length)
{
	for(unsigned int i=0;i<length;i++)
		pdata[i]=array.pdata[i];
}
//析构函数
template <class T> Array<T>::~Array()
{
	delete []pdata;
}
//得到指向数组的指针变量
template <class T>T const* Array<T>::GetData()const
{
	return pdata;
}
//得到数组长度
template <class T> unsigned int Array<T>::GetLenght()
{
	return length;
}
//重载操作符
template <class T>Array<T>& Array<T>::operator + (Array& array)
{
	if(array.length!=length)
		throw("Error: Array do not match !");
	for(unsigned int i=0;i<length;i++)
		pdata[i]+=array[i];
	return *this;
}
template <class T>Array<T>& Array<T>::operator - (Array& array)
{
	if(array.length!=length)
		throw("Error: Array do not match !");
	for(unsigned int i=0;i<length;i++)
		pdata[i]-=array[i];
	return *this;
}
template <class T> void Array<T>::operator = (Array& array)
{
	if(array.length!=length)
		throw;
	for(unsigned int i=0;i<length;i++)
		pdata[i]=array[i];
	return;
}
//下标操作
template <class T> T const& Array<T>::operator[](unsigned int position)const
{
	if(position>=length)
		throw("Error: Array--Out_of_range!");
	return pdata[position];
}
template <class T> T& Array<T>::operator[](unsigned int position)
{
	if(position>=length)
		throw("Error: Array--Out_of_range!");
	return pdata[position];
}
//数组大小重新调整
template <class T> void Array<T>::SetLength(unsigned int newLength,bool bCopyData)
{
	T* const pnewData=new T[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 Matrix
{
protected:
	unsigned int numberOfRows;
	unsigned int numberOfColumns;
	Array<double> array;
public:
	class Row
	{
		Matrix& matrix;
		unsigned int const row;
	public:
		Row (Matrix& _matrix,unsigned int _row):matrix(_matrix),row(_row){}
		double& operator [](unsigned int column)const
		{return matrix.Select(row,column);}
	};

	Matrix(unsigned int, unsigned int);
	Matrix(Matrix& mat);
	Matrix();

	virtual ~Matrix();

	double& Select(unsigned int, unsigned int);
	Row operator[](unsigned int);

	Matrix operator + (Matrix& mat);
	Matrix operator - (Matrix& mat);
	Matrix operator * (Matrix& mat);
	bool operator == (Matrix& mat);

	Array<double>& 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);
};
//-----------------------------------------
//构造函数
Matrix::Matrix(unsigned int rows,unsigned int cols):
numberOfRows(rows),numberOfColumns(cols),array(rows*cols)
{//ZeroMatrix();
}
//拷贝构造函数
Matrix::Matrix(Matrix& mat):
	numberOfRows(mat.numberOfRows),numberOfColumns(mat.numberOfColumns),
		array(mat.numberOfRows*mat.numberOfColumns)
{*this=mat;
}
Matrix::Matrix():
	numberOfRows(1),numberOfColumns(1),
		array(1)
{//array[0]=0.0;
}
Matrix::~Matrix()
{
}
//重新调整矩阵大小
bool Matrix::ResetMatrix(unsigned int rows, unsigned int cols)
{
	try{	
		Matrix 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 Matrix::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 Matrix::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!");

	Array<double> 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& Matrix::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];
}
Array<double>& Matrix::GetData()
{
	return array;
}
//得到行数
unsigned int Matrix::GetNumberOfRows()
{
	return numberOfRows;
}
//得到列数
unsigned int Matrix::GetNumberOfColumns()
{
	return numberOfColumns;
}
//下标操作
Matrix::Row Matrix::operator [] (unsigned int row)//返回Row对象
{
	return Row(*this,row);
}
//重载操作符
Matrix Matrix::operator + (Matrix& mat)
{
	Matrix 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;
}
Matrix Matrix::operator - (Matrix& mat)
{
	Matrix 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;
}
Matrix Matrix::operator * (Matrix& mat)
{
	if(mat.numberOfRows!=numberOfColumns)
		throw("The matrices do not match!");
	Matrix 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 Matrix::operator == (Matrix& 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 Matrix::ZeroMatrix()
{
	for(unsigned int i=0;i<numberOfRows;i++)
			for(unsigned int j=0;j<numberOfColumns;j++)
				Select(i,j)=0;
}
void Matrix::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 Matrix::ReverseMatrix()
{
	double tmp;
	try{
		if(numberOfRows!=numberOfColumns)
		{
			Array<double> 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 + -