码迷,mamicode.com
首页 > 其他好文 > 详细

4.3.2 稀疏矩阵转置(1)

时间:2020-04-12 12:26:08      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:i++   mat   pos   cpp   parse   tran   space   pre   div   

SparsrMatrix.h

#pragma once
#include<iostream>
using namespace std;

class Tri {
public:
    int row;
    int col;
    int value;
};

class SparseMatrix {
public:
    int sRow;
    int sCol;
    int sTerm;
    Tri* elements;//to save terms.

    SparseMatrix(int r = 10, int c = 10, int term = 2) {
        sRow = r;
        sCol = c;
        sTerm = term;
        elements = new Tri[sTerm];
    }

    void transpose(SparseMatrix& B) {
        int index_B = 0;
        if (sTerm > 0) {
            for (int i = 0; i < sCol; i++) {
                for (int j = 0; j < sTerm; j++) {
                    if (elements[j].col == i) {
                        B.elements[index_B].col = elements[j].row;
                        B.elements[index_B].row = elements[j].col;
                        B.elements[index_B].value = elements[j].value;
                        index_B++;
                    }
                }
            }
        }
    }

    void show() {
        for (int i = 0; i < sTerm; i++) {
            cout << elements[i].row << , << elements[i].col << , << elements[i].value << endl;
        }
    }
};

main.cpp

#include"SparseMatrix.h"

int  main() {
    SparseMatrix  A, B;
    A.elements[0].row = 1;
    A.elements[0].col = 3;
    A.elements[0].value = 6;
    A.elements[1].row = 5;
    A.elements[1].col = 1;
    A.elements[1].value = 8;
    A.show();
    cout << endl;
    cout << "----------------------------" << endl;
    cout << endl;
    A.transpose(B);
    B.show();
    return 0;
}

 

4.3.2 稀疏矩阵转置(1)

标签:i++   mat   pos   cpp   parse   tran   space   pre   div   

原文地址:https://www.cnblogs.com/SlowIsFast/p/12684498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!