From ef7a127ec01bfcc2a3db55635890256921ec2c20 Mon Sep 17 00:00:00 2001 From: "Miraziz.Khidoyatov" <49793609+Mirazyzz@users.noreply.github.com> Date: Thu, 21 Sep 2023 22:18:12 +0500 Subject: [PATCH 1/2] Homework lesson-03, lesson-04 --- Lesson04/Lesson04/Homework.cs | 109 +++++++++++++++ Lesson04/Lesson04/IRunnable.cs | 7 - Lesson04/Lesson04/Person.cs | 13 -- Lesson04/Lesson04/Program.cs | 241 +++++++++++++-------------------- Lesson04/Lesson04/Student.cs | 17 --- 5 files changed, 200 insertions(+), 187 deletions(-) create mode 100644 Lesson04/Lesson04/Homework.cs delete mode 100644 Lesson04/Lesson04/IRunnable.cs delete mode 100644 Lesson04/Lesson04/Person.cs delete mode 100644 Lesson04/Lesson04/Student.cs 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..556db10 100644 --- a/Lesson04/Lesson04/Program.cs +++ b/Lesson04/Lesson04/Program.cs @@ -2,191 +2,132 @@ { 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); + // 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[] evenNumbers = { 2, 4, 6, 8, 10 }; + int[] oddNumbers = { 1, 3, 5, 7, 9 }; + int[] evenNumbersWithOneOdd = { 2, 4, 6, 8, 9 }; + int[] oddNumbersWithOneEven = { 1, 3, 5, 7, 8 }; + + int[] positiveNumbers = { 1, 2, 3, 4, 5 }; + int[] negativeNumbers = { -1, -2, -3, -4, -5 }; + int[] positiveNumbersWithOneNegative = { 1, 2, 3, 4, -5 }; + int[] negativeNumbersWithOnePositive = { -1, -2, -3, -4, 5 }; + + #region All + + Console.WriteLine("---- All ----"); + + // check if all numbers are positive + bool isAllEven = Homework.All(evenNumbers, IsEven); // true + bool isAllOdd = Homework.All(oddNumbers, IsOdd); + bool res1 = Homework.All(evenNumbersWithOneOdd, IsEven); + bool res2 = Homework.All(oddNumbersWithOneEven, IsOdd); + + Console.WriteLine(isAllEven); + Console.WriteLine(isAllOdd); + Console.WriteLine(res1); + Console.WriteLine(res2); #endregion - #region Generics - - // PrintRed("Hello"); - //PrintRed("Hello"); - //PrintRed(123); - //PrintRed(true); + #region Any - //Student student = new Student() - //{ - // Id = 2, - // Name = "Jane", - // StudentNumber = 512 - //}; + Console.WriteLine("---- Any ----"); - // student.DisplayInfo(); + bool any1 = Homework.Any(positiveNumbers, IsNegative); // true + bool any2 = Homework.Any(negativeNumbers, IsPositive); + bool any3 = Homework.Any(positiveNumbersWithOneNegative, IsNegative); + bool any4 = Homework.Any(negativeNumbersWithOnePositive, IsPositive); - // 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(any1); + Console.WriteLine(any2); + Console.WriteLine(any3); + Console.WriteLine(any4); #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) - { - // database -> registration + #region Sum - bool isSuccess = true; //registration + Console.WriteLine("---- Sum ----"); - if (isSuccess) - { - printMessage(person.Name); - } - } + int sum1 = Homework.Sum(positiveNumbers, Square); // 55 + int sum2 = Homework.Sum(negativeNumbers, Square); + int sum3 = Homework.SumFunc(positiveNumbersWithOneNegative, Cube); + int sum4 = Homework.SumFunc(negativeNumbersWithOnePositive, Cube); - static void GreetUser(string name) - { - Console.WriteLine($"Hello, {name}"); - } + Console.WriteLine(sum1); + Console.WriteLine(sum2); + Console.WriteLine(sum3); + Console.WriteLine(sum4); - static void GreetUserUz(string name) - { - Console.WriteLine($"Salom, {name}"); - } + #endregion - static void GreetUserRu(string name) - { - Console.WriteLine($"Привет, {name}"); - } + #region Average - static void PrintHello(string name) - { - Console.WriteLine($"Hello: {name}"); - } + Console.WriteLine("---- Average ---- "); - static void PrintGoodbye(string name) - { - Console.WriteLine($"Goodbye: {name}"); - } + int average1 = Homework.Average(positiveNumbers, Square); + int average2 = Homework.Average(negativeNumbers, Square); + int average3 = Homework.AverageFunc(positiveNumbersWithOneNegative, Cube); + int average4 = Homework.AverageFunc(negativeNumbersWithOnePositive, Cube); - static int CalculateLength(string name) - { - return name.Length; - } + Console.WriteLine(average1); + Console.WriteLine(average2); + Console.WriteLine(average3); + Console.WriteLine(average4); - static void PrintError(string errorMessage, int errorCode) - { - Console.WriteLine($"Error: {errorMessage} ({errorCode})"); - } - - static void ExecuteOperation(int a, int b, Func func, Action display) - { - int result = func(a, b); - - display(result); - } - - static int Add(int a, int b) - { - return a + b; + #endregion } - static int Multiply(int a, int b) + static bool IsEven(int number) { - return a * b; + return number % 2 == 0; } - static int Subtract(int a, int b) + static bool IsOdd(int number) { - return a - b; + return number % 2 != 0; } - #endregion - - #region Generic methods - - static void PrintGreen(T value) where T : struct + static bool IsPositive(int number) { - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine(value); - Console.ResetColor(); + return number >= 0; } - static void PrintRed(T value) where T : struct + static bool IsNegative(int number) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ResetColor(); + return number < 0; } - static void PrintBlue(T value) where T : struct + static int Square(int number) { - Console.ForegroundColor = ConsoleColor.Blue; - Console.WriteLine(value); - Console.ResetColor(); + return number * number; } - static void Swap(ref T a, ref T b) + static int Cube(int number) { - T temp = a; - a = b; - b = temp; + return (int)Math.Pow(number, 3); } - - #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}"); - } - } -} From 85800ae0cbb62547df9004ab10d69a193d239a4f Mon Sep 17 00:00:00 2001 From: MuhammadMangitov Date: Sat, 23 Sep 2023 18:16:03 +0500 Subject: [PATCH 2/2] Homework_4_muhammad --- Lesson04/Lesson04/Program.cs | 179 ++++++++++++++++++++++++----------- 1 file changed, 122 insertions(+), 57 deletions(-) diff --git a/Lesson04/Lesson04/Program.cs b/Lesson04/Lesson04/Program.cs index 556db10..c058e26 100644 --- a/Lesson04/Lesson04/Program.cs +++ b/Lesson04/Lesson04/Program.cs @@ -1,7 +1,11 @@ -namespace Lesson04 +using System.ComponentModel.DataAnnotations; + +namespace Lesson04 { internal class Program { + public delegate void Print1(string som); + // 1. // COUNT(int[] array, Predicate predicate) -> int // massiv qabul qilib, shartni qanoatlantiradigan @@ -24,82 +28,123 @@ internal class Program public static void Main(string[] args) { - int[] evenNumbers = { 2, 4, 6, 8, 10 }; - int[] oddNumbers = { 1, 3, 5, 7, 9 }; - int[] evenNumbersWithOneOdd = { 2, 4, 6, 8, 9 }; - int[] oddNumbersWithOneEven = { 1, 3, 5, 7, 8 }; - int[] positiveNumbers = { 1, 2, 3, 4, 5 }; - int[] negativeNumbers = { -1, -2, -3, -4, -5 }; - int[] positiveNumbersWithOneNegative = { 1, 2, 3, 4, -5 }; - int[] negativeNumbersWithOnePositive = { -1, -2, -3, -4, 5 }; + 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 All + #region Count - Console.WriteLine("---- All ----"); + /*Console.WriteLine(Count(number, IsEven)); + Console.WriteLine(Count(number, IsPositive)); + Console.WriteLine(Count(number, IsOdd));*/ - // check if all numbers are positive - bool isAllEven = Homework.All(evenNumbers, IsEven); // true - bool isAllOdd = Homework.All(oddNumbers, IsOdd); - bool res1 = Homework.All(evenNumbersWithOneOdd, IsEven); - bool res2 = Homework.All(oddNumbersWithOneEven, IsOdd); + #endregion - Console.WriteLine(isAllEven); - Console.WriteLine(isAllOdd); - Console.WriteLine(res1); - Console.WriteLine(res2); + #region Max + /* + Console.WriteLine(Max1(number, IsEven)); + Console.WriteLine(Max1(number, IsOdd)); + Console.WriteLine(Max1(number, IsNegative)); + Console.WriteLine(Max1(number, Ildiz)); + */ #endregion - #region Any - - Console.WriteLine("---- Any ----"); - bool any1 = Homework.Any(positiveNumbers, IsNegative); // true - bool any2 = Homework.Any(negativeNumbers, IsPositive); - bool any3 = Homework.Any(positiveNumbersWithOneNegative, IsNegative); - bool any4 = Homework.Any(negativeNumbersWithOnePositive, IsPositive); + #region WHERE + /* + Where(number, IsEven); + Console.WriteLine(); + Where(number, Ildiz); + Console.WriteLine(); + Where(number, IsOdd); + */ + #endregion + + #region CONVERTER - Console.WriteLine(any1); - Console.WriteLine(any2); - Console.WriteLine(any3); - Console.WriteLine(any4); + Convert(numberValutaUSD, USD_UZS); + Console.WriteLine("---------------------"); + Convert(numberValuteUZS, UZS_USD); #endregion - #region Sum - Console.WriteLine("---- Sum ----"); + } - int sum1 = Homework.Sum(positiveNumbers, Square); // 55 - int sum2 = Homework.Sum(negativeNumbers, Square); - int sum3 = Homework.SumFunc(positiveNumbersWithOneNegative, Cube); - int sum4 = Homework.SumFunc(negativeNumbersWithOnePositive, Cube); + #region Count method + public static int Count(int[] numbers, Predicate predicate) + { + int count = 0; + foreach (int i in numbers) + { + if (predicate(i) == true) + { + count++; + } + } + return count; + } - Console.WriteLine(sum1); - Console.WriteLine(sum2); - Console.WriteLine(sum3); - Console.WriteLine(sum4); + #endregion - #endregion + #region MaxMethod + public static int Max1(int[] numbers, Predicate predicate) + { + 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; + } + + #endregion - #region Average + #region WhereMethod - Console.WriteLine("---- Average ---- "); + public static void Where(int[] numbers, Predicate predicate) + { + 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; + } - int average1 = Homework.Average(positiveNumbers, Square); - int average2 = Homework.Average(negativeNumbers, Square); - int average3 = Homework.AverageFunc(positiveNumbersWithOneNegative, Cube); - int average4 = Homework.AverageFunc(negativeNumbersWithOnePositive, Cube); + #endregion - Console.WriteLine(average1); - Console.WriteLine(average2); - Console.WriteLine(average3); - Console.WriteLine(average4); + #region ConvertMethod - #endregion + public static void Convert(decimal[] decimals, Func converter) + { + for (int i = 0; i < decimals.Length; i++) + { + decimals[i] = converter(decimals[i]); + Console.WriteLine(decimals[i] + " "); + } } + #endregion + static bool IsEven(int number) { return number % 2 == 0; @@ -120,14 +165,34 @@ static bool IsNegative(int number) return number < 0; } - static int Square(int number) + static bool Ildiz(int number) { - return number * number; + double a = Math.Sqrt(number); + double b = Math.Pow(a, 2); + + return (number == b); } - static int Cube(int number) + public static decimal USD_UZS(decimal decimals) + { + decimal HozirgiKursUSD = 12374;//somda 1usd + + return HozirgiKursUSD * decimals; + } + + public static decimal UZS_USD(decimal decimals) + { + decimal HozirgiKursUZS = (decimal)0.081; + + return HozirgiKursUZS * decimals; + } + public static void SOM(string som) + { + Console.WriteLine(" som"); + } + public static void Dollar(string dollar) { - return (int)Math.Pow(number, 3); + Console.WriteLine(" $"); } } }