2 meidong52617 meidong52617 于 2013.05.29 22:23 提问

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
u010712606   2013.05.29 22:34

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!