Chinaunix首页 | 论坛 | 博客
  • 博客访问: 44854
  • 博文数量: 44
  • 博客积分: 50
  • 博客等级: 民兵
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-16 06:05
文章分类

全部博文(44)

文章存档

2012年(32)

2011年(12)

我的朋友

分类:

2012-06-22 00:46:16


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct {
  4.     int row;
  5.     int col;
  6.     int e;
  7. }Triplet;
  8. typedef struct {
  9.     Triplet data[10];
  10.     int rows;
  11.     int cols;
  12.     int nums;
  13. }Table;

  14. void Create (Table *t,int *Matrix,int m,int n)
  15. {
  16.     int i,j;
  17.     /*
  18.     for (i=0;i<3;i++)
  19.     {
  20.     for (j=0;j<4;j++)
  21.             printf ("%d ",*(Matrix+i*n+j));
  22.         printf ("\n");
  23.     }
  24.     getch ();
  25.     */
  26.     t->rows=m;
  27.     t->cols=n;
  28.     t->nums=0;
  29.     for (i=0;i<m;i++)
  30.         for (j=0;j<n;j++)
  31.             if (*(Matrix+i*n+j)!=0)
  32.             {
  33.                 t->nums++;
  34.                 t->data[t->nums].row=i+1;
  35.                 t->data[t->nums].col=j+1;
  36.                 t->data[t->nums].e=*(Matrix+i*n+j);
  37.             }
  38. }
  39. void Trans (Table M,Table *T)
  40. {
  41.     int col,t,p,q;
  42.     int num[10];
  43.     int cpot[10];
  44.     T->rows=M.cols;
  45.     T->cols=M.rows;
  46.     T->nums=M.nums;
  47.     if (T->nums)
  48.     {
  49.         for (col=1;col<=M.cols;++col)
  50.             num[col]=0;
  51.         for (t=1;t<=M.nums;++t)
  52.             ++num[M.data[t].col];
  53.         cpot[1]=1;
  54.         for (col=2;col<=M.cols;++col)
  55.             cpot[col]=cpot[col-1]+num[col-1];
  56.         for (p=1;p<=M.nums;++p)
  57.         {
  58.             col=M.data[p].col;
  59.             q=cpot[col];
  60.             T->data[q].row=M.data[p].col;
  61.             T->data[q].col=M.data[p].row;
  62.             T->data[q].e=M.data[p].e;
  63.             ++cpot[col];
  64.         }
  65.     }

  66. }
  67. void ToMatrix(Table t,int *Matrix,int m,int n)
  68. {
  69.     int i,j;
  70.     for (i=0;i<m;i++)
  71.         for (j=0;j<n;j++)
  72.             *(Matrix+i*n+j)=0;
  73.     for (i=1;i<=t.nums;i++)
  74.     {
  75.         *(Matrix+(t.data[i].row-1)*n+(t.data[i].col-1))=t.data[i].e;
  76.     }
  77. }
  78. int main ()
  79. {
  80.     int i,j;
  81.     Table A,TA;
  82.     int MatrixA[3][4] =
  83.         {{3,0,0,5},
  84.          {0,-1,0,0},
  85.          {0,2,0,0}};
  86.     int MatrixB[4][3];
  87.     Create (&A,MatrixA,3,4);
  88.     Trans (A, &TA);
  89.     ToMatrix (TA,MatrixB,4,3);
  90.     for (i=0;i<4;i++)
  91.     {
  92.         for (j=0;j<3;j++)
  93.             printf ("%d ",MatrixB[i][j]);
  94.         printf ("\n");
  95.     }
  96. }

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct {
  4.     int row;
  5.     int col;
  6.     int e;
  7. }Triplet;
  8. typedef struct {
  9.     Triplet data[10];
  10.     int rows;
  11.     int cols;
  12.     int nums;
  13. }Table;

  14. void Create (Table *t,int *Matrix,int m,int n)
  15. {
  16.     int i,j;
  17.     /*
  18.     for (i=0;i<3;i++)
  19.     {
  20.     for (j=0;j<4;j++)
  21.             printf ("%d ",*(Matrix+i*n+j));
  22.         printf ("\n");
  23.     }
  24.     getch ();
  25.     */
  26.     t->rows=m;
  27.     t->cols=n;
  28.     t->nums=0;
  29.     for (i=0;i<m;i++)
  30.         for (j=0;j<n;j++)
  31.             if (*(Matrix+i*n+j)!=0)
  32.             {
  33.                 t->nums++;
  34.                 t->data[t->nums].row=i+1;
  35.                 t->data[t->nums].col=j+1;
  36.                 t->data[t->nums].e=*(Matrix+i*n+j);
  37.             }
  38. }
  39. void Trans (Table M,Table *T)
  40. {
  41.     int col,t,p,q;
  42.     int num[10];
  43.     int cpot[10];
  44.     T->rows=M.cols;
  45.     T->cols=M.rows;
  46.     T->nums=M.nums;
  47.     if (T->nums)
  48.     {
  49.         for (col=1;col<=M.cols;++col)
  50.             num[col]=0;
  51.         for (t=1;t<=M.nums;++t)
  52.             ++num[M.data[t].col];
  53.         cpot[1]=1;
  54.         for (col=2;col<=M.cols;++col)
  55.             cpot[col]=cpot[col-1]+num[col-1];
  56.         for (p=1;p<=M.nums;++p)
  57.         {
  58.             col=M.data[p].col;
  59.             q=cpot[col];
  60.             T->data[q].row=M.data[p].col;
  61.             T->data[q].col=M.data[p].row;
  62.             T->data[q].e=M.data[p].e;
  63.             ++cpot[col];
  64.         }
  65.     }

  66. }
  67. void MultSMatrix (Table M,Table N,Table *Q)
  68. {
  69.     int arow,brow,tp,p,q,t,ccol,row,i;
  70.     int ctemp[10];
  71.     int rpos[10];
  72.     int num[10];
  73.     int Mrpos[10];
  74.     int Nrpos[10];
  75.     if (M.cols!=N.rows)
  76.         exit (1);
  77.     Q->rows=M.rows;
  78.     Q->cols=N.cols;
  79.     Q->nums=0;
  80.     for (row=1;row<=M.rows;++row)
  81.          num[row]=0;
  82.     for (t=1;t<=M.nums;++t)
  83.         ++num[M.data[t].row];
  84.     Mrpos[1]=1;
  85.     for (row=2;row<=M.rows;++row)
  86.         Mrpos[row]=Mrpos[row-1]+num[row-1];
  87.     for (row=1;row<=N.rows;++row)
  88.          num[row]=0;
  89.     for (t=1;t<=N.nums;++t)
  90.         ++num[N.data[t].row];
  91.     Nrpos[1]=1;
  92.     for (row=2;row<=N.rows;++row)
  93.         Nrpos[row]=Nrpos[row-1]+num[row-1];
  94.     if (M.nums*N.nums!=0)
  95.     {
  96.         for (arow=1;arow<=M.rows;++arow)
  97.         {
  98.             for (i=0;i<10;i++)
  99.                 ctemp[i]=0;
  100.             rpos[arow]=Q->nums+1;
  101.             if (arow<M.rows)
  102.                 tp=Mrpos[arow+1];
  103.             else
  104.                 tp=M.nums+1;
  105.             for (p=Mrpos[arow];p<tp;++p)
  106.             {
  107.                 brow=M.data[p].col;
  108.                 if (brow<N.rows)
  109.                     t=Nrpos[brow+1];
  110.                 else
  111.                     t=N.nums+1;
  112.                 for (q=Nrpos[brow];q<t;++q)
  113.                 {
  114.                     ccol=N.data[q].col;
  115.                     ctemp[ccol]=ctemp[ccol]+M.data[p].e*N.data[q].e;
  116.                 }
  117.             }
  118.             for (ccol=1;ccol<=Q->cols;++ccol)
  119.                 if (ctemp[ccol])
  120.                 {
  121.                     Q->nums++;
  122.                     Q->data[Q->nums].row=arow;
  123.                     Q->data[Q->nums].col=ccol;
  124.                     Q->data[Q->nums].e=ctemp[ccol];
  125.                 }
  126.         }
  127.     }
  128. }

  129. void ToMatrix(Table t,int *Matrix,int m,int n)
  130. {
  131.     int i,j;
  132.     for (i=0;i<m;i++)
  133.         for (j=0;j<n;j++)
  134.             *(Matrix+i*n+j)=0;
  135.     for (i=1;i<=t.nums;i++)
  136.     {
  137.         *(Matrix+(t.data[i].row-1)*n+(t.data[i].col-1))=t.data[i].e;
  138.     }
  139. }
  140. int main ()
  141. {
  142.     int i,j;
  143.     Table A,B,AB;
  144.     int MatrixA[3][2] =
  145.         {{2,3},
  146.          {0,4},
  147.          {1,2}};
  148.     int MatrixB[2][3] =
  149.         {{2,5,1},
  150.          {0,2,4}};
  151.     int MatrixAB[3][3];
  152.     Create (&A,MatrixA,3,2);
  153.     Create (&B,MatrixB,2,3);
  154.     MultSMatrix (A,B,&AB);

  155.     ToMatrix (AB,MatrixAB,3,3);
  156.     for (i=0;i<3;i++)
  157.     {
  158.         for (j=0;j<3;j++)
  159.             printf ("%d ",MatrixAB[i][j]);
  160.         printf ("\n");
  161.     }
  162.     getchar ();
  163. }

阅读(142) | 评论(0) | 转发(0) |
0

上一篇:Notes of SICP

下一篇:python写的最简单的外挂

给主人留下些什么吧!~~