以表格形式呈现矩阵 C 编程

Present Matrix in tabular form C Programming

目前,我正在尝试使用链接节点来表示矩阵。我的代码工作正常,虽然我不确定是否可以用表格形式表示我的矩阵而不是 (x,y) = value 我想表示它(由零元素组成和非零元素。)

1   2   3
0   5   0
7   8   9

下面是我的代码与矩阵中的链接节点,我的程序将从用户读取行、列和值并打印出来。

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

typedef struct node
{
    int column;
    int value;
    int row;
    struct node *next;
} element;

void Init(element *x[])
{
    int i;
    for (i = 0; i < 3; i++) x[i] = NULL;
}

void Insert(element *x[], int row, int column, int value)
{
    int r = row;
    element *p;

    element *new = malloc(sizeof(element));
    new->row = row;
    new->column = column;
    new->value = value;

    if (x[r] == NULL)
    {
        x[r] = new;
        new->next = NULL;
    }
    else
    {
        p = x[r];
        if (new->column < p->column)
        {
            new->next = p;
            x[r] = new;
        }
        else if (new->column > p->column)
        {
            while (p->next != NULL && p->next->column < new->column)
            {
                p = p->next;
            }
            new->next = p->next;
            p->next = new;
        }
        else printf("An element already exists there!!\n");
    }
}

void Printout(element *x[])
{
    int i, test = 0;
    element *temp;

    for (i = 0; i < 3; i++)
    {
        temp = x[i];
        while (temp != NULL)
        {
            printf("Element position (%d,%d) = %d\n", i, temp->column, temp->value);
            test = 1;
            temp = temp->next;
        }
    }
    if (test == 0) printf("This matrix is empty!!\n");
}

int main(int argc, const char * argv[])
{
    int choice, column, row, value, number;
    element *a[3], *b[3], *sum[3];
    Init(a);    Init(b);    Init(sum);
    do
    {
        printf("\n***\tADDING SPARSE MATRICES\t***\n");
        printf("\n 1.) Insert in A");
        printf("\n 2.) Insert in B");
        printf("\n 3.) Printout both");
        printf("\n 0.) EXIT");
        printf("\nChoose ---------> ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:     /*Insert in A */
            do
            {
                printf("Enter row -> ");
                scanf("%d", &row);
            } while (row < 0 || row > 3);

            do
            {
                printf("Enter column -> ");
                scanf("%d", &column);
            } while (column < 0);

            printf("Enter value -> ");
            scanf("%d", &value);

            Insert(a, row, column, value);

            break;
        case 2:     /*Insert in B */
            do
            {
                printf("Enter row -> ");
                scanf("%d", &row);
            } while (row < 0 || row > 2);

            do
            {
                printf("Enter column -> ");
                scanf("%d", &column);
            } while (column < 0);

            printf("Enter value -> ");
            scanf("%d", &value);

            Insert(b, row, column, value);

            break;
        case 3:     /* Printout A & B */
            printf("\n::::::: MATRIX A :> \n\n");
            Printout(a);
            printf("\n::::::: MATRIX B :> \n\n");
            Printout(b);
            break;

        default:
            printf("\nWRONG CHOICE");
        }
    } while (choice != 0);

    return 0;
}

我需要有人开导我。

首先,代码需要计算出矩阵的width。这可以通过扫描所有元素以找到具有最大 column 的元素来完成。矩阵width最大column加一

然后,要打印矩阵中的每一行,打印零直到达到正确的 column。然后打印元素中的值,并移动到下一个元素。打印完所有元素后,打印额外的零,直到达到 width

void Printout(element *x[])
{
    // find the width of the matrix
    int width = -1;
    for (int row = 0; row < 3; row++)
    {
        for (element *node = x[row]; node != NULL; node = node->next)
            if (node->column > width)
                width = node->column;
    }
    width++;  // width is max column plus one

    // print each row of the matrix
    for (int row = 0; row < 3; row++)
    {
        int col = 0;
        for (element *node = x[row]; node != NULL; node = node->next)
        {
            for (; col < node->column; col++) // print zeros until the column is reached
                printf("0  ");
            printf("%d  ", node->value);      // print the value for the current element
            col = node->column + 1;
        }
        for (; col < width; col++)            // print zeros until the width is reached
            printf("0  ");
        printf("\n");
    }
}