diff --git a/Lesson04/Lesson04/Homework.cs b/Lesson04/Lesson04/Homework.cs new file mode 100644 index 0000000..947f733 --- /dev/null +++ b/Lesson04/Lesson04/Homework.cs @@ -0,0 +1,109 @@ +namespace Lesson04 +{ + + public delegate bool NumberCheck(int number); + public delegate int Calculate(int number); + + internal static class Homework + { + public static bool All(int[] array, NumberCheck checker) + { + foreach (var number in array) + { + if (!checker(number)) + { + return false; + } + } + + return true; + } + + public static bool AllPredicate(int[] array, Predicate predicate) + { + foreach (var number in array) + { + if (!predicate(number)) + { + return false; + } + } + + return true; + } + + public static bool Any(int[] array, NumberCheck checker) + { + foreach (var number in array) + { + if (checker(number)) + { + return true; + } + } + + return false; + } + + public static bool AnyPredicate(int[] array, Predicate predicate) + { + foreach (var number in array) + { + if (predicate(number)) + { + return true; + } + } + + return false; + } + + public static int Sum(int[] array, Calculate calculate) + { + int sum = 0; + + foreach (var number in array) + { + sum += calculate(number); + } + + return sum; + } + + public static int SumFunc(int[] array, Func func) + { + int sum = 0; + + foreach (var number in array) + { + sum += func(number); + } + + return sum; + } + + public static int Average(int[] array, Calculate calculate) + { + int sum = 0; + // 1 + 4 + 9 + 16 + 25 = 55 + foreach (var number in array) + { + sum += calculate(number); + } + + return sum / array.Length; + } + + public static int AverageFunc(int[] array, Func func) + { + int sum = 0; + + foreach (var number in array) + { + sum += func(number); + } + + return sum / array.Length; + } + } +} diff --git a/Lesson04/Lesson04/IRunnable.cs b/Lesson04/Lesson04/IRunnable.cs deleted file mode 100644 index 7f2682a..0000000 --- a/Lesson04/Lesson04/IRunnable.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Lesson04 -{ - internal interface IRunnable - { - void Run(); - } -} diff --git a/Lesson04/Lesson04/Person.cs b/Lesson04/Lesson04/Person.cs deleted file mode 100644 index 67675e6..0000000 --- a/Lesson04/Lesson04/Person.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Lesson04 -{ - internal class Person - { - public int Id { get; set; } - public string Name { get; set; } - - public virtual void DisplayInfo() - { - Console.WriteLine($"Id: {Id}, Name: {Name}"); - } - } -} diff --git a/Lesson04/Lesson04/Program.cs b/Lesson04/Lesson04/Program.cs index e5093ce..d675312 100644 --- a/Lesson04/Lesson04/Program.cs +++ b/Lesson04/Lesson04/Program.cs @@ -1,192 +1,158 @@ -namespace Lesson04 +using System.Runtime.ExceptionServices; + +namespace Lesson04 { internal class Program { - public delegate void PrintMessage(string message); - - // Func, Func, .... Func - // Action, Action, ... Action - // Predicate, Predicate, ... Predicate - static void Main(string[] args) + public static void Main(string[] args) { - #region delegates review - - // PrintMessage messagePrinter = new Delegate(PrintMessage); - //PrintMessage messagePrinter = PrintHello; - //messagePrinter += PrintGoodbye; - - //messagePrinter("John"); - - //messagePrinter.Invoke("Jane"); - - //messagePrinter = null; - - //if (messagePrinter != null) - //{ - // messagePrinter("Robert"); - //} - - //messagePrinter?.Invoke("Robert"); - - // --- // + int[] someNumbers = { 3, 21 , 24, 27, 9 , 91 , 63,-147}; - //Person person = new Person() - //{ - // Id = 1, - // Name = "John" - //}; - - //RegisterUser(person, GreetUser); - //RegisterUser(person, GreetUserRu); - //RegisterUser(person, GreetUserUz); + #region Count + int Divisible3Count = Count(someNumbers, DivisibleBy3); + int Divisible7Count = Count(someNumbers, DivisibleBy7); + Console.WriteLine($"Massivdagi 3 ga bo'linuvchilar soni: {Divisible3Count}"); + Console.WriteLine($"Massivdagi 7 ga bo'linuvchilar soni: {Divisible7Count}"); + Console.WriteLine(); #endregion + #region Max + int maxDivisible3 = Max(someNumbers, DivisibleBy3); + int maxDivisible7 = Max(someNumbers, DivisibleBy7); - #region Generics - - // PrintRed("Hello"); - //PrintRed("Hello"); - //PrintRed(123); - //PrintRed(true); - - //Student student = new Student() - //{ - // Id = 2, - // Name = "Jane", - // StudentNumber = 512 - //}; - - // student.DisplayInfo(); - - // PrintRed(student); - - // var newStudent = PrintRed(student, 2); - - //Person john = new Student(); - //john.Id = 4; - //john.Name = "John"; - //Student student = (Student)john; - //student.StudentNumber = 5; - - //Console.WriteLine(student.Name); - //student.DisplayInfo(); - - //Console.WriteLine(); - //student.Name = "Student"; - - //Console.WriteLine(student.Name); - //Console.WriteLine(john.Name); - + Console.WriteLine($"Massiivdagi eng katta 3 ga bo'linuvchi : {maxDivisible3}"); + Console.WriteLine($"Massiivdagi eng katta 7 ga bo'linuvchi : {maxDivisible7}"); + Console.WriteLine(); #endregion + #region Min + int minDivisible3 = Min(someNumbers, DivisibleBy3); + int minDivisible7 = Min(someNumbers, DivisibleBy7); - //ExecuteOperation(5, 7, Add, PrintGreen); - //ExecuteOperation(5, 7, Multiply, PrintRed); - //ExecuteOperation(5, 7, Subtract, PrintBlue); - } - - #region Delegate methods - - static void RegisterUser(Person person, PrintMessage printMessage) - { - // database -> registration - - bool isSuccess = true; //registration + Console.WriteLine($"Massiivdagi eng kichik 3 ga bo'linuvchi : {minDivisible3}"); + Console.WriteLine($"Massiivdagi eng kichik 7 ga bo'linuvchi : {minDivisible7}"); + Console.WriteLine(); + #endregion + #region Where + int[] elementsDivisible3 = Where(someNumbers, DivisibleBy3); + int[] elementsDivisible7 = Where(someNumbers, DivisibleBy7); - if (isSuccess) + Console.WriteLine("massivning 3 ga bo'linuvchi elementalari : "); + foreach (int number in elementsDivisible3) { - printMessage(person.Name); + Console.Write(number + " "); } - } + Console.WriteLine(); - static void GreetUser(string name) - { - Console.WriteLine($"Hello, {name}"); - } + Console.WriteLine("massivning 7 ga bo'linuvchi elementalari : "); + foreach (int number in elementsDivisible3) + { + Console.Write(number + " "); + } + Console.WriteLine(); + Console.WriteLine(); + #endregion + #region Converter + decimal[] USD = { 10_000, 20_000, 4_000 }; + decimal[] RUB = { 10_000, 20_000, 4_000 }; - static void GreetUserUz(string name) - { - Console.WriteLine($"Salom, {name}"); - } + decimal[] UZS1 = Convert(USD, GetUSDCourse); + decimal[] UZS2 = Convert(RUB, GetUSDCourse); - static void GreetUserRu(string name) - { - Console.WriteLine($"Привет, {name}"); - } + Console.Write("USD : "); + foreach(var i in USD) + { + Console.Write(i + " "); + } + Console.WriteLine(); - static void PrintHello(string name) - { - Console.WriteLine($"Hello: {name}"); - } + Console.Write("UZS : "); + foreach (var i in UZS1) + { + Console.Write(i + " "); + } + Console.WriteLine(); - static void PrintGoodbye(string name) - { - Console.WriteLine($"Goodbye: {name}"); - } + Console.Write("RUB : "); + foreach (var i in RUB) + { + Console.Write(i + " "); + } + Console.WriteLine(); - static int CalculateLength(string name) - { - return name.Length; + Console.Write("UZS : "); + foreach (var i in UZS2) + { + Console.Write(i + " "); + } + Console.WriteLine(); + #endregion } - - static void PrintError(string errorMessage, int errorCode) + #region methods with delegates + static int Count(int[] array, Predicate predicate) { - Console.WriteLine($"Error: {errorMessage} ({errorCode})"); + return array + .Where(x => predicate(x)) + .ToList() + .Count; } - - static void ExecuteOperation(int a, int b, Func func, Action display) + static int Max(int[] array, Predicatepredicate) { - int result = func(a, b); - - display(result); + return array + .Where(x => predicate(x)) + .Max(); } - - static int Add(int a, int b) + static int Min(int[] array, Predicate predicate) { - return a + b; + return array + .Where(x => predicate(x)) + .Min(); } + static int[] Where(int[] array , Predicate predicate) + { + return array + .Where(x => predicate(x)) + .ToArray(); - static int Multiply(int a, int b) - { - return a * b; + ///simple version + List list = new List(); + foreach(int i in array) + { + if (predicate(i)) + { + list.Add(i); + } + } + return list.ToArray(); } - - static int Subtract(int a, int b) + static decimal[] Convert(decimal[] array, Func converter) { - return a - b; + List list = new List(); + foreach(var i in array) + { + list.Add(converter(i)); + } + return list.ToArray(); } - #endregion - #region Generic methods - - static void PrintGreen(T value) where T : struct + #region simple Methods + static bool DivisibleBy3(int x) { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine(value); - Console.ResetColor(); + return x % 3 == 0; } - - static void PrintRed(T value) where T : struct + static bool DivisibleBy7(int x) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ResetColor(); + return x % 7 == 0; } - - static void PrintBlue(T value) where T : struct + static decimal GetUSDCourse(decimal x) { - Console.ForegroundColor = ConsoleColor.Blue; - Console.WriteLine(value); - Console.ResetColor(); + return x * 12_800; } - - static void Swap(ref T a, ref T b) + static decimal GetRUBCourse(decimal x) { - T temp = a; - a = b; - b = temp; + return x * 110; } - #endregion + } -} \ No newline at end of file +} diff --git a/Lesson04/Lesson04/Student.cs b/Lesson04/Lesson04/Student.cs deleted file mode 100644 index 35b2244..0000000 --- a/Lesson04/Lesson04/Student.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Lesson04 -{ - internal class Student : Person, IRunnable - { - public int StudentNumber { get; set; } - - public void Run() - { - Console.WriteLine("Student is running."); - } - - public override void DisplayInfo() - { - Console.WriteLine($"S number: {StudentNumber}"); - } - } -}