Skip to content

Commit bece249

Browse files
committed
done
1 parent 70078df commit bece249

File tree

5 files changed

+402
-17
lines changed

5 files changed

+402
-17
lines changed

task4.1/Program.cs

+129
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
using System;
23
using System.Collections;
34
namespace task4._1;
@@ -123,4 +124,132 @@ public static void Main(string[] args)
123124
unoFloat.iterForeach((x) => Console.Write(x * x * x + " "));
124125
Console.WriteLine("\n");
125126
}
127+
=======
128+
using System;
129+
using System.Collections;
130+
namespace task4._1;
131+
132+
class Program
133+
{
134+
public static void Main(string[] args)
135+
{
136+
Console.WriteLine("-----------------------int-----------------------");
137+
UnoDimensional<int> unoInt = new(5);
138+
139+
unoInt.Add(1);
140+
unoInt.Add(215);
141+
unoInt.Add(-334);
142+
unoInt.Add(40);
143+
unoInt.Add(-50);
144+
unoInt.Print();
145+
146+
Console.Write("\nSorted - ");
147+
unoInt.Sorting();
148+
unoInt.Print();
149+
150+
Console.Write("\nReversed - ");
151+
unoInt.Reverse();
152+
unoInt.Print();
153+
154+
Func<int, int, int> compare = (x, y) => x.CompareTo(y);
155+
Console.WriteLine($"\nMin {unoInt.FindMin<int>(compare)}");
156+
Console.WriteLine($"Max {unoInt.FindMax<int>(compare)}");
157+
158+
Console.Write("\nElement removed - ");
159+
unoInt.Remove(4);
160+
unoInt.Print();
161+
162+
Console.WriteLine();
163+
unoInt.ForEachAction<int>((x) =>
164+
{
165+
double pow = Math.Pow(x, 2);
166+
Console.WriteLine("Value = " + x + ", pow^2 = " + pow);
167+
});
168+
169+
Console.WriteLine($"\nAll elementes - {unoInt.CountArray<int>()}");
170+
171+
Console.WriteLine($"With condition - {unoInt.CountArrayWithCondition<int>((x) => x % 2 == 1)}");
172+
173+
Console.WriteLine($"With at least one - {unoInt.EvenOneCondition<int>((x) => {
174+
if (x > 50) { return true; }
175+
else { return false; }
176+
})}");
177+
178+
Console.WriteLine($"With all - {unoInt.AllCondition<int>((x) => {
179+
if (x >= 0) { return true; }
180+
else { return false; }
181+
})}");
182+
183+
Console.WriteLine($"\nContains - {unoInt.Contains<int>(215)}");
184+
185+
int[] numbersInt = unoInt.Where<int>((x) => x < 100);
186+
Console.WriteLine($"Array with condition - {string.Join(' ', numbersInt)}");
187+
188+
Console.WriteLine($"\nArray from index - {unoInt.CountArrayFromIndex<int>(1)}");
189+
190+
Console.Write("\nForeach - ");
191+
unoInt.iterForeach((x) => Console.Write(x * x * x + " "));
192+
193+
194+
Console.WriteLine("\n\n\n-----------------------float-----------------------");
195+
UnoDimensional<float> unoFloat = new(4);
196+
197+
unoFloat.Add(5.1f);
198+
unoFloat.Add(100.500f);
199+
unoFloat.Add(33f);
200+
unoFloat.Add(-500.17f);
201+
unoFloat.Print();
202+
203+
Console.Write("\nSorted - ");
204+
unoFloat.Sorting();
205+
unoFloat.Print();
206+
207+
Console.Write("\nReversed - ");
208+
unoFloat.Reverse();
209+
unoFloat.Print();
210+
211+
Func<float, float, int> compareFloat = (x, y) => x.CompareTo(y);
212+
Console.WriteLine($"\nMin {unoFloat.FindMin<float>(compareFloat)}");
213+
Console.WriteLine($"Max {unoFloat.FindMax<float>(compareFloat)}");
214+
215+
Console.Write("\nElement removed - ");
216+
unoFloat.Remove(8);
217+
unoFloat.Print();
218+
219+
Console.WriteLine();
220+
unoFloat.ForEachAction<float>((x) =>
221+
{
222+
double pow = Math.Pow(x, 2);
223+
Console.WriteLine("Value = " + x + ", pow^2 = " + pow);
224+
});
225+
226+
Console.WriteLine($"\nAll elementes - {unoFloat.CountArray<float>()}");
227+
228+
Console.WriteLine($"With condition - {unoFloat.CountArrayWithCondition<float>((x) => x % 2 == 1)}");
229+
230+
Console.WriteLine($"With at least one - {unoFloat.EvenOneCondition<float>((x) =>
231+
{
232+
if (x > 50) { return true; }
233+
else { return false; }
234+
})}");
235+
236+
Console.WriteLine($"With all - {unoFloat.AllCondition<float>((x) =>
237+
{
238+
if (x >= 0) { return true; }
239+
else { return false; }
240+
})}");
241+
242+
Console.WriteLine($"\nContains - {unoFloat.Contains<float>(21.5f)}");
243+
244+
float[] numbersFloat = unoFloat.Where<float>((x) => x < 100f);
245+
Console.WriteLine($"Array with condition - {string.Join(' ', numbersFloat)}");
246+
247+
Console.WriteLine($"\nArray from index - {unoFloat.CountArrayFromIndex<float>(1)}");
248+
249+
Console.Write("\nForeach - ");
250+
unoFloat.iterForeach((x) => Console.Write(x * x * x + " "));
251+
Console.WriteLine("\n");
252+
253+
}
254+
>>>>>>> 9908cc8 (done!)
126255
}

task4.1/uno.cs

+240
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
using System;
23
using System.Xml.Linq;
34

@@ -235,3 +236,242 @@ public override void Print()
235236
Console.WriteLine(string.Join(" ", _array));
236237
}
237238
}
239+
=======
240+
using System;
241+
using System.Xml.Linq;
242+
243+
namespace task4._1;
244+
245+
246+
public class UnoDimensional<T> : ArrayBase
247+
{
248+
private static T[] _array;
249+
private int _size, _capacity;
250+
251+
252+
public UnoDimensional(int capacity)
253+
{
254+
_capacity = capacity;
255+
_size = 0;
256+
_array = new T[_capacity];
257+
}
258+
259+
public UnoDimensional() : this(7) {}
260+
261+
public void Add(T element)
262+
{
263+
if (_size >= _capacity)
264+
{
265+
_capacity = _capacity * 2 + 1;
266+
T[] newArray = new T[_capacity];
267+
Array.Copy(newArray, 0, _array, 0, _size);
268+
_array = newArray;
269+
}
270+
_array[_size++] = element;
271+
}
272+
273+
public void Remove(int index)
274+
{
275+
if (index <= _size)
276+
{
277+
T[] newArray = new T[_capacity];
278+
for (int i = 0; i < _size; i++)
279+
{
280+
if (i != index)
281+
{
282+
newArray[i] = _array[i];
283+
}
284+
}
285+
_size--;
286+
_array = newArray;
287+
Array.Resize(ref _array, _size);
288+
}
289+
else
290+
{
291+
Console.WriteLine("You can't do this operation");
292+
}
293+
}
294+
295+
296+
public T[] Reverse()
297+
{
298+
T[] newArray = new T[_capacity];
299+
newArray = _array;
300+
T count = default(T);
301+
try
302+
{
303+
for (int i = 0; i < (newArray.Length / 2); i++)
304+
{
305+
count = newArray[i];
306+
newArray[i] = newArray[newArray.Length - i - 1];
307+
newArray[newArray.Length - i - 1] = count;
308+
}
309+
return newArray;
310+
}
311+
catch (Exception e)
312+
{
313+
Console.WriteLine($"{e}, \n so you can't do this operation");
314+
return _array;
315+
}
316+
}
317+
318+
public void Sorting()
319+
{
320+
Array.Sort(_array);
321+
}
322+
323+
public T[] Where<U>(Func<T, bool> condition)
324+
{
325+
T[] _arr = new T[_array.Length];
326+
int count = 0;
327+
for (int i = 0; i < _array.Length; i++)
328+
{
329+
if (condition(_arr[i]))
330+
{
331+
_arr[count++] = _array[i];
332+
}
333+
}
334+
Array.Resize(ref _arr, count);
335+
return _arr;
336+
}
337+
338+
public void PrintArrayWithCondition<U> (Func<T, bool> conditionFunc)
339+
{
340+
for (int i = 0; i < _array.Length; i++)
341+
{
342+
if (conditionFunc(_array[i]))
343+
{
344+
Console.Write(value: _array[i] + " ");
345+
}
346+
}
347+
Console.WriteLine();
348+
}
349+
350+
public int CountArrayWithCondition<U>(Func<T, bool> conditionFunc)
351+
{
352+
int count = 0;
353+
for (int i = 0; i < _array.Length; i++)
354+
{
355+
if (conditionFunc(_array[i]))
356+
{
357+
count += 1;
358+
}
359+
}
360+
Console.WriteLine();
361+
return count;
362+
}
363+
364+
public int CountArray<U>()
365+
{
366+
int count = 0;
367+
for (int i = 0; i < _array.Length; i++)
368+
{
369+
count += 1;
370+
}
371+
372+
return count;
373+
}
374+
375+
public int CountArrayFromIndex<U>(int index)
376+
{
377+
int count = 0;
378+
for (int i = index; i < _array.Length; i++)
379+
{
380+
count += 1;
381+
}
382+
return count;
383+
}
384+
385+
public T FindFirstElementByCondition(Func<T, bool> condition)
386+
{
387+
foreach (var element in _array)
388+
{
389+
if (condition(element))
390+
{
391+
return element;
392+
}
393+
}
394+
Console.WriteLine("Òàêèõ ýëåìåíòîâ íåò");
395+
return (T)Convert.ChangeType(-1, typeof(T));
396+
}
397+
398+
public bool EvenOneCondition<U>(Func<T, bool> conditionFunc)
399+
{
400+
for (int i = 0; i < _array.Length; i++)
401+
{
402+
if (conditionFunc(_array[i]))
403+
{
404+
return true;
405+
}
406+
}
407+
return false;
408+
}
409+
public bool AllCondition<U>(Func<T, bool> conditionFunc)
410+
{
411+
for (int i = 0; i < _array.Length; i++)
412+
{
413+
if (!conditionFunc(_array[i]))
414+
{
415+
return false;
416+
}
417+
}
418+
return true;
419+
}
420+
421+
public void ForEachAction<U>(Action<T> action)
422+
{
423+
for (int i = 0; i < _array.Length; i++)
424+
{
425+
action(_array[i]);
426+
}
427+
}
428+
429+
public T FindMin<U>(Func<T, T, int> compare) where U : IComparable<U>
430+
{
431+
T min = _array[0];
432+
for (int i = 1; i < _array.Length; i++)
433+
{
434+
if (compare(_array[i], min) < 0)
435+
{
436+
min = _array[i];
437+
}
438+
}
439+
return min;
440+
}
441+
442+
public T FindMax<U>(Func<T, T, int> compare) where U : IComparable<U>
443+
{
444+
T max = _array[0];
445+
for (int i = 1; i < _array.Length; i++)
446+
{
447+
if (compare(_array[i], max) > 0)
448+
{
449+
max = _array[i];
450+
}
451+
}
452+
return max;
453+
}
454+
455+
public void iterForeach(Action<T> action)
456+
{
457+
foreach (var item in _array)
458+
{
459+
if (!item.Equals(_array))
460+
{
461+
action(item);
462+
}
463+
}
464+
}
465+
466+
public bool Contains<U>(T element)
467+
{
468+
return EvenOneCondition<U>((x) => x.Equals(element));
469+
}
470+
471+
public override void Print()
472+
{
473+
Console.WriteLine();
474+
Console.WriteLine(string.Join(" ", _array));
475+
}
476+
}
477+
>>>>>>> 9908cc8 (done!)

0 commit comments

Comments
 (0)