Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Guys 10 for 10, ok?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace UzhashnayaProga
- {
- class Program
- {
- /// <summary>
- /// Create array MxN
- /// </summary>
- /// <param name="M">row</param>
- /// <param name="N">col</param>
- /// <param name="random"></param>
- /// <returns></returns>
- public static int[,] CreateArray(int M, int N, Random random)
- {
- if (M <= 0 || N <= 0) throw new Exception("Аргумент метода должен быть положительным!");
- int[,] Array = new int[M, N];
- for (int i = 0; i < M; i++)
- for (int j = 0; j < N; j++)
- Array[i, j] = random.Next(0, 11);
- return Array;
- }
- /// <summary>
- /// Multiply matrixs
- /// </summary>
- /// <param name="A">The first one</param>
- /// <param name="B">The second</param>
- /// <param name="fl"></param>
- /// <returns></returns>
- public static int[,] MatrixMult(int[,] A, int[,] B, out bool fl)
- {
- int[,] C = new int[A.GetUpperBound(0)+1, B.GetUpperBound(1)+1];
- fl = false;
- if (A.GetUpperBound(1) != B.GetUpperBound(0)) return C;
- else
- {
- fl = true;
- for (int row = 0; row <= A.GetUpperBound(0); row++)
- for (int col = 0; col <= B.GetUpperBound(1); col++)
- for (int inner = 0; inner <= A.GetUpperBound(1); inner++)
- C[row, col] += A[row, inner] * B[inner, col];
- return C;
- }
- }
- /// <summary>
- /// ...
- /// </summary>
- /// <param name="A"></param>
- /// <returns></returns>
- public static string MatrixToString(int[,] A)
- {
- string st = "";
- for (int row = 0; row <= A.GetUpperBound(0); row++)
- {
- for (int col = 0; col <= A.GetUpperBound(1); col++)
- st += A[row, col].ToString() + " ";
- st += "\n";
- }
- return st;
- }
- /// <summary>
- /// String to int
- /// </summary>
- /// <param name="name"></param>
- /// <param name="num"></param>
- /// <returns></returns>
- public static int StrToInt(string name, string num)
- {
- int b;
- Console.WriteLine("Enter number of {0} in the {1} matrix.", name, num);
- while (!int.TryParse(Console.ReadLine(), out b))
- Console.WriteLine("Please enter number of {0} again.", name);
- return b;
- }
- /// <summary>
- /// Idk why i did it.
- /// </summary>
- private static void JustDoIt()
- {
- int M, N;
- bool fl;
- int[,] A, B,C;
- string a, b, c;
- do
- {
- Random random = new Random();
- try
- {
- M = StrToInt("rows", "first");
- N = StrToInt("columns", "first");
- A = CreateArray(M, N, random);
- M = StrToInt("rows", "second");
- N = StrToInt("columns", "second");
- B = CreateArray(M, N, random);
- C = MatrixMult(A, B, out fl);
- a = MatrixToString(A);
- b = MatrixToString(B);
- }catch(Exception ex)
- {
- Console.WriteLine(ex.Message);
- Console.WriteLine("Press Esc to exit.");
- continue;
- }
- if (fl)
- {
- c = MatrixToString(C);
- Console.WriteLine("Matrix A{0}{1}{0}Matrix B{0}{2}{0}AxB=C{0}Matrix C{0}{3}", "\n", a, b, c);
- }
- else
- Console.WriteLine("Matrix A{0}{1}{0}Matrix B{0}{2}{0}Unfortunately, it is impossible to multiply matrix A and matrix B. :(", "\n", a, b);
- Console.WriteLine("Do you want to clear screen?\nPress Y if you want else press any other key.");
- if (Console.ReadKey().Key == ConsoleKey.Y) Console.Clear();
- Console.WriteLine();
- Console.WriteLine("Press Esc to exit.");
- } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
- }
- static void Main()
- {
- JustDoIt();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement