__2017-12-16 如一模式识别研究

如一模式识别研究

vc++>>在Visual C++ 6.0上实现矩阵的各种运算

转自:http://hi.baidu.com/nochu/item/ed458062719f5498c4d24965

//#include "stdafx.h"

/*

* 文件:全局函数.h

*/

#include

#include

#include

#define eps (1e-06)

#define KMAX 60 //最大迭代次数

using namespace std;

int cifang(int i,int j); //全局函数:求-1的(i+j)次方

int chartoint(const char* p);//字符转为整数型

int getRank(double *Array,int row,int col);//全局函数:求秩

int IsZero(double x){ return fabs(x) <= 0.00001?1:0;} //全局函数:判断是否为零

int jacobieigen(double *a,double *u,int jie);//a 和u 都是jie*jie的方阵

void getEigen(double *Array,int dim);//全局函数:获得特征值及特征向量。Array是dim*dim方阵

void show(double array[],int jie); //全局函数:显示矩阵

void transpose(double array[],int jie); //全局函数:转置

bool IsSymmetricMatrix(double *Array, int dim);//判断是对称矩阵

bool notfindchar(const char* p,int index);

bool Is0Array( double *Array,int row,int col); //全局函数:判断是否是0矩阵

bool chardouble(const char *p);//全局函数:判断字符是否满足转换要求

double det(double array[],int Jie); //全局函数:求行列式

double chartodouble(const char* p);//全局函数:字符型转double型

double* doInverse(double Array[],int row);//全局函数:求逆

int cifang(int i,int j) //全局函数:求-1的(i+j)次方

{     if( i < 0 || j < 0 )

	{

	cerr<<"i,j非法!" << endl;

	exit(-1);

	}

	int temp = i+j;

	if( temp%2 == 0)

		return 1;

	else

		return -1;

}

int chartoint(const char* p)//字符转为整数型

{

	if( p == NULL)

	return 0;

	int length = strlen(p);

	int INT = 0,temp =0;

	if( p[0] != '-' )

	{

		for( int i = 0; i < length; i++)

		{

			temp = p[i] - '0';

			INT = INT*10 + temp;

		}

		return INT;

	}

	else

	{

		for( int j = 1; j < length; j++)

		{

			temp = p[j] - '0';

			INT = INT*10 + temp;

		}

		return (-INT);

	}

}

int getRank(double *Array,int row,int col) //全局函数:求秩

{

	int i,j,k,l,i1,j1,main_row,main_col,rank;

	double main_element,temp;

	for( i = 0; i < row; i++ ) //行循环

	{

		main_element = *(Array + i*col + i );//对角元素

		main_row = i;//主元坐标:主对角元(i,i)

		main_col=i;

		for( j = i; j < row; j++ ) //循环:寻找当前(i,i)元之后的最大元,并记为坐标(main_row,mai_col)

		for( k = i; k < col; k++ )

		{

			if( fabs(*( Array + j*col + k )) >= fabs(main_element) && (i != j) )

			{

				main_element = *(Array + j*col + k);

				main_row=j;

				main_col=k;

			}

		}

		for(l = 0; l < col; l++ )

		{

			temp = *(Array + main_row*col + l);

			*(Array + main_row*col + l) = *(Array +i*col + l);

			*(Array +i*col + l) = temp;

		}

		for(l = 0; l < row; l++ )

		{

			temp = *(Array + l*col + main_col);

			*(Array + l*col + main_col) = *(Array + l*col + i);

			*(Array + l*col + i) = temp;

		}

		if( IsZero(main_element) == 1)

			break;

		for( i1 = i+1; i1 < row; i1++ )

		{

			temp = *(Array + i1*col + i);

			for( j1 = 0; j1 < col; j1++ )

				*(Array + i1*col + j1) = *(Array + i1*col +j1) - *(Array + i*col + j1)*temp/main_element;

		}

	}

	rank = 0;

	for( i = 0; i < row; i++ )

	{ for( j = 0; j < col; j++ )

		{ if( IsZero( *(Array + i*col + j)) == 0 )

			{ rank++;

				break;

			}

			else

				continue;

			}

		}
	
		return rank;

	}

int jacobieigen(double *a,double *u,int jie)//a 和u 都是jie*jie的方阵

{

int i,j,k,p,q;

double d,m,x,y,sn,cn,w;

for( i = 0; i < jie; i++ ) //产生单位矩阵

{

(*(u+i*jie+i)) = 1;

for( j = 0; j < jie; j++ )

{ if( i!=j )

(*(u+i*jie+j)) = 0;

}

}

k = 1;

while(1)

{ m = 0;

for( i = 0; i <= jie-1; i++ ) //选取绝对值最大的对角线元素

{

for( j = 0; j <= i-1; j++ )

{

d = fabs((*(a+i*jie+j)));

if( (i!=j) && (d>m) )

{

m = d;

p = i;

q = j;

}

}

}

if( m < eps ) //满足精度要求,正常返回

return(1);

if( k > KMAX ) //超过最大迭代次数返回

return(-1);

k = k + 1;

x = -(*(a+p*jie+q));

y = ( (*(a+q*jie+q)) - (*(a+p*jie+p)) )/2.0;

w = x/sqrt( x*x + y*y );

if( y < 0 )

w = -w;

sn = 1 + sqrt( 1 - w*w );

sn = w/sqrt( 2*sn );

cn = sqrt( 1 - sn*sn );

m = (*(a+p*jie+p)); //计算矩阵A的新元素

(*(a+p*jie+p)) = m*cn*cn + (*(a+q*jie+q))*sn*sn + (*(a+p*jie+q))*w;

(*(a+q*jie+q)) = m*sn*sn + (*(a+q*jie+q))*cn*cn - (*(a+p*jie+q))*w;

(*(a+p*jie+q)) = 0;

(*(a+q*jie+p)) = 0;

for( j = 0; j < jie; j++ )

{

if( (j!=p) && (j!=q) )

{

m = (*(a+p*jie+j));

(*(a+p*jie+j)) = m*cn + (*(a+q*jie+j))*sn;

(*(a+q*jie+j)) = -m*sn + (*(a+q*jie+j))*cn;

}

}

for( i = 0; i < jie; i++ )

{

if( (i!=p)&&(i!=q) )

{

m = (*(a+i*jie+p));

(*(a+i*jie+p)) = m*cn + (*(a+i*jie+q))*sn;

(*(a+i*jie+q)) = -m*sn + (*(a+i*jie+q))*cn;

}

}

for( i = 0; i < jie; i++ )

{

m = (*(u+i*jie+p));

(*(u+i*jie+p)) = m*cn + (*(u+i*jie+q))*sn;

(*(u+i*jie+q)) = -m*sn + (*(u+i*jie+q))*cn;

}

}

}

void getEigen(double *Array,int dim)//全局函数:获得特征值及特征向量。Array是dim*dim方阵

{

double *a = new double[dim*dim],*b = new double[dim*dim],*v = new double[dim*dim];

int i,j,k;

for( i = 0; i < dim; i++ )

for( j = 0; j < dim; j++ )

*(a+i*dim+j) = *(Array+i*dim+j);

if( !IsSymmetricMatrix(a,dim) )

{

cerr << "不求特征值及特征向量\n";

return;

}

k = jacobieigen(a,v,dim);

if( k == 1 )

{ cout << "\n特征值依次是\n";

for( i = 0; i < dim; i++ )

{

cout << i+1 << ":";

for( j = 0; j < dim; j++ )

if( i == j )

printf("%11f",(*(a+i*dim+j)));

cout << endl;

}

cout << endl;

for( i = 0; i < dim; i++ )

for( j = 0; j < dim; j++ )

*(b+i*dim+j) = *(v+i*dim+j);

cout << "对应的特征向量\n";

for( i = 0; i < dim; i++ )

{

printf(" %d:",i+1);

for( j = 0; j < dim; j++ )

printf(" %11f",*(b+i*dim+j));

cout << endl;

}

}

else

cout << "dimoEigenValue\n";

}

void transpose(double array[],int jie) //全局函数:转置

{

if( jie <= 0){cerr<< "数组非法!" << endl;exit(-1);}

double temp;

for( int i = 0; i < jie; i++ )

for( int j = 0; j < i; j++ )

{

temp = *(array + i*jie + j);

*(array + i*jie + j) = *(array + j*jie + i );

*(array + j*jie + i) = temp;

}

}

void show(double array[],int jie) //全局函数:方阵输出

{

if(jie <= 0)

{

cerr << " 非法" << endl;

exit(-1);

}

cout << "矩阵输出为: " << endl;

int i, j;

for( i = 0; i < jie; i++ )

{

for( j = 0; j < jie; j++ )

cout << *(array + i*jie + j) << '\t';

cout << endl;

}

}

bool IsSymmetricMatrix(double *Array, int dim)//判断是对称矩阵

{

if( dim <= 0 )

{

cerr << "参数非法,退出!" << endl;

exit(-1);

}

int i,j;

for( i = 0; i < dim; i++ )

for( j = 0; j <= i; j++ )

{ if( *(Array+i*dim+j) != *(Array+j*dim+i) )

{ cout << "不是对称矩阵" << endl;

return false;

}

}

cout << "是对称矩阵!" << endl;

return true;

}

bool Is0Array( double *Array,int row,int col)//全局函数:判断是否是0矩阵

{

if( row <= 0 || col<= 0 )

{

cerr <<"行列值非法,退出!\n";

exit(-1);

}

int i,j;

double temp1 = 0,temp2 = 0;

for( i = 0; i < row; i++ )

for( j = 0; j < col; j++ )

{

temp2 = fabs(*(Array + i*col +j));

temp1 += temp2;

}

if(temp1 < 0.00001)

{ cout << "是" << row << '*' << col << "的0矩阵\n";

return true;

}

return false;

}

bool notfindchar(const char* p,int index)

{

if( p == NULL )

return true;

int length = strlen(p);

for( int i = index; i <= length-1; i++ )

if(p[i]<'0' || p[i] >'9')

return false;

return true;

}

double det(double array[],int Jie) //全局函数:求行列式

{

if( Jie <= 0 )

{

cerr << "阶小于0或等于0!" << endl;

return 0;

}

else if( Jie == 1)

return array[0];

else

{

int i,j,k,tag;

double *subArray[500]; //须填固定值?

for( i = 0; i < Jie; i++ )

subArray[i] = new double[(Jie-1)*(Jie-1)];

for( i = 0; i < Jie; i++ )

for( j = 0; j < Jie-1; j++ )

for( k = 0; k < Jie-1; k++ )

*(subArray[i] + j*(Jie-1) + k) = 0;

for( i = 0; i < Jie; i++ )

for( j = 0; j < Jie-1; j++ )

for( k = 0; k < Jie-1; k++ )

{ if( k < i )

*(subArray[i] + j*(Jie-1) + k) = *(array + (j+1)*Jie + k );

else

*(subArray[i] + j*(Jie-1) + k) = *(array + (j+1)*Jie + k+1 );

}

double temp= 0;

tag = 1;

for( i = 0 ; i < Jie; i++)

{

temp += tag * det(subArray[i],Jie-1) * array[i];

tag *= -1;

}

return temp;

}

}

double *doInverse(double Array[],int row)//全局函数:求逆

{

double d_det = det(Array,row);

if(d_det == 0)

{

cerr << "行列式为0,不存在逆矩阵!" << endl;

exit(-1);

}

int i,j,k,h,subRow = row-1;

double *subArray[1000]; //须填固定值?

for( i = 0; i < 500; i++ )

{

if( i < row*row )

{

subArray[i] = new double[subRow*subRow];

for( j = 0; j < subRow; j++ )

for( k = 0; k < subRow; k++ )

*(subArray[i] + j*subRow + k) = 0;

}

else

subArray[i] = NULL;

}

for( i= 0; i < row; i++ )

for( h = 0; h < row; h++ )

for( j = 0; j < subRow; j++ )

for( k = 0; k < subRow; k++ )

{

if( j < i && k < h )

*(subArray[i*row+h] + j*subRow + k) = *(Array + j*row + k );

if( j < i && k >= h )

*(subArray[i*row+h] + j*subRow + k) = *(Array + j*row + k+1 );

if( j >= i && k < h )

*(subArray[i*row+h] + j*subRow + k) = *(Array + (j+1)*row + k );

if( j >= i && k >= h)

*(subArray[i*row+h] + j*subRow + k) = *(Array + (j+1)*row + k+1 );

}

double *tempArray = new double[row*row]; //创建临时数组,赋值后并返回

for( i = 0; i < row*row; i++ ) //初始化

tempArray[i] = 0;

for( i = 0; i < row; i++ )

for( j = 0; j < row; j++ )

*(tempArray + i*row + j) = cifang(i,j)*det(subArray[i*row+j],subRow)/d_det;//求逆矩阵的(i,j)元

transpose(tempArray,row); //调用转置后的伴随

return tempArray;

}

bool chardouble(const char *p)

{

if( p == NULL )return true;//风险?

int length=strlen(p),count1=0,count2=0,i;

for( i = 0; i < length; i++ )

{

if( p[i] =='.')

count1++;

if( p[i] < '0' || p[i] > '9' )

count2++;

}

if( count2 == 0)

return true;

if( count2 == 1 && p[0] == '-' && p[1]!='\0' )

return true;

if( count2 == 1 && count1 == 1 && p[0]!='.' )

return true;

if( count2 == 2 && count1 == 1 && p[0] == '-' && p[1] !='.' )

return true;

return false;

}

double chartodouble(const char* p)

{

if( p == NULL )return 0;

double DOUBLE = 0,DOUBLE1 = 0;

int length=strlen(p),count1=0,count2=0,temp = 0,i,j=-1;

for( i = 0; i < length; i++ )

{

if( p[i] =='.')

{

count1++;

j=i;

}

if( p[i] < '0' || p[i] > '9' )

count2++;

}

if( count2 == 0)

{

for( i = 0;i< length;i++)

{

temp = p[i]-'0';

DOUBLE = DOUBLE*10 + temp;

}

return DOUBLE;

}

if( count2 == 1 && p[0] == '-'&& p[1]!='\0' )

{

for( i = 1;i < length;i++)

{

temp = p[i] - '0';

DOUBLE = DOUBLE*10 + temp;

}

return -DOUBLE;

}

if( count2 == 1 && count1 == 1 && p[0]!='.' )

{

for( i = 0;i < j;i++)

{

temp = p[i] - '0';

DOUBLE = DOUBLE*10 + temp;

}

for( i = length-1; i > j;i--)

{

temp = p[i] - '0';

DOUBLE1 = DOUBLE1*0.1+temp;

}

return (DOUBLE+0.1*DOUBLE1);

}

if( count2 == 2 && count1 == 1 && p[0] == '-' && p[1] != '.' )

{

for( i = 1;i < j;i++)

{

temp = p[i] - '0';

DOUBLE = DOUBLE*10 + temp;

}

for( i = length-1; i > j;i--)

{

temp = p[i] - '0';

DOUBLE1 = DOUBLE1*0.1+temp;

}

return -(DOUBLE+0.1*DOUBLE1);

}

return 0;

}

#include

#include

#include

#include

#define eps (1e-06)

#define KMAX 60 //最大迭代次数

#define defaultMAXCHAR = 50;//字符数组大小

using namespace std;

void experiment1(); //全局函数:测试函数1

void experiment2();//全局函数:测试函数2

void experiment();//全局函数:测试函数

bool chardouble(const char *p);//全局函数:判断字符是否合格

double chartodouble(const char* p);//全局函数:字符转双精度型

class Matrix; //友元函数声明

ostream& operator <<(ostream& out, const Matrix& m);

class Matrix; //友元函数声明

istream& operator >>(istream& in, Matrix& m);

class Matrix // Matrix类定义

{

private:

int row,col;

double *Array;

public:

Matrix(int r=1,int c=1) //默认构造函数

{

if( r <= 0 || c <= 0 )

{ cerr << "行列值存在0或负数!" << endl;

exit(-1);

}

row = r;

col = c;

Array = new double[row*col];

for( int i = 0; i < row; i++ )

for( int j = 0; j < col; j++ )

*(Array + i*col + j)=0;

}

Matrix(const Matrix& m); //拷贝构造函数

Matrix(double array[], int mrow, int mcol); //构造函数,参数为数组表示的矩阵

~Matrix() { delete []Array; } //析构函数

double &element(int i,int j); //取矩阵的(i,j)元

double element(int i,int j)const;//取矩阵的(i,j)元,为常对象提供

Matrix& operator= ( const Matrix& m); //重载操作符=

Matrix operator + ( const Matrix& m)const; //重载操作符+

Matrix& operator+=( const Matrix& m); //重载操作符+=

Matrix operator - ( const Matrix& m)const; //重载操作符-

Matrix& operator-=( const Matrix& m); //重载操作符-=

Matrix operator * ( const Matrix& m)const; //重载操作符*

Matrix& operator*=( const Matrix& m); //重载操作符*=

bool operator==(const Matrix& m)const; //重载操作符==

Matrix transposition()const; //矩阵转置

double determinant()const; //矩阵求行列式

Matrix inverse()const; //矩阵求逆

int rank()const; //矩阵求秩

void Eigenvalue(); //矩阵求特征值及特征向量

bool Is0Matrix()const; //矩阵是零矩阵吗

void displayAll()const; //供operator <<(ostream& out,const Matrix& m)调用输出

friend istream& operator >>(istream& in, Matrix& m); //重载操作符>>

friend ostream& operator <<(ostream& out, const Matrix& m); //重载操作符<<

};

Matrix::Matrix(const Matrix& m)//拷贝构造函数

{ row = m.row;

col = m.col;

Array = new double[row*col];

for( int i = 0; i < row; i++ )

for( int j = 0; j < col; j++ )

*(Array + i*col + j ) = m.element(i,j);

}

Matrix::Matrix(double array[], int mrow, int mcol)//构造函数,参数为数组表示的矩阵

{

if( mrow <= 0 || mcol <= 0 )

{ cerr << "非法数组参数! " << endl;

exit(-1);

}

row = mrow;

col = mcol;

Array = new double[row*col];

for( int i = 0; i < row; i++ )

for( int j = 0; j < col; j++ )

*(Array + i*col + j) = array[i*col+j];

}

double &Matrix::element(int i,int j)//取矩阵的(i,j)元

{ if(i < 0 || j < 0 || i >= row || j >= col)

{ cerr << "非法参数! " << endl;

exit(-1);

}

return *( Array + i*col + j );

}

double Matrix::element(int i,int j)const//取矩阵的(i,j)元,为常对象提供

{ if(i < 0 || j < 0 || i >= row || j >= col)

{ cerr << "非法矩阵! " << endl;

exit(-1);

}

double temp = *( Array + i*col + j );

return temp;

}

Matrix& Matrix::operator =( const Matrix& m)//重载操作符=

{ if( &m == this )

{ cerr << "矩阵本身,无须赋值!" << endl;

return *this;

}

else

{ delete []Array;

row = m.row;

col = m.col;

Array = new double[row*col];

for( int i = 0; i < row; i++ )

for( int j = 0; j < col; j++ )

*(Array + i*col + j ) = *(m.Array + i*col + j );

return *this;

}

}

Matrix Matrix::operator + ( const Matrix& m)const//重载操作符+

{

Matrix temp(row,col);

if( row != m.row || col != m.col )

{

if( row != m.row )

cout << "原行数" << row << "不等于" << "新行数 " << m.row << ',';

else

cout << "原列数" << col << "不等于" << "新列数" << m.col << ',';

cout << "不能相加减!";

temp = (*this);

return temp;

}

for( int i = 0; i < row; i++ )

for( int j = 0; j < col; j++ )

temp.element(i,j) = (*this).element(i,j) + m.element(i,j);

return temp;

}

Matrix& Matrix::operator +=(const Matrix& m)//重载操作符+=

{ Matrix temp(*this);

(*this) = temp + m;

return *this;

}

Matrix Matrix::operator - ( const Matrix& m)const //重载操作符-

{

Matrix temp(row,col);

if( row != m.row || col != m.col )

{

if( row != m.row )

cout << "原行数" << row << "不等于" << "新行数 " << m.row << ',';

else

cout << "原列数" << col << "不等于" << "新列数" << m.col << ',';

cout << "不能相加减!";

temp = (*this);

return temp;

}

for( int i = 0; i < row; i++ )

for( int j = 0; j < col; j++ )

temp.element(i,j) = (*this).element(i,j) - m.element(i,j);

return temp;

}

Matrix& Matrix::operator -=(const Matrix& m)//重载操作符-=

{ Matrix temp(*this);

(*this) = temp - m;

return *this;

}

Matrix Matrix::operator *(const Matrix& m)const //重载操作符*

{

Matrix temp(row,m.col);

if( m.row <= 0 || m.col <= 0 || col != m.row )

{

if( col != m.row )

cout << "原列数" << col << "不等于新行数" << m.row;

cout << "不相乘!" << endl;

temp = (*this);

return temp;

}

for( int i = 0; i < temp.row; i++)

for( int j = 0; j < temp.col; j++)

for( int k = 0; k < col; k++)

temp.element(i,j) += (*this).element(i,k)*m.element(k,j);

return temp;

}

Matrix& Matrix::operator *=(const Matrix& m) //重载操作符*=

{

if( m.row <= 0 || m.col <= 0 || col != m.row )

{

if( col!=m.row)

cout << "原矩阵列数" << col << "不等于新矩阵行数" << m.row << ',';

return *this;

}

Matrix temp(row,m.col);

for( int i = 0; i < row; i++)

for( int j = 0; j < m.col; j++)

for( int k = 0; k < col; k++)

temp.element(i,j) += (*this).element(i,k)*m.element(k,j);

(*this) = temp;

return *this;

}

bool Matrix::operator==(const Matrix& m)const //重载操作符==

{ if( &m == this )

return true;

else if(row != m.row || col != m.col )

return false;

else

{ int i,j;

for( i = 0; i < row; i++ )

for( j = 0; j < col; j++ )

if( (*(Array+i*col+j))!=(*(m.Array+i*col+j)))

return false;

return true;

}

}

Matrix Matrix::transposition()const //矩阵转置

{ if( row <= 0 || col <= 0 )

{

cerr << "空矩阵!" << endl;

exit(-1);

}

Matrix temp(col,row);

for( int i = 0; i < row; i++ )

for( int j = 0; j < col; j++ )

temp.element(j,i) = this->element(i,j);

return temp;

}

double Matrix::determinant()const//矩阵求行列式

{ if( row != col )

{

cerr << "不是方阵,不存在行列式!\n退出!" << endl;

exit(-1);

}

return det(Array,row);

}

Matrix Matrix::inverse()const//矩阵求逆

{ if( row != col)

{

cerr << "不是方阵,不存在逆矩阵!" << endl;

exit(-1);

}

if(row == 1 && col == 1 && *(Array) != 0)

{

Matrix temp(1,1);

*(temp.Array) = 1/(*Array);

return temp;

}

else if( this->determinant() == 0)

{

cout << "行列式 = 0;不存在逆!" << endl;

exit(-1);

}

else

{

double * p = doInverse(Array,row);

Matrix temp(row,row);

temp.row = row; temp.col = row;

for( int i = 0; i < row; i++ )

for( int j = 0; j < row; j++ )

*(temp.Array + i*row + j ) = p[i*row+j];

return temp;

}

}

int Matrix::rank()const //矩阵求秩

{ Matrix temp(row,col);

temp = (*this);

int rank = getRank(temp.Array,row,col);

return rank;

}

void Matrix::Eigenvalue()//获得特征值

{ if( row!=col )

{

cerr <<"不是方阵,不存在特征值及特征向量!" << endl;

return;

}

Matrix temp((*this));

getEigen(temp.Array,temp.row);

}

bool Matrix::Is0Matrix()const //矩阵是零矩阵吗

{ return (Is0Array(Array,row,col));

}

void Matrix::displayAll()const//供operator <<(ostream& out,const Matrix& m)调用输出

{ if( row > 0 && col > 0 )

cout << "这是一个" << row << "*" << col << "矩阵.\n" <<"按行输出为:" << endl;

for( int i = 0; i < row; i++)

{

for( int j = 0; j < col; j++)

cout << this->element(i,j) << '\t';

cout << endl;

}

}

class Matrix;

ostream& operator <<(ostream& out, const Matrix& m);

ostream& operator <<(ostream& out, const Matrix& m)

{ cout << "矩阵输出为 " << endl;

int i,j;

for( i = 0; i < m.row; i++ )

{

for( j = 0; j < m.col; j++ )

out << '\t' << m.element(i,j);

out << endl;

}

return out;

}

class Matrix; //友元函数再次声明

istream& operator >>(istream& in, Matrix& m);

istream& operator >>(istream& in, Matrix& m)

{ int i,j;

char p[20];

cout << "按行输入" << m.row << '*' << m.col << "矩阵\n";

for( i = 0; i < m.row; i++ )

for( j = 0; j < m.col; j++ )

{

in >> p;

while(!(chardouble(p)))

{

cout << "输入不是纯数字,请重新输入" << endl;

in >> p;

}

m.element(i,j) = chartodouble(p);

}

return in;

}

void experiment1() //测试函数

{

cout << "\n创建矩阵t1......\n";

char rc[10];

char cc[10];

int r,c;

cout << "行 = ";

cin >> rc;

while( !notfindchar(rc,0) )

{

cerr << "输入不是纯数字,请重新输入:\n";

cout << "行 = ";

cin >> rc;

}

r = chartoint(rc);

cout << "列 = ";

cin >> cc;

while( !notfindchar(cc,0) )

{

cerr << "输入不是纯数字,请重新输入:\n";

cout << "列 = ";

cin >> cc;

}

c = chartoint(cc);

Matrix t(r,c);

cin >> t;

cout << "\n创建矩阵t2.......\n";

char rc1[10];

char cc1[10];

int r1,c1;

cout << "行 = ";

cin >> rc1;

while( !notfindchar(rc1,0) )

{

cerr << "输入不是纯数字,请重新输入:\n";

cout << "行 = ";

cin >> rc1;

}

r1 = chartoint(rc1);

cout << "列 = ";

cin >> cc1;

while( !notfindchar(cc1,0) )

{

cerr << "输入不是纯数字,请重新输入:\n";

cout << "列 = ";

cin >> cc1;

}

c1 = chartoint(cc1);

Matrix t2(r1,c1);

cin >> t2;

bool goout2 = 1;

while(goout2)

{

char *t2case = new char[50];

cout << "\n\n->请选择你想要的操作:\n";

cout << "1. t1+t2\t2. t1-t2\t3. t1*t2\n4. t1==t2?\t5. t1=t2\t6. t1+=t2\n7. t1-=t2\t8. t1*= t2\t9.返回上一级!\n";

cin >> t2case;

switch(t2case[0])

{

case '1': if(t2case[1]=='\0')cout << "t1+t2" << endl <<(t+t2);break;

case '2': if(t2case[1]=='\0')cout << "t1-t2" << endl <<(t-t2);break;

case '3': if(t2case[1]=='\0')cout << "t1*t2" << endl <<(t*t2);break;

case '4': if(t2case[1]=='\0')

{

if(t == t2)

cout << "相等\n";

else

cout << "不相等\n";

}

break;

case '5': if(t2case[1]=='\0'){t = t2; cout << "t1" << t;}break;

case '6': if(t2case[1]=='\0'){t += t2; cout << "t1" << t;}break;

case '7': if(t2case[1]=='\0'){t -= t2; cout << "t1" << t;}break;

case '8': if(t2case[1]=='\0'){t *= t2; cout << "t1" << t;}break;

case '9': if(t2case[1]=='\0')goout2 = 0;break;

default:cout << "\t\t\t\t\t\t提示:要返回上一级菜单请选择9\n";break;

}

}

}

void experiment2()

{

cout << "\n创建矩阵......\n";

char rc[10];

char cc[10];

int r,c;

cout << "行 = ";

cin >> rc;

while( !notfindchar(rc,0) )

{

cerr << "输入不是纯数字,请重新输入:\n";

cout << "行 = ";

cin >> rc;

}

r = chartoint(rc);

cout << "列 = ";

cin >> cc;

while( !notfindchar(cc,0) )

{

cerr << "输入不是纯数字,请重新输入:\n";

cout << "列 = ";

cin >> cc;

}

c = chartoint(cc);

Matrix t(r,c);

cin >> t;

bool goout = 1;

while(goout)

{

cout << "\n\n请选择你想要的操作:\n";

cout << "0. 输出\t\t1. 转置\t\t\t2. 求逆\n3. 求秩\t\t4. 行列式\t\t5. 特征值及特征向量\t6. 返回上一级!\n";

char *tcase = new char[50];

cin >> tcase;

switch(tcase[0])

{

case '0': if(tcase[1]=='\0')cout << "********" << t;

else cout << "提示:要返回上一级请选择6\n";break;

case '1': if(tcase[1]=='\0')cout << "********转置:" << t.transposition();

else cout << "提示:要返回上一级请选择6\n";break;

case '2': if(tcase[1]=='\0')cout << "********逆" << t.inverse();

else cout << "提示:要返回上一级请选择6\n";break;

case '3': if(tcase[1]=='\0')cout << "********秩=\t" << t.rank() << endl;

else cout << "提示:要返回上一级请选择6\n";break;

case '4': if(tcase[1]=='\0')cout << "********行列式=\t" << t.determinant() << endl;

else cout << "提示:要返回上一级请选择6\n";break;

case '5': if(tcase[1]=='\0'){cout << "********";t.Eigenvalue();}//特征值

else cout << "提示:要返回上一级请选择6\n";break;

case '6': if(tcase[1]=='\0')goout = 0; break;

default: cout << "\t\t\t\t\t\t\t提示:返回上级请选择6\n";break;

}

}

}

void experiment()

{

bool goout = 1;

while(goout)

{

cout << "请选择:\n";

cout << "1. 单个矩阵的操作\t\t2. 多个矩阵的操作\t\t3. 退出!\n";

char tc[20];

cin >> tc;

switch(tc[0])

{

case '1': if(tc[1]=='\0')experiment2();

else cout << "提示:要退出请选择3\n\n" << endl; break;

case '2': if(tc[1]=='\0')experiment1();

else cout << "提示:要退出请选择3\n\n" << endl; break;

case '3': if(tc[1]=='\0')goout = 0;break;

default:cout << "\t\t\t\t\t\t\t提示:要退出请选择3\n\n" << endl;break;

}

}

}

int main(int argc, char* argv[]) //MAIN函数

{

experiment();

system("pause");

return 0;

}

评论留言区

:
  

作者: 游客 ; *
评论内容: *
带*号为必填项目

如一模式识别更新提示

matlab在图像处理方面的应用有更新

如一模式识别 友情链接

关于本站作者     chinaw3c     mozilla