以表格形式 C++ 表示矩阵
Represent Matrix in tabular form C++
目前,我正在尝试使用链接节点来表示矩阵。我的代码工作正常,但我不确定是否可以用表格形式表示我的矩阵而不是 (x,y) = value.
而我的 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 :> \n\n";
Printout(a);
cout << "\n::::::: MATRIX B :> \n\n";
Printout(b);
break;
default:
cout << "WRONG CHOICE\n\n";
}
} 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";
}
}
目前,我正在尝试使用链接节点来表示矩阵。我的代码工作正常,但我不确定是否可以用表格形式表示我的矩阵而不是 (x,y) = value.
而我的 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 :> \n\n";
Printout(a);
cout << "\n::::::: MATRIX B :> \n\n";
Printout(b);
break;
default:
cout << "WRONG CHOICE\n\n";
}
} 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";
}
}