西北工业大学数据结构noj2-4

稀疏矩阵乘法

question

问号是空格

完整代码

// 稀疏矩阵乘法
#include <iostream>
#include <vector>
#include <iomanip>

#define MAX_SIZE 1000
struct Trida
{
    int row, col;
    int val;
};

struct SparseMatrix
{
    Trida data[MAX_SIZE];
    int m, n, len; // m为行数,n为列数
};

void buildSparseMatrix(SparseMatrix &Mat);

void insertSparseMatrix(SparseMatrix &Mat, int row, int col, int val, int pos);

int GetValue(SparseMatrix Mat, int i, int j);

SparseMatrix Multiply(SparseMatrix A, SparseMatrix B);

void showSparseMatrix(SparseMatrix Mat);

void SparsetoNormal(SparseMatrix Mat);

int main()
{
    SparseMatrix A, B;

    std::cin >> A.m >> A.n;
    buildSparseMatrix(A);

    std::cin >> B.m >> B.n;
    buildSparseMatrix(B);

    // std::cout << "A----------------------------" << std::endl;
    // SparsetoNormal(A);
    // std::cout << "B----------------------------" << std::endl;
    // SparsetoNormal(B);

    if (A.n != B.m)
    {
        std::cout << "输入的矩阵不符合乘法运算!" << std::endl;
        return 0;
    }

    else
    {
        SparseMatrix result = Multiply(A, B);

        // std::cout << "----------------------------" << std::endl;
        showSparseMatrix(result);
        // std::cout << "C----------------------------" << std::endl;
        // SparsetoNormal(result);
        system("pause");
        return 0;
    }
}

void buildSparseMatrix(SparseMatrix &Mat)
{
    int row, col, val, cnt = 0;
    while (true)
    {
        std::cin >> row >> col >> val;
        if (row == 0 && col == 0 && val == 0)
            break;
        else
        {
            Mat.data[cnt].row = row;
            Mat.data[cnt].col = col;
            Mat.data[cnt].val = val;
            cnt++;
        }
    }
    Mat.len = cnt;
}

void insertSparseMatrix(SparseMatrix &Mat, int row, int col, int val, int pos)
{
    Mat.data[pos].row = row;
    Mat.data[pos].col = col;
    Mat.data[pos].val = val;
}

int GetValue(SparseMatrix Mat, int row, int col)
{
    int k = 0;
    while (k < Mat.len && (Mat.data[k].row != row || Mat.data[k].col != col))
    {
        k++;
    }
    if (k < Mat.len)
        return Mat.data[k].val;
    return 0;
}

SparseMatrix Multiply(SparseMatrix A, SparseMatrix B)
{
    SparseMatrix C;
    C.m = A.m, C.n = B.n;
    // 初始化C
    for (auto i = 0; i < C.m * C.n; ++i)
    {
        C.data[i].row = 0;
        C.data[i].col = 0;
        C.data[i].val = 0;
    }

    int pos = 0, temp = 0;

    for (auto i = 0; i < A.m; ++i)
    {
        for (auto j = 0; j < B.n; ++j)
        {
            for (auto k = 0; k < B.m; ++k)
                temp += GetValue(A, i, k) * GetValue(B, k, j);
            if (temp == 0)
                continue;
            insertSparseMatrix(C, i, j, temp, pos);
            temp = 0, pos++;
        }
    }

    C.len = pos;
    return C;
}

void showSparseMatrix(SparseMatrix Mat)
{
    for (auto i = 0; i < Mat.len; ++i)
    {
        std::cout << Mat.data[i].row << " " << Mat.data[i].col << " " << Mat.data[i].val << std::endl;
    }
}

void SparsetoNormal(SparseMatrix Mat)
{
    std::vector<std::vector<int>> temp(Mat.m, std::vector<int>(Mat.n, 0));
    for (auto i = 0; i < Mat.len; ++i)
    {
        temp[Mat.data[i].row][Mat.data[i].col] = Mat.data[i].val;
    }

    for (auto i = 0; i < Mat.m; ++i)
    {
        for (auto j = 0; j < Mat.n; ++j)
        {
            std::cout << std::setw(3) << temp[i][j] << ' ';
        }
        std::cout << std::endl;
    }
}

// 3 3
// 1 1 1
// 2 2 2
// 2 3 4
// 3 1 -4
// 0 0 0
// 3 3
// 1 3 -2
// 2 3 -5
// 3 1 8
// 3 2 -6
// 0 0 0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值