Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Лабораторная_6а
- {
- class Program
- { // Все строки матрицы с нечетными номерами содержат одинаковое количество нулей.
- static void Main(string[] args)
- {
- int[,] matr; bool flag = false;
- InputMatrixFile(out matr);
- string choice = "0";
- while (choice != "6")
- {
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- Console.WriteLine();
- Console.WriteLine(" 1. Ввод матрицы с клавиатуры.");
- Console.WriteLine(" 2. Ввод матрицы из файла.");
- Console.WriteLine(" 3. Вычисление характеристики.");
- Console.WriteLine(" 4. Нахождение куба матрицы. ");
- Console.WriteLine(" 5. Печать матрицы.");
- Console.WriteLine(" 6. Выход.");
- Console.WriteLine(" ");
- Console.WriteLine(" Введите номер пункта меню ");
- Console.ForegroundColor = ConsoleColor.Yellow;
- choice = Console.ReadLine();
- Console.ForegroundColor = ConsoleColor.White;
- switch (choice)
- {
- case "1": //Ввод с клавиатуры
- Console.ForegroundColor = ConsoleColor.Cyan;
- InputMatrix(out matr);
- flag = true;
- Console.ForegroundColor = ConsoleColor.White;
- break;
- case "2": //Ввод из файла
- Console.ForegroundColor = ConsoleColor.Cyan;
- InputMatrixFile(out matr);
- Console.WriteLine("Матрица успешно введена!");
- flag = true;
- Console.ForegroundColor = ConsoleColor.White;
- break;
- case "3": //Характеристика
- if (flag)
- {
- if (CharacteristicMatrix(ref matr))
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Характеристика выполняется :)");
- Console.ForegroundColor = ConsoleColor.White;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Характеристика не выполняется :(");
- Console.ForegroundColor = ConsoleColor.White;
- }
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("МАТРИЦА НЕ ВВЕДЕНА!");
- Console.ForegroundColor = ConsoleColor.White;
- }
- break;
- case "4":
- if (flag)
- {
- if (matr.GetLength(0) == matr.GetLength(1))
- {
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("Куб матрицы ");
- PrintMatrix(MatrixStepen(ref matr, 3));
- Console.ForegroundColor = ConsoleColor.White;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ТАКУЮ МАТРИЦУ НЕЛЬЗЯ ВОЗВЕСТИ В КУБ!");
- Console.ForegroundColor = ConsoleColor.White;
- }
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("МАТРИЦА НЕ ВВЕДЕНА!");
- Console.ForegroundColor = ConsoleColor.White;
- }
- break;
- case "5": //Печать матрицы
- if (flag)
- {
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("Текущая матрица ");
- PrintMatrix(matr);
- Console.ForegroundColor = ConsoleColor.White;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("МАТРИЦА НЕ ВВЕДЕНА!");
- Console.ForegroundColor = ConsoleColor.White;
- }
- break;
- case "6": //Выход
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.WriteLine(" До свидания!");
- Console.ForegroundColor = ConsoleColor.White;
- break;
- default:
- Console.WriteLine(" Введите 1, 2, 3, 4 или 5");
- break;
- }
- }
- Console.ReadLine();
- }
- private static void InputMatrix(out int[,] matr)
- {
- Console.WriteLine("Введите размеры матрицы ");
- string[] str = Console.ReadLine().Split(' ');
- int n = int.Parse(str[0]);
- int m = int.Parse(str[1]);
- matr = new int[n, m];
- Console.WriteLine("Введите матрицу ");
- for (int i = 0; i < n; i++)
- {
- str = Console.ReadLine().Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
- for (int j = 0; j < m; j++)
- {
- matr[i, j] = int.Parse(str[j]);
- }
- }
- }
- private static void InputMatrixFile(out int[,] matr)
- {
- using (var sr = new StreamReader("text1.txt"))
- {
- // ввод размерности
- string[] str = sr.ReadLine().Split(' ');
- var n = int.Parse(str[0]);
- var m = int.Parse(str[1]);
- matr = new int[n, m];
- // ввод матрицы
- int i;
- string line;
- for (i = 0; (i < n) && ((line = sr.ReadLine()) != null); i++)
- {
- string[] numbers = line.Split(new char[] { ' ', ',', '\t' });
- var j = 0;
- foreach (string numString in numbers)
- {
- int x;
- bool canConvert = int.TryParse(numString, out x);
- if (canConvert)
- {
- matr[i, j] = x;
- j++;
- }
- }
- }
- }
- }
- static void PrintMatrix(int[,] a)
- {
- Console.ForegroundColor = ConsoleColor.Cyan;
- for (int i = 0; i < a.GetLength(0); i++)
- {
- for (int j = 0; j < a.GetLength(1); j++)
- {
- Console.Write("{0} \t", a[i, j]);
- }
- Console.WriteLine();
- }
- Console.ForegroundColor = ConsoleColor.White;
- }
- static bool CharacteristicMatrix(ref int[,] matr)
- {
- int countNull = 0, countString = 0, countNull2 = 0, countString2 = 0;
- if (matr.GetLength(0) < 2 && matr.GetLength(1) < 2)
- {
- return true;
- }
- for (int i = 0; i < matr.GetLength(1); i++)//Нулей в первой строке
- {
- if (matr[0, i] == 0)
- {
- countNull++;
- }
- }
- for (int i = 2; i < matr.GetLength(0); i += 2)
- {
- countString++;
- for (int j = 0; j < matr.GetLength(1); j++)
- {
- if (matr[i, j] == 0)
- {
- countNull2++;
- }
- }
- if (countNull2 == countNull)
- {
- countString2++;
- }
- }
- if (countString == countString2)
- {
- return true;
- }
- else return false;
- }
- static int[,] MatrixStepen(ref int[,] matr, int stepen)
- {
- /*
- var matrB = new int[matr.GetLength(0), matr.GetLength(1)];
- for (int i = 0; i < matr.GetLength(0); i++)
- {
- for (int j = 0; j < matr.GetLength(0); j++)
- {
- for (int k = 0; k < matr.GetLength(1); k++)
- {
- matrB[i, j] += matr[i, k] * matr[k, j];
- }
- }
- }
- var matrC = new int[matr.GetLength(0), matr.GetLength(1)];
- for (int i = 0; i < matr.GetLength(0); i++)
- {
- for (int j = 0; j < matr.GetLength(0); j++)
- {
- for (int k = 0; k < matr.GetLength(1); k++)
- {
- matrC[i, j] += matr[i, k] * matrB[k, j];
- }
- }
- }
- matr = matrC;
- return matr;
- */
- var firstMatr = matr;
- for (int a = 1; a < stepen; a++)
- {
- var matrB = new int[matr.GetLength(0), matr.GetLength(1)];
- for (int i = 0; i < matr.GetLength(0); i++)
- {
- for (int j = 0; j < matr.GetLength(0); j++)
- {
- for (int k = 0; k < matr.GetLength(1); k++)
- {
- matrB[i, j] += firstMatr[i, k] * matr[k, j];
- }
- }
- }
- matr = matrB;
- }
- return matr;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement