Overload the operator to complete the calculation program complex

main.cpp

 1 #include <stdio.h>
 2 #include "Complex.h"
 3 int main()
 4 {
 5     complex c1(1,2);
 6     complex c2(3,4);
 7     complex c3 = c2 / c1;
 8     complex c4(0, 0);
 9     (c4 = c3) = c1;
10     printf("c1.a=%f,c1.b=%f\n", c1.getA(), c1.getB());
11     printf("% F = c2.a, c2.b F =% \ n- " , c2.getA (), c2.getB ());
 12 is      the printf ( " c3.a =% F, F% = c3.b \ n- " , c3.getA (), c3.getB ());
 13 is      the printf ( " c4.a =% F, F% = c4.b \ n- " , c4.getA (), c4.getB ());
 14      IF (C1 == c4)
 15      {
 16          the printf ( " C1 and equal c4 \ n- " );
 . 17      }
 18 is      IF (! C1 = c2)
 . 19      {
 20 is          the printf ( " C1 and c2 is not equal to \ n- " );
 21 is      }
22 
23     complex c5(0,0);
24     complex c6 = c5++;
25     printf("c6.a=%f\n",c6.getA());
26     printf("c6.b=%f\n", c6.getB());
27     complex c7 = ++c5;
28     printf("c7.a=%f\n", c7.getA());
29     printf("c7.b=%f\n", c7.getB());
30     return 0;
31 }

Complex.cpp

 1 #include "Complex.h"
 2 #include "math.h"
 3 
 4 double complex::getA()
 5 {
 6     return this->a;
 7 }
 8 double complex::getB()
 9 {
10     return this->b;
11 }
12 double complex::getModulus()
13 {
14     return sqrt(a*a + b*b);
15 }
16 
17 complex complex:: operator + (const complex&c)
18 {
19     double na = a + c.a;
20     double nb = b + c.b;
21     complex ret(na,nb);
22     return ret;
23 }
24 complex complex:: operator - (const complex&c)
25 {
26     double na = a - c.a;
27     double nb = b - c.b;
28     complex ret(na,nb);
29     return ret;
30 }
31 complex complex:: operator * (const complex&c)
32 {
33     double na = a*c.a - b*c.b;
34     double nb = b*c.a - a*c.b;
35     complex ret(na,nb);
36     return ret;
37 }
38 complex complex:: operator / (const complex&c)
39 {
40     double cm = c.a * c.a + c.b * c.b;
41     double na = (a*c.a + b*c.b) / cm;
42     double nb = (b*c.a - a*c.b) / cm;
43     complex ret(na, nb);
44     return ret;
45 
46 }
47 bool complex:: operator == (const complex&c)
48 {
49     return (a == c.a) && (b == c.b);
50 }
51 bool complex:: operator != (const complex&c)
52 {
53     return !(*this == c);
54 }
55 
56 complex& complex:: operator = (const complex& c)
57 {
58     if (this != &c)
59     {
60         a = c.a;
61         b = c.b;
62     }
63     return *this;
64 }
65 
66 complex& complex:: operator ++()
67 { 
68     a = a + 1;
69     b = b + 1;
70     return *this;
71 }
72 complex complex:: operator ++(int)
73 {
74     complex ret(a, b);
75     a = a + 1;
76     b = b + 1;
77     return ret;
78 }

Complex.h

 1 #ifndef _COMPLEX_H_
 2 #define _COMPLEX_H_
 3 
 4 class complex
 5 {
 6     double a;
 7     double b;
 8 public:
 9     complex(double a,double b) :a(0), b(0)
10     {
11         this->a = a;
12         this->b = b;
13     }
14     double getA();
15     double getB();
16     double getModulus();
17     complex operator + (const complex&c);
18     complex operator - (const complex&c);
19     complex operator * (const complex&c);
20     complex operator / (const complex&c);
21     bool operator == (const complex&c);
22     bool operator != (const complex&c);
23     complex& operator = (const complex&c);
24     complex& operator ++();
25     complex operator ++(int);
26 
27 };
28 
29 #endif

 

Guess you like

Origin www.cnblogs.com/chengeputongren/p/12237261.html