求解答一个C++题目
利用运算符重载,定义复数类的加法和减法,使之能够执行下列运算。Complexa(2,5)b(7,8)b(0,0)c=a+b;c=4.1+b;请高手给解答一个,谢谢了!我要...
利用运算符重载,定义复数类的加法和减法,使之能够执行下列运算。
Complex a(2,5) b(7,8) b(0,0)
c=a+b;
c=4.1+b;
请高手给解答一个,谢谢了!
我要写进卷子里的,哪个才是对的啊。。看着好复杂 展开
Complex a(2,5) b(7,8) b(0,0)
c=a+b;
c=4.1+b;
请高手给解答一个,谢谢了!
我要写进卷子里的,哪个才是对的啊。。看着好复杂 展开
2个回答
展开全部
//实现了复数同复数的加减
//实现了复数同实数的加减
//实现了实数同复数的加减
//经测试,程序正确执行
//希望能帮助你
#include <iostream>
#include <cstdlib>
using namespace std;
class Complex
{
public:
Complex()
{
real=0.0;
imag=0.0;
}
Complex(float r,float i)
{
real=r;
imag=i;
}
Complex operator+(Complex &);
Complex operator-(Complex &);
Complex operator+(float);
friend Complex operator-(float,Complex &);
friend Complex operator+(float,Complex &);
Complex operator-(float);
void print()
{
if(real>0)
cout<<imag<<"i+"<<real<<endl;
else if(real<0)
cout<<imag<<"i-"<<-real<<endl;
else cout<<imag<<"i"<<endl;
}
private:
float real;
float imag;
};
Complex Complex::operator+(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator+(float c1)
{
Complex c;
c.real=c1+real;
c.imag=imag;
return c;
}
Complex operator+(float c1,Complex &c2)
{
Complex c;
c.real=c2.real+c1;
c.imag=c2.imag;
return c;
}
Complex Complex::operator-(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex operator-(float c1,Complex &c2)
{
Complex c;
c.real=c1-c2.real;
c.imag=-c2.imag;
return c;
}
Complex Complex::operator-(float c2)
{
Complex c;
c.real=real-c2;
c.imag=imag;
return c;
}
int main()
{
Complex a(2,5),b(7,8),c;
c=a+b;
c.print();
c=4.1+b;
c.print();
c=a-8;
c.print();
c=a+3.3;
c.print();
c=4-b;
c.print();
return 0;
}
//实现了复数同实数的加减
//实现了实数同复数的加减
//经测试,程序正确执行
//希望能帮助你
#include <iostream>
#include <cstdlib>
using namespace std;
class Complex
{
public:
Complex()
{
real=0.0;
imag=0.0;
}
Complex(float r,float i)
{
real=r;
imag=i;
}
Complex operator+(Complex &);
Complex operator-(Complex &);
Complex operator+(float);
friend Complex operator-(float,Complex &);
friend Complex operator+(float,Complex &);
Complex operator-(float);
void print()
{
if(real>0)
cout<<imag<<"i+"<<real<<endl;
else if(real<0)
cout<<imag<<"i-"<<-real<<endl;
else cout<<imag<<"i"<<endl;
}
private:
float real;
float imag;
};
Complex Complex::operator+(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator+(float c1)
{
Complex c;
c.real=c1+real;
c.imag=imag;
return c;
}
Complex operator+(float c1,Complex &c2)
{
Complex c;
c.real=c2.real+c1;
c.imag=c2.imag;
return c;
}
Complex Complex::operator-(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex operator-(float c1,Complex &c2)
{
Complex c;
c.real=c1-c2.real;
c.imag=-c2.imag;
return c;
}
Complex Complex::operator-(float c2)
{
Complex c;
c.real=real-c2;
c.imag=imag;
return c;
}
int main()
{
Complex a(2,5),b(7,8),c;
c=a+b;
c.print();
c=4.1+b;
c.print();
c=a-8;
c.print();
c=a+3.3;
c.print();
c=4-b;
c.print();
return 0;
}
更多追问追答
追问
貌似不是我要问的东西啊,大神。。
追答
你要的东西全都包含在里面了,我还给你扩展了。纯手动,为你的题目编写的程序,还完整的测试了。你详细说明你的需求,我给你简略。
展开全部
#include <iostream>
using namespace std;
class Complex {
public:
Complex (double r = 0.0, double i = 0.0) : m_r (r), m_i (i) {}
const Complex operator+ (const Complex& c) const {
return Complex (m_r + c.m_r, m_i + c.m_i);
}
const Complex operator- (const Complex& c) const {
return Complex (m_r - c.m_r, m_i - c.m_i);
}
private:
double m_r;
double m_i;
};
using namespace std;
class Complex {
public:
Complex (double r = 0.0, double i = 0.0) : m_r (r), m_i (i) {}
const Complex operator+ (const Complex& c) const {
return Complex (m_r + c.m_r, m_i + c.m_i);
}
const Complex operator- (const Complex& c) const {
return Complex (m_r - c.m_r, m_i - c.m_i);
}
private:
double m_r;
double m_i;
};
追问
这个不对吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询