NOJ数据结构012——以三元组为存储结构实现矩阵相加

在这里插入图片描述
本题练习三元组的应用。
思路较为简单,即比较两个三元组a、b行数列数后进行排序。
需要特别注意的是要考虑两个元素相加为0的情况,不能让这样的元素放在c中。

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int row,col;   /*非零元行下标、列下标*/
    int data;      /*非零元数据*/
}Triple;

typedef struct
{
    Triple *elem;   /*非零元的三元组表*/
    int len;        /*非零元个数*/
}TSMatrix;

TSMatrix a, b, c;

TSMatrix InitMatrix(int t);          //初始化三元矩阵
void CreateMatrix(TSMatrix *mat);    //输入矩阵数据
void AddMatrix(TSMatrix *a, TSMatrix *b, TSMatrix *c); //矩阵相加
void PrintMatrix(TSMatrix *c);       //打印矩阵

int main()
{
    int t1, t2;
    scanf("%d%d", &t1, &t2);
    a = InitMatrix(t1);
    b = InitMatrix(t2);
    c = InitMatrix(t1 + t2);
    CreateMatrix(&a);
    CreateMatrix(&b);
    AddMatrix(&a, &b, &c);
    PrintMatrix(&c);
    return 0;
}

TSMatrix InitMatrix(int t)
{    //初始化三元矩阵
    TSMatrix Mat;
    Mat.elem = (Triple*) malloc (sizeof(Triple)*t);
    Mat.len = t;
    return Mat;
}

void CreateMatrix(TSMatrix *mat)
{   //输入矩阵数据
    int tmp_row, tmp_col, tmp_data;
    for (int i = 0; i < mat->len; i++) {
        scanf("%d%d%d", &tmp_row, &tmp_col, &tmp_data);
        mat->elem[i].row = tmp_row;
        mat->elem[i].col = tmp_col;
        mat->elem[i].data = tmp_data;
    }
}

void AddMatrix(TSMatrix *a, TSMatrix *b, TSMatrix *c)
{   //矩阵相加
    int i = 0, j = 0, k = 0;
    c->len = 0;
    while (i < a->len && j < b->len) {
        if (a->elem[i].row < b->elem[j].row) { //a的行数小于b的行数
            c->elem[k] = a->elem[i];
            c->len++;
            i++;
            k++;
        }
        else if (a->elem[i].row > b->elem[j].row) { //a的行数大于b的行数
            c->elem[k] = b->elem[j];
            c->len++;
            j++;
            k++;
        }
        else if (a->elem[i].row == b->elem[j].row) { //a的行数等于b的行数
            if (a->elem[i].col < b->elem[j].col) { //a的列数小于b的列数
                c->elem[k] = a->elem[i];
                c->len++;
                i++;
                k++;
            }
            else if (a->elem[i].col > b->elem[j].col) {//a的列数大于b的列数
                c->elem[k] = b->elem[j];
                c->len++;
                j++;
                k++;
            }
            else if (a->elem[i].col == b->elem[j].col) {//a的列数等于b的列数
                int tmp = a->elem[i].data + b->elem[j].data;
                if (tmp != 0) {  //两元素相加不为0
                    c->elem[k].row = a->elem[i].row;
                    c->elem[k].col = a->elem[i].col;
                    c->elem[k].data = a->elem[i].data + b->elem[j].data;
                    c->len++;
                    i++;
                    j++;
                    k++;
                }
                else {
                    i++;
                    j++;
                }
            }
        }
    }
    /*当两个三元组有剩余元素时*/
    while(i < a->len) {
        c->elem[k] = a->elem[i];
        c->len++;
        k++;
        i++;
    }
    while(j < b->len) {
        c->elem[k] = b->elem[j];
        c->len++;
        k++;
        j++;
    }
}

void PrintMatrix(TSMatrix *c)
{  //打印矩阵
    for (int i = 0; i < c->len; i++) {
        printf("%d %d %d\n", c->elem[i].row, c->elem[i].col, c->elem[i].data);
    }
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值