跪求数据结构某习题答案!高手进啊!!!

2024-12-17 12:05:02
推荐回答(1个)
回答1:

#include
#include
#include

#define OK 1
#define ERROR 0
#define MAXSIZE 12500
typedef int Status;
typedef int ElemType;

typedef struct
{
int i,j;
ElemType e;
}Triple;
typedef struct
{
Triple date[MAXSIZE+1];
int mu,nu,tu;
}TSMatrix;

Status CreatSMatrix( TSMatrix *M)
//建立三元组
{
int row,col,date,k;
printf("请输入行数列数和非零元个数\n");
scanf("%d,%d,%d",&(*M).mu,&(*M).nu,&(*M).tu);
while( M->mu <= 0 || M->nu <= 0 || M->tu > ( M->mu * M->nu ) || M->tu > MAXSIZE)
{
printf("输入不正确,请重新输入\n");
fflush(stdin);
scanf("%d,%d,%d",&M->mu,&M->nu,&M->tu);
}
(*M).date[0].i = 0;
for( k = 1; k <= (*M).tu ; k++)
{
printf("Please input the row,col and date\n");
scanf("%d,%d,%d",&row,&col,&date);
M->date[k].i = row;
M->date[k].j = col;
M->date[k].e = date;
}
printf("输入非空元素组成的三元组完毕!\n");
return OK;
}

Status comp( int a, int b)
//比较两个数字的大小AddSmatrix函数使用
{
int i;
if( a < b)
i = 1;
if( a == b)
i = 0;
if( a > b)
i = -1;
return i;
}

Status AddSMatrix( TSMatrix &A, TSMatrix &B, TSMatrix *C)
//矩阵的相加
{
Triple *Ap,*Bp,*Ae,*Be,*Ch,*Ce;
if( A.mu != B.mu || A.nu != B.nu)
{
printf("\nA and B is not compared\n");
return ERROR;
}
// if( (*C).date )
// free( (*C).date );

C->mu = A.mu;
C->nu = A.nu;

Ap = &A.date[1];
Bp = &B.date[1];
Ae = &A.date[A.tu];
Be = &B.date[B.tu];
Ch = Ce = C->date;
while( Ap <= Ae && Bp <= Be)
{
Ce++;
switch( comp( Ap->i, Bp->i ) )
{
case 1:
{
*Ce = *Ap;
Ap++;
}break;
case -1:
{
*Ce = *Bp;
Bp++;
}break;
case 0:
{
switch( comp(Ap->j,Bp->j) )
{
case 0:
{
*Ce = *Ap;
Ce->e += Bp->e;
if( !Ce->e )
Ce--;
Ap++;
Bp++;
}break;
case 1:
{
*Ce = *Ap;
Ap++;
}break;
case -1:
{
*Ce = *Bp;
Bp++;
}
}
}break;
}
}
if( Ap > Ae)
while( Bp <= Be )
{
Ce++;
*Ce = *Bp;
Bp++;
}
if( Bp > Be)
while( Ap <= Ae)
{
Ce++;
*Ce = *Ap;
Ap++;
}
C->tu = Ce - Ch;
return OK;
}

Status Transpose( TSMatrix M, TSMatrix &T)
//采用三元组表存储表示,求稀疏矩阵M的转置矩阵T
{
int k;
T.mu = M.nu;
T.nu = M.mu;
T.tu = M.tu;
if( T.tu )
{
for( k = 1; k <= M.tu; k++)
{
T.date[k].i = M.date[k].j;
T.date[k].j = M.date[k].i;
T.date[k].e = M.date[k].e;
}
}
printf("\n矩阵转置完毕!");
return OK;
}

Status CopySMatrix( TSMatrix M, TSMatrix &T)
//矩阵的复制
{
int k;
if( M.tu )
{
for( k = 1; k <= M.tu; k++)
{
T.date[k].i = M.date[k].i;
T.date[k].j = M.date[k].j;
T.date[k].e = M.date[k].e;
}
}
printf("复制完毕!");
return OK;
}

Status DestroySMatrix( TSMatrix &M)
//销毁稀疏矩阵的三元组顺序表
{
if( M.mu <= 0 || M.nu <= 0 || M.tu > M.mu * M.nu)
{
printf("\n不存在矩阵!\n");
return ERROR;
}
for( int k = 1; k < M.tu + 1; k++)
{
M.date[k].i = 0;
M.date[k].j = 0;
M.date[k].e = 0;
}
M.mu = 0;
M.nu = 0;
M.tu = 0;
printf("\n销毁完毕!\n");
return OK;
}

Status PrintSMatrix( TSMatrix M)
//显示稀疏矩阵的三元组顺序表
{
int k;
if( M.mu <= 0 && M.nu <= 0)
{
printf("\n稀疏矩阵的三元组顺序表不存在\n");
return ERROR;
}
else
{
printf("稀疏矩阵的三元组顺序表是:\n");
for( k = 1; k <= M.tu; k++)
{
printf(" (%3d%3d%3d) ",M.date[k].i,M.date[k].j,M.date[k].e);
}
printf("\n显示完毕!");
return OK;
}
}

void main()
{
TSMatrix M, T, A, B, C;

CreatSMatrix( &M );
PrintSMatrix( M );
Transpose( M, T );
printf("\n转置后的矩阵是:\n");
PrintSMatrix( T );
printf("\n矩阵的复制\n");
CopySMatrix( M, T );
printf("\n\n\n复制的矩阵为\n");
PrintSMatrix( T );

printf("\n\n\n");
printf("建立矩阵A");
CreatSMatrix( &A );
printf("\n建立矩阵B");
CreatSMatrix( &B );
printf("\nA+B\n");
AddSMatrix( A, B, &C );
printf("\n矩阵M和B相加为:\n");
PrintSMatrix( C );

DestroySMatrix( M );
DestroySMatrix( T );
DestroySMatrix( B );
DestroySMatrix( C );
// PrintSMatrix( M );

printf("\n\n\n\t\t\t程序完毕!\n\n\n");
}