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

Posted chengeputongren

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用操作符重载来完成复数的计算程序相关的知识,希望对你有一定的参考价值。

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
", c1.getA(), c1.getB());
11     printf("c2.a=%f,c2.b=%f
", c2.getA(), c2.getB());
12     printf("c3.a=%f,c3.b=%f
", c3.getA(), c3.getB());
13     printf("c4.a=%f,c4.b=%f
", c4.getA(), c4.getB());
14     if (c1 == c4)
15     {
16         printf("c1和c4相等
");
17     }
18     if (c1 != c2)
19     {
20         printf("c1与c2不相等
");
21     }
22 
23     complex c5(0,0);
24     complex c6 = c5++;
25     printf("c6.a=%f
",c6.getA());
26     printf("c6.b=%f
", c6.getB());
27     complex c7 = ++c5;
28     printf("c7.a=%f
", c7.getA());
29     printf("c7.b=%f
", 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

 

以上是关于使用操作符重载来完成复数的计算程序的主要内容,如果未能解决你的问题,请参考以下文章

(45)C#里使用操作符重载来提高阅读性和性能

(45)C#里使用操作符重载来提高阅读性和性能

c++简单复数计算器

操作符重载的概念

操作符重载的概念(二十四)

CPP0048复数类四则运算及插入/提取操作