您的位置:首页 > 其它

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

2015-11-02 16:30 435 查看
/* 

 *Copyright(c) 2015, 烟台大学计算机学院 

 *All rights reserved. 

 *文件名称:稀疏矩阵的三元组表示的实现及应用(1).cpp 

 *作    者:周洁 

 *完成日期:2015年 11月2日 

 *版 本 号: 

 * 

 *问题描述:建立稀疏矩阵三元组表示的算法库,包括:

① 头文tup.h,定义数据类型,声明函数;

② 源文件tup.cpp,实现稀疏矩阵三元组表示的基本运算,主要算法包括:
<code class="hljs cs has-numbering"><span class="hljs-keyword">void</span> CreatMat(TSMatrix &t,ElemType A[M]
);  <span class="hljs-comment">//从一个二维稀疏矩阵创建其三元组表示</span>
<span class="hljs-keyword">bool</span> Value(TSMatrix &t,ElemType x,<span class="hljs-keyword">int</span> i,<span class="hljs-keyword">int</span> j);  <span class="hljs-comment">//三元组元素赋值</span>
<span class="hljs-keyword">bool</span> Assign(TSMatrix t,ElemType &x,<span class="hljs-keyword">int</span> i,<span class="hljs-keyword">int</span> j);  <span class="hljs-comment">//将指定位置的元素值赋给变量</span>
<span class="hljs-keyword">void</span> DispMat(TSMatrix t); <span class="hljs-comment">//输出三元组</span>
<span class="hljs-keyword">void</span> TranTat(TSMatrix t,TSMatrix &tb);<span class="hljs-comment">//矩阵转置</span></code><ul style="FILTER: ; ZOOM: 1" class="pre-numbering"><li>1</li></ul>

③ 设计main函数,测试上面实现的算法

 *输入描述:若干数据

 *程序输出:三元组对应操作的输出

*/

 

代码:

(1)头文件 t.h
#ifndef TUP_H_INCLUDED

#define TUP_H_INCLUDED

#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]
);  //从一个二维稀疏矩阵创建其三元组表示

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);//矩阵转置

#endif // TUP_H_INCLUDED

 

(2)源文件 t.cpp
#include "stdio.h"

#include "t.h"

void CreatMat(TSMatrix &t,ElemType A[M]
)  //从一个二维稀疏矩阵创建其三元组表示

{

    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;               //失败时返回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;                        //成功时返回true

}

bool Assign(TSMatrix t,ElemType &x,int i,int j)  //将指定位置的元素值赋给变量

{

    int k=0;

    if (i>=t.rows || j>=t.cols)

        return false;           //失败时返回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;            //成功时返回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;                    //q为tb.data的下标

    tb.rows=t.cols;

    tb.cols=t.rows;

    tb.nums=t.nums;

    if (t.nums!=0)                  //当存在非零元素时执行转置

    {

        for (v=0; v<t.cols; v++)        //tb.data[q]中的记录以c域的次序排列

            for (p=0; p<t.nums; p++)    //p为t.data的下标

                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++;

                }

    }

}

 

(3)源文件 main.cpp

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;

}

运行结果:



 

知识点总结:

稀疏矩阵的三元组表示的实现及应用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: