老师要求用带行逻辑信息的三元组,加减乘,转制还好说,拿三元组搞还行,可求逆就不行了,没办法,搞了两种求逆的方法。一种是通过求伴随矩阵来搞另一种就是通过与单位矩阵结合,基本变换来求。不说了,把这几天的劳动成果附上。
/***********************建了个matrix.h头文件*************************/
#ifndef MATRIX_H_INCLUDED// 调用时需加stdio.h 和stdlib.h
#define MATRIX_H_INCLUDED
const int maxn = 1e3+100;
typedef double datatype ;
typedef struct node
{
int i, j;
datatype val;
} node;
typedef struct spmartix
{
node data[maxn];
int H[maxn]; //第i行起始位置
int mu, nu, tu; //稀疏矩阵行和列
} spmatrix;
typedef struct matrix
{
datatype S[maxn][maxn];
int row , col;
}matrix;
matrix *minit()
{
matrix *M = ( matrix *) malloc ( sizeof(matrix));
memset( M->S, 0, sizeof(M->S) );
M->row=0;
M->col=0;
return M;
}
spmatrix *init()
{
spmatrix *A = ( spmatrix * )malloc( sizeof ( spmatrix ) );
memset(A->H,-1,sizeof(A->H));
A->mu = 0;
A->nu = 0;
A->tu = 0;
return A;
}
void create( spmatrix *A )
{
printf( "是否文件输入 ? y:文件输入 n:手动输入 ");
char ans[20];
scanf("%s",ans);
if( ans[0] == 'y' || ans[0] == 'Y' )
{
FILE *fp;
printf("请输入文件名: ");
char file_name[100];
scanf( "%s", file_name );
fp = fopen( file_name, "r" );
if( !fp )
{
printf( " 打开文件失败\n" );
return ;
}
else
{
int num = 0;
fscanf( fp,"%d%d", &A->mu,&A->nu);
while( !feof(fp) )
{
fscanf(fp,"%d%d%lf", &A->data[num].i, &A->data[num].j, &A->data[num].val);
if( num == 0 || A->data[num].i != A->data[num-1].i )
{
A->H[A->data[num].i] = num;
}
num++;
}
A->tu = num;
}
fclose( fp );
return ;
}
else
{
printf( "请输入行和列: " );
scanf( "%d%d" , &A->mu, &A->nu );
printf("请输入非零元素的个数: ");
scanf( "%d" , &A->tu);
int num = 0;
int i, j;
int last=0,b;
for( i = 0; i < A->tu ;i++)
{
scanf( "%d%d%lf", &A->data[i].i, &A->data[i].j, &A->data[i].val );
if( i == 0 || A->data[i].i != A->data[i-1].i )
{
A->H[A->data[i].i] = i;
}
}
}
}
void print( spmatrix *A )
{
int i,j,k;
int num = 0 ;
if( A->mu == 0 && A->nu == 0)
{
printf(" 处理失败\n");
return ;
}
for( i = 1; i <=A->mu ; i++ )
{
for( j = 1 ; j <= A->nu ; j++)
{
if( j != 1) printf(" ");
if( num != A->tu && i == A->data[num].i && j == A->data[num].j)
{
printf( "%5.0lf", A->data[num].val);
num++;
}
else putchar( '0' );
}
printf("\n");
}
}
void display(matrix *A)
{
for( int i = 1; i <= A->row ; i++ )
{
for(int j = 1; j <= A->col ; j++)
{
if( j != 1)
{
printf(" ");
}
printf("%5.2lf",A->S[i][j]);
}
printf("\n");
}
}
spmatrix *union_1( spmatrix *A ,spmatrix *B, int c )
{
if( A->mu != B->mu || A->nu != B->nu )
{
printf( "处理失败\n" );
return NULL;
}
spmatrix *C=init();
int num = 0;
int a, b;
int i, j;
a = 0;
b = 0;
while( a < A-&