/
Program.cs
343 lines (287 loc) · 11.8 KB
/
Program.cs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HomeWork_EPAM
{
class Program
{
static void Main(string[] args)
{
#region Rectangle
Console.WriteLine("------------------ Task 1 ------------------\n");
Rectangle rectangle = new Rectangle();
Console.Write("Please, enter upper left point X coordinate: ");
rectangle.upperLeftX = Convert.ToInt32(Console.ReadLine());
Console.Write("Please, enter upper left point Y coordinate: ");
rectangle.upperLeftY = Convert.ToInt32(Console.ReadLine());
Console.Write("Please, enter down right point X coordinate: ");
rectangle.downRightX = Convert.ToInt32(Console.ReadLine());
Console.Write("Please, enter down right point Y coordinate: ");
rectangle.downRightY = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Square is: " + rectangle.Square());
Console.WriteLine("Perimeter is: " + rectangle.Perimeter());
#endregion
#region Circle
Circle circle = new Circle();
Console.Write("Please, enter the radius of the circle: ");
circle.radius = Convert.ToDouble(Console.ReadLine());
circle.CircleLenght();
circle.CircleSquare();
#endregion
#region Complex Number
Console.Write("Please, enter first real part: ");
double real1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Please, enter first imaginary part: ");
double imaginary1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Please, enter second real part: ");
double real2 = Convert.ToDouble(Console.ReadLine());
Console.Write("Please, enter second imaginary part: ");
double imaginary2 = Convert.ToDouble(Console.ReadLine());
ComplexNumber firstComplex = new ComplexNumber(real1, imaginary1);
ComplexNumber secondComplex = new ComplexNumber(real2, imaginary2);
ComplexNumber multiplying = firstComplex * secondComplex;
Console.WriteLine($"Multipling of complex numbers = {multiplying.Real} + {multiplying.Imiginary}i");
ComplexNumber dividing = firstComplex / secondComplex;
Console.WriteLine($"Dividing of complex numbers = {dividing.Real} + {dividing.Imiginary}i");
#endregion
#region Draw
Console.WriteLine("\n------------------ Task 2 ------------------\n");
Rectangle_2 r = new Rectangle_2();
r.Draw();
Square s = new Square();
s.Draw();
#endregion
#region Collections
Console.WriteLine("\n------------------ Task 3 ------------------\n");
Console.WriteLine("Display name and age of each created person:\n");
var people = new List<Person>
{
new Person{Name = "Emma", Age = 18, PhoneNumbers = new List<string>() { "+ Kyivstar", "+ Life", "+ Vodafone" } },
new Person{Name = "Olivia", Age = 19, PhoneNumbers = new List<string>() { "+ Kyivstar", "+ Life", "+ Vodafone" } },
new Person{Name = "Ava", Age = 20, PhoneNumbers = new List<string>() { "+ Kyivstar", "+ Life", "+ Vodafone" } },
new Person{Name = "Isabella", Age = 21, PhoneNumbers = new List<string>() { "+ Kyivstar", "+ Life", "+ Vodafone" } },
new Person{Name = "Sophia", Age = 22, PhoneNumbers = new List<string>() { "+ Kyivstar", "+ Life", "+ Vodafone" } },
new Person{Name = "Charlotte", Age = 23, PhoneNumbers = new List<string>() { "+ Kyivstar", "+ Life", "+ Vodafone" } },
};
foreach (var person in people)
{
Console.WriteLine("Name: {0}, Age: {1}", person.Name, person.Age);
}
Console.WriteLine("\nPhone numbers of all created persons:\n");
people.AddRange(new List<Person>
{
new Person{Name = "Mia", Age = 24, PhoneNumbers = new List<string>() { "+ Kyivstar", "+ Life", "+ Vodafone" } },
new Person{Name = "Amelia", Age = 25, PhoneNumbers = new List<string>() { "+ Kyivstar", "+ Life", "+ Vodafone" } }
});
foreach (var person in people)
{
Console.WriteLine("Name: {0}", person.Name);
foreach (var number in person.PhoneNumbers)
{
Console.WriteLine($"{number}");
}
}
Console.WriteLine("\nList of randomly generated elements:\n");
RandomString random = new RandomString();
random.ElementsNumber();
random.DisplayPage();
#endregion
Console.ReadKey();
}
class Rectangle
{
private int FirstSide;
private int SecondSide;
public int upperLeftX { get; set; }
public int upperLeftY { get; set; }
public int downRightX { get; set; }
public int downRightY { get; set; }
public void LengthSide()
{
if (upperLeftX < downRightX)
FirstSide = downRightX - upperLeftX;
else if (upperLeftX > downRightX)
FirstSide = upperLeftX - downRightX;
else
FirstSide = 0;
if (upperLeftY < downRightY)
SecondSide = downRightY - upperLeftY;
else if (upperLeftY > downRightY)
SecondSide = upperLeftY - downRightY;
else
SecondSide = 0;
}
public int Square()
{
LengthSide();
return FirstSide * SecondSide;
}
public int Perimeter()
{
LengthSide();
return 2 * (FirstSide + SecondSide);
}
}
class Circle
{
public double radius { get; set; }
public const double pi = 3.14;
public void CircleLenght()
{
if (radius <= 0)
{
Console.WriteLine("The radius of a circle cannot be less or equal to zero");
}
else
{
Console.WriteLine("Circle length is: " + 2 * radius * pi);
}
}
public void CircleSquare()
{
if (radius <= 0)
{
Console.WriteLine("The radius of a circle cannot be less or equal to zero");
}
else
{
Console.WriteLine("Circle square is: " + radius * radius * pi);
}
}
}
class ComplexNumber
{
public double Real { get; set; }
public double Imiginary { get; set; }
public ComplexNumber()
{
this.Real = 0;
this.Imiginary = 0;
}
public ComplexNumber(double a, double b)
{
this.Real = a;
this.Imiginary = b;
}
public static ComplexNumber operator *(ComplexNumber a, ComplexNumber b)
{
ComplexNumber complex = new ComplexNumber();
complex.Real = a.Real * b.Real - a.Imiginary * b.Imiginary;
complex.Imiginary = a.Real * b.Imiginary + a.Imiginary * b.Real;
return complex;
}
public static ComplexNumber operator /(ComplexNumber a, ComplexNumber b)
{
ComplexNumber complex = new ComplexNumber();
complex.Real = ((a.Real * b.Real) + (a.Imiginary * b.Real)) / ((b.Real * b.Real) + (b.Imiginary * b.Imiginary));
complex.Imiginary = ((b.Real * a.Imiginary) - (a.Real * b.Imiginary)) / ((b.Real * b.Real) + (b.Imiginary * b.Imiginary));
return complex;
}
}
internal interface IDrawable
{
void Draw();
}
abstract class Figure
{
public readonly double X;
public readonly double Y;
public Figure()
{
}
public Figure(double x, double y)
{
X = x;
Y = y;
}
public abstract void Draw();
}
class Rectangle_2 : Figure, IDrawable
{
public Rectangle_2()
{
}
public Rectangle_2(double x, double y) : base(x, y)
{
}
public override void Draw()
{
Console.WriteLine("Rectangle class");
}
}
class Square : Figure
{
public Square()
{
}
public Square(double x, double y) : base(x, y)
{
}
public override void Draw()
{
Console.WriteLine("Square class");
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public IEnumerable<String> PhoneNumbers { get; set; }
}
class RandomString
{
public List<string> randomStr = new List<string>();
public void ElementsNumber()
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int n = 0; n < 101; n++)
{
builder.Clear();
for (int i = 0; i < 4; i++)
{
builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))));
}
randomStr.Add(builder.ToString());
}
Console.WriteLine("Number of elements in the list: " + randomStr.Count);
HashSet<string> newString = new HashSet<string>(randomStr);
List<string> stringNoZ = new List<string>();
foreach (var s in newString)
{
if (!s.StartsWith("Z"))
{
stringNoZ.Add(s);
}
}
randomStr = stringNoZ;
randomStr.Sort();
randomStr.Reverse();
Console.WriteLine("Number of elements in the list after transformation: " + randomStr.Count);
}
public void DisplayPage()
{
Console.Write("Input page number: ");
bool pageCheck = int.TryParse(Console.ReadLine(), out int pageNumber);
if (!pageCheck)
{
return;
}
const int itemsOnPage = 5;
int length = randomStr.Count / itemsOnPage;
if (pageNumber <= 0 || pageNumber > length)
{
Console.WriteLine("Invalid input");
return;
}
int element = (pageNumber - 1) * itemsOnPage;
for (; element < (pageNumber * itemsOnPage); element++)
{
Console.WriteLine(randomStr[element]);
}
}
}
}
}