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

标签 c++ matrix tabular-form

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

关于c++ - 以表格形式 C++ 表示矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50428261/

相关文章:

javascript - oracle apex 表格形式的动态计算 : sys. htp.p

oracle-apex - 带有 [行选择器] : How to set value of column in selected rows on submit 的 APEX 表格形式

c++ - 里面有结构的类.. C++

c++ - 返回替代代码而不是 int C++

c++ - 虚拟继承 - 与其他派生类共享的基础

random - 根据任意分布设置 Eigen::Matrix 的系数

C++ cin 与 C sscanf

r - 如何读取 R 中缺少末端元素的矩阵?

c - 矩阵乘法问题