/*注意:CM == Coefficient_Matrix;ACM == Augmented_Coefficient_Matrix*/
/*re == row_element;ce == column_element*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#define CM 1
#define ACM 0
void Matrix_Simplify(void);
unsigned char Matrix_GetRank(unsigned char type);
char unknows_name[100];
char string_backup[100];
char *string;
float matrix[100][100];
unsigned char re=0,ce=0;
unsigned int row,column;
main(void)
{
unsigned char a,b,c,d,rank_cm,rank_acm;
int the_amount_of_unknows=0;
printf("Please enter the unknown quantity in the equations(The two unknowns are separated by commas)\n");
/*除了换行符其他字符都读入*/
scanf("%[^\n]",unknows_name);
strcpy(string_backup,unknows_name);
for(a=0;a<100;a++)
{
if(unknows_name[a]==',')
the_amount_of_unknows++;
else if(unknows_name[a]=='\0')
break;
}
the_amount_of_unknows++;
column=the_amount_of_unknows+1;
printf("Please enter the number of equations:\n");
scanf("%d",&row);
printf("\n Please enter the augmented coefficient matrix of the equations:\n");
for(a=0;a<row;a++)
{
printf("Please enter the elements on line %d, separated by spaces.\n",a+1);
for(b=0;b<column;b++)
{
scanf("%f",&matrix[a][b]);
}
}
printf("\n the augmented coefficient matrix of the equations:\n\n");
for(a=0;a<row;a++)
{
for(b=0;b<column;b++)
printf("%8.4g",matrix[a][b]);
printf("\n\n");
}
/*把增广矩阵化为行最简型*/
for(ce=0;ce<column-1;)
{
if(matrix[re][ce]==0)
{
/*把aii的值都转化为非0值(寻找该列自aii以下首个非零元所在的行与第i行互换)*/
for(a=re;a<row;a++)
{
if(matrix[a][ce]!=0)
break;
}
/*如果aii下元素都为0,则此列不做处理,进行下一列的转化*/
if(a==row)
ce++;
else
{
for(b=0;b<column;b++)
{
matrix[99][b]=matrix[a][b];
matrix[a][b]=matrix[re][b];
matrix[re][b]=matrix[99][b];
}
Matrix_Simplify();
}
}
else
{
Matrix_Simplify();
}
}
/*输出增广矩阵的行最简型形式*/
printf("Row simplest matrix of augmented coefficient matrix:\n\n");
for(a=0;a<row;a++)
{
for(b=0;b<column;b++)
{
if(fabs(matrix[a][b])<0.0001)
matrix[a][b]=0;
printf("%8.4g",matrix[a][b]);
}
printf("\n\n");
}
/*判断增广矩阵解的个数与求解的过程*/
rank_cm=Matrix_GetRank(CM);
rank_acm=Matrix_GetRank(ACM);
if(rank_cm!=rank_acm)
printf("The equations have no solution!");
/*非齐次方程组有解的充分必要条件:系数矩阵 CM 与增广矩阵 ACM 的秩相等。*/
else if(rank_cm==rank_acm)
{
/*未知量的个数等于增广矩阵的秩时有唯一解*/
if(the_amount_of_unknows==rank_acm)
{
printf("The equations have unique solutions!\n");
/*此处利用strtok函数按','标识符分割未知量*/
string=strtok(unknows_name,",");
printf("%s=%g\n",string,matrix[0][column-1]);
for(a=1;a<the_amount_of_unknows;a++)
{
string=strtok(NULL,",");
printf("%s=%g\n",string,matrix[a][column-1]);
}
}
/*未知量的个数大于增广矩阵的秩时有无穷多解*/
else if(the_amount_of_unknows>rank_acm)
{
printf("The equations have infinite solutions!\n");
re=0;
ce=0;
c=1;
d=0;
while(ce<the_amount_of_unknows)
{
if(matrix[re][ce]==0)
{
/*行最简型第i行及以下都为0,则X(i<a<the_amount_of_unknows)都为任意常数*/
strcpy(unknows_name,string_backup);
string=strtok(unknows_name,",");
/*数组函数'strtok'多次应用,用来间隔输出未知量*/
for(a=1;a<=ce;a++)
string=strtok(NULL,",");
printf("%s=c%d\n",string,c);
ce++;
c++;
}
else
{
/*正常情况matrix[i][i] = 1,Xi是X(i<a<the_amount_of_unknows)的函数*/
strcpy(unknows_name,string_backup);
string=strtok(unknows_name,",");
for(a=1;a<=ce;a++)
string=strtok(NULL,",");
printf("%s=",string);
for(a=ce+1;a<column-1;a++)
{
if(matrix[re][a]!=0)
{
/*只要该行该列不等于0,则都*(-1)作为Xi的表达因子;等于0则不进行操作。*/
strcpy(unknows_name,string_backup);
string=strtok(unknows_name,",");
for(b=1;b<=a;b++)
string=strtok(NULL,",");
if(!d)
{
if(matrix[re][a]==1)
{
printf("-%s",string);
d=1;
}
else if(matrix[re][a]==-1)
{
printf("%s",string);
d=1;
}
else
{
printf("%g%s",-matrix[re][a],string);
d=1;
}
}
/*继续添加表达因子*/
else
{
if(matrix[re][a]==1)
printf("-%s",string);
else if(matrix[re][a]==-1)
printf("+%s",string);
else
printf("%+g%s",-matrix[re][a],string);
}
}
}
/*加上每行最后的常数项*/
if(matrix[re][column-1])
printf("%+g\n",matrix[re][column-1]);
else
printf("\n");
re++;
ce++;
d=0;
}
}
}
}
}
/*对增广矩阵转化为行最简型矩阵的过程*/
void Matrix_Simplify(void)
{
unsigned int i,j;
float n;
/*使元素matrix[i][i]都化为1(第i行同乘1/matrix[i][i])*/
n=1/matrix[re][ce];
for(j=0;j<column;j++)
{
matrix[re][j]=n*matrix[re][j];
}
/*把非零行的首个元素1所在的列中位于1之上的元素都化为0*/
for(i=0;i<re;i++)
{
n=-matrix[i][ce]/matrix[re][ce];
for(j=0;j<column;j++)
{
matrix[i][j]=matrix[i][j]+n*matrix[re][j];
}
}
/*把非零行的首个元素1所在的列中位于1之下的元素都化为0*/
for(i=re+1;i<row;i++)
{
n=-matrix[i][ce]/matrix[re][ce];
for(j=0;j<column;j++)
{
matrix[i][j]=matrix[i][j]+n*matrix[re][j];
}
}
re++;
ce++;
}
/*计算系数矩阵、增广矩阵的秩*/
unsigned char Matrix_GetRank(unsigned char type)
{
unsigned char i,j;
/*最终i的值是非零行的行数,即为秩*/
for(i=0;i<row;i++)
{
for(j=0;j<column-type;j++)
{
if(matrix[i][j]!=0)
break;
}
if(j==column-type)
break;
}
return i;
}
比如下面的例子: