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;
}
``````

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