| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1467 人关注过本帖
标题:想问问为啥错了。。想不明白
只看楼主 加入收藏
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
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册: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
快速回复:想问问为啥错了。。想不明白
数据加载中...
 
   



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

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