稀疏矩阵的压缩存储与快速转置 三元组法 类与对象实现

贴个主函数 这酸爽~

#include "TSMatrix.h"
#include"Triple.cpp"
#include"TSMatrix.cpp"
void transMatrix(TSMatrix a,TSMatrix &b);///求三元组顺序表方式,转置矩阵简单的方法,将稀疏矩阵a转置为b
void quickTransMatrix(TSMatrix a,TSMatrix &b);///快速转置法
int main(){
    Triple a_matrix[8];
    a_matrix[0] = Triple(0,1,12);///先前的错误写法 t[0] = new Triple(1,2,12);
    a_matrix[1] = Triple(0,2,9);
    a_matrix[2] = Triple(2,0,-3);
    a_matrix[3] = Triple(2,5,14);
    a_matrix[4] = Triple(3,2,24);
    a_matrix[5] = Triple(4,1,18);
    a_matrix[6] = Triple(5,0,15);
    a_matrix[7] = Triple(5,3,-7);

    Triple b_matrix[8];///简单转置法
    Triple c_matrix[8];///快速转置法

    TSMatrix a = TSMatrix(7,6,8);///建立了一个7*6的数组,里面的非零元有8个
    TSMatrix b = TSMatrix();
    TSMatrix c = TSMatrix();

    a.setMatrix(a_matrix);
    b.setMatrix(b_matrix);
    c.setMatrix(c_matrix);
    int flag = 0;
    cout<<"a矩阵如下:\n";
    for(int i = 0,flag = 0; i< a.getRows();i++){
        for(int j = 0; j < a.getCols(); j++){
            if(a.getMatrix()[flag].getI()==i&&a.getMatrix()[flag].getJ()==j){
                cout<<a.getMatrix()[flag].getData()<<"\t";
                flag++;
                }else{
                    cout<<"0\t";
                }
            }
        cout<<endl;
        }
    cout<<endl;
    cout<<"存储的实际上是这些:";
    for(int i =0; i < 8;i++){
        cout<<" ("<<a.getMatrix()[i].getI()<<","<<a.getMatrix()[i].getJ()<<","<<a.getMatrix()[i].getData()<<") ";
        }
    cout<<endl;
    ///开始转置
    transMatrix(a,b);
    quickTransMatrix(a,c);
    cout<<endl<<"简单转置法转置后:\n";
    for(int i = 0,flag = 0; i< b.getRows();i++){
        for(int j = 0; j < b.getCols(); j++){
            if(b.getMatrix()[flag].getI()==i&&b.getMatrix()[flag].getJ()==j){
                cout<<b.getMatrix()[flag].getData()<<"\t";
                flag++;
                }else{
                    cout<<"0\t";
                }
            }
        cout<<endl;
        }
    cout<<endl;
    cout<<endl<<"快速转置法转置后:\n";
    for(int i = 0,flag = 0; i< b.getRows();i++){
        for(int j = 0; j < b.getCols(); j++){
            if(b.getMatrix()[flag].getI()==i&&b.getMatrix()[flag].getJ()==j){
                cout<<b.getMatrix()[flag].getData()<<"\t";
                flag++;
                }else{
                    cout<<"0\t";
                }
            }
        cout<<endl;
        }

    }
void transMatrix(TSMatrix a,TSMatrix &b){///求三元组顺序表方式,转置矩阵简单的方法,将稀疏矩阵a转置为b
    b.setRows(a.getCols());///将a 的列数设置为b的行数
    b.setCols(a.getRows());///将a 的行数设置为b的列数
    b.setNum(a.getNum());///总数都一样
    if(b.getNum()>0){///如果总数大于0
        int flag = 0;
        for(int i = 0; i < a.getRows();i++){
            for(int j = 0; j < a.getNum();j++ ){
    /// 没有设置为公共属性,下面的写法看起来有些蛋疼。。,
    ///如果都设置为公共属性的话就是这个样子了a.matrix[i].j =
               /// 下面的操作就是给b矩阵赋值
                
                if(a.getMatrix()[j].getJ() ==i){
                    b.getMatrix()[flag].setI(a.getMatrix()[j].getJ());
                    b.getMatrix()[flag].setJ(a.getMatrix()[j].getI());
                    b.getMatrix()[flag].setData(a.getMatrix()[j].getData());
                    flag++;
                    }
                }
            }
        }

    }
void quickTransMatrix(TSMatrix a,TSMatrix &b){
    b.setRows(a.getCols());///将a 的列数设置为b的行数
    b.setCols(a.getRows());///将a 的行数设置为b的列数
    b.setNum(a.getNum());///总数都一样

    int* closNum = new int[a.getCols()];///存储每列的非0元的个数
    int* closPos = new int[a.getCols()];///存储每列的第一个非零元出现的位置
    if(a.getNum()>0){
        for(int i = 0; i < a.getCols();i++){///都初始化为0
            closNum[i]=0;
            }
        for(int i = 0; i < a.getNum();i++){///扫描a里的所有元素,求出每一列中非0元的个数
            closNum[a.getMatrix()[i].getJ()]++;
            }
        closPos[0] = 0;
        for(int i = 1 ; i < a.getCols();i++){///确定矩阵a中的的第i列中第一个非零元素在b中的位置
            closPos[i] = closPos[i-1] + closNum[i-1];
            }
        for(int i = 0 ; i < a.getNum();i++){
            int k = closPos[a.getMatrix()[i].getJ()]; ///k即矩阵a第j列中第一个非零元素在b中的位置
            b.getMatrix()[k].setI(a.getMatrix()[i].getJ());
            b.getMatrix()[k].setJ(a.getMatrix()[i].getI());
            b.getMatrix()[k].setData(a.getMatrix()[i].getData());
            closPos[a.getMatrix()[i].getJ()]++;///矩阵a第col列中第一个非零元素在b中的位置向前移动一位
            }
        }
    delete []closNum;
    delete []closPos;
}

a矩阵如下:
0       12      9       0       0       0
0       0       0       0       0       0
-3      0       0       0       0       14
0       0       24      0       0       0
0       18      0       0       0       0
15      0       0       -7      0       0
0       0       0       0       0       0

存储的实际上是这些: (0,1,12)  (0,2,9)  (2,0,-3)  (2,5,14)  (3,2,24)  (4,1,18)
(5,0,15)  (5,3,-7)

简单转置法转置后:
0       0       -3      0       0       15      0
12      0       0       0       18      0       0
9       0       0       24      0       0       0
0       0       0       0       0       -7      0
0       0       0       0       0       0       0
0       0       14      0       0       0       0


快速转置法转置后:
0       0       -3      0       0       15      0
12      0       0       0       18      0       0
9       0       0       24      0       0       0
0       0       0       0       0       -7      0
0       0       0       0       0       0       0
0       0       14      0       0       0       0

Process returned 0 (0x0)   execution time : 3.468 s
Press any key to continue.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值