⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sparsematrixcalculate.c

📁 矩阵的加法,乘法,减法,除法,大家可以参考一下.
💻 C
字号:
#define MAXSIZE  400
#include "conio.h"
#include "stdio.h"
#include "string.h"

typedef struct
 { int  i,j;           /*矩阵的行列*/
   int  e;
 }Triple;              /*矩阵的元素值*/
struct TSMstrix
 { Triple  data[MAXSIZE+1];
   int  mu,nu,tu;                                     /*矩阵的三元组*/
 }M,N,Q;
struct TSMstrix CreatSMatrix(  )                      /*矩阵的创建函数*/
 {  struct TSMstrix M;
    int m;
    printf("please input Matrix's row,col ,and tu:");
    scanf("%d,%d,%d",&M.mu,&M.nu,&M.tu);
   for(m=1;m<=M.tu;m++)
    { printf("please input %dth information:",m);
      scanf("%d,%d,%d",&M.data[m].i,&M.data[m].j,&M.data[m].e);
    } return M;
  }

void AddSMatrix(struct TSMstrix M,struct TSMstrix N)   /*矩阵的加法*/
  {
    int p,q;

    int  S[20][20];
    for(p=0;p<20;p++)
    { for(q=0;q<20;q++)
       { S[p][q]=0;
       }
    }

   if(M.mu!=N.mu||M.nu!=N.nu)
      { printf("ERROR");getch();return;
       }

    for(p=1;p<=M.tu;++p)
      { S[M.data[p].i][M.data[p].j]+=M.data[p].e;
       }
     for(q=1;q<=N.tu;++q)
      { S[N.data[q].i][N.data[q].j]+=N.data[q].e;
       }
    for(p=1;p<=M.mu;++p)
     { printf("|");
       for(q=1;q<=N.nu;++q)
     { printf("%2d ",S[p][q]);
           }
        printf("|\n");
      }getch();
   }
 void SubtSMatrix(struct TSMstrix M,struct TSMstrix N)   /*矩阵的减法*/
 { int p,q;
   int S[20][20];
    for(p=0;p<20;p++)
     { for(q=0;q<20;q++)
    { S[p][q]=0;
    }
     }
    if(M.mu!=N.mu||M.nu!=N.nu)
      { printf("ERROR");getch();return;
       }
    for(p=1;p<=M.tu;++p)
      { S[M.data[p].i][M.data[p].j]-=M.data[p].e;
       }
     for(q=1;q<=N.tu;++q)
      { S[N.data[q].i][N.data[q].j]+=N.data[q].e;
       }
    for(p=1;p<=M.mu;++p)
     { printf("|");
       for(q=1;q<=N.nu;++q)
     { printf("%2d ",S[p][q]);
           }
        printf("|\n");
      }getch();
  }
MultSMatrix(struct TSMstrix M,struct TSMstrix N)         /*矩阵的乘法*/
 {  int arow,brow,ccol,tp,t,r,p,q,m,a;
    int  ctemp[20],rpos1[20],rpos2[20],rpos3[20];
    printf("please input M's row,col,and tu:");
    scanf("%d,%d,%d",&M.mu,&M.nu,&M.tu);
   for(m=1;m<=M.tu;m++)
    { printf("please input %dth information:",m);
      scanf("%d,%d,%d",&M.data[m].i,&M.data[m].j,&M.data[m].e);
    }
   printf("please input N's row,nu,and tu:");
   scanf("%d,%d,%d",&N.mu,&N.nu,&N.tu);
   for(m=1;m<=N.tu;m++)
    { printf("please input %dth information:",m);
      scanf("%d,%d,%d",&N.data[m].i,&N.data[m].j,&N.data[m].e);
    }
     for(t=1;t<=M.mu;t++)
      { a=1;
    while(M.data[a].i!=t)
       a++;
    rpos1[t]=a;
       }
    for(t=1;t<=N.mu;t++)
      { a=1;
    while(N.data[a].i!=t)
       a++;
        rpos2[t]=a;
       }
    if(M.nu!=N.mu)
      { printf("ERROR");getch();return(0);
      }
    Q.mu=M.mu;  Q.nu=N.nu;  Q.tu=0;
    if(M.tu*N.tu!=0)
      { for(arow=1;arow<=M.mu;++arow)
     { for(r=1;r<=M.mu;r++)
       { ctemp[r]=0;
       }
    rpos3[arow]=Q.tu+1;
    if(arow<M.mu)    tp=rpos1[arow+1];
    else   { tp=M.tu+1;   }
    for(p=rpos1[arow];p<tp;++p)
      { brow=M.data[p].j;
        if(brow<N.mu)   t=rpos2[brow+1];
        else  { t=N.tu+1;   }
       for(q=rpos2[brow];q<t;++q)
         { ccol=N.data[q].j;
           ctemp[ccol]+=M.data[p].e*N.data[q].e;
         }
       }
        for(ccol=1;ccol<=Q.nu;++ccol)
          { if(ctemp[ccol])
       { if(++Q.tu>MAXSIZE)
         return(0);
         Q.data[Q.tu].i=arow;
         Q.data[Q.tu].j=ccol;
         Q.data[Q.tu].e=ctemp[ccol];
        }
      }
      }
 Q.mu=M.mu;  Q.nu=N.nu;
 t=1;
   for(p=1;p<=Q.mu;p++)
     { printf("|");
      for(q=1;q<=Q.nu;q++)
        { if(p==Q.data[t].i&&q==Q.data[t].j)
           { printf("%2d ",Q.data[t].e);
             ++t;
            }
          else  { printf(" 0");   }
         }
       printf("|\n");
     }
}getch();
}
  void TransposeSMatrix(struct TSMstrix M)            /*矩阵的转置*/
 {int p,q,k,m;
  int col;
  printf("please input M's row,col,and tu:");
    scanf("%d,%d,%d",&M.mu,&M.nu,&M.tu);
   for(m=1;m<=M.tu;m++)
    { printf("please input %dth information:",m);
      scanf("%d,%d,%d",&M.data[m].i,&M.data[m].j,&M.data[m].e);
    }
  Q.mu=M.nu;
  Q.nu=M.mu;
  Q.tu=M.tu;
  if(Q.tu)
  {q=0;
   for(col=1;col<=M.nu;++col)
    for(p=1;p<=M.tu;++p)
     if(M.data[p].j==col)
      { Q.data[q].i=M.data[p].j;
        Q.data[q].j=M.data[p].i;
        Q.data[q].e=M.data[p].e;
        ++q;
      }
    }

     k=0;
     for(p=1;p<=Q.mu;p++)
     { printf("|");
       for(q=1;q<=Q.nu;q++)
      {
        if(p==Q.data[k].i&&q==Q.data[k].j)
      { printf("%2d",Q.data[k].e);
         ++k;
      }
          else  { printf(" 0");   }
      }
       printf("|\n");
   }getch();
}

main()
 {  struct TSMstrix M,N;
   char ch;

   clrscr();
   gotoxy(4,2);
   printf("Add(1)  Subtract(2)  Multiply(3)  Transpose(4)  Quit(5) \n");
   loop:printf("Please choose the operater:");
   scanf("%c",&ch);
   switch(ch)
    { case'1':AddSMatrix(M=CreatSMatrix(  ),N=CreatSMatrix(  ));
              goto loop;
      case'2':SubtSMatrix(M=CreatSMatrix(  ),N=CreatSMatrix(  ));
              goto loop;
      case'3':MultSMatrix(M,N);
               goto loop;
      case'4': TransposeSMatrix(M);
                goto loop;
      case'5':  break;
      default:  goto loop;

    }

 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -