以表格形式C编程表示矩阵

时间:2018-05-20 01:30:37

标签: c matrix tabular-form

目前,我正在尝试使用链接节点来表示矩阵。我的代码工作正常,但我不确定是否有可能以表格形式表示我的矩阵而不是(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;
}

我需要有人来启发我。

1 个答案:

答案 0 :(得分: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");
    }
}