📄 经典转置.txt
字号:
/*---------------------经典转置矩阵-------------------*/
#include <stdio.h>
#include <stdlib.h>
#define H 6 /* 行 */
#define L 7 /* 列 */
#define MAXSIZE 1250 /*非零个数的最大值*/
int M[H][L]={
{0,12,9,0,0,0,0},
{0,0,0,0,0,0,0},
{-3,0,0,0,0,14,0},
{0,0,24,0,0,0,0},
{0,18,0,0,0,0,0},
{15,0,0,-7,0,0,0}};
struct Triple
{
int i,j;
int e;
};
struct TSMatrix
{
struct Triple data[MAXSIZE+1];
int mu,nu,tu;
}; /* 稀疏矩阵的三元组顺序表存储表示 */
void createTSMatrix(struct TSMatrix *pt) /* 稀疏矩阵转为三元组存储 */
{ int wid,col,n=1;
for(wid=0;wid<H;wid++)
for(col=0;col<L;col++)
if(M[wid][col]&&n<=MAXSIZE) {
pt->data[n].i=wid+1;
pt->data[n].j=col+1;
pt->data[n].e=M[wid][col];
n++;
}
pt->mu=H;
pt->nu=L;
pt->tu=--n;
}
int transposeSMatrix(struct TSMatrix *M,struct TSMatrix *T) /* transpose M to T */
{ int p,q,col;
T->mu=M->mu;
T->nu=M->nu;
T->tu=M->tu;
if(T->tu) {
q=1;
for(col=1;col<=M->nu;col++)
for(p=1;p<=M->tu;p++)
if(M->data[p].j==col) {
T->data[q].i=M->data[p].j;
T->data[q].j=M->data[p].i;
T->data[q].e=M->data[p].e;
q++;
}
return 1; /* ok! */
}
else return 0; /* NULL */
}
void main()
{ int n;
struct TSMatrix *a=(struct TSMatrix *)malloc(sizeof(struct TSMatrix));
struct TSMatrix *b=(struct TSMatrix *)malloc(sizeof(struct TSMatrix));
createTSMatrix(a);
if(transposeSMatrix(a,b)) {
printf("\na is :\n"); /* PRINT MATRIX A */
printf(" i j v\n");
printf(" ---------\n");
if(a->tu)
for(n=1;n<=a->tu;n++)
printf("%4d%4d%4d\n",a->data[n].i,a->data[n].j,a->data[n].e);
printf("\na's transposematrix b is :\n"); /* PRINT MATRIX A */
printf(" i j v\n");
printf(" ---------\n");
if(b->tu)
for(n=1;n<=b->tu;n++)
printf("%4d%4d%4d\n",b->data[n].i,b->data[n].j,b->data[n].e);
}
else printf("There's no element in the matrix!");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -