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..c058e26 100644 --- a/Lesson04/Lesson04/Program.cs +++ b/Lesson04/Lesson04/Program.cs @@ -1,192 +1,198 @@ -namespace Lesson04 +using System.ComponentModel.DataAnnotations; + +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) - { - #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"); - - // --- // - - //Person person = new Person() - //{ - // Id = 1, - // Name = "John" - //}; - - //RegisterUser(person, GreetUser); - //RegisterUser(person, GreetUserRu); - //RegisterUser(person, GreetUserUz); + public delegate void Print1(string som); + + // 1. + // COUNT(int[] array, Predicate predicate) -> int + // massiv qabul qilib, shartni qanoatlantiradigan + // elementlar sonini qaytaradi + // 2. + // MAX(int[] array, Predicate predicate) -> int + // massiv qabul qilib, shartni qanoatlantiradigan + // elementlardan maksimal qiymanti qaytaradi + // 3. + // MIN(int[] array, Predicate predicate) -> int + // massiv qabul qilib, shartni qanoatlantiradigan + // elementlardan minimal qiymanti qaytaradi + // 4. + // Where (int[] array, Predicate predicate) -> int[] + // massiv qabul qilib, shartni qanoatlantiradigan elementlarni + // massivini qaytaradi. + // 5. + // Convert(decimal[] array, Func converter) -> decimal[] + // massiv qabul qilib, sonlarni o'zgartirib elementlarni natijasini qayataridi + + public static void Main(string[] args) + { + + int[] number = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + decimal[] numberValutaUSD = { 100, 1000, 43, 12, 56, 113 }; + decimal[] numberValuteUZS = { 1_000_000, 1_2374_000, 3_000, 34_000}; + + #region Count + + /*Console.WriteLine(Count(number, IsEven)); + Console.WriteLine(Count(number, IsPositive)); + Console.WriteLine(Count(number, IsOdd));*/ #endregion - #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; + #region Max + /* + Console.WriteLine(Max1(number, IsEven)); + Console.WriteLine(Max1(number, IsOdd)); + Console.WriteLine(Max1(number, IsNegative)); + Console.WriteLine(Max1(number, Ildiz)); + */ + #endregion - //Console.WriteLine(student.Name); - //student.DisplayInfo(); - //Console.WriteLine(); - //student.Name = "Student"; + #region WHERE + /* + Where(number, IsEven); + Console.WriteLine(); + Where(number, Ildiz); + Console.WriteLine(); + Where(number, IsOdd); + */ + #endregion + + #region CONVERTER - //Console.WriteLine(student.Name); - //Console.WriteLine(john.Name); + Convert(numberValutaUSD, USD_UZS); + Console.WriteLine("---------------------"); + Convert(numberValuteUZS, UZS_USD); #endregion - //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) + #region Count method + public static int Count(int[] numbers, Predicate predicate) { - // database -> registration - - bool isSuccess = true; //registration - - if (isSuccess) + int count = 0; + foreach (int i in numbers) { - printMessage(person.Name); + if (predicate(i) == true) + { + count++; + } } + return count; } - static void GreetUser(string name) - { - Console.WriteLine($"Hello, {name}"); - } + #endregion - static void GreetUserUz(string name) + #region MaxMethod + public static int Max1(int[] numbers, Predicate predicate) { - Console.WriteLine($"Salom, {name}"); + int max = int.MinValue; + for (int i = 0; i < numbers.Length; i++) + { + if (predicate(numbers[i])) + { + if (numbers[i] > max) + { + max = numbers[i]; + } + } + } + return max; } - static void GreetUserRu(string name) - { - Console.WriteLine($"Привет, {name}"); - } + #endregion - static void PrintHello(string name) - { - Console.WriteLine($"Hello: {name}"); - } + #region WhereMethod - static void PrintGoodbye(string name) + public static void Where(int[] numbers, Predicate predicate) { - Console.WriteLine($"Goodbye: {name}"); + int[] arr = new int[100]; + int count = 0; + for (int i = 0; i < numbers.Length; i++) + { + if (predicate(numbers[i])) + { + arr[count] = numbers[i]; + count++; + } + // Console.WriteLine(); + } + for (int i = 0; i < count; i++) + { + Console.Write(arr[i] + " "); + } + //return 1; } - static int CalculateLength(string name) - { - return name.Length; - } + #endregion + + #region ConvertMethod - static void PrintError(string errorMessage, int errorCode) + public static void Convert(decimal[] decimals, Func converter) { - Console.WriteLine($"Error: {errorMessage} ({errorCode})"); + for (int i = 0; i < decimals.Length; i++) + { + decimals[i] = converter(decimals[i]); + Console.WriteLine(decimals[i] + " "); + } } - static void ExecuteOperation(int a, int b, Func func, Action display) - { - int result = func(a, b); + #endregion - display(result); + static bool IsEven(int number) + { + return number % 2 == 0; } - static int Add(int a, int b) + static bool IsOdd(int number) { - return a + b; + return number % 2 != 0; } - static int Multiply(int a, int b) + static bool IsPositive(int number) { - return a * b; + return number >= 0; } - static int Subtract(int a, int b) + static bool IsNegative(int number) { - return a - b; + return number < 0; } - #endregion - - #region Generic methods - - static void PrintGreen(T value) where T : struct + static bool Ildiz(int number) { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine(value); - Console.ResetColor(); + double a = Math.Sqrt(number); + double b = Math.Pow(a, 2); + + return (number == b); } - static void PrintRed(T value) where T : struct + public static decimal USD_UZS(decimal decimals) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ResetColor(); + decimal HozirgiKursUSD = 12374;//somda 1usd + + return HozirgiKursUSD * decimals; } - static void PrintBlue(T value) where T : struct + public static decimal UZS_USD(decimal decimals) { - Console.ForegroundColor = ConsoleColor.Blue; - Console.WriteLine(value); - Console.ResetColor(); - } + decimal HozirgiKursUZS = (decimal)0.081; - static void Swap(ref T a, ref T b) + return HozirgiKursUZS * decimals; + } + public static void SOM(string som) { - T temp = a; - a = b; - b = temp; + Console.WriteLine(" som"); + } + public static void Dollar(string dollar) + { + Console.WriteLine(" $"); } - - #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}"); - } - } -}