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/HW47/HW47.csproj b/HW47/HW47.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW47/HW47.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW47/Program.cs b/HW47/Program.cs new file mode 100644 index 0000000..08bc503 --- /dev/null +++ b/HW47/Program.cs @@ -0,0 +1,37 @@ +/* Задать двумерный массив размером m×n, заполненный случайными вещественными числами. +m = 3, n = 4. +0,5 7 -2 -0,2 +1 -3,3 8 -9,9 +8 7,8 -7,1 9 */ + +int m = 3; +int n = 4; + +double[,] array = new double[m,n]; +void FillArray(double[,] arr) +{ + for(int i = 0; i < arr.GetLength(0); i++) + { + for(int j = 0; j < arr.GetLength(1); j++) + { + arr[i, j] = Convert.ToDouble(new Random().Next(-10, 10)) / 10; + } + } +} + +void PrintArray(double[,] 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(); + } +} + +FillArray(array); +Console.WriteLine(); +PrintArray(array); +Console.WriteLine(); diff --git a/HW50/HW50.csproj b/HW50/HW50.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW50/HW50.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW50/Program.cs b/HW50/Program.cs new file mode 100644 index 0000000..219898b --- /dev/null +++ b/HW50/Program.cs @@ -0,0 +1,35 @@ +// Принять на вход позиции элемента в двумерном массиве, вернуть значение этого элемента. +// Или указать, что такого элемента нет. + +Console.WriteLine("Введите число строк: "); +int m = Convert.ToInt32(Console.ReadLine()); +Console.WriteLine("Введите число столбцов: "); +int n = Convert.ToInt32(Console.ReadLine()); + +int[,] array = new int[m, n]; +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(0,100); + } + } +} + +FillArray(array); + +Console.WriteLine ("Введите строку элемента: "); +int row = Convert.ToInt32(Console.ReadLine()); +Console.WriteLine ("Введите столбец элемента: "); +int column = Convert.ToInt32(Console.ReadLine()); + +if (row > m || column > n) +{ + Console.WriteLine ("Такого элемента нет!"); +} +else +{ + Console.WriteLine ("Элемент массива: " + array[row,column]); +} diff --git a/HW52/HW52.csproj b/HW52/HW52.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW52/HW52.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW52/Program.cs b/HW52/Program.cs new file mode 100644 index 0000000..14276f9 --- /dev/null +++ b/HW52/Program.cs @@ -0,0 +1,52 @@ +// Задать двумерный массив из целых чисел. Найти среднее арифметическое элементов в каждом столбце. + +int m = 4; +int n = 5; + +int[,] array = new int[m,n]; +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, 100); + } + } +} + +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(); + } +} + +FillArray(array); +PrintArray(array); +Console.WriteLine(); + +double ArithmeticMean(int column ) // Среднее арифметическое +{ + double count = 0; + double average = 0; + for (int i = 0; i < m; i++) + { + count += array [i, column]; + } + average = count / m; + return average; +} + +double result = 0; +for (int j = 0; j < n; j++) +{ + result = ArithmeticMean(j); + Console.WriteLine("Среднее арифметическое столбца: " + result); + +} diff --git a/HW64/HW64.csproj b/HW64/HW64.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW64/HW64.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW64/Program.cs b/HW64/Program.cs new file mode 100644 index 0000000..2d4d8cb --- /dev/null +++ b/HW64/Program.cs @@ -0,0 +1,27 @@ +// Задать значения M и N. Программа выведет все натуральные числа в промежутке от M до N. +using System; + +namespace HW64 +{ + class Program + { + static void Main() + { + int M = 1; + int N = 10; + + Console.WriteLine(Matrix(M, N)); + } + + static string Matrix(int M, int N) + { + string final = String.Empty; + + for (int i = M; i < N; i++) + { + final += i + ", "; + } + return final + N; + } + } +} diff --git a/HW66/HW66.csproj b/HW66/HW66.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW66/HW66.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW66/Program.cs b/HW66/Program.cs new file mode 100644 index 0000000..6923bea --- /dev/null +++ b/HW66/Program.cs @@ -0,0 +1,39 @@ +// Задать значения M и N. Находить сумму натуральных элементов в промежутке от M до N. + +using System; + +namespace HW66 +{ + class Program + { + static void Main() + { + int M = 4; + int N = 8; + + Console.WriteLine(Matrix(M, N)); // Вывод для упрощения проверки работы алгоритма + Console.WriteLine(Sum(M, N)); + } + + static string Matrix(int M, int N) + { + string final = String.Empty; + + for (int i = M; i < N; i++) + { + final += i + ", "; + } + return final + N; + } + + static int Sum(int M, int N) + { + int sum = 0; + for (int i = M; i <= N; i++) + { + sum += i; + } + return sum; + } + } +} diff --git a/HW68/HW68.csproj b/HW68/HW68.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/HW68/HW68.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HW68/Program.cs b/HW68/Program.cs new file mode 100644 index 0000000..4c49642 --- /dev/null +++ b/HW68/Program.cs @@ -0,0 +1,31 @@ +// Вычисление функции Аккермана с помощью рекурсии. Даны два неотрицательных числа m и n. + +using System; + +namespace HW68 +{ + class Program + { + static void Main() + { + int M = 4; + int N = 0; + + Console.WriteLine(A(M, N)); + } + + static int A(int M, int N) + { + if (M == 0) + { + return N + 1; + } + if (M > 0 && N == 0) + { + return A(M - 1, 1); + } + // M > 0 && N > 0 + return A(M - 1, A(M, N - 1)); + } + } +} 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, 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, 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 + + +