#include "stdafx.h"
#include "全局函數(shù).h"
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cmath>
#define eps (1e-06)
#define KMAX 60 //最大迭代次數(shù)
#define defaultMAXCHAR = 50;//字符數(shù)組大小
using namespace std;
void experiment1(); //全局函數(shù):測試函數(shù)1
void experiment2();//全局函數(shù):測試函數(shù)2
void experiment();//全局函數(shù):測試函數(shù)
bool chardouble(const char *p);//全局函數(shù):判斷字符是否合格
double chartodouble(const char* p);//全局函數(shù):字符轉(zhuǎn)雙精度型
class Matrix; //友元函數(shù)聲明
ostream& operator <<(ostream& out, const Matrix& m);
class Matrix; //友元函數(shù)聲明
istream& operator >>(istream& in, Matrix& m);
class Matrix // Matrix類定義
{
private:
int row,col;
double *Array;
public:
Matrix(int r=1,int c=1) //默認(rèn)構(gòu)造函數(shù)
{
if( r <= 0 || c <= 0 )
{ cerr << "行列值存在0或負(fù)數(shù)!" << 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); //拷貝構(gòu)造函數(shù)
Matrix(double array[], int mrow, int mcol); //構(gòu)造函數(shù),參數(shù)為數(shù)組表示的矩陣
~Matrix() { delete []Array; } //析構(gòu)函數(shù)
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; //矩陣轉(zhuǎn)置
double determinant()const; //矩陣求行列式
Matrix inverse()const; //矩陣求逆
int rank()const; //矩陣求秩
void Eigenvalue(); //矩陣求特征值及特征向量
bool Is0Matrix()const; //矩陣是零矩陣嗎
void displayAll()const; //供operator <<(ostream& out,const Matrix& m)調(diào)用輸出
friend istream& operator >>(istream& in, Matrix& m); //重載操作符>>
friend ostream& operator <<(ostream& out, const Matrix& m); //重載操作符<<
};
Matrix::Matrix(const Matrix& m)//拷貝構(gòu)造函數(shù)
{ 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)//構(gòu)造函數(shù),參數(shù)為數(shù)組表示的矩陣
{
if( mrow <= 0 || mcol <= 0 )
{ cerr << "非法數(shù)組參數(shù)! " << 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 << "非法參數(shù)! " << 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 << "原行數(shù)" << row << "不等于" << "新行數(shù) " << m.row << ',';
else
cout << "原列數(shù)" << col << "不等于" << "新列數(shù)" << 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 << "原行數(shù)" << row << "不等于" << "新行數(shù) " << m.row << ',';
else
cout << "原列數(shù)" << col << "不等于" << "新列數(shù)" << 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 << "原列數(shù)" << col << "不等于新行數(shù)" << 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 << "原矩陣列數(shù)" << col << "不等于新矩陣行數(shù)" << 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 //矩陣轉(zhuǎn)置
{ 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)調(diào)用輸出
{ if( row > 0 && col > 0 )
cout << "這是一個(gè)" << 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; //友元函數(shù)再次聲明
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 << "輸入不是純數(shù)字,請重新輸入" << endl;
in >> p;
}
m.element(i,j) = chartodouble(p);
}
return in;
}
void experiment1() //測試函數(shù)
{
cout << "\n創(chuàng)建矩陣t1......\n";
char rc[10];
char cc[10];
int r,c;
cout << "行 = ";
cin >> rc;
while( !notfindchar(rc,0) )
{
cerr << "輸入不是純數(shù)字,請重新輸入:\n";
cout << "行 = ";
cin >> rc;
}
r = chartoint(rc);
cout << "列 = ";
cin >> cc;
while( !notfindchar(cc,0) )
{
cerr << "輸入不是純數(shù)字,請重新輸入:\n";
cout << "列 = ";
cin >> cc;
}
c = chartoint(cc);
Matrix t(r,c);
cin >> t;
cout << "\n創(chuàng)建矩陣t2.......\n";
char rc1[10];
char cc1[10];
int r1,c1;
cout << "行 = ";
cin >> rc1;
while( !notfindchar(rc1,0) )
{
cerr << "輸入不是純數(shù)字,請重新輸入:\n";
cout << "行 = ";
cin >> rc1;
}
r1 = chartoint(rc1);
cout << "列 = ";
cin >> cc1;
while( !notfindchar(cc1,0) )
{
cerr << "輸入不是純數(shù)字,請重新輸入:\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創(chuàng)建矩陣......\n";
char rc[10];
char cc[10];
int r,c;
cout << "行 = ";
cin >> rc;
while( !notfindchar(rc,0) )
{
cerr << "輸入不是純數(shù)字,請重新輸入:\n";
cout << "行 = ";
cin >> rc;
}
r = chartoint(rc);
cout << "列 = ";
cin >> cc;
while( !notfindchar(cc,0) )
{
cerr << "輸入不是純數(shù)字,請重新輸入:\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. 轉(zhuǎn)置\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 << "********轉(zhuǎn)置:" << 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. 單個(gè)矩陣的操作\t\t2. 多個(gè)矩陣的操作\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函數(shù)
{
experiment();
system("pause");
return 0;
}
#include <iostream>
#include <iomanip>
#include <cmath>
#define eps (1e-06)
#define KMAX 60 //最大迭代次數(shù)
using namespace std;
int cifang(int i,int j); //全局函數(shù):求-1的(i+j)次方
int chartoint(const char* p);//字符轉(zhuǎn)為整數(shù)型
int getRank(double *Array,int row,int col);//全局函數(shù):求秩
int IsZero(double x){ return fabs(x) <= 0.00001?1:0;} //全局函數(shù):判斷是否為零
int jacobieigen(double *a,double *u,int jie);//a 和u 都是jie*jie的方陣
void getEigen(double *Array,int dim);//全局函數(shù):獲得特征值及特征向量。Array是dim*dim方陣
void show(double array[],int jie); //全局函數(shù):顯示矩陣
void transpose(double array[],int jie); //全局函數(shù):轉(zhuǎn)置
bool IsSymmetricMatrix(double *Array, int dim);//判斷是對稱矩陣
bool notfindchar(const char* p,int index);
bool Is0Array( double *Array,int row,int col); //全局函數(shù):判斷是否是0矩陣
bool chardouble(const char *p);//全局函數(shù):判斷字符是否滿足轉(zhuǎn)換要求
double det(double array[],int Jie); //全局函數(shù):求行列式
double chartodouble(const char* p);//全局函數(shù):字符型轉(zhuǎn)double型
double* doInverse(double Array[],int row);//全局函數(shù):求逆
int cifang(int i,int j) //全局函數(shù):求-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)//字符轉(zhuǎn)為整數(shù)型
{
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) //全局函數(shù):求秩
{
int i,j,k,l,i1,j1,main_row,main_col,rank;
double main_element,temp;
for( i = 0; i < row; i++ ) //行循環(huán)
{
main_element = *(Array + i*col + i );//對角元素
main_row = i;//主元坐標(biāo):主對角元(i,i)
main_col=i;
for( j = i; j < row; j++ ) //循環(huán):尋找當(dāng)前(i,i)元之后的最大元,并記為坐標(biāo)(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++ ) //產(chǎn)生單位矩陣
{
(*(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 ) //超過最大迭代次數(shù)返回
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)); //計(jì)算矩陣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)//全局函數(shù):獲得特征值及特征向量。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 << "對應(yīng)的特征向量\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) //全局函數(shù):轉(zhuǎn)置
{
if( jie <= 0){cerr<< "數(shù)組非法!" << 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) //全局函數(shù):方陣輸出
{
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 << "參數(shù)非法,退出!" << 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)//全局函數(shù):判斷是否是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) //全局函數(shù):求行列式
{
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)//全局函數(shù):求逆
{
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]; //創(chuàng)建臨時(shí)數(shù)組,賦值后并返回
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); //調(diào)用轉(zhuǎn)置后的伴隨
return tempArray;
}
bool chardouble(const char *p)
{
if( p == NULL )return true;//風(fēng)險(xiǎn)?
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;
}