求c++矩阵相加和相减

征服全世界Orz 2012-04-01 02:02:08
1.编程实现矩阵相加和相减。(提示:定义矩阵类,数据成员为数组,重载+,-两个运算符)。
例如:
123 213 336
231 + 132 = 363
321 321 633
...全文
541 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
GYTAnonymous 2012-04-01
  • 打赏
  • 举报
回复
注意结贴啊。。。不然别人不愿意给你解决问题奥
GYTAnonymous 2012-04-01
  • 打赏
  • 举报
回复
正好我又写过。。

给你参考参考。。我多了个重载*号,,还有 = << >> ;,

头文件

#ifndef MATRIX_H
#define MATRIX_H

class Matrix
{
public:
Matrix( int =0, int =0);
Matrix( double** , int , int );
Matrix( const Matrix& );
~Matrix();
void Set( double**, int, int );
friend Matrix operator+ ( const Matrix&, const Matrix& );
friend Matrix operator- ( const Matrix&, const Matrix& );
friend Matrix operator* ( const Matrix&, const Matrix& );
Matrix& operator= ( const Matrix& );
friend istream& operator>> ( istream&, Matrix& );
friend ostream& operator<< ( ostream&, const Matrix& );
protected:
double** ma;
int length;
int width;
};

//functions
Matrix::Matrix( int wid, int len )
{
if( len==0 || wid==0 )
{
length = width = 0;
ma = '\0';
return ;
}
length = len;
width = wid;
ma = new double*[width];
for( int i=0; i<width; i++ )
ma[i] = new double[length];
}
Matrix::Matrix( double** p, int wid, int len )
{
if( wid<=0 || len <=0 )
{
cout << "error !\n";
exit(0);
}
Set( p, wid, len );
}
Matrix::Matrix( const Matrix& Matr )
{
Set( Matr.ma, Matr.width, Matr.length );
}
Matrix::~Matrix()
{
for( int i=0; i<width; i++ )
delete[] ma[i];
delete[] ma;
ma = NULL;
width = length =0;
}
void Matrix::Set( double** p, int wid, int len )
{
length = len;
width = wid;
ma = new double*[width];
for( int i=0; i<width; i++ )
{
ma[i] = new double[length];
for( int j=0; j<length; j++ )
ma[i][j] = p[i][j];
}
}
Matrix operator+ ( const Matrix& ma1, const Matrix& ma2 )
{
if( ma1.width != ma1.width || ma1.length != ma2.length )
{
cout << "error !\n";
exit(0);
}
Matrix temp( ma1.width, ma1.length );
for( int i=0; i<temp.width; i++ )
for( int j=0; j<temp.length; j++ )
temp.ma[i][j] = ma1.ma[i][j] + ma2.ma[i][j];
return temp;
}
Matrix operator- ( const Matrix& ma1, const Matrix& ma2 )
{
if( ma1.width != ma1.width || ma1.length != ma2.length )
{
cout << "error !\n";
exit(0);
}
Matrix temp( ma1.width, ma1.length );
for( int i=0; i<temp.width; i++ )
for( int j=0; j<temp.length; j++ )
temp.ma[i][j] = ma1.ma[i][j] - ma2.ma[i][j];
return temp;
}
Matrix operator* ( const Matrix& ma1, const Matrix& ma2 )
{
if( ma1.length != ma2.width )
{
cout << "error !\n";
exit(0);
}
Matrix temp( ma1.width, ma2.length );
for( int i=0; i<temp.width; i++ )
for( int j=0; j<temp.length; j++ )
temp.ma[i][j] = 0;
for( int i=0; i<ma1.width; i++ )
for( int j=0; j<ma2.length; j++ )
for( int k=0; k<ma1.length; k++ )
temp.ma[i][j] += ma1.ma[i][k] * ma2.ma[k][j];
return temp;
}
Matrix& Matrix:: operator= ( const Matrix& matr )
{
for( int i=0; i<width; i++ )
for( int j=0; j<length; j++ )
ma[i][j] = matr.ma[i][j];
return *this;
}
istream& operator>> ( istream& input, Matrix& matr )
{
for( int i=0; i<matr.width; i++ )
for( int j=0; j<matr.length; j++ )
cin >> matr.ma[i][j];
cout << endl;
return input;
}
ostream& operator<< ( ostream& output, const Matrix& matr )
{
for( int i=0; i<matr.width; i++ )
{
for( int j=0; j<matr.length; j++ )
cout << setw(8) << matr.ma[i][j] << " ";
cout << endl;
}
cout << endl;
return output;
}
#endif



主函数

#include<iostream>
#include<iomanip>
using namespace std;
#include"Matrix.h"
//main function

int main()
{
int wid, len;
cout << "Enter the width and length of the matrix1\n";
cin >> wid >> len;
Matrix ma1( wid, len );
cout << "Enter the matrix1\n";
cin >> ma1;

cout << "Enter the width and length of the matrix2\n";
cin >> wid >> len;
Matrix ma2( wid, len );
cout << "Enter the matrix2\n";
cin >> ma2;

cout << " ma1 + ma2 : \n" << ma1 + ma2;
cout << " ma1 - ma2 : \n" << ma1 - ma2;
cout << " ma1 * ma2 : \n" << ma1 * ma2;
}
cbzjzsb123 2012-04-01
  • 打赏
  • 举报
回复
有一本书,叫《常用算法程序集(C++语言描述)》,徐士良编著。里边第一章。

yuxi1989 2012-04-01
  • 打赏
  • 举报
回复
有一本书,叫《常用算法程序集(C++语言描述)》,徐士良编著。里边第一章。

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧