📄 complex.cpp
字号:
#include <iostream>#include <cmath>using namespace std;// Declaration of Complex class, for working with complex// numbers as easily as with doublesclass Complex {public: // Constructors Complex( double re = 0.0, double im = 0.0 ); Complex( const Complex& z ); // Destructor ~Complex(); // Extract real and imaginary parts double Re() const; double Im() const; // These are the polar coordinates of the // complex number: z=a+bi=r*exp(it) where // r=|z|=sqrt(a^2+b^2), tan(b/a)=t // Abs() returns the modulus r // Arg() returns the argument t double Abs() const; double Arg() const; // Complex conjugate a-bi, where z=a+bi Complex Conj() const; // Overloads of arithmetic operators Complex operator-() const; // unary minus Complex operator+( const Complex& z ) const; Complex operator-( const Complex& z ) const; Complex operator*( const Complex& z ) const; Complex operator/( const Complex& z ) const; Complex operator+( double x ) const; Complex operator-( double x ) const; Complex operator*( double x ) const; Complex operator/( double x ) const; // Overloads of assignment operators Complex& operator=( const Complex& z ); Complex& operator+=( const Complex& z ); Complex& operator-=( const Complex& z ); Complex& operator*=( const Complex& z ); Complex& operator/=( const Complex& z ); Complex& operator=( double x ); Complex& operator+=( double x ); Complex& operator-=( double x ); Complex& operator*=( double x ); Complex& operator/=( double x ); // For conversion to double (ignores imaginary part) operator double() const; private: // Private data members store the real and // imaginary parts of this complex number double m_re; double m_im;} ;// For these operators, a Complex object is not the first// argument, so they must be declared outside the classComplex operator+( double x, const Complex& z );Complex operator-( double x, const Complex& z );Complex operator*( double x, const Complex& z );Complex operator/( double x, const Complex& z );ostream& operator<<( ostream& out, const Complex& z );istream& operator>>( istream& in, Complex& z );///////////////////////////////// Definitions of operations /////////////////////////////////// Output operatorostream& operator<<( ostream& out, const Complex& z ){ // Output z in form a+bi or a-bi, where a is the // real part and b is the absolute value of the // imaginary part out << z.Re(); if ( z.Im() >= 0.0 ) out << "+"; out << z.Im(); out << "i"; return out;}// Input operatoristream& operator>>( istream& in, Complex& z ){ // Read real part double re; in >> re; // Check if we can continue if ( in.bad() ) return in; // Read + or - char sign; do { in >> sign; if ( in.bad() ) return in; } while ( isspace(sign) ); // If we don't have a + or -, just take // the preceding double as the real part, // and put the invalid input back. We // assume that only a real number was given. if ( sign != '+' && sign != '-' ) { Complex z_in( re, 0.0 ); z = z_in; in.putback(sign); return in; } // Read imaginary part double im; in >> im; if ( in.bad() ) return in; // We read the + or - earlier, so a - is // not part of the imaginary part. The // number im we just read is always positive. if ( sign == '-' ) im = -im; // Finally, read the i or I that should // follow the imaginary part. char i; in >> i; if ( in.bad() ) return in; // If we read an i or I, then reading is // successful and we can set the value of // the argument z. Otherwise, it's up to us // to signal that the input stream is in a // bad state. if ( i == 'I' || i == 'i' ) { Complex z_in( re, im ); z = z_in; } else in.setstate( ios::failbit ); return in;}// ConstructorsComplex::Complex( double re, double im ){ // Create a complex number object from // given real and imaginary parts m_re = re; m_im = im;}Complex::Complex( const Complex& z ){ // Copy constructor m_re = z.m_re; m_im = z.m_im;}// DestructorComplex::~Complex(){ // The only data in this object are the // real and imaginary parts, and they will // be de-allocated automatically, so we // don't have to do anything here}// Operationsdouble Complex::Re() const{ return m_re;}double Complex::Im() const{ return m_im;}double Complex::Abs() const{ return sqrt( m_re * m_re + m_im * m_im );}double Complex::Arg() const{ return atan2( m_im, m_re );}Complex Complex::Conj() const{ Complex z( m_re, -m_im ); return z;}// Overloaded arithmetic operatorsComplex Complex::operator-() const{ Complex z( -m_re, -m_im ); return z;}Complex Complex::operator+( const Complex& z ) const{ Complex sum( m_re + z.m_re, m_im + z.m_im ); return sum;}Complex Complex::operator-( const Complex& z ) const{ Complex diff( m_re - z.m_re, m_im - z.m_im ); return diff;}Complex Complex::operator*( const Complex& z ) const{ Complex prod( m_re * z.m_re - m_im * z.m_im, m_re * z.m_im + m_im * z.m_re ); return prod;}Complex Complex::operator/( const Complex& z ) const{ Complex quot( m_re * z.m_re + m_im * z.m_im, -m_re * z.m_im + m_im * z.m_re ); double modz = z.Abs(); double den = modz * modz; quot.m_re /= den; quot.m_im /= den; return quot;}// Arithmetic with right operand of doubleComplex Complex::operator+( double x ) const{ Complex sum( m_re + x, m_im ); return sum;}Complex Complex::operator-( double x ) const{ Complex diff( m_re - x, m_im * x ); return diff;}Complex Complex::operator*( double x ) const{ Complex prod( m_re * x, m_im * x ); return prod;}Complex Complex::operator/( double x ) const{ Complex quot( m_re / x, m_im / x ); return quot;}// Overloaded assignment operatorsComplex& Complex::operator=( const Complex& z ){ m_re = z.m_re; m_im = z.m_im; return *this;}Complex& Complex::operator+=( const Complex& z ){ m_re += z.m_re; m_im += z.m_im; return *this;}Complex& Complex::operator-=( const Complex& z ){ m_re -= z.m_re; m_im -= z.m_im; return *this;}Complex& Complex::operator*=( const Complex& z ){ double re = m_re; double im = m_im; m_re = re * z.m_re - im * z.m_im; m_im = re * z.m_im + im * z.m_re; return *this;}Complex& Complex::operator/=( const Complex& z ){ double re = m_re; double im = m_im; m_re = re * z.m_re + im * z.m_im; m_im = -re * z.m_im + im * z.m_re; double modz = z.Abs(); double den = modz * modz; m_re /= den; m_im /= den; return *this;}// Assignment operators with double valueComplex& Complex::operator=( double x ){ m_re = x; m_im = 0.0; return *this;}Complex& Complex::operator+=( double x ){ m_re += x; return *this;}Complex& Complex::operator-=( double x ){ m_re -= x; return *this;}Complex& Complex::operator*=( double x ){ m_re *= x; m_im *= x; return *this;}Complex& Complex::operator/=( double x ){ m_re /= x; m_im /= x; return *this;}// Conversion to double (useful in case Complex// object has zero imaginary part)Complex::operator double() const{ return m_re;}// Arithmeic Operators with left operand of doubleComplex operator+( double x, const Complex& z ){ Complex sum( x + z.Re(), z.Im() ); return sum;}Complex operator-( double x, const Complex& z ){ Complex diff( x - z.Re(), -z.Im() ); return diff;}Complex operator*( double x, const Complex& z ){ Complex prod( x * z.Re(), x * z.Im() ); return prod;}Complex operator/( double x, const Complex& z ){ double modz = z.Abs(); double den = modz * modz; Complex quot( x * z.Re() / den, -x * z.Im() / den ); return quot;}int main(){ Complex z1( 3.0, 4.0 ); Complex z2; // Test the << operator cout << "Enter complex number: "; cin >> z2; // Test several of the class' member functions on // z1 and z2 cout << "z1=" << z1 << endl; cout << "z2=" << z2 << endl; cout << "Re(z2)=" << z2.Re() << endl; cout << "Im(z2)=" << z2.Im() << endl; cout << "|z2|=" << z2.Abs() << endl; cout << "arg(z2)=" << z2.Arg() << " radians" << endl; cout << "z1+z2=" << z1+z2 << endl; cout << "z1-z2=" << z1-z2 << endl; cout << "z1*z2=" << z1*z2 << endl; cout << "z1/z2=" << z1/z2 << endl; // Test operations with doubles and Complex objects cout << "2z1=" << 2.0 * z1 << endl; cout << "z1*2=" << z1 * 2.0 << endl; cout << "2+z1=" << 2.0 + z1 << endl; cout << "z1+2=" << z1 + 2.0 << endl; // Test conversion to double cout << "(double) z2=" << double(z1) << endl; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -