第九周 项目3-稀疏矩阵的三元组表示的实现及应用(1)

来源:互联网 发布:邮件服务器软件选择 编辑:程序博客网 时间:2024/06/07 15:27
/*
*Copyright (c)2016,烟台大学计算机学院
*All rights reserved.
*文件名称:传写.cpp
*作者:李欣
*完成日期:2016年12月15日
*版本号:v1.0

*/


main.cpp


#include <stdio.h>  
#include "tup.h"  
int main()  
{  
    TSMatrix t,tb;  
    int x,y=10;  

    int A[6][7]=  
    {  
        {0,0,1,0,0,0,0},  
        {0,2,0,0,0,0,0},  
        {3,0,0,0,0,0,0},  
        {0,0,0,5,0,0,0},  
        {0,0,0,0,6,0,0},  
        {0,0,0,0,0,7,4}  
    };  
    CreatMat(t,A);  
    printf("b:\n");  
    DispMat(t);  
    if (Assign(t,x,2,5)==true)  //调用时返回true  
        printf("Assign(t,x,2,5)=>x=%d\n",x);  
    else  //调用时返回false  
        printf("Assign(t,x,2,5)=>参数错误\n");  
    Value(t,y,2,5);  
    printf("执行Value(t,10,2,5)\n");  
    if (Assign(t,x,2,5)==true)  //调用时返回true  
        printf("Assign(t,x,2,5)=>x=%d\n",x);  
    else  //调用时返回false  
        printf("Assign(t,x,2,5)=>参数错误\n");  
    printf("b:\n");  
    DispMat(t);  
    TranTat(t,tb);  
    printf("矩阵转置tb:\n");  
    DispMat(tb);  
    return 0;  
}  


tup.cpp


#include "tup.h"  
#include <stdio.h>  
void CreatMat(TSMatrix &t,ElemType A[M][N])  
{  
    int i,j;  
    t.rows=M;t.cols=N;t.nums=0;  
    for(i=0;i<M;i++)  
    {  
        for(j=0;j<N;j++)  
        {  
            if(A[i][j]!=0)  
            {  
                t.data[t.nums].r=i;t.data[t.nums].c=j;  
                t.data[t.nums].d=A[i][j];t.nums++;  
            }  
        }  
    }  
}  
bool Value(TSMatrix &t,ElemType x,int i,int j)  
{  
    int k=0,k1;  
    if(i>=t.rows || j>=t.cols)  
        return false;  
    while (k<t.nums && i>t.data[k].r) k++;  
    while (k<t.nums && i== t.data[k].r && j>t.data[k].c) k++;  
    if(t.data[k].r==i && t.data[k].c==j)  
        t.data[k].d=x;  
    else  
    {  
        for(k1=t.nums-1;k1>=k;k1--)  
        {  
            t.data[k1+1].r=t.data[k1].r;  
            t.data[k1+1].c=t.data[k1].c;  
            t.data[k1+1].d=t.data[k1].d;  
        }  
        t.data[k].r=i;t.data[k].c=j;t.data[k].d=x;  
        t.nums++;  
    }  
    return true;  
}  
bool Assign(TSMatrix t,ElemType &x,int i,int j)  
{  
   int k=0;  
   if(i>=t.rows || j>=t.cols)  
       return false;  
   while (k<t.nums && i>t.data[k].r) k++;  
   while (k<t.nums && i==t.data[k].r && j>t.data[k].c) k++;  
   if (t.data[k].r==i && t.data[k].c==j)  
       x=t.data[k].d;  
   else  
       x=0;  
   return true;  
}  
void DispMat(TSMatrix t)  
{  
     int i;  
     if (t.nums<=0)  
         return ;  
     printf("\t%d\t%d\t%d\n",t.rows,t.cols,t.nums);  
     printf("\t--------------------\n");  
     for(i=0;i<t.nums;i++)  
        printf("\t%d\t%d\t%d\n",t.data[i].r,t.data[i].c,t.data[i].d);  
}  
void TranTat(TSMatrix t,TSMatrix &tb)  
{  
    int p,q=0,v;  
    tb.rows=t.cols;tb.cols=t.rows;tb.nums=t.nums;  
    if(t.nums!=0)  
    {  
        for (v=0;v<t.cols;v++)  
           for(p=0;p<t.nums;p++)  
               if(t.data[p].c==v)  
               {     
                  tb.data[q].r=t.data[p].c;  
                  tb.data[q].c=t.data[p].r;  
                  tb.data[q].d=t.data[p].d;  
                  q++;  
               }  
    }  
}  


tup.h


#include "stdio.h"   
#define M 6  
#define N 7  
#define MaxSize 100  
typedef int ElemType;  
  
typedef struct  
{  
    int r;  
    int c;  
    ElemType d;  
}TupNode;  
typedef struct  
{  
    int rows;  
    int cols;  
    int nums;  
    TupNode data[MaxSize];  
}TSMatrix;  
void CreatMat(TSMatrix &t,ElemType A[M][N]);  
bool Value(TSMatrix &t,ElemType x,int i,int j);  
bool Assign(TSMatrix t,ElemType &x,int i,int j);  
void DispMat(TSMatrix t);  
void TranTat(TSMatrix t,TSMatrix &tb);  


运行结果:


  

0 0