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

时间:2018-05-19 18:24:31

标签: 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 ++编程,需要有人来启发我。谢谢你的关注。

1 个答案:

答案 0 :(得分:0)

由于您的链接列表用于保存内存使用量,您可以编写一些代码来打印零,直到达到宽度。

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";
    }
}