标题:想问问为啥错了。。想不明白
只看楼主
LRLIG
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2019-12-17
结帖率:0
已结贴  问题点数:20 回复次数:2 
想问问为啥错了。。想不明白
#ifndef CLASS_H
#define CLASS_H

#include <iostream>
#include <cstdlib>    //(rand()%10)
#include <ctime>
using namespace std;

class Matrix
{
private:
    int row;
    int column;
    int* data;

public:
    Matrix(int row2, int column2);
    Matrix();
    ~Matrix();
    Matrix operator+ (const Matrix& a) const;
    Matrix operator- (const Matrix& a) const;
    Matrix operator* (const Matrix& a) const;
    friend ostream& operator<< (ostream& out, Matrix& a);

};

Matrix::Matrix(int row2, int column2)
{
    srand((int)time(0));
    row = row2;
    column = column2;
    int i = 0, x = 0;

    int** p;
    p = new int* [row2]; //行 //申请行的空间
    //每行的列申请空间
    for (i = 0; i < row; i++)
    {
        p[i] = new int[column];
    }
   
    while (i < row2)
    {
        while (x < column)
        {
            p[i][x]=rand()%10;
            x++;
        }
        i++;
    }
    data = p[row2];
}

Matrix::Matrix()
{
    row = 0;
    column = 0;
    data = NULL;
}

Matrix::~Matrix()
{
    delete []data;
}



Matrix Matrix::operator+ (const Matrix& a) const
{
    Matrix A(row, column);
    int i = 0,x =0;
    while (i < row )
    {
        while (x < 4)
        {
            A.data[i][x] = data[i][x] + a.data[i][x];
            x++;
        }
        i++;
    }
    return A;
}

Matrix Matrix::operator- (const Matrix& a) const
{
    Matrix B(row, column);
    int i = 0,x = 0;
    while (i < row)
    {
        while (x < column)
        {
            B.data[i][x] = data[i][x] + a.data[i][x];
            x++;
        }
        i++;
    }
    return B;
}

Matrix Matrix::operator* (const Matrix& a) const
{
    Matrix C(row, a.column);
    int i = 0, x = 0;
    while (i < row)
    {
        while (x < column)
        {
            C.data[i][x] = data[row][x] * a.data[i][a.column];
            x++;
        }
        i++;
    }
    return C;
}

ostream& operator<< (ostream& out, Matrix& a)
{
    int i = 0;
    while (i < a.row * a.column)
    {
        out << *(a.data + i) << "   ";
        if (a.column / 4 + 1 == a.column)          /**/
        {
            out << endl;
        }
    }
    return out;
}

#endif
搜索更多相关主题的帖子: data int Matrix const row 
2020-11-18 00:13
LRLIG
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2019-12-17
得分:0 
有大佬解答吗
2020-11-18 00:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
看不懂你写的是什么;另外,少贴图

程序代码:
#include <iostream>

class Matrix
{
public:
    Matrix();
    Matrix( size_t row, size_t column );
    Matrix( const Matrix& a );
    ~Matrix();
    Matrix& operator=( const Matrix& a );
    int& at( size_t r, size_t c ) { return data_[r*column_+c]; }
    int at( size_t r, size_t c ) const { return data_[r*column_+c]; }

    void random();

    Matrix operator+ (const Matrix& a) const;
    Matrix operator- (const Matrix& a) const;
    Matrix operator* (const Matrix& a) const;
    friend std::ostream& operator<< (std::ostream& out, const Matrix& a);

private:
    size_t row_;
    size_t column_;
    int* data_;
};

#include <algorithm>
#include <ctime>
using namespace std;

Matrix::Matrix() : row_(), column_(), data_()
{
}
Matrix::Matrix( size_t row, size_t column ) : row_(row), column_(column), data_(new int[row_*column_]())
{
}
Matrix::Matrix( const Matrix& a ) : row_(a.row_), column_(a.column_), data_(new int[row_*column_])
{
    std::copy( a.data_, a.data_+a.row_*a.column_, data_ );
}
Matrix::~Matrix()
{
    delete[] data_;
}
Matrix& Matrix::operator=( const Matrix& a )
{
    if( this != &a )
    {
        delete[] data_;
        row_ = a.row_;
        column_ = a.column_;
        data_ = new int[row_*column_];
        std::copy( a.data_, a.data_+a.row_*a.column_, data_ );
    }
    return *this;
}

void Matrix::random()
{
    for( size_t r=0; r!=row_; ++r )
        for( size_t c=0; c!=column_; ++c )
            at(r,c) = rand()%10;
}

Matrix Matrix::operator+ (const Matrix& a) const
{
    size_t row = std::min( row_, a.row_ );
    size_t column = std::min( column_, a.column_ );
    Matrix tmp( row, column );

    for( size_t r=0; r!=row; ++r )
        for( size_t c=0; c!=column; ++c )
            tmp.at(r,c) = at(r,c) + a.at(r,c);
    return tmp;
}

Matrix Matrix::operator- (const Matrix& a) const
{
    size_t row = std::min( row_, a.row_ );
    size_t column = std::min( column_, a.column_ );
    Matrix tmp( row, column );

    for( size_t r=0; r!=row; ++r )
        for( size_t c=0; c!=column; ++c )
            tmp.at(r,c) = at(r,c) - a.at(r,c);
    return tmp;
}

Matrix Matrix::operator* (const Matrix& a) const
{
    size_t row = row_;
    size_t n = std::min( column_, a.row_ );
    size_t column = a.column_;
    Matrix tmp( row, column );

    for( size_t r=0; r!=row; ++r )
    {
        for( size_t c=0; c!=column; ++c )
        {
            tmp.at(r,c) = 0;
            for( size_t i=0; i!=n; ++i )
                tmp.at(r,c) += at(r,i) * a.at(i,c);
        }
    }
    return tmp;
}

std::ostream& operator<< (std::ostream& out, const Matrix& a)
{
    for( size_t r=0; r!=a.row_; ++r )
        for( size_t c=0; c!=a.column_; ++c )
            out << a.at(r,c) << (c+1==a.column_?"\n":"   ");
    return out;
}

int main( void )
{
    Matrix a(2,3), b(2,3), c(3,2);

    srand( (int)time(NULL) );
    a.random();
    b.random();
    c.random();

    cout << "a:\n" << a << endl;
    cout << "b:\n" << b << endl;
    cout << "a+b:\n" << a+b << endl;
    cout << "a-b:\n" << a-b << endl;

    cout << "-------------------------\n";
    cout << "a:\n" << a << endl;
    cout << "c:\n" << c << endl;
    cout << "a*b:\n" << a*c << endl;
}
2020-11-18 12:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-503923-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.205828 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved