Complete implementation of the plural class (C + + version)

Source: Internet
Author: User

implement a complete plural class, including complex number of +,-, *,/,+=,-=,*=,/=, front + +, Post + +, front--, post--, = = (conditional judgment),!=,>,<=,= (assignment operator) 1. plural +,-, *,/,+=,-=,*=,/=here the plural subtraction does not do the narration (middle school knowledge), and the addition, such as, and so on, and so on, in addition to the complex number of subtraction similar, different is added, such as, minus, etc., in addition to adding, subtracting, multiplying, the result of division directly +,-, *,/to the original data, here also do not add, The results are as follows:        2. Complex number of Front + +, Post + +, front--, rear--we know that in C + + is allowed to overload, that mulao how to distinguish between pre + +, and post + +? Only need to add a parameter on the basis of the overload, the test can only pass a shaping (int) parameter , from the front + + ( first add re-use ) and the Post + + ( First use Plus) of the characteristics, to simulate the implementation of the front + + It is very easy to only need the real part and the imaginary part directly add and return after the addition of the complex number can be, and the simulation of the implementation of the Post + + is not very easy, in fact, we only need to use the copy constructor to construct a new complex number, and then add the real part, the imaginary part, and finally return is a copy of Complex.h        
#define _crt_secure_no_warnings 1#ifndef __complex_h__#define __complex_h__#include<iostream>using namespace Std;class Complex{public:complex (double real,double image), void Print (), bool operator== (const Complex &c); complex& operator++ ();   Front ++complex operator++ (int); Posterior ++complex& operator--();   Front--complex operator--(int); Posterior--bool operator> (const Complex &c); complex& operator= (const Complex &c); Complex operator+ (const Complex &c); complex& operator+= (const Complex &c); Complex operator-(const Complex &c); complex& operator-= (const Complex &c); Complex operator* (const Complex &c); complex& operator*= (const Complex &c); Complex operator/(const Complex &c); complex& operator/= (const Complex &c);p rivate:double _real;double _image;}; #endif//__complex_h__


Complex.cpp           
#define _crt_secure_no_warnings 1#include "Complex.h" Complex::complex (double real=0.0,double image=0.0)//constructor: _real (real), _image (image) {}void Complex::P rint () {if (_image = = 0.0)//imaginary part is 0{cout<<_real<<endl;} else{cout<<_real<< "+" <<_image<< "*i" <<endl;}} Complex complex::operator+ (const Complex &c) {Complex tmp;tmp._real=_real+c._real;tmp._image=_image+c._image; return TMP;} Complex complex::operator-(const Complex &c) {Complex tmp;tmp._real=_real-c._real;tmp._image=_image-c._image; return TMP;} Complex complex::operator* (const Complex &c) {Complex tmp;tmp._real=_real*c._real-_image*c._image;tmp._image=_ Real*c._image+_image*c._real;return tmp;} Complex complex::operator/(const Complex &c) {Complex tmp;double t=c._real*c._real+c._image*c._image;tmp._real= ( _real*c._real-_image* (-c._image))/t;tmp._image= (_real* (-c._image) +_image*c._real)/t;return tmp;} complex& complex::operator+= (const Complex &c) {_real+=c._real;_image+=c._image;return *this;} complex& complex::operator-= (const Complex &c) {_real-=c._real;_image-=c._image;return *this;}  complex& complex::operator*= (const Complex &c) {Complex tmp (*this); copy constructor _real=tmp._real*c._real-_image*c._image;_image=tmp._real*c._image+tmp._image*c._real;return *this;} complex& complex::operator/= (const Complex &c) {Complex tmp (*this);d ouble t=c._real*c._real+c._image*c._ Image;_real= (tmp._real*c._real-tmp._image* (-c._image))/t;_image= (tmp._real* (-c._image) +tmp._image*c._real)/t; return *this;} BOOL complex::operator== (const Complex &c) {return (_real = = c._real) && (_image = = C._image);} complex& complex::operator++ ()//Front ++{_real++;_image++;return *this;}  Complex complex::operator++ (int)//Post ++{complex tmp (*this); The copy constructor holds the value pointed to by this _real++;_image++;return tmp; complex& complex::operator--()//Front--{_real--;_image--;return *this;} Complex complex::operator--(int)//Post--{complex tmp (*this); _real--;_image--;return tmp;} BOOL Complex::operator>(const Complex &AMP;C) {return (_real > C._real) && (_image > C._image);} complex& complex::operator= (const Complex &c) {if (This! = &c) {_real=c._real;_image=c._image;} return *this;}

Test code: Test.cpp            
#define _crt_secure_no_warnings 1#include "Complex.h" void Test1 () {Complex C1 (1.0,2.0); Complex C2 (3.0,6.0); Complex c3=c1+c2; Complex c4=c1-c2; Complex c5=c1*c2; Complex c6=c1/c2;cout<< "C1:"; C1. Print ();cout<< "C2:"; C2. Print ();cout<< "C1+C2:"; C3. Print ();cout<< "C1-C2:"; C4. Print ();cout<< "c1*c2:"; C5. Print ();cout<< "C1/C2:"; C6. Print ();} void Test2 () {Complex C1 (1.0,2.0); Complex C2 (3.0,6.0);cout<< "C1:"; C1. Print ();cout<< "C2:"; C2. Print ();//complex c3=c1+=c2;//cout<< "C1+=C2:";//c1. Print ();//complex c3=c1-=c2;//cout<< "C1-=C2:";//c3. Print ();//c1. Print ();//complex c3=c1*=c2;//cout<< "C1*=C2:";//c3. Print ();//c1. Print (); Complex c3=c1/=c2;cout<< "C1/=C2:"; C3. Print ();} void Test3 () {Complex C1 (1.0,2.0); Complex C2 (3.0,6.0); Complex C3 (C1);//c1.  Print ();  1+2*i//complex c4=++c1; C4.  Print (); 2+3*i//c1.  Print (); 2+3*i//c1.  Print (); 1+2*i//complex c5=c1++;//c5.  Print (); 1+2*i//c1.  Print (); 2+3*i//c2.  Print (); 3+6*i//complex c4=--c2;//C4.  Print (); 2+5*i//c2.  Print (); 2+5*ic2.  Print (); 3+6*icomplex c4=c2--;c4.  Print (); 3+6*ic2.  Print (); 2+5*i}void test4 () {Complex C1 (1.0,2.0); Complex C2 (3.0,6.0);    Complex C3 (C1);//cout<< (C1 = C3) <<endl;    1 ==//cout<< (C1 = = C2) <<endl; 0 ==//cout<< (!) ( C1 = = C2)) <<endl;     1!=//cout<< (C2 > C1) <<endl; 1 >//cout<< (!) (  C2 > C1) <<endl; 0 <=c3.  Print (); 1+2*ic3=c2;c3.  Print (); 3+6*i}int Main () {//test1 ();//test2 (); Test3 ();//test4 (); System ("pause"); return 0;}

Complete implementation of the plural class (C + + version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.