[C++] 稀疏矩阵运算(稀疏矩阵初始化、稀疏矩阵的输入、稀疏矩阵的输出、稀疏矩阵的转置等) →→→→→进入此内容的聊天室

来自 , 2020-09-29, 写在 C++, 查看 167 次.
URL http://www.code666.cn/view/f442d33f
  1. #include<iostream.h>
  2. const int smax=50;
  3. typedef int elemtype;
  4. struct triple
  5. {
  6.         int i,j;
  7.         elemtype v;
  8. };
  9. struct smatrix
  10. {
  11.         int m,n,t;
  12.         triple sm[smax+1];
  13. };
  14. void initmatrix ( smatrix &M )
  15. {
  16.         M.m=M.n=0;
  17.         M.t=0;
  18. }
  19.  
  20. void inputmatrix ( smatrix &M,int m, int n )
  21. {
  22.         M.m=m;
  23.         M.n=n;
  24.         int row,col;
  25.         elemtype val;
  26.         int k=0;
  27.         cin>>row>>col>>val;
  28.         while ( row!=0 )
  29.         {
  30.                 k++;
  31.                 M.sm[k].i=row;
  32.                 M.sm[k].j=col;
  33.                 M.sm[k].v=val;
  34.                 cin>>row>>col>>val;
  35.         }
  36.         M.t=k;
  37. }
  38. void outputmatrix ( smatrix &M )
  39. {
  40.         cout<<'(';
  41.         for ( int i=1; i<M.t; i++ )
  42.         {
  43.                 cout<<'('<<M.sm[i].i<<',';
  44.                 cout<<M.sm[i].j<<',';
  45.                 cout<<M.sm[i].v<<')'<<',';
  46.         }
  47.         if ( M.t!=0 )
  48.         {
  49.                 cout<<'('<<M.sm[M.t].i<<',';
  50.                 cout<<M.sm[M.t].j<<',';
  51.                 cout<<M.sm[M.t].v<<')';
  52.         }
  53.         cout<<')'<<endl;
  54. }
  55.  
  56. smatrix transpose ( smatrix &M )
  57. {
  58.         smatrix s;
  59.         initmatrix ( s );
  60.         int m,n,t;
  61.         m=M.m;
  62.         n=M.n;
  63.         t=M.t;
  64.         s.m=n;
  65.         s.n=m;
  66.         s.t=t;
  67.         if ( t==0 )
  68.                 return s;
  69.         int num[smax+1];
  70.         int cpot[smax+1];
  71.         for ( int col=1; col<=n; col++ )
  72.                 num[col]=0;
  73.         for ( int k=1; k<=t; k++ )
  74.                 ++num[M.sm[k].j];
  75.         cpot[1]=1;
  76.         for ( col=2; col<=n; col++ )
  77.                 cpot[col]=cpot[col-1]+num[col-1];
  78.         for ( int p=1; p<=t; p++ )
  79.         {
  80.                 col=M.sm[p].j;
  81.                 int q=cpot[col];
  82.                 s.sm[q].i=M.sm[p].j;
  83.                 s.sm[q].j=M.sm[p].i;
  84.                 s.sm[q].v=M.sm[p].v;
  85.                 ++cpot[col];
  86.         }
  87.         return s;
  88. }
  89.  
  90. void main()
  91. {
  92.         smatrix M;
  93.         int m,n;
  94.         initmatrix ( M );
  95.         cout<<"input M,N:"<<endl;
  96.         cin>>m>>n;
  97.         inputmatrix ( M,m,n );
  98.         cout<<"m="<<M.m<<',';
  99.         cout<<"n="<<M.n<<',';
  100.         cout<<"t="<<M.t<<endl;
  101.         outputmatrix ( M );
  102.         cout<<"m="<<M.m<<',';
  103.         cout<<"n="<<M.n<<',';
  104.         cout<<"t="<<M.t<<endl;
  105.         outputmatrix ( transpose ( M ) );
  106. }
  107.  

回复 "稀疏矩阵运算(稀疏矩阵初始化、稀疏矩阵的输入、稀疏矩阵的输出、稀疏矩阵的转置等)"

这儿你可以回复上面这条便签

captcha