The implementation of the plural class:
This is the complement of the previous study, record it.
The concept of the plural class itself is to have a real part _real and imaginary part _image, then realize the subtraction of the complex number, self-addition and equal to the sign of the overload. It's a basic connection.
Nonsense not much to say, look at the code, very simple.
Complex_class.h
#include <iostream> #include <math.h>using namespace std;class complex{ Private:double _real;double _imag;public:complex (Double real = 0.0,double imag = 0.0); Complex (complex &cur);friend ostream& operator << (ostream& OUTPUT,COMPLEX&&NBSP;C);friend istream& operator >> (istream& input , complex& c); friend complex operator+ (Const complex& c1,const complex &&NBSP;C2); friend complex operator-(const complex& c1,const complex& &NBSP;C2); friend complex operator* (CONST&NBSP;COMPLEX&&NBSP;C1,CONST&NBSP;COMPLEX&&NBSP;C2); friend complex operator/(CONST&NBSP;COMPLEX&&NBSP;C1,CONST&NBSP;COMPLEX&&NBSP;C2); complex& operator ++ (); // Front ++complex operator ++ ( int); // ++complex& operator --(); // front -Complex operator --(int); // -complex& operator -= (const complex& c ); complex& operator += (const complex& c );bool operator < (const &NBSP;COMPLEX&&NBSP;C);bool operator > (const complex& c);};
Complex.cpp
#include "Complex_class.h" Complex::complex (double real,double imag) {_real = real;_ Imag = imag;} The overload of the output operator. ostream& operator << (ostream& output,complex& c) {output<< "(" < <c._real;if (c._imag >= 0) {output<< "+" <<c._imag<< "i)";} else{output<<c._imag<< "i)";} Return output;} Complex::complex (complex &cur) {_real = cur._real;_real = cur._imag;} The overload of the input operator. Istream& operator >> (istream& input,complex& c) {int a,b; char sign,i; do { cout<< "Input a complex number (A+bi or A-bi):"; input>>a>>sign>>b>>i; } whIle (! ( (sign == ' + ' | | sign == '-') &&i == ' i '); c._real=a; c._imag= (sign== ' + ')?b:-b; return input; }//complex sum, (A+BI) + (C+di) = (a+c) + (b+d) i;complex operator+ (const complex& c1,const COMPLEX&&NBSP;C2) {Complex resultcomplex;resultcomplex._imag = c1._imag + c2._imag ; Resultcomplex._real = c1._real + c2._real;return resultcomplex;} Plural subtraction, A+bi)-(C+di) = (a-c) + (b-d) icomplex operator-(const complex& c1,const complex& &NBSP;C2) {complex resultcomplex;resultcomplex._imag = c1._imag - c2._imag; Resultcomplex._real = c1._real - c2._real;return resultcomplex;} Multiply plural: (a+bi) • (c+di) = (AC-BD) + (Bc+ad) icomplex operator* (Const complex& c1,const complex &&NBSP;C2) {Complex resultcomplex;resultcomplex._real = (c1._real * c2._real) - (C1._IMAG&NBSP;*&NBSP;C2._IMAG); resultcomplex._imag = (c1._imag * c2._real) + (C1._REAL&NBSP;*&NBSP;C2._IMAG); Return resultcomplex;} Divide by complex number: (A+BI)/(C+di) = (AC+BD)/(c^2+d^2) + (bc-ad)/(C^2+D^2) i complex operator/(const &NBSP;COMPLEX&&NBSP;C1,CONST&NBSP;COMPLEX&&NBSP;C2) {complex resultcomplex;resultcomplex._real= (c1 . _real*c2._real+c1._imag*c2._imag)/(C2._REAL*C2._REAL+C2._IMAG*C2._IMAG); resultcomplex._imag= (C1._imag*c2._real-c1._real*c2._imag)/(C2._REAL*C2._REAL+C2._IMAG*C2._IMAG); Return resultcomplex;} complex& complex::operator ++ () // front ++{this->_imag++;this- >_real++;return *this;} complex complex::operator ++ (int) // rear ++{complex before (this->_real,this- >_IMAG); ++*this;return before;} Complex& complex::operator --() // front -{this->_imag--;this->_real--; Return *this;} complex complex::operator --(int) // rear-{complex before (this->_real,this->_imag );--*this;return before;} complex& complex::operator -= (const complex& c ) {*this = *this - c;return *this;} complex& complex::operator += (const complex& c ) {*this = *this + c;return *this;} bool complex::operator < (const complex& c) {return (Pow (_real,2) +pow (_imag,2)) < (POW (c._real,2) +pow (c._imag,2)) true:false;} Bool complex::operator > (const complex& c) {return (Pow (_real,2) +pow (_imag,2)) > (POW (c._real,2) +pow (c._imag,2)) true:false;}
The implementation of a complex number class is complete. is not very simple.
This article from "Left Egg June" blog, reproduced please contact the author!
Implementation of the "C + +" plural class