diff --git a/.DS_Store b/.DS_Store index 4285d94..ab50e17 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/4KindsMethods/4KindsMethods.csproj b/4KindsMethods/4KindsMethods.csproj new file mode 100644 index 0000000..b686486 --- /dev/null +++ b/4KindsMethods/4KindsMethods.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _4KindsMethods + enable + enable + + + diff --git a/4KindsMethods/Program.cs b/4KindsMethods/Program.cs new file mode 100644 index 0000000..ab68a96 --- /dev/null +++ b/4KindsMethods/Program.cs @@ -0,0 +1,52 @@ +// 4 kinds of common Methods overview + +void Method1() +{ + Console.WriteLine("Автор..."); +} +Method1(); + + +void Method2(string msg) +{ + Console.WriteLine(msg); +} +Method2("Текст сообщения"); + +void Method21(string msg, int count) +{ + int i = 0; + while (i < count) + { + Console.WriteLine(msg); + i++; + } + +} +Method21(count: 4, msg: "Текст"); // Для именованных переменных порядок значения не имеет. Count - сколько раз. + + +int Method3() +{ + return DateTime.Now.Year; +} + +int year = Method3(); +Console.WriteLine(year); + + +string Method4(int count, string c) // Вместо string тут можно char +{ + int i = 0; + string result = String.Empty; + + while(i < count) + { + result = result + c; + i++; + } + return result; +} + +string res = Method4(10, "some_text "); +Console.WriteLine(res); diff --git a/ArraySorting/ArraySorting.csproj b/ArraySorting/ArraySorting.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/ArraySorting/ArraySorting.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/ArraySorting/Program.cs b/ArraySorting/Program.cs new file mode 100644 index 0000000..680714a --- /dev/null +++ b/ArraySorting/Program.cs @@ -0,0 +1,37 @@ +// Упорядочить данные внутри массива методом выбора (от минимального к максимальному). + +int[] arr = {5, 2, 1, 6, 8, 20, 7, 2, 23, 3, 1}; + +void PrintArray(int[] array) +{ + int count = array.Length; + for (int i = 0; i < count; i++) + { + Console.Write($"{array[i]} "); // Вывели массив + } + Console.WriteLine(); +} + +void SelectionSort(int[] array) +{ + for (int i = 0; i < array.Length; i++) + { + int minPosition = i; + + for(int j = i + 1; j < array.Length; j++) // На лекции было (array.Length - 1), но так первый эл-т встает в конец + { + if (array[j] < array[minPosition]) + { + minPosition = j; + } + } + int temporary = array[i]; + array[i] = array[minPosition]; + array[minPosition] = temporary; + Console.Write($"{array[i]} "); // Вывели массив + } + Console.WriteLine(); +} + +PrintArray(arr); +SelectionSort(arr); diff --git a/DoubleArray/DoubleArray.csproj b/DoubleArray/DoubleArray.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/DoubleArray/DoubleArray.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DoubleArray/Program.cs b/DoubleArray/Program.cs new file mode 100644 index 0000000..941c664 --- /dev/null +++ b/DoubleArray/Program.cs @@ -0,0 +1,55 @@ +// Двумерные массивы + +string[,] table = new string[5, 8]; // по умолчанию String.Empty + +for(int rows = 0; rows < 5; rows++) +{ + for(int columns = 0; columns < 8; columns++) + { + Console.Write($"{table[rows, columns]}"); + } + Console.WriteLine(); // Разрыв для аккуратности вывода +} + + +int[,] matrix = new int [3,4]; // По умолчанию заполняется 0 +matrix[1,2] = 15; // обратились к элементу и изменили его + +for(int rows = 0; rows < matrix.GetLength(0); rows++) +{ + for(int columns = 0; columns < matrix.GetLength(1); columns++) + { + Console.Write($"{matrix[rows, columns]}"); + } + Console.WriteLine(); +} + + +int[,] array = new int[4,7]; +void PrintArray(int[,] arr) +{ + for(int i = 0; i < arr.GetLength(0); i++) + { + for(int j = 0; j < arr.GetLength(1); j++) + { + Console.Write($"{arr[i, j]}"); + } + Console.WriteLine(); + } +} + +void FillArray(int[,] arr) +{ + for(int i = 0; i < arr.GetLength(0); i++) + { + for(int j = 0; j < arr.GetLength(1); j++) + { + arr[i, j] = new Random().Next(1, 10); + } + } +} + +FillArray(array); +Console.WriteLine(); +PrintArray(array); +Console.WriteLine(); diff --git a/Factorial/Factorial.csproj b/Factorial/Factorial.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/Factorial/Factorial.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Factorial/Program.cs b/Factorial/Program.cs new file mode 100644 index 0000000..f73fdf7 --- /dev/null +++ b/Factorial/Program.cs @@ -0,0 +1,20 @@ +// Факториал через рекурсию + +double Factorial (int n) // Тип int переполянется на 17, надо заменить на double +{ + if(n == 1) + { + return 1; + } + else + { + return n * Factorial(n - 1); + } +} + +Console.WriteLine(Factorial(40)); + +for(int i = 1; i < 40; i++) // Проверка работы алгоритма, вывод всех чисел ДО искомого +{ + Console.WriteLine($"{i}! = {Factorial(i)}"); +} diff --git a/Fibonacci/Fibonacci.csproj b/Fibonacci/Fibonacci.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/Fibonacci/Fibonacci.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Fibonacci/Program.cs b/Fibonacci/Program.cs new file mode 100644 index 0000000..14782e4 --- /dev/null +++ b/Fibonacci/Program.cs @@ -0,0 +1,25 @@ +// Числа Фибоначчи + +int Fibonacci(int n) +{ + /* + if(n == 0) + { + return 0; + } + if(n == 1 || n == 2) + { + return 1; + } + */ + if (n == 0 || n == 1) + { + return n; + } + return Fibonacci(n - 1) + Fibonacci(n - 2); +} + +for(int i = 0; i < 17; i++) // Число можно задать любое, после 40 начнет работать медленно из-за затрат ресурсов +{ + Console.WriteLine(Fibonacci(i)); +} diff --git a/HW10/HW10.csproj b/HW10/HW10.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW10/HW10.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW10/Program.cs b/HW10/Program.cs new file mode 100644 index 0000000..5448a34 --- /dev/null +++ b/HW10/Program.cs @@ -0,0 +1,32 @@ +// Принять трёхзначное число, вывести 2й знак + +void FillArray(int[] numbers) // Рассматриваем число как массив - создаем и заполняем массив +{ + int length = numbers.Length; + int index = 0; + while(index < length) + { + numbers[index] = new Random().Next(1,9); + index++; + } +} + +void PrintArray(int[] nums) +{ + int count = nums.Length; + int ind = 0; + while(ind < count) + { + Console.Write(nums[ind]); + ind++; + } +} + + +int[] array = new int[3]; + +FillArray(array); +PrintArray(array); + +Console.WriteLine(":"); +Console.WriteLine(array[1]); diff --git a/HW13/HW13.csproj b/HW13/HW13.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW13/HW13.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW13/Program.cs b/HW13/Program.cs new file mode 100644 index 0000000..c69ce1b --- /dev/null +++ b/HW13/Program.cs @@ -0,0 +1,22 @@ +// Вывести 3-й знак заданного числа или сообщить, что 3-его знака нет (для двузначных чисел и цифр) + +class Program +{ + static void Main(string[] args) + { + // 1st method + Console.WriteLine("Введите Ваше число: "); + // 2nd method + string? userNumber = Console.ReadLine(); + Console.WriteLine(ThirdChar(userNumber)); + } + + static string ThirdChar(string? userNumber) + { + if (userNumber?.Length > 2) + { + return $"Третий знак - {userNumber[2]}"; + } + return "Третьего знака нет!"; + } +} diff --git a/HW15/HW15.csproj b/HW15/HW15.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW15/HW15.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW15/Program.cs b/HW15/Program.cs new file mode 100644 index 0000000..05fa6c4 --- /dev/null +++ b/HW15/Program.cs @@ -0,0 +1,21 @@ +// Принять на вход цифру, обозначающую день недели, и проверить, выходной ли это день. + + +class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Введите день недели (цифру от 1 до 7): "); + string? day = Console.ReadLine(); + Console.WriteLine(usersDay(day)); + } + + static string usersDay(string? day) + { + if (day == "6" || day == "7") + { + return "День выходной"; + } + return "День будний"; + } +} diff --git a/HW35/HW35.csproj b/HW35/HW35.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW35/HW35.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW35/Program.cs b/HW35/Program.cs new file mode 100644 index 0000000..c153ddb --- /dev/null +++ b/HW35/Program.cs @@ -0,0 +1,49 @@ +// Задать одномерный массив из 123 случайных чисел. +// Найти количество элементов массива, значения которых лежат в отрезке [10,99]. + +class Program +{ + static void Main() + { + int[] array = CreateRandomArray(); + PrintArray(array); + Console.WriteLine(Inside(array, 10, 99)); + } + + static int[] CreateRandomArray() + { + int[] array = new int[123]; + return FillArray(array); + } + + static int[] FillArray(int[] arr) + { + for (int i = 0; i < arr.Length; i++) + { + arr[i] = new Random().Next(1, 100); + } + return arr; + } + + static void PrintArray(int[] arr) + { + for (int i = 0; i < arr.Length; i++) + { + Console.Write($"{arr[i]} "); + } + Console.WriteLine(); + } + + static int Inside(int[] array, int from, int to) + { + int count = 0; + for (int i = 0; i < array.Length; i++) + { + if (array[i] >= from && array[i] <= to) + { + count++; + } + } + return count; + } +} diff --git a/HW37/HW37.csproj b/HW37/HW37.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW37/HW37.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW37/Program.cs b/HW37/Program.cs new file mode 100644 index 0000000..2a21479 --- /dev/null +++ b/HW37/Program.cs @@ -0,0 +1,108 @@ +// Найти произведение пар чисел в одномерном массиве. +// Парой считаем первый и последний элемент, второй и предпоследний и т.д. +// Результат записать в новом массиве. + +class Program +{ + static void Main() + { + int[] array = CreateRandomArray(); + PrintArray(array); + PrintArray(Multiply(array)); + } + + static int[] CreateRandomArray() + { + int[] array = new int[3]; + return FillArray(array); + } + + static int[] FillArray(int[] arr) + { + for (int i = 0; i < arr.Length; i++) + { + arr[i] = new Random().Next(1, 100); + } + return arr; + } + + static void PrintArray(int[] arr) + { + for (int i = 0; i < arr.Length; i++) + { + Console.Write($"{arr[i]} "); + } + Console.WriteLine(); + } + + static int[] Multiply(int[] array) + { + int[] newArray = new int[array.Length % 2 == 0 ? array.Length / 2 : array.Length / 2 + 1]; + if (array.Length % 2 == 1) + { + newArray[newArray.Length - 1] = array[newArray.Length - 1]; + } + for (int i = 0; i < array.Length / 2; i++) + { + newArray[i] = array[i] * array[array.Length - (i + 1)]; + } + return newArray; + } +} + + +// Найти произведение пар чисел в одномерном массиве. +// Парой считаем первый и последний элемент, второй и предпоследний и т.д. +// Результат записать в новом массиве. +/* +class Program +{ + static void Main() + { + int[] array = CreateRandomArray(); + PrintArray(array); + PrintArray(Multiply(array)); + } + + static int[] CreateRandomArray() + { + int[] array = new int[3]; + return FillArray(array); + } + + static int[] FillArray(int[] arr) + { + for (int i = 0; i < arr.Length; i++) + { + arr[i] = new Random().Next(1, 100); + } + return arr; + } + + static void PrintArray(int[] arr) + { + for (int i = 0; i < arr.Length; i++) + { + Console.Write($"{arr[i]} "); + } + Console.WriteLine(); + } + + static int[] Multiply(int[] array) + { + int[] newArray = new int[array.Length % 2 == 0 ? array.Length / 2 : array.Length / 2 + 1]; + for (int i = 0; i < newArray.Length; i++) + { + newArray[i] = array[i] * array[array.Length - (i + 1)]; // i + 1 потому что индекс с нуля, а Length даст длину (с 1) + } + + // TODO Костыль - исправить! + if (array.Length % 2 == 0) + { + return newArray; + } + newArray[newArray.Length - 1] = array[newArray.Length - 1]; + return newArray; + } +} +*/ diff --git a/Multiplication/Multiplication.csproj b/Multiplication/Multiplication.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/Multiplication/Multiplication.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Multiplication/Program.cs b/Multiplication/Program.cs new file mode 100644 index 0000000..e55a034 --- /dev/null +++ b/Multiplication/Program.cs @@ -0,0 +1,10 @@ +// Таблица умножения + +for(int i =2; i < 10; i++) +{ + for(int j = 2; j <= 10; j++) + { + Console.WriteLine($"{i} * {j} = {i * j}"); + } + Console.WriteLine(); // Разрыв +} diff --git a/Recursion/Program.cs b/Recursion/Program.cs new file mode 100644 index 0000000..1242e45 --- /dev/null +++ b/Recursion/Program.cs @@ -0,0 +1,64 @@ +// Рекурсивные функции. Вывод ч/б изображение, его заливка с помощью правила обхода + +//int[,] pic = new int[23, 25]; +int[,] pic = new int[,] +{ + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, + {0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, +}; + +void PrintImage(int[,] image) +{ + for(int i = 0; i < image.GetLength(0); i++) + { + for(int j = 0; j < image.GetLength(1); j++) + { + if(image[i,j] == 0) + { + Console.Write($" "); + } + else + { + Console.Write($"+"); + } + } + Console.WriteLine(); + } +} + +void FillImage(int row, int col) +{ + if(pic[row, col] == 0) + { + pic[row, col] = 1; + FillImage(row - 1, col); + FillImage(row, col - 1); + FillImage(row + 1, col); + FillImage(row, col + 1); + } +} + +PrintImage(pic); +FillImage(13,13); +PrintImage(pic); diff --git a/Recursion/Recursion.csproj b/Recursion/Recursion.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/Recursion/Recursion.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/TextEdit/Program.cs b/TextEdit/Program.cs new file mode 100644 index 0000000..9e4a458 --- /dev/null +++ b/TextEdit/Program.cs @@ -0,0 +1,24 @@ +// Замена символов в тексте + +string text= "- Я думаю, сказал, улыбаясь, князь, - что, " + + "ежели бы Вас послали вместо нашего..."; +string Replace(string text, char oldValue, char newValue) +{ + string result = string.Empty; + + int length = text.Length; + for(int i = 0; i < length; i++) + { + if(text[i] == oldValue) result = result + $"{newValue}"; + else result = result + $"{text[i]}"; + } + return result; +} + +string newText = Replace(text, ' ', '|'); +Console.WriteLine(newText); +Console.WriteLine(); + +newText = Replace(newText, 'а', 'А'); +Console.WriteLine(newText); +Console.WriteLine(); diff --git a/TextEdit/TextEdit.csproj b/TextEdit/TextEdit.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/TextEdit/TextEdit.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +