第八周项目四

来源:互联网 发布:鬼泣4特别版优化补丁 编辑:程序博客网 时间:2024/06/06 07:09
  1. /*  
  2. 烟台大学计算机学院  
  3.   
  4. 文件名称:xiangmu.cpp  
  5.   
  6. 作者:刘照京  
  7.   
  8. 完成日期:2017年11月9日  
  9.   
  10. 问题描述:稀疏矩阵的三元组表示的实现及应用 
  11.   
  12. 输入描述:无 
  13.   
  14. 输出描述:三元组表示的形式的各元素 
  15.   
  16. */   
  17.   
  18.   
  19.   
  20.   
  21. //tup.h:  
  22.   
  23. #define M 6  
  24. #define N 7  
  25. #define MaxSize  100         //矩阵中非零元素最多个数  
  26. typedef int ElemType;  
  27.   
  28. typedef struct  
  29. {  
  30.     int r;                  //行号  
  31.     int c;                  //列号  
  32.     ElemType d;             //元素值  
  33. } TupNode;                  //三元组定义  
  34.   
  35. typedef struct  
  36. {  
  37.     int rows;               //行数  
  38.     int cols;               //列数  
  39.     int nums;               //非零元素个数  
  40.     TupNode data[MaxSize];  
  41. } TSMatrix;                 //三元组顺序表定义  
  42.   
  43. void CreatMat(TSMatrix &t,ElemType A[M][N]);  //从一个二维稀疏矩阵创建其三元组表示  
  44. bool Value(TSMatrix &t,ElemType x,int i,int j);  //三元组元素赋值  
  45. bool Assign(TSMatrix t,ElemType &x,int i,int j); //将指定位置的元素值赋给变量  
  46. void DispMat(TSMatrix t);//输出三元组  
  47. void TranTat(TSMatrix t,TSMatrix &tb);//矩阵转置  
  48.   
  49.   
  50. //tup.cpp  
  51.   
  52.   
  53. #include "stdio.h"  
  54. #include "tup.h"  
  55.   
  56. void CreatMat(TSMatrix &t,ElemType A[M][N])  //从一个二维稀疏矩阵创建其三元组表示  
  57. {  
  58.     int i,j;  
  59.     t.rows=M;  
  60.     t.cols=N;  
  61.     t.nums=0;  
  62.     for (i=0; i<M; i++)  
  63.     {  
  64.         for (j=0; j<N; j++)  
  65.             if (A[i][j]!=0)     //只存储非零元素  
  66.             {  
  67.                 t.data[t.nums].r=i;  
  68.                 t.data[t.nums].c=j;  
  69.                 t.data[t.nums].d=A[i][j];  
  70.                 t.nums++;  
  71.             }  
  72.     }  
  73. }  
  74.   
  75. bool Value(TSMatrix &t,ElemType x,int i,int j)  //三元组元素赋值  
  76. {  
  77.     int k=0,k1;  
  78.     if (i>=t.rows || j>=t.cols)  
  79.         return false;               //失败时返回false  
  80.     while (k<t.nums && i>t.data[k].r) k++;                  //查找行  
  81.     while (k<t.nums && i==t.data[k].r && j>t.data[k].c) k++;//查找列  
  82.     if (t.data[k].r==i && t.data[k].c==j)   //存在这样的元素  
  83.         t.data[k].d=x;  
  84.     else                                    //不存在这样的元素时插入一个元素  
  85.     {  
  86.         for (k1=t.nums-1; k1>=k; k1--)  
  87.         {  
  88.             t.data[k1+1].r=t.data[k1].r;  
  89.             t.data[k1+1].c=t.data[k1].c;  
  90.             t.data[k1+1].d=t.data[k1].d;  
  91.         }  
  92.         t.data[k].r=i;  
  93.         t.data[k].c=j;  
  94.         t.data[k].d=x;  
  95.         t.nums++;  
  96.     }  
  97.     return true;                        //成功时返回true  
  98. }  
  99.   
  100. bool Assign(TSMatrix t,ElemType &x,int i,int j)  //将指定位置的元素值赋给变量  
  101. {  
  102.     int k=0;  
  103.     if (i>=t.rows || j>=t.cols)  
  104.         return false;           //失败时返回false  
  105.     while (k<t.nums && i>t.data[k].r) k++;                  //查找行  
  106.     while (k<t.nums && i==t.data[k].r && j>t.data[k].c) k++;//查找列  
  107.     if (t.data[k].r==i && t.data[k].c==j)  
  108.         x=t.data[k].d;  
  109.     else  
  110.         x=0;                //在三元组中没有找到表示是零元素  
  111.     return true;            //成功时返回true  
  112. }  
  113.   
  114. void DispMat(TSMatrix t)        //输出三元组  
  115. {  
  116.     int i;  
  117.     if (t.nums<=0)          //没有非零元素时返回  
  118.         return;  
  119.     printf("\t%d\t%d\t%d\n",t.rows,t.cols,t.nums);  
  120.     printf("\t------------------\n");  
  121.     for (i=0; i<t.nums; i++)  
  122.         printf("\t%d\t%d\t%d\n",t.data[i].r,t.data[i].c,t.data[i].d);  
  123. }  
  124.   
  125. void TranTat(TSMatrix t,TSMatrix &tb)       //矩阵转置  
  126. {  
  127.     int p,q=0,v;                    //q为tb.data的下标  
  128.     tb.rows=t.cols;  
  129.     tb.cols=t.rows;  
  130.     tb.nums=t.nums;  
  131.     if (t.nums!=0)                  //当存在非零元素时执行转置  
  132.     {  
  133.         for (v=0; v<t.cols; v++)        //tb.data[q]中的记录以c域的次序排列  
  134.             for (p=0; p<t.nums; p++)    //p为t.data的下标  
  135.                 if (t.data[p].c==v)  
  136.                 {  
  137.                     tb.data[q].r=t.data[p].c;  
  138.                     tb.data[q].c=t.data[p].r;  
  139.                     tb.data[q].d=t.data[p].d;  
  140.                     q++;  
  141.                 }  
  142.     }  
  143. }  
  144.   
  145.   
  146. //main  
  147.   
  148.   
  149. #include <stdio.h>  
  150. #include "tup.h"  
  151. int main()  
  152. {  
  153.     TSMatrix t,tb;  
  154.     int x,y=10;  
  155.     int A[6][7]=  
  156.     {  
  157.         {0,0,1,0,0,0,0},  
  158.         {0,2,0,0,0,0,0},  
  159.         {3,0,0,0,0,0,0},  
  160.         {0,0,0,5,0,0,0},  
  161.         {0,0,0,0,6,0,0},  
  162.         {0,0,0,0,0,7,4}  
  163.     };  
  164.     CreatMat(t,A);  
  165.     printf("b:\n");  
  166.     DispMat(t);  
  167.     if (Assign(t,x,2,5)==true)  //调用时返回true  
  168.         printf("Assign(t,x,2,5)=>x=%d\n",x);  
  169.     else  //调用时返回false  
  170.         printf("Assign(t,x,2,5)=>参数错误\n");  
  171.     Value(t,y,2,5);  
  172.     printf("执行Value(t,10,2,5)\n");  
  173.     if (Assign(t,x,2,5)==true)  //调用时返回true  
  174.         printf("Assign(t,x,2,5)=>x=%d\n",x);  
  175.     else  //调用时返回false  
  176.         printf("Assign(t,x,2,5)=>参数错误\n");  
  177.     printf("b:\n");  
  178.     DispMat(t);  
  179.     TranTat(t,tb);  
  180.     printf("矩阵转置tb:\n");  
  181.     DispMat(tb);  
  182.     return 0;  
  183. }  

运行结果:


学习心得:学会了稀疏矩阵得三元组表示的实现及应用

原创粉丝点击