VC2010 + win7makefile编译,makefile在源文件头部
double*[N];
A_B=new double*[N];
X=new double*[N];
for(int i=0;i<N;i++)
{
A[i]=new double[N];
B[i]=new double[1];
A_B[i]=new double[N+1];
X[i]=new double[1];
}
cout<<"您选择了"<<N<<"阶方程组计算.\n 请输入系数矩阵 A(按行输入):"<<endl;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
cin>>A[i][j];
}
}
cout<<"请输入矩阵B:"<<endl;
for(int i=0;i<N;i++)
{
cin>>B[i][0];
}
AUB(N,A,B,A_B);
}
//销毁数据
void Destroy_Data()
{
for(int i=0;i<N;i++)
{
delete A[i];
delete B[i];
delete A_B[i];
delete X[i];
}
delete A;
delete B;
delete A_B;
delete X;
}
//求增广矩阵
double** AUB(int N,double **A,double **B,double **A_B)
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
A_B[i][j]=A[i][j];
}
A_B[i][N]=B[i][0];
}
return A_B;
}
//输出矩阵A 的row x col 个元素
void Show_Matrix(double **A,int row,int col)
{
cout<<endl;
for(int i=0;i<row;i++)
{
cout<<"|";
for(int j=0;j<col;j++)
{
cout<<setw(10)<<A[i][j];
}
cout<<" |";
cout<<endl;
}
cout<<endl;
}
//输出最终结果
void Show_Result(int x,int N)
{
if(x==0) // Gauss_Colum_Main_Element返回0,无解
{
cout<<"由于消元过程中出现主元小于所设定的精度,且此时增广矩阵中B!~0\n"
<<"所以:方程无解"<<endl;
return ;
}
if(x==-1) // Gauss_Colum_Main_Element返回-1,为任意解
{
cout<<"方程组为任意解"<<endl;
return ;
}
cout<<"所求方程组的根为:(x1,x2,x3)=( ";
for(int i=0;i<N;i++)
{
cout<<X[i][0]<<" ,";
}
cout<<"\b)."<<endl<<endl;
}
//高斯列主元消去法的主调函数
int Gauss_Colum_Main_Element(
int N, //方程阶数
double **A, //系数矩阵
double**B, //
double e) //精度控制
{
cout<<endl<<"Ax=B的增广矩阵为:"<<endl;
Show_Matrix(A_B,N,N+1);
cout<<"解方程Ax=B的过程如下:"<<endl;
for(int i=0;i<N;i++)
{
int row=Choose_Colum_Main_Element(N,A_B,i);
if(Main_Element<=e) goto A_0;
Exchange(A_B,N+1,row,i);
Elimination(N,A_B,i);
cout<<"选取列主元后第"<<i+1<<"次消元:"<<endl;
Show_Matrix(A_B,N,N+1);
}
Unitization(N,A_B);
cout<<"单位化之后的结果:"<<endl;
Show_Matrix(A_B,N,N+1);
for(int i=0;i<N;i++)
{
X[i][0]=A_B[i][N];
}
return 1;
A_0: //如果出现增广矩阵中|A|~0 的情况处理如下
f
or(int i=0;i<N;i++)
{
//增广矩阵中|B|!~0
if(abs(A_B[i][N])>e) return 0;
}
//增广矩阵中|B|~0,即方程解为任意解的情况
return -1;
}
//选取主