Dormant7 2013-05-29 14:23 采纳率: 33.3%
浏览 2081

C++问题 矩阵运算的问题

#ifndef MATRIX_H
#define MATRIX_H

#include<iostream>
using namespace std;

class Matrix;
Matrix operator+ (const Matrix &a, const Matrix &b);
Matrix operator- (const Matrix &a, const Matrix &b);
Matrix operator* (const Matrix &a, const Matrix &b);

class Matrix
{

    friend Matrix operator+ (const Matrix &a, const Matrix &b);
    friend Matrix operator- (const Matrix &a, const Matrix &b);
    friend Matrix operator* (const Matrix &a, const Matrix &b);

public:
    Matrix(int l, int c);
    Matrix(const Matrix &m);
    ~Matrix();

    void setLine(int l);
    void setElems();
    void setCol(int c);

    int getLine() const;
    int getCol() const;
    void print() const;

    Matrix operator=(const Matrix &m);
    Matrix operator~() const;

private:
    int line;
    int col;
    int *elems;
};

#endif


 **函数实现:** 

#include"Matrix.h"

Matrix operator+ (const Matrix &a, const Matrix &b)
{
    if(a.col != b.line)
    {
        cerr<<"第一个矩阵的列数和第二个矩阵的行数不相等!"<<endl;
        exit(EXIT_FAILURE);
    }

    int line = a.line;
    int col = b.col;
    Matrix temp(line, col);

    for(int i = 0; i < line * col; i++)
        temp.elems[i] = 0;

    for(int j = 0; j < line * col; j++)
        temp.elems[j] = a.elems[j] + b.elems[j];

    return temp;
}

Matrix operator- (const Matrix &a, const Matrix &b)
{
    if(a.col != b.line)
    {
        cerr<<"第一个矩阵的列数和第二个矩阵的行数不相等!"<<endl;
        exit(EXIT_FAILURE);
    }

    int line = a.line;
    int col = b.col;
    Matrix temp(line, col);

    for(int i = 0; i < line * col; i++)
        temp.elems[i] = 0;

    for( int j = 0; j < line * col; j++)
        temp.elems[j] = a.elems[j] - b.elems[j];

    return temp;
}

Matrix operator* (const Matrix &a, const Matrix &b)
{
    if(a.col != b.line)
    {
        cerr<<"第一个矩阵的列数和第二个矩阵的行数不相等!"<<endl;
        exit(EXIT_FAILURE);
    }

    int line = a.line;
    int col = b.col;
    int s , sum = 0;
    Matrix temp(line, col);

    for(int m = 0; m < line * col; m++)
        temp.elems[m] = 0;

    for(int i = 0; i < line; i++)
    {
        for(int k = 0; k < col; k++)
        {
            for(int j = 0; j< b.line; j++)
            {
                s = a.elems[i + j + line * i] * b.elems[k + line * j];
                sum = sum + s;
            }
            temp.elems[line * i + k] = sum;
        }
    }
    return temp;
}

Matrix::Matrix(int l, int c)
{
    line = l;
    col = c;
    elems = new int [line * col];
}

Matrix::Matrix(const Matrix &m)
{
    line = m.line;
    col = m.col;
}

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

void Matrix::setLine(int l)
{
    line = l;
}

void Matrix::setCol(int c)
{
    col = c;
}

void Matrix::setElems()
{
    for( int i = 0; i < line * col; i++)
    {
        cin>> elems[i];
    }    
    cout<<endl;
}

int Matrix::getLine() const
{
    return line;
}

int Matrix::getCol() const
{
    return col;
}

void Matrix::print() const
{
    for(int i = 0; i < line * col; i++)
    {
        cout<<elems[i]<<" ";
        if((i + 1) % 3 == 0)
            cout<<endl;
    }
}

Matrix Matrix::operator=(const Matrix &m)
{ 
    this->line = m.line;
    this->col = m.col;
    this->elems = m.elems;
    return *this;
}

Matrix Matrix::operator~() const
{

    Matrix temp(line, col);
    for(int i = 0; i < line * col; i++)
        temp.elems[i] = 0;

    for(int i = 0; i < line; i++)
    {
        for(int j = 0; j < col; j++)
        {
            temp.elems[j + line * i] = elems[j * col + i];
        }
    }
    return temp;
}


 **主函数:** 
#include"Matrix.h"

int main()
{
    Matrix a(3,3), b(3,3);
    cout<<"请输入第一个矩阵的元素:"<<endl;
    a.setElems();
    cout<<"请输入第二个矩阵的元素:"<<endl;
    b.setElems();

    Matrix c(3,3);
    cout<<"两矩阵的乘积为:"<<endl;
    c = a * b;
    c.print();

    cout<<"\n转置矩阵为:"<<endl;
    (~c).print();
    return 0;
}

这是我按书上要求写的一个程序,就是实现矩阵乘法,加法,减法,和转置的程序,但不知哪错了,得不到想要的结果,想请各位牛人给改一改,谢谢大家!

声明: 要用一维数组进行编写程序,我不清楚这个elems怎么初始化,和动态申请与释放,和其中的函数实现

要求的最终实现:

请输入第一个矩阵的元素:
1 2 3 4 5 6 7 8 9
请输入第一个矩阵的元素:
9 8 7 6 5 4 3 2 1
两矩阵的乘积为:
30 24 18
84 69 54
138 114 90

转置矩阵为:
30 84 138
24 69 114
18 54 90

  • 写回答

1条回答

  • u010712606 2013-05-29 14:34
    关注

    这个问题很复杂,矩阵应该是30 84 138

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决