返回信息流搞定了 180斤大汉跪谢诸位帝捧场
这是一条镜像帖。来源:北邮人论坛 / cpp / #73227同步于 2013/8/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
Re: c++作业题,求帝
sharonyue
2013/8/15镜像同步18 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
change
istream& operator>>(istream& is, const complex& complex_)
to
istream& operator>>(istream& is, complex& complex_)
【 在 sharonyue 的大作中提到: 】
: #include <iostream>
: using namespace std;
: class complex
: ...................
【 在 tonyjansan 的大作中提到: 】
: change
: istream& operator>>(istream& is, const complex& complex_)
: to
: ...................
#include <iostream>
using namespace std;
class complex
{
private:
double real_;
double imaginary_;
public:
complex();
complex(double real,double imaginary_);
~complex();
complex operator+(const complex& complex_)const;
complex operator-(const complex& complex_)const;
complex operator*(const complex& complexa_,const complex& complexb_)const;
friend complex operator*(double a,const complex& complex_);
friend ostream& operator<<(ostream& os, const complex& complex_);
friend istream& operator>>(istream& os, complex& complex_);
};
complex::complex(){real_ = imaginary_ = 0;}
complex::complex(double real,double imaginary)
{
real_ = real;
imaginary_ = imaginary;
}
complex::~complex(){};
complex complex::operator+(const complex& complex_)const
{
complex sum;
sum.real_ = real_ + complex_.real_;
sum.imaginary_ = imaginary_ + complex_.imaginary_;
return sum;
}
complex complex::operator-(const complex& complex_)const
{
complex sub;
sub.real_= real_ - complex_.real_;
sub.imaginary_ = imaginary_ - complex_.imaginary_;
return sub;
}
complex operator*(double a,const complex& complex_)//友元不需要作用于操作符
{
complex multiple;
multiple.real_ = a * complex_.real_ ;
multiple.imaginary_ = complex_.imaginary_ * a;
return multiple;
}
complex complex::operator*(const complex& complexa_,const complex& complexb_)const
{
complex multiple;
multiple.real_ = complexa_.real_ * complexb_.real_ ;
multiple.imaginary_ = complexa_.imaginary_ * complexb_.imaginary_;
return multiple;
}
ostream& operator<<(ostream& os, const complex& complex_)
{
os <<"(" << complex_.real_ <<"," << complex_.imaginary_ <<"i)" <<endl;
return os;
}
istream& operator>>(istream& is, complex& newone)
{
is >> newone.real_;
is >> newone.imaginary_;
return is;
}
int main()
{
complex a (3.0,4.0);
complex c;
cout << "Enter a complex number(q to quit): \n";
while(cin >> c)
{
cout << "c is" << c <<'\n';
cout <<"a is " << a << '\n';
cout <<"a+c is" << a + c << '\n';
cout <<"2*c is" << 2 * c << '\n';
cout << "Enter a complex number(q to quit): \n";
}
cout << "Done";
return 0;
}
增加了一部分,报错,求女神中肯意见顺带跪舔。
complex complex::operator*(const complex& complex_)const
{
complex mul;
mul.real_ = mul.real_ * complex_.real_ ;
mul.imaginary_ = mul.imaginary_ * complex_.imaginary_;
return mul;
}
我改成这个之后,结果不对。但是编译成功
【 在 sharonyue 的大作中提到: 】
: 为什么需要复制构造函数呢,我里面没有用new啊。
我看错了,你想达到什么效果?实现两个复数相乘?
change
complex operator*(const complex& complexa_,const complex& complexb_)const
to
friend complex operator*(const complex& complexa_,const complex& complexb_);
or
complex operator*(const complex& complex_)const;
and are you sure that your complex multiplication is correct?
【 在 sharonyue 的大作中提到: 】
:
: #include <iostream>
: using namespace std;
: ...................
【 在 tonyjansan 的大作中提到: 】
: change
: complex operator*(const complex& complexa_,const complex& complexb_)const
: to
: ...................
complex operator*(const complex& complex_)const;
I have tried this but failed, see the last post, In mathematics this is wrong, while this doesnot matter, its just a excercise sweetie.
class complex
{
public:
complex operator*(const complex& complex_)const;
};
complex complex::operator*(const complex &complex_)const
{
complex mul;
mul.real_ = this->real_ * complex_.real_;
mul.imaginary_ = this->imaginary_ * complex_.imaginary_;
return mul;
}
你是想这样?
【 在 sharonyue 的大作中提到: 】
: complex operator*(const complex& complex_)const;
: Times New Roman
【 在 luotuo818 的大作中提到: 】
: 我看错了,你想达到什么效果?实现两个复数相乘?
恩恩 类似吧 比如 3,4i * 2,2i = 6,8i