以表格形式表示矩阵C++

Represent Matrix in tabular form C++

本文关键字:C++ 表示 表格      更新时间:2023-10-16

目前,我正在尝试使用链接节点来表示矩阵。我的代码工作正常,但我不确定是否可以以表格形式而不是(x,y( = 值表示我的矩阵。

虽然我的 printout(( 方法仅在 temp != NULL 时打印出来,这就是结果

Element position(3,3) = 9

我想像这样表示它

1   2   3
4   5   6
7   8   9

以下是我在矩阵中链接节点的代码

#include <iostream>
#include <conio.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
using namespace std;

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

void Init(element *x[])
{
int i;
for (i = 0; i < 11; i++) {
x[i] = NULL;
}
}
void Insert(element *x[], int row, int column, int value)
{
int r = row;
element *p;
element *news = (element*)malloc(sizeof(element));
news->row = row;
news->column = column;
news->value = value;
if (x[r] == NULL)
{
x[r] = news;
news->next = NULL;
}
else
{
p = x[r];
if (news->column < p->column)
{
news->next = p;
x[r] = news;
}
else if (news->column > p->column)
{
while (p->next != NULL && p->next->column < news->column)
{
p = p->next;
}
news->next = p->next;
p->next = news;
}
else cout << "An element already exists there!!n";
}
}

void Printout(element *x[])
{
int i, test = 0;
element *temp;
for (i = 0; i < 11; i++) {
temp = x[i];
while (temp != NULL) {
cout << "Element position" << "(" << i << "," << temp->column << ") = " << temp->value << endl;
test = 1;
temp = temp->next;
}
}
if (test == 0) {
cout << "This matrix is empty" << endl;
}
}

int main(int argc, const char * argv[]) {
int choice, column, row, value, number;
element *a[10], *b[10], *sum[10];
Init(a);    Init(b);    Init(sum);
do
{
cout << "Add Sparse Matrix" << endl;
cout << "1. Insert in A" << endl;
cout << "2. Insert in B" << endl;
cout << "3. Print 2 matrix" << endl;
cout << "0. Exit" << endl;
cout << "Please enter your option" << endl;
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter row -> ";
cin >> row;
} while (row < 0 || row > 11);
do
{
cout << "Enter column -> ";
cin >> column;
} while (column < 0);
cout << "Enter value -> ";
cin >> value;
Insert(a, row, column, value);
break;
case 2:
do
{
cout << "Enter row -> ";
cin >> row;
} while (row < 0 || row > 11);
do
{
cout << "Enter column -> ";
cin >> column;
} while (column < 0);
cout << "Enter value -> ";
cin >> value;
Insert(b, row, column, value);
break;
case 3:
cout << "n::::::: MATRIX A :> nn";
Printout(a);
cout << "n::::::: MATRIX B :> nn";
Printout(b);
break;
default:
cout << "WRONG CHOICEnn";
}
} while (choice != 0);


return 0;

}

很抱歉问这个问题,我刚开始学习编程C++,需要有人启发我。感谢您的关注。

由于链表用于节省内存使用量,因此您可以编写一些代码来打印零,直到达到宽度。

void SM::Printout(element *x[])
{
int width = -1;
for (int row = 0; row < 11; row++)
{
for (element *node = x[row]; node != NULL; node = node->next)
if (node->column > width)
width = node->column;
}
width++;
for (int row = 0; row < 11; row++)
{
int col = 0;
for (element *node = x[row]; node != NULL; node = node->next)
{
for (; col < node->column; col++)
cout << "0  ";
if (node->value != NULL)
cout << node->value << "  ";
col = node->column + 1;
}
for (; col < width; col++)
cout << "0  ";
cout << "n";
}
}