Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Lesson3
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[,] matrixA = new int[3, 3];
- int rows = matrixA.GetLength(0);
- int cols = matrixA.GetLength(1);
- int result;
- string[] userInput;
- Console.Write("Работа с матрицей." +
- "\nДля экспериментов необходима матрица 3х3." +
- "\nЕсли хотите сами заполнить матрицу, введите \"Да\", иначе заполним случайными числами: ");
- bool isManual = Console.ReadLine().ToLower() == "да";
- Console.Clear();
- bool isInputCorrect = false;
- Random random = new Random();
- if (isManual)
- {
- Console.WriteLine($"Введите {matrixA.Length} целых чисел через пробел:");
- userInput = Console.ReadLine().Split(' ');
- isInputCorrect = matrixA.Length == userInput.Length;
- if (isInputCorrect)
- {
- int currentRow;
- int currentCol;
- for (int i = 0; i < matrixA.Length; i++)
- {
- currentRow = i / rows;
- currentCol = i % rows;
- matrixA[currentRow, currentCol] = Convert.ToInt32(userInput[i]);
- }
- }
- else
- {
- Console.WriteLine("Введено неверное число символов. Матрица заполнена случайными числами.");
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- matrixA[i, j] = random.Next(0, 10);
- }
- }
- }
- }
- else
- {
- Console.WriteLine("Матрица заполнена случайными числами.");
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- matrixA[i, j] = random.Next(0, 10);
- }
- }
- }
- Console.WriteLine("Работаем с матрицей:");
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- Console.Write($"{matrixA[i, j]}\t");
- }
- Console.WriteLine();
- }
- result = 0;
- for (int j = 0; j < cols; j++)
- {
- result += matrixA[1, j];
- }
- Console.WriteLine($"Сумма чисел второй строки {result}.");
- result = 1;
- for (int i = 0; i < rows; i++)
- {
- result *= matrixA[i, 0];
- }
- Console.WriteLine($"Произведение чисел первого столбца {result}.");
- char sign;
- string dimension;
- int lineNumber;
- int numbersInLine = 0;
- int maxLineNumber = 0;
- const char signPlus = '+';
- const char signMultiplication = '*';
- const string rowName = "строка";
- const string colName = "столбец";
- bool quit;
- do
- {
- Console.WriteLine("\nЧто посчитаем дальше?");
- do
- {
- Console.Write("Что будем делать с числами (\"+\", \"*\")?: ");
- sign = Console.ReadLine()[0];
- isInputCorrect = sign == signPlus ||
- sign == signMultiplication;
- if (!isInputCorrect)
- {
- Console.WriteLine("Введен неверный знак, попробуйте еще раз.");
- }
- } while (!isInputCorrect);
- do
- {
- Console.Write("С числами в строке или столбце будем производить действия (\"строка\", \"столбец\"): ");
- dimension = Console.ReadLine().ToLower();
- switch (dimension)
- {
- case rowName:
- numbersInLine = cols;
- maxLineNumber = rows - 1;
- isInputCorrect = true;
- break;
- case colName:
- numbersInLine = rows;
- maxLineNumber = cols - 1;
- isInputCorrect = true;
- break;
- default:
- Console.WriteLine("Введена неверная команда, попробуйте еще раз");
- isInputCorrect = false;
- break;
- }
- } while (!isInputCorrect);
- do
- {
- Console.Write($"Введите номер линии, с числами которой работаем - {dimension} №: ");
- lineNumber = Convert.ToInt32(Console.ReadLine().ToLower());
- if (lineNumber <= maxLineNumber)
- {
- Console.WriteLine($"\nРаботаем с числами из {dimension} № {lineNumber}" +
- $"\nПрименяем операцию \"{sign}\"\n");
- isInputCorrect = true;
- }
- else
- {
- isInputCorrect = false;
- Console.WriteLine("Введен неверный номер, попробуйте еще раз.");
- }
- } while (!isInputCorrect);
- result = 0;
- switch (sign)
- {
- case signPlus:
- if (dimension == rowName)
- {
- for (int i = 0; i < numbersInLine; i++)
- {
- result += matrixA[lineNumber, i];
- }
- }
- else
- {
- for (int i = 0; i < numbersInLine; i++)
- {
- result += matrixA[i, lineNumber];
- }
- }
- break;
- case signMultiplication:
- result = 1;
- if (dimension == rowName)
- {
- for (int i = 0; i < numbersInLine; i++)
- {
- result *= matrixA[lineNumber, i];
- }
- }
- else
- {
- for (int i = 0; i < numbersInLine; i++)
- {
- result *= matrixA[i, lineNumber];
- }
- }
- break;
- default:
- Console.WriteLine("Ошибка!");
- break;
- }
- Console.WriteLine($"Результат операции {result}\n");
- Console.Write("Чтобы посчитать еще введите \"да\", при вводе других символов программа завершится");
- quit = Console.ReadLine().ToLower() != "да";
- } while (!quit);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement