[C++] 구조체로 표현한 희소행렬과 두 행렬의 덧셈과 전치

희소행렬간의 덧셈과 전치
#include <iostream>
#include <vector>

using namespace std;

#define MAX_TERMS 100

typedef struct {
    int row;
    int col;
    int value;
} element;

typedef struct Matrix{
    vector<element> data;
    int rows;
    int cols;
    int terms;

    // 생성자에서 data를 MAX_TERMS로 초기화
    Matrix() : data(MAX_TERMS), rows(0), cols(0), terms(0) {}
} matrix;

Matrix matrix_transpose2(Matrix &a) {
    matrix b;
    //b 행렬을 a의 전치행렬 형태로
    //0이 아닌 항의 갯수는 똑같이
    int bindex=0;
    b.rows = a.cols;
    b.cols = a.rows;
    b.terms = a.terms;

    if (a.terms > 0) {
        for (int c = 0; c < a.cols; c++) { //cols만큼 실행
            for (int i = 0; i < a.terms; i++) { //0이 아닌 것의 갯수만큼
                if (a.data[i].col == c) {
                    b.data[bindex].row = a.data[i].col;
                    b.data[bindex].col = a.data[i].row;
                    b.data[bindex].value = a.data[i].value;
                    bindex++;
                }
            }
        }
    }

    return b;
}

Matrix Matrix_Sum(Matrix &a, Matrix &b) {

    matrix result;
    result.cols = a.cols;
    result.rows = a.rows;

    int i = 0, j = 0, terms=0; 
    while (i < a.terms && j < b.terms) {
        //row와 col 둘다 같으면 그 안에 value를 더한값을 넣는다
        if (a.data[i].row == b.data[j].row && a.data[i].col == b.data[j].col) {
            int sum = a.data[i].value + b.data[j].value;
            if (sum != 0) {
                result.data[terms++] = { a.data[i].row, a.data[i].col, sum };
            }
            i++; j++;
        }
        //a보다 b의 row가 크거나, row는 같지만 col이 크면 작은값부터 들어가야 하므로 a먼저 넣는다.
        else if (a.data[i].row < b.data[j].row || (a.data[i].row == b.data[j].row && a.data[i].col < b.data[j].col)) {
            result.data[terms++] = a.data[i++];
        }
        //위와 반대
        else {
            result.data[terms++] = b.data[j++];
        }
    }

    //만약 희소행렬의 data요소가 다르면 나머지 값들을 넣어준다.
    while (i < a.terms) {
        result.data[terms++] = a.data[i++];
    }
    while (j < b.terms) {
        result.data[terms++] = b.data[j++];
    }

    result.terms = terms;
    return result;
   
}

void Matrix_Pring(matrix* m) {
    cout << "======================= " << "\n";
    for (int i = 0; i < m->terms; i++) {
        cout << m->data[i].row << " " << m->data[i].col << " " << m->data[i].value << "\n";
    }
    cout << "======================= " << "\n";
}


int main() {
    matrix m;

    m.data = {
       {0, 3, 7},
       {1, 0, 9},
       {1, 5, 8},
       {3, 0, 6},
       {3, 1, 5},
       {4, 5, 1},
       {5, 2, 2}
    };
    m.rows = 6;
    m.cols = 6;
    m.terms = 7;

    matrix m2;
    m2.data = {
       {0, 4, 3},
       {1, 0, 4},
       {1, 3, 4},
       {2, 4, 6},
       {3, 2, 7},
       {4, 0, 5},
       {5, 5, 2}
    };
    m2.rows = 6;
    m2.cols = 6;
    m2.terms = 7;

    matrix result;

    cout << "전치행렬" << "\n";
    result = matrix_transpose2(m);
    Matrix_Pring(&result);

    cout << "행렬 합" << "\n";

    result = Matrix_Sum(m,m2);
    Matrix_Pring(&result);

    return 0;
}

 

element : 행렬의 좌표와 값을 가진 구조체

Matrix : 행렬 그 자체(element의 벡터구조체들, 행 크기, 열 크기, 0이 아닌 값들의 갯수)

 

C언어로 쉽게 풀어쓴 자료구조 개정3판의 프로그램 3.5를 C++로 표현하였고, 문제1번의 주어진 2개의 행렬을 더하는 함수를 주소값을 받아와 직접참조로 해보았습니다.