求下面这道题的C++程序,哪位高手肯帮忙则个?

2025-03-29 08:37:11
推荐回答(2个)
回答1:

#include
#include
class Complex
{

public:

Complex() : _real(0), _imag(0) {}
explicit Complex( double r) : _real(r), _imag(0) {}
Complex(double r, double i) : _real(r), _imag(i) {}

Complex& operator+=(const double& d)
{
_real += d;
return *this;
}

Complex& operator+=(const Complex& c)
{
_real += c._real;
_imag += c._imag;
return *this;
}

Complex& operator-=(const double &d)
{
_real -= d;
return *this;
}

Complex& operator-=(const Complex& c)
{
_real -= c._real;
_imag -= c._imag;
return *this;
}

Complex& operator*=(const double& d)
{
_real *= d;
_imag *= d;
return *this;
}

Complex& operator*=(const Complex& c)
{
double re = _real;
double im = _imag;
_real = re * c._real - im * c._imag;
_imag = re * c._imag + im * c._real;
return *this;
}

Complex& operator/=(const double& d)
{
_real /= d;
_imag /= d;
return *this;
}

Complex& operator/=(const Complex& c)
{
double re = _real;
double im = _imag;
double d = c._real * c._real + c._imag * c._imag;
_real = (re * c._real + im * c._imag) / d;
_imag = (im * c._real - re * c._imag) / d;
return *this;
}

Complex Conj() const
{
return Complex(_real, -_imag);
}
double Real() const { return _real; }
double Imag() const { return _imag; }
void Real(const double& re) { _real = re ; }
void Imag(const double& im) { _imag = im ; }
void Set(const double& re, const double& im){ _real = re; _imag = im ; }
double Modsq() const { return _real*_real + _imag * _imag ; }
double Mod() const { return sqrt(_real*_real + _imag * _imag); }

private:
double _real;
double _imag;

};

inline Complex operator+(const Complex& c)
{
return Complex(c.Real(), c.Imag());
}

inline Complex operator-(const Complex& c)
{
return Complex(-c.Real(), -c.Imag());
}

inline Complex operator+(const Complex& c, const double& d)
{
return Complex(c.Real() + d, c.Imag());
}

inline Complex operator+(const double& d, const Complex& c)
{
return Complex(d + c.Real(), c.Imag());
}

inline Complex operator+(const Complex& c1, const Complex& c2)
{
return Complex(c1.Real() + c2.Real(), c1.Imag() + c2.Imag());
}

inline Complex operator-(const Complex& c, const double& d)
{
return Complex(c.Real() - d, c.Imag());
}

inline Complex operator-(const double& d, const Complex& c)
{
return Complex(d - c.Real(), -c.Imag());
}

inline Complex operator-(const Complex& c1, const Complex& c2)
{
return Complex(c1.Real() - c2.Real(), c1.Imag() - c2.Imag());
}

inline Complex operator*(const Complex& c, const double& d)
{
return Complex(c.Real() * d, c.Imag() * d);
}

inline Complex operator*(const double& d, const Complex& c)
{
return Complex(c.Real() * d, c.Imag() * d);
}

inline Complex operator*(const Complex& c1, const Complex& c2)
{
double real = c1.Real() * c2.Real() - c1.Imag() * c2.Imag();
double imag = c1.Real() * c2.Imag() + c1.Imag() * c2.Real();
return Complex(real, imag);
}

inline Complex operator/(const Complex& c, const double& d)
{
return Complex(c.Real() / d, c.Imag() / d);
}

inline Complex operator/(const double& d, const Complex& c)
{
double dd = c.Real() * c.Real() + c.Imag() * c.Imag();
return Complex((d * c.Real())/dd, (-d * c.Imag())/dd);
}

inline Complex operator/(const Complex& c1, const Complex& c2)
{
double d = c2.Real() * c2.Real() + c2.Imag() * c2.Imag();
double real = (c1.Real() * c2.Real() + c1.Imag() * c2.Imag()) / d;
double imag = (c1.Imag() * c2.Real() - c1.Real() * c2.Imag()) / d;
return Complex(real, imag);
}

inline double real(const Complex &c)
{
return c.Real();
}

inline double imag(const Complex &c)
{
return c.Imag();
}

inline double abs(const Complex &c)
{
return sqrt(c.Real() * c.Real() + c.Imag() * c.Imag());
}

inline double norm(const Complex &c)
{
return c.Real() * c.Real() + c.Imag() * c.Imag();
}

inline Complex conj(const Complex &c)
{
return Complex(c.Real(), -c.Imag());
}

ostream &operator<<(ostream &os, const Complex &c)
{
os<return os;
}

int main()
{
Complex a(1, 2), b, c;
c = a+b;
cout<c = a-b;
cout<return 0;
}

回答2:

//XP + VC2005调试通过

//类定义文件Complex.h
#include
class Complex;

using namespace std;
ostream& operator<< ( ostream &os, const Complex& rhs );

class Complex
{
friend ostream& operator<< ( ostream &os, const Complex& rhs );
public:
Complex();
Complex( double a, double b );

~Complex(void);
public:
Complex& operator+( const Complex& );
Complex& operator=( const Complex& );

void Input();

void SetA( double a);
double GetA() const;
void SetB( double b);
double GetB() const;

private:
double _a;
double _b;
};

//类实现文件Complex.cpp
#include "Complex.h"

ostream& operator<<(ostream &os, const Complex &rhs)
{
os<< rhs.GetA() <<"+"< return os;
}
Complex::Complex(void) :_a(0),_b(0)
{
}
Complex::Complex(double a, double b) : _a(a), _b(b)
{

}

Complex::~Complex(void)
{
}

double Complex::GetA() const
{
return _a;
}
void Complex::SetA( double a)
{
_a=a;
}

double Complex::GetB() const
{
return _b;
}
void Complex::SetB( double b)
{
_b=b;
}

Complex& Complex::operator +(const Complex &rhs )
{
_a+= rhs.GetA();
_b+= rhs.GetB();
return *this;
}

Complex& Complex::operator =(const Complex &rhs )
{
if ( this != &rhs )
{
_a = rhs.GetA();
_b = rhs.GetB();
}
return *this;
}

void Complex::Input()
{
cout<<"请输入复数 a+bi"< cin>>_a;
cout<<"b=";
cin>>_b;

}
//主函数文件

#include
#include "Complex.h"

int main()
{
using namespace std;

Complex c1, c2, c3;

int a, b;

c1.Input();
c2.Input();

c3 = c1+c2;

cout<
return 0;
}

//PS: C++标准库有实现复数类complex, 如下:
//#include
// using namespace std;
// complex c1;