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

用表格形式C编程表示矩阵,c,matrix,tabular-form,C,Matrix,Tabular Form,目前,我正在尝试使用链接节点来表示矩阵。我的代码运行良好,但我不确定是否可以用表格形式表示矩阵,而不是(x,y)=值我希望将其表示为(由零元素和非零元素组成) 下面是我在矩阵中链接节点的代码,我的程序将从用户那里读取行、列和值并打印出来 #include <stdio.h> #include <stdlib.h> typedef struct node { int column; int value; int row; struct no

目前,我正在尝试使用链接节点来表示矩阵。我的代码运行良好,但我不确定是否可以用表格形式表示矩阵,而不是(x,y)=值我希望将其表示为(由零元素和非零元素组成)

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

#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;
}
#包括
#包括
类型定义结构节点
{
int列;
int值;
int行;
结构节点*下一步;
}元素;
void Init(元素*x[]
{
int i;
对于(i=0;i<3;i++)x[i]=NULL;
}
空插入(元素*x[],整数行,整数列,整数值)
{
int r=行;
元素*p;
元素*新=malloc(sizeof(元素));
新建->行=行;
新建->列=列;
新建->值=值;
如果(x[r]==NULL)
{
x[r]=新的;
新建->下一步=空;
}
其他的
{
p=x[r];
如果(新建->列列)
{
新建->下一步=p;
x[r]=新的;
}
else if(新建->列>p->列)
{
而(p->next!=NULL&&p->next->columncolumn)
{
p=p->next;
}
新建->下一步=p->下一步;
p->next=新建;
}
else printf(“该元素已存在!!\n”);
}
}
无效打印输出(元素*x[])
{
int i,测试=0;
元件*温度;
对于(i=0;i<3;i++)
{
温度=x[i];
while(temp!=NULL)
{
printf(“元素位置(%d,%d)=%d\n”,i,temp->column,temp->value);
测试=1;
温度=温度->下一步;
}
}
如果(test==0)printf(“此矩阵为空!!\n”);
}
int main(int argc,const char*argv[]
{
int选项、列、行、值、数字;
元素*a[3],*b[3],*sum[3];
初始值(a);初始值(b);初始值(和);
做
{
printf(“\n***\t加密稀疏矩阵\t***\n”);
printf(“\n 1.)插入A”);
printf(“\n 2.)插入B”);
printf(“\n 3.)打印输出两者”);
printf(“\n 0.)退出”);
printf(“\n选择---------------->”;
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:/*在A中插入*/
做
{
printf(“输入行->”;
scanf(“%d”行和第行);
}而(行数<0 | |行数>3);
做
{
printf(“输入列->”;
scanf(“%d”列和列);
}而(列数<0);
printf(“输入值->”;
scanf(“%d”,和值);
插入(a、行、列、值);
打破
案例2:/*在B中插入*/
做
{
printf(“输入行->”;
scanf(“%d”行和第行);
}而(行数<0 | |行数>2);
做
{
printf(“输入列->”;
scanf(“%d”列和列);
}而(列数<0);
printf(“输入值->”;
scanf(“%d”,和值);
插入(b、行、列、值);
打破
案例3:/*打印输出A和B*/
printf(“\n:MATRIX A:>\n\n”);
打印输出(a);
printf(“\n:MATRIX B:>\n\n”);
打印输出(b);
打破
违约:
printf(“强选择”);
}
}while(选项!=0);
返回0;
}

我需要有人来启发我。

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

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

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");
    }
}
void打印输出(元素*x[]
{
//求矩阵的宽度
整数宽度=-1;
对于(int行=0;行<3;行++)
{
对于(元素*node=x[行];节点!=NULL;节点=node->next)
如果(节点->列>宽度)
宽度=节点->列;
}
宽度+++;//宽度是最大列加上一
//打印矩阵的每一行
对于(int行=0;行<3;行++)
{
int col=0;
对于(元素*node=x[行];节点!=NULL;节点=node->next)
{
对于(;colcolumn;col++)//打印零,直到到达该列为止
printf(“0”);
printf(“%d”,节点->值);//打印当前元素的值
列=节点->列+1;
}
对于(;col
请澄清您的具体问题或添加其他详细信息,以突出显示您所需的内容。正如目前所写的,很难准确地说出你在问什么。请参阅页面以获取澄清此问题的帮助。我不明白您想要什么,例如
x[a][b]
?请注意,就像您是初学者一样,您的代码没有多大意义,例如,您似乎使用链表,但您使用数组
元素*a[3]
来存储它们。这真的不清楚,请不要犹豫,描述更多你想要的。顺便说一句,这个问题不太可能有用,你应该读一些关于C的书。有很多资源需要学习。在这里,我们将给您一些好的代码,但您可能不会从中学到很多东西。使用
new
作为变量名不是一个好主意,因为它在C++/Java/中是一个关键字。。它可能会导致混乱,因为它工作得很好。我会接受它作为我们的答案
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");
    }
}