求指导!!!急求!!!

s12rhxgc 2011-06-12 10:27:08
定义一个二维方阵类,通过重载实现二元运算“=”、“+”、 “-”、 “*”、 “~”, 分别实现方阵的赋值、矩阵加、矩阵减,矩阵乘及矩阵的转置
要求如下:
 定义几个矩阵类,包含一个二维数组,或者二维指针(高级,可以加分)
 矩阵C=A*B乘定义为 A的第i行乘以B的第j列的值的累加为C的第i行的第j元素,
 转置矩阵的定义为,A=~B B的第i行第j列的元素为A的第j行第i列元素。
[color=#FF0000]求高手帮修改下 急求
#include "stdafx.h"
#include"iostream.h"

const int r=3; const int c=3 const int k=3
class matrix
{
int a[r][c];
public:
matrix();
matrix(int t[r][c])
{
a[i][j]=t[i][j];
}

friend matrix operator+ (matrix & a, matrix & b); //矩阵加
friend matrix operator- (matrix & a, matrix & b); //矩阵减
friend matrix operator* (matrix & a, matrix & b); //矩阵乘
friend matrix operator~ (matrix & a); //矩阵转置
void display();
}
matrix operator +(matrix & a, matrix & b);
{
for (i=0;i<r;i++)
for(j=0;j<c;j++)
c[i][j]=a[i][j]+b[i][j];
}
matrix operator -(matrix & a, matrix & b);
{
for (i=0;i<r;i++)
for(j=0;j<c;j++)
c[i][j]=a[i][j]-b[i][j];
}
matrix operator+ (matrix & a, matrix & b);
{
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
c[i][j]=0
for(l=0;l<k;l++)
c[i][j]=a[i][j]+b[i][j];
}
}

matrix operator~ (matrix & a);
{
for(i=0;i<k;i++)
for(j=0;j<c;j++)
c[j][i]=a[i][j];
}
matrik::void display()
{
for(i=0;i<r;i++)
for(j<0;j<c;j++)
cout<<c[i][j]<<endl;
}
void main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int b[3][3]={1,2,3, 0,1,2, -1,0,1}
matrix x(a), y(b);
cout<<"-------- x= ------------"<<endl;
x.display(); //按设定格式显示出第一个矩阵
x cout<<"-------- y= ------------"<<endl;
y.display() //按设定格式显示出第二个矩阵
y cout<<"--------- x+y= ---------"<<endl;
(x+y).display(); //输出结果矩阵 x+y
cout<<"--------- x-y= ---------"<<endl;
(x-y).display(); //输出结果矩阵x-y
cout<<"--------- x*y= ---------"<<endl;
(x*y).display(); //输出结果矩阵x*y
cout<<"--------- ~x= ---------"<<endl;
(~x).display(); //输出结果矩阵~x
}


...全文
84 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
DAsama 2011-06-12
  • 打赏
  • 举报
回复

matrix operator* (matrix & m, matrix & n)
{
int d[3][3];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
for(int r=0;r<c;r++)
d[i][j]=m.a[i][r]*n.a[r][j];
}
return matrix(d);
}

补充个乘法的
ryfdizuo 2011-06-12
  • 打赏
  • 举报
回复
矩阵不能直接a[i][j]取元素,需要重载operator[][]或者直接调用成员数组a
ryfdizuo 2011-06-12
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

const int r=3;
const int c=3;
const int k=3;

class matrix
{
int a[r][c];
public:
matrix() {}
matrix(int t[r][c])
{
for (int i=0; i<r; i++)
for(int j=0; j<c; j++)
a[i][j]=t[i][j];
}

friend matrix operator+ (matrix & a, matrix & b); //矩阵加
friend matrix operator- (matrix & a, matrix & b); //矩阵减
friend matrix operator* (matrix & a, matrix & b); //矩阵乘
friend matrix operator~ (matrix & a); //矩阵转置
void display();
}; //注意分号

matrix operator +(matrix & a, matrix & b) //分号去掉
{
matrix ret;
for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
ret.a[i][j] = a.a[i][j] + b.a[i][j];

return ret;
}
matrix operator -(matrix & a, matrix & b)
{
matrix ret;
for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
ret.a[i][j] = a.a[i][j]-b.a[i][j];

return ret;
}
matrix operator* (matrix & a, matrix & b)
{
matrix ret;

for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
ret.a[i][j]=0;
for(int l=0; l<k; l++)
ret.a[i][j] = a.a[i][j] + b.a[i][j];
}

return ret;
}

matrix operator~ (matrix & a)
{
matrix ret;

for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
ret.a[j][i] = ~a.a[i][j];

return ret;
}
void matrix::display()
{
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
cout<< this->a[i][j] << " ";

cout << endl;
}
}

void main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int b[3][3]={1,2,3, 0,1,2, -1,0,1};
matrix x(a);
matrix y(b);
cout<<"-------- x= ------------"<<endl;
x.display(); //按设定格式显示出第一个矩阵
y.display(); //按设定格式显示出第二个矩阵

(x+y).display(); //输出结果矩阵 x+y
cout<<"--------- x-y= ---------"<<endl;
(x-y).display(); //输出结果矩阵x-y
cout<<"--------- x*y= ---------"<<endl;
(x*y).display(); //输出结果矩阵x*y
cout<<"--------- ~x= ---------"<<endl;
(~x).display(); //输出结果矩阵~x

system("PAUSE");
}
  • 打赏
  • 举报
回复

#include <iostream>
#include <stdio.h>
#include <iostream>

using namespace std;

const int r=3; const int c=3; const int k=3;
class matrix
{
int a[r][c];
public:
matrix(){}
matrix(int t[3][3])
{
for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j] = t[i][j];
}

friend matrix operator+ (matrix & a, matrix & b); //矩阵加
friend matrix operator- (matrix & a, matrix & b); //矩阵减
//friend matrix operator* (matrix & a, matrix & b); //矩阵乘
friend matrix operator~ (matrix & a); //矩阵转置
void display();
};


matrix operator +(matrix & m, matrix & n)
{
int d[3][3];
for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
d[i][j]=m.a[i][j]+n.a[i][j];

return matrix(d);
}
matrix operator -(matrix & m, matrix & n)
{
int d[3][3];
for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
d[i][j]=m.a[i][j]-n.a[i][j];

return matrix(d);
}
//matrix operator* (matrix & m, matrix & n)
//{
//
// int d[3][3];
//for(int i=0;i<r;i++)
//for(int j=0;j<c;j++)
//{
// d[i][j]=m.a[i][j]*n.a[i][j];
//
//}




matrix operator~ (matrix & n)
{

int d[3][3];
for(int i=0;i<k;i++)
for(int j=0;j<c;j++)
d[j][i]=n.a[i][j];

return matrix(d);
}

void matrix::display()
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cout<<a[i][j]<<endl;
}
void main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int b[3][3]={1,2,3, 0,1,2, -1,0,1};
matrix x(a), y(b);
matrix z;
cout<<"-------- x= ------------"<<endl;
x.display(); //按设定格式显示出第一个矩阵
cout<<"-------- y= ------------"<<endl;
y.display(); //按设定格式显示出第二个矩阵
cout<<"--------- x+y= ---------"<<endl;
z = x+y;
z.display(); //输出结果矩阵 x+y
cout<<"--------- x-y= ---------"<<endl;
z = x-y;
z.display(); //输出结果矩阵x-y
cout<<"--------- x*y= ---------"<<endl;

cout<<"--------- ~x= ---------"<<endl;
z = ~x;
(~x).display(); //输出结果矩阵~x
}

给你个能运行的,不过 矩阵乘那块,你的实现有点问题,我给你注掉了,我刚才看了,这个可以正常运行


楼主给分吧
  • 打赏
  • 举报
回复

#include <iostream>
#include <stdio.h>
#include <iostream>

using namespace std;

const int r=3; const int c=3; const int k=3;
class matrix
{
int a[r][c];
public:
matrix(){}
matrix(int t[3][3])
{
for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j] = t[i][j];
}

friend matrix operator+ (matrix & a, matrix & b); //矩阵加
friend matrix operator- (matrix & a, matrix & b); //矩阵减
friend matrix operator* (matrix & a, matrix & b); //矩阵乘
friend matrix operator~ (matrix & a); //矩阵转置
void display();
};


matrix operator +(matrix & a, matrix & b)
{
int d[3][3];
for (int i=0;i<r;i++)
for(int j=0;j<c;j++)
d[i][j]=a[i][j]+b[i][j];

return matrix(d);
}
matrix operator -(matrix & a, matrix & b)
{
int d[3][3];
for (i=0;i<r;i++)
for(j=0;j<c;j++)
d[i][j]=a[i][j]-b[i][j];

return matrix(d);
}
matrix operator+ (matrix & a, matrix & b)
{

int d[3][3];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
d[i][j]=a[i][j]+b[i][j];

}


return matrix(d);
}

matrix operator~ (matrix & a)
{

int d[3][3];
for(int i=0;i<k;i++)
for(int j=0;j<c;j++)
d[j][i]=a[i][j];

return matrix(d);
}

void matrix::display()
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cout<<a[i][j]<<endl;
}
void main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int b[3][3]={1,2,3, 0,1,2, -1,0,1};
matrix x(a), y(b);
matrix z;
cout<<"-------- x= ------------"<<endl;
x.display(); //按设定格式显示出第一个矩阵
cout<<"-------- y= ------------"<<endl;
y.display(); //按设定格式显示出第二个矩阵
cout<<"--------- x+y= ---------"<<endl;
z = x+y;
z.display(); //输出结果矩阵 x+y
cout<<"--------- x-y= ---------"<<endl;
z = x-y;
z.display(); //输出结果矩阵x-y
cout<<"--------- x*y= ---------"<<endl;
z = x*y;
z.display(); //输出结果矩阵x*y
cout<<"--------- ~x= ---------"<<endl;
z = ~x;
(~x).display(); //输出结果矩阵~x
}

s12rhxgc 2011-06-12
  • 打赏
  • 举报
回复
有问题 实现不了 很纠结 帮忙改改吧 新手 很困难啊

64,639

社区成员

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

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