找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 636|回复: 2

请教一个运算符重载程序

[复制链接]
发表于 2009-11-3 01:43:54 | 显示全部楼层 |阅读模式 IP:江苏扬州
有点错误 希望大家帮我找一找 我们正在学这个呢 谢谢大家了 如果有逻辑错误的话 请不要笑话我:
#include<iostream.h>
#include<iomanip.h>
class matrix
{
short rows,cols;
double *elems;
public:
matrix(){}
matrix(short r,short c);
double operator()(short r,short c);
void setelem(short r,short c,double v);
friend matrix operator+(matrix p,matrix q);
friend matrix operator-(matrix p,matrix q);
friend matrix operator*(matrix p,matrix q);
void print();
};
matrix::matrix(short r,short c)
{
rows=r;
cols=c;
elems=new double[r*c];
}
double matrix::operator ()(short r,short c)
{
return (r>=1&&r<=rows&&c>=1&&c<=cols)?elems[(r-1)*cols+(c-1)]:0.0;
}
void matrix::setelem(short r,short c,double v)
{
if(r>=1&&r<=rows&&c>=1&&c<=cols)
elems[(r-1)*cols+(c-1)]=v;
}
matrix operator+(matrix p,matrix q)
{
matrix m(p.rows,p.cols);
if(p.rows!=q.rows||p.cols!=q.cols)
{
cout<<"error";
}
for(int i=1;i<=p.rows;i++)
{
for(int j=1;j<=p.cols;j++)
{
m.setelem(i,j,p(i,j)+q(i,j));
}
}
return m;
}
matrix operator-(matrix p,matrix q)
{
matrix m(p.rows,p.cols);
if(p.rows!=q.rows||p.cols!=q.cols)
{
cout<<"error";
}
for(int i=1;i<=p.rows;i++)
{
for(int j=1;j<=p.cols;j++)
{
m.setelem(i,j,p(i,j)-q(i,j));
}
}
return m;
}
matrix operator*(matrix p,matrix q)
{
matrix m(p.rows,q.cols);
if(p.cols!=q.rows)
cout<<"error";
for(int i=1;i<=p.rows;i++)
{
for(int j=1;j<=q.cols;j++)
{
m.setelem(i,j,0.0);
for(int k=1;k<=p.cols;k++)
{
m.setelem(i,j,m(i,j)+p(i,k)+q(k,j));
}
return m;
}
void matrix::print()
{
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=cols;j++)
{
cout<<setw(7)<<(*this)(i,j);
}
cout<<endl;
}
}
void main()
{
matrix a(2,3),b(2,3),c(3,2),d(2,3),e(2,2);
a.setelem(1,1,-1.1);
a.setelem(1,2,2.2);
a.setelem(1,3,-3.3);
a.setelem(2,1,4.4);
a.setelem(2,2,-5.5);
a.setelem(2,3,6.6);
cout<<"A:"<<endl;a.print();
发表于 2009-11-3 01:43:56 | 显示全部楼层 IP:江苏扬州
以下是引用heliujin在2006-5-6 8:46:00的发言:
有点错误 希望大家帮我找一找 我们正在学这个呢 谢谢大家了 如果有逻辑错误的话 请不要笑话我:
#include<iostream.h>
#include<iomanip.h>
class matrix
{
short rows,cols;
double *elems;
public:
matrix(){}
matrix(short r,short c);
double operator()(short r,short c);
void setelem(short r,short c,double v);
friend matrix operator+(matrix p,matrix q);
friend matrix operator-(matrix p,matrix q);
friend matrix operator*(matrix p,matrix q);
void print();
};
matrix::matrix(short r,short c)
{
rows=r;
cols=c;
elems=new double[r*c];
}
double matrix::operator ()(short r,short c)
{
return (r>=1&&r<=rows&&c>=1&&c<=cols)?elems[(r-1)*cols+(c-1)]:0.0;
}
void matrix::setelem(short r,short c,double v)
{
if(r>=1&&r<=rows&&c>=1&&c<=cols)
elems[(r-1)*cols+(c-1)]=v;
}
matrix operator+(matrix p,matrix q)
{
matrix m(p.rows,p.cols);
if(p.rows!=q.rows||p.cols!=q.cols)
{
cout<<"error";
}
for(int i=1;i<=p.rows;i++)
{
for(int j=1;j<=p.cols;j++)
{
m.setelem(i,j,p(i,j)+q(i,j));
}
}
return m;
}
matrix operator-(matrix p,matrix q)
{
matrix m(p.rows,p.cols);
if(p.rows!=q.rows||p.cols!=q.cols)
{
cout<<"error";
}
for(int i=1;i<=p.rows;i++)
{
for(int j=1;j<=p.cols;j++)
{
m.setelem(i,j,p(i,j)-q(i,j));
}
}
return m;
}
matrix operator*(matrix p,matrix q)
{
matrix m(p.rows,q.cols);
if(p.cols!=q.rows)
cout<<"error";
for(int i=1;i<=p.rows;i++)
{
for(int j=1;j<=q.cols;j++)
{
m.setelem(i,j,0.0);
for(int k=1;k<=p.cols;k++)
{
m.setelem(i,j,m(i,j)+p(i,k)+q(k,j));
}
return m;
}
} //掉了两个括号,其他没错
}
void matrix::print()
{
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=cols;j++)
{
cout<<setw(7)<<(*this)(i,j);
}
cout<<endl;
}
}
void main()
{
matrix a(2,3),b(2,3),c(3,2),d(2,3),e(2,2);
a.setelem(1,1,-1.1);
a.setelem(1,2,2.2);
a.setelem(1,3,-3.3);
a.setelem(2,1,4.4);
a.setelem(2,2,-5.5);
a.setelem(2,3,6.6);
cout<<"A:"<<endl;a.print();
}
回复

使用道具 举报

发表于 2009-11-3 01:43:59 | 显示全部楼层 IP:江苏扬州
谢谢你了 以后我会注意的
呵呵
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 07:26 , Processed in 0.271140 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表