繁体   English   中英

矩阵 class 中的任意矩阵维数

[英]Arbitrary matrix dimension in matrix class

任务如下: 用组件数据描述 class“数字矩阵”:矩阵的维度,元素的指针。 重载操作:<<(矩阵output到屏幕),+(矩阵相加),一元¬-(改变每个元素的符号),/=(每个元素除以一个数字)。 我执行了它,并且正确地执行了它,但是您需要从键盘设置矩阵维度,并且如您所见,它是为我预先设置的 [3] [3]。 这听起来很简单,但我真的很愚蠢。 在此先感谢您的帮助。 这是代码:

#include "pch.h"
#include <iostream>
using namespace std;
class Matrix
{
public:
    Matrix()
    {
        int Table[3][3];
    }
    int Table[3][3];
    void Create()
    {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                Table[i][j] = 10;
    }
};
ostream& operator <<(ostream& t, Matrix a)
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            t << a.Table[i][j] << " ";
        t << "\n";
    }
    return t;
}
Matrix& operator /=(Matrix& a, int num)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            a.Table[i][j] /= num;
    return a;
}
Matrix& operator -(Matrix& a, int empty)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            a.Table[i][j] = -a.Table[i][j];
    return a;
}
Matrix& operator +(Matrix& a, Matrix b)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            a.Table[i][j] += b.Table[i][j];
    return a;
}
int main()
{
    int u;
    setlocale(LC_ALL, "Russian");
    Matrix Example;
    Example.Create();
    Matrix Example1;
    Example1.Create();


    cout << Example;

    cout << Example1;
    cout << "Сумма матриц: "<<endl;
    cout << Example + Example1;

                Example - 1;

                Example1 - 1;


cout<< Example + Example1;

            cout << "На сколько вы хотите её поделить?\n";
            cin >> u;

                Example /= u;

                Example1 /= u;
                cout << Example;

                cout << Example1;


}`

您需要动态创建矩阵。 为此,您需要使用指针(*)。 更改 int table[3][3] 双表**;

如何实现它的一个例子(注意我使用矩阵而不是表格)

class Matrix {
private:
    double** matrix;
    int col;
    int row;
public:
    Matrix(){};
    void Create(int row, int col);
};


void Matrix::Create(int row_, int col_){

    double val = 0.0;
    col = col_; // initalize private members
    row = row_;

    matrix = new double*[row]; // Create a new array of size row_

    for(int i = 0; i < row; i++)
    {
        matrix[i] = new double[col]; // Create new cols of size col (inside array of row)
    }

    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            matrix[i][j] = val;
            val = val + 1.0;
        }
    }
}

为了简单起见,我尝试重用您的设计,但我真的建议您尝试在构造函数中指定矩阵的维度,甚至可以在其中构造矩阵。 像这样的东西:

Matrix(int row_, int col_) : row(row_), col(col_) {*/ create matrix here /*};

如果需要,您可以跳过“在此处创建矩阵”部分并使用您自己的 Create()。

为此,您需要动态 memory 分配。 除非明确告知您,否则我不会摆弄指针(新建/删除)。 作为初学者,您可能应该使用标准模板库 (STL) 工具:

#include <vector>并使用std::vector<std::vector<int>> Table而不是int Table[3][3] 然后像这样写一个构造函数:

Matrix(std::size_t rows, std::size_t cols)
{
    Table.resize(rows);
    for (unsigned int i = 0; i < Table.size(); ++i)
        Table[i].resize(cols);
}

您还可以存储矩阵的维度,但没有必要这样做,因为您可以从向量中获取信息。 用相应的动态大小(存储或从向量中提取)替换所有循环中的硬编码维度。 例如:


Matrix& operator +(Matrix& a, Matrix b)
{
    unsigned int rows = a.Table.size();
    unsigned int cols = a.Table[0].size();

    for (unsigned int i = 0; i < rows; i++)
        for (unsigned int j = 0; j < cols; j++)
            a.Table[i][j] += b.Table[i][j];
    return a;
}

然而,这个向量的向量并不是真正有效的。 最好是单个向量,但我想对于初学者来说还可以。

问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM