1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CubicEquation
{
public class Complex : IComparable<Complex>, IFormattable
{
public double Real { get; set; }
public double Imaginary { get; set; }
public Complex()
{
}
public Complex(double real)
{
Real = real;
Imaginary = 0;
}
public Complex(double real, double imaginary)
{
Real = real;
Imaginary = imaginary;
}
public static Complex operator +(Complex a, Complex b)
{
return new Complex()
{
Imaginary = a.Imaginary + b.Imaginary,
Real = a.Real + b.Real
};
}
public static Complex operator -(Complex a, Complex b)
{
return new Complex()
{
Imaginary = a.Imaginary - b.Imaginary,
Real = a.Real - b.Real
};
}
public static Complex operator +(Complex a, double k)
{
return new Complex()
{
Imaginary = a.Imaginary,
Real = a.Real + k
};
}
public static Complex operator -(Complex a, double k)
{
return new Complex()
{
Imaginary = a.Imaginary,
Real = a.Real - k
};
}
// Перегрузка оператора равенства. ВАЖНО, т.к. сравниваются числа с плавающей запятой, то сравнивать нужно их разницу по модулю
//
public static bool operator ==(Complex a, Complex b)
{
if ((Math.Abs(a.Imaginary - b.Imaginary) < 0.0000001 && Math.Abs(a.Real - b.Real) < 0.0000001))
{
return true;
}
return false;
}
public static bool operator !=(Complex a, Complex b)
{
if (Math.Abs(a.Imaginary - b.Imaginary) > 0.0000001 && Math.Abs(a.Real - b.Real) > 0.0000001)
{
return true;
}
return false;
}
public override bool Equals(System.Object obj)
{
if (obj == null)
{
return false;
}
Complex n = obj as Complex;
if ((System.Object)n == null)
{
return false;
}
return (Math.Abs(Imaginary - n.Imaginary) < 0.0000001) && (Math.Abs(Real - n.Real) < 0.0000001);
}
public bool Equals(Complex n)
{
if ((object)n == null)
{
return false;
}
return (Math.Abs(Imaginary - n.Imaginary) < 0.0000001) && (Math.Abs(Real - n.Real) < 0.0000001);
}
// Указаний по реализации нет. Реализован таким образом
public override int GetHashCode()
{
return (int)Imaginary ^ (int)Real;
}
public override string ToString()
{
if (Imaginary == 0) return string.Format("{0}", Math.Round(Real,6));
return string.Format("{0} + {1}i", Math.Round(Real,6), Math.Round(Imaginary,6));
}
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format.ToUpperInvariant())
{
case "P":
return string.Format("({0}, {1})", Real, Imaginary);
case "A":
return string.Format("({0} + i{1})", Real, Imaginary);
default:
throw new FormatException(String.Format("Формат строки: {0} - не поддерживается", format));
}
}
// Возможно лучшее решение. Т.к. указаний не дано по сравнению комплексных чисел (только равенства и неравенство),
// то реализовано таким образом
public int CompareTo(Complex other)
{
if (this == other) return 0;
if (Math.Abs(this.Real - other.Real) < 0.0000001)
{
if (this.Imaginary - other.Imaginary > 0) return 1;
else return -1;
}
if (this.Real > other.Real) return 1;
return -1;
}
// Задания по перегрузки операторов умножения и деления также нет, но вот они на всякий пожарный.
public static Complex operator *(Complex a, Complex b)
{
return new Complex()
{
Real = a.Real * b.Real - a.Imaginary * b.Imaginary,
Imaginary = a.Real * b.Imaginary + a.Imaginary * b.Real
};
}
public static Complex operator *(Complex a, double d)
{
return new Complex()
{
Real = a.Real * d,
Imaginary = a.Imaginary * d
};
}
public static Complex operator /(Complex a, Complex b)
{
return new Complex()
{
Real = (a.Real * b.Real + a.Imaginary * b.Imaginary) / (b.Real * b.Real + b.Imaginary * b.Imaginary),
Imaginary = (a.Imaginary * b.Real - a.Real * b.Imaginary) / (b.Real * b.Real + b.Imaginary * b.Imaginary)
};
}
public static Complex operator /(Complex a, double d)
{
return new Complex()
{
Real = a.Real / d,
Imaginary = a.Imaginary / d
};
}
}
} |