点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int row;
- int col;
- int e;
- }Triplet;
- typedef struct {
- Triplet data[10];
- int rows;
- int cols;
- int nums;
- }Table;
- void Create (Table *t,int *Matrix,int m,int n)
- {
- int i,j;
- /*
- for (i=0;i<3;i++)
- {
- for (j=0;j<4;j++)
- printf ("%d ",*(Matrix+i*n+j));
- printf ("\n");
- }
- getch ();
- */
- t->rows=m;
- t->cols=n;
- t->nums=0;
- for (i=0;i<m;i++)
- for (j=0;j<n;j++)
- if (*(Matrix+i*n+j)!=0)
- {
- t->nums++;
- t->data[t->nums].row=i+1;
- t->data[t->nums].col=j+1;
- t->data[t->nums].e=*(Matrix+i*n+j);
- }
- }
- void Trans (Table M,Table *T)
- {
- int col,t,p,q;
- int num[10];
- int cpot[10];
- T->rows=M.cols;
- T->cols=M.rows;
- T->nums=M.nums;
- if (T->nums)
- {
- for (col=1;col<=M.cols;++col)
- num[col]=0;
- for (t=1;t<=M.nums;++t)
- ++num[M.data[t].col];
- cpot[1]=1;
- for (col=2;col<=M.cols;++col)
- cpot[col]=cpot[col-1]+num[col-1];
- for (p=1;p<=M.nums;++p)
- {
- col=M.data[p].col;
- q=cpot[col];
- T->data[q].row=M.data[p].col;
- T->data[q].col=M.data[p].row;
- T->data[q].e=M.data[p].e;
- ++cpot[col];
- }
- }
- }
- void ToMatrix(Table t,int *Matrix,int m,int n)
- {
- int i,j;
- for (i=0;i<m;i++)
- for (j=0;j<n;j++)
- *(Matrix+i*n+j)=0;
- for (i=1;i<=t.nums;i++)
- {
- *(Matrix+(t.data[i].row-1)*n+(t.data[i].col-1))=t.data[i].e;
- }
- }
- int main ()
- {
- int i,j;
- Table A,TA;
- int MatrixA[3][4] =
- {{3,0,0,5},
- {0,-1,0,0},
- {0,2,0,0}};
- int MatrixB[4][3];
- Create (&A,MatrixA,3,4);
- Trans (A, &TA);
- ToMatrix (TA,MatrixB,4,3);
- for (i=0;i<4;i++)
- {
- for (j=0;j<3;j++)
- printf ("%d ",MatrixB[i][j]);
- printf ("\n");
- }
- }
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int row;
- int col;
- int e;
- }Triplet;
- typedef struct {
- Triplet data[10];
- int rows;
- int cols;
- int nums;
- }Table;
- void Create (Table *t,int *Matrix,int m,int n)
- {
- int i,j;
- /*
- for (i=0;i<3;i++)
- {
- for (j=0;j<4;j++)
- printf ("%d ",*(Matrix+i*n+j));
- printf ("\n");
- }
- getch ();
- */
- t->rows=m;
- t->cols=n;
- t->nums=0;
- for (i=0;i<m;i++)
- for (j=0;j<n;j++)
- if (*(Matrix+i*n+j)!=0)
- {
- t->nums++;
- t->data[t->nums].row=i+1;
- t->data[t->nums].col=j+1;
- t->data[t->nums].e=*(Matrix+i*n+j);
- }
- }
- void Trans (Table M,Table *T)
- {
- int col,t,p,q;
- int num[10];
- int cpot[10];
- T->rows=M.cols;
- T->cols=M.rows;
- T->nums=M.nums;
- if (T->nums)
- {
- for (col=1;col<=M.cols;++col)
- num[col]=0;
- for (t=1;t<=M.nums;++t)
- ++num[M.data[t].col];
- cpot[1]=1;
- for (col=2;col<=M.cols;++col)
- cpot[col]=cpot[col-1]+num[col-1];
- for (p=1;p<=M.nums;++p)
- {
- col=M.data[p].col;
- q=cpot[col];
- T->data[q].row=M.data[p].col;
- T->data[q].col=M.data[p].row;
- T->data[q].e=M.data[p].e;
- ++cpot[col];
- }
- }
- }
- void MultSMatrix (Table M,Table N,Table *Q)
- {
- int arow,brow,tp,p,q,t,ccol,row,i;
- int ctemp[10];
- int rpos[10];
- int num[10];
- int Mrpos[10];
- int Nrpos[10];
- if (M.cols!=N.rows)
- exit (1);
- Q->rows=M.rows;
- Q->cols=N.cols;
- Q->nums=0;
- for (row=1;row<=M.rows;++row)
- num[row]=0;
- for (t=1;t<=M.nums;++t)
- ++num[M.data[t].row];
- Mrpos[1]=1;
- for (row=2;row<=M.rows;++row)
- Mrpos[row]=Mrpos[row-1]+num[row-1];
- for (row=1;row<=N.rows;++row)
- num[row]=0;
- for (t=1;t<=N.nums;++t)
- ++num[N.data[t].row];
- Nrpos[1]=1;
- for (row=2;row<=N.rows;++row)
- Nrpos[row]=Nrpos[row-1]+num[row-1];
- if (M.nums*N.nums!=0)
- {
- for (arow=1;arow<=M.rows;++arow)
- {
- for (i=0;i<10;i++)
- ctemp[i]=0;
- rpos[arow]=Q->nums+1;
- if (arow<M.rows)
- tp=Mrpos[arow+1];
- else
- tp=M.nums+1;
- for (p=Mrpos[arow];p<tp;++p)
- {
- brow=M.data[p].col;
- if (brow<N.rows)
- t=Nrpos[brow+1];
- else
- t=N.nums+1;
- for (q=Nrpos[brow];q<t;++q)
- {
- ccol=N.data[q].col;
- ctemp[ccol]=ctemp[ccol]+M.data[p].e*N.data[q].e;
- }
- }
- for (ccol=1;ccol<=Q->cols;++ccol)
- if (ctemp[ccol])
- {
- Q->nums++;
- Q->data[Q->nums].row=arow;
- Q->data[Q->nums].col=ccol;
- Q->data[Q->nums].e=ctemp[ccol];
- }
- }
- }
- }
- void ToMatrix(Table t,int *Matrix,int m,int n)
- {
- int i,j;
- for (i=0;i<m;i++)
- for (j=0;j<n;j++)
- *(Matrix+i*n+j)=0;
- for (i=1;i<=t.nums;i++)
- {
- *(Matrix+(t.data[i].row-1)*n+(t.data[i].col-1))=t.data[i].e;
- }
- }
- int main ()
- {
- int i,j;
- Table A,B,AB;
- int MatrixA[3][2] =
- {{2,3},
- {0,4},
- {1,2}};
- int MatrixB[2][3] =
- {{2,5,1},
- {0,2,4}};
- int MatrixAB[3][3];
- Create (&A,MatrixA,3,2);
- Create (&B,MatrixB,2,3);
- MultSMatrix (A,B,&AB);
- ToMatrix (AB,MatrixAB,3,3);
- for (i=0;i<3;i++)
- {
- for (j=0;j<3;j++)
- printf ("%d ",MatrixAB[i][j]);
- printf ("\n");
- }
- getchar ();
- }
相关热门文章
给主人留下些什么吧!~~
评论热议