2007-06-14 23:28:00
1using System;
2
3namespace _07
4{
5 class Complex
6 {
7 private double m_dRealPart;
8 private double m_dImagePart;
9
10 public Complex(double dRealPart, double dImagePart)
11 {
12 m_dRealPart = dRealPart;
13 m_dImagePart = dImagePart;
14 }
15 public Complex(double dRealPart)
16 {
17 m_dRealPart = dRealPart;
18 m_dImagePart = 0;
19 }
20 public Complex()
21 {
22 m_dRealPart = 0;
23 m_dImagePart = 0;
24 }
25 public Complex(Complex orig)
26 {
27 m_dRealPart=orig.m_dRealPart ;
28 m_dImagePart=orig.m_dImagePart ;
29 }
30
31 static public Complex operator +(Complex op1,Complex op2)
32 {
33 Complex res = new Complex();
34 res.m_dRealPart = op1.m_dRealPart + op2.m_dRealPart;
35 res.m_dImagePart = op1.m_dImagePart + op2.m_dImagePart;
36 return res;
37 }
38 static public Complex operator -(Complex op)
39 {
40 Complex res = new Complex();
41 res.m_dRealPart = -op.m_dRealPart;
42 res.m_dImagePart = -op.m_dImagePart;
43 return res;
44 }
45 static public Complex operator -(Complex op1, Complex op2)
46 {
47 Complex res = new Complex();
48 res = op1 + (-op2);
49 return res;
50 }
51 static public Complex operator *(Complex op1, Complex op2)
52 {
53 Complex res = new Complex();
54 res.m_dRealPart = op1.m_dRealPart * op2.m_dRealPart - op1.m_dImagePart * op2.m_dImagePart;
55 res.m_dImagePart = op1.m_dImagePart * op2.m_dRealPart + op1.m_dRealPart * op2.m_dImagePart;
56 return res;
57 }
58 static public Complex operator /(Complex op1, Complex op2)
59 {
60 Complex res = new Complex();
61 double temp = op2.m_dRealPart * op2.m_dRealPart + op2.m_dRealPart * op2.m_dImagePart;
62 res.m_dRealPart = (op1.m_dRealPart * op2.m_dRealPart + op1.m_dImagePart * op2.m_dImagePart) / temp;
63 res.m_dImagePart = (op1.m_dImagePart * op2.m_dRealPart - op1.m_dRealPart * op2.m_dImagePart) / temp;
64 return res;
65 }
66
67 public double GetRealPart()
68 {
69 return m_dRealPart;
70 }
71
72 public double GetImagePart()
73 {
74 return m_dImagePart;
75 }
76
77 public double NormSquare()
78 {
79 return m_dRealPart * m_dRealPart + m_dImagePart * m_dImagePart;
80 }
81
82 public override string ToString()
83 {
84 string res;
85 if (m_dRealPart == 0 && m_dImagePart == 0)
86 {
87 res = "0";
88 }
89 else if (m_dRealPart == 0 && m_dImagePart != 0)
90 {
91 res = m_dRealPart.ToString();
92 }
93 else if (m_dRealPart != 0 && m_dImagePart == 0)
94 {
95 res = m_dImagePart.ToString() + "i";
96 }
97 else if (m_dImagePart > 0)
98 {
99 res = m_dRealPart.ToString() + "+" + m_dImagePart.ToString() + "i";
100 }
101 else
102 {
103 res = m_dRealPart.ToString() + m_dImagePart.ToString() + "i";
104 }
105
106 return res;
107 }
108 }
109 class Program
110 {
111 static void Main(string[] args)
112 {
113 Complex a, b, c;
114 a = new Complex(1, 2);
115 Console.WriteLine("a = {0}", a.ToString());
116 Console.WriteLine("Re(a) = {0}", a.GetRealPart());
117 Console.WriteLine("Im(a) = {0}", a.GetImagePart());
118 Console.WriteLine("|a|^2 = {0}", a.NormSquare());
119 b = new Complex(3, -4);
120 Console.WriteLine("a = {0}", b.ToString());
121 c = new Complex(a);
122 Console.WriteLine("c = a = {0}", c.ToString());
123 Console.WriteLine("({0}) + ({1}) = {2}", a.ToString(), b.ToString(), (a + b).ToString());
124 Console.WriteLine("({0}) - ({1}) = {2}", a.ToString(), b.ToString(), (a - b).ToString());
125 Console.WriteLine("({0}) * ({1}) = {2}", a.ToString(), b.ToString(), (a * b).ToString());
126 Console.WriteLine("({0}) / ({1}) = {2}", a.ToString(), b.ToString(), (a / b).ToString());
127
128 Console.ReadKey(true);
129 }
130 }
131}
132
133/*
134 * 学习手记
135 *
136 * 没想到 C# 的重载跟 C++ 差那么多!
137 * = 不能重载了
138 * 也不用 this —— 这个倒明朗得多了
139 *
140 * 2007-06-14
141 */
首发:https://blog.csdn.net/cnStreamlet/article/details/1653001