码迷,mamicode.com
首页 > 其他好文 > 详细

使用操作符重载来完成复数的计算程序

时间:2020-01-28 09:41:14      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:相等   mod   printf   int   操作符重载   double   def   操作符   使用   

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("c2.a=%f,c2.b=%f\n", c2.getA(), c2.getB());
12     printf("c3.a=%f,c3.b=%f\n", c3.getA(), c3.getB());
13     printf("c4.a=%f,c4.b=%f\n", c4.getA(), c4.getB());
14     if (c1 == c4)
15     {
16         printf("c1和c4相等\n");
17     }
18     if (c1 != c2)
19     {
20         printf("c1与c2不相等\n");
21     }
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

 

使用操作符重载来完成复数的计算程序

标签:相等   mod   printf   int   操作符重载   double   def   操作符   使用   

原文地址:https://www.cnblogs.com/chengeputongren/p/12237261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!