Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. //Guys 10 for 10, ok?
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace UzhashnayaProga
  9. {
  10.     class Program
  11.     {
  12.         /// <summary>
  13.         /// Create array MxN
  14.         /// </summary>
  15.         /// <param name="M">row</param>
  16.         /// <param name="N">col</param>
  17.         /// <param name="random"></param>
  18.         /// <returns></returns>
  19.         public static int[,] CreateArray(int M, int N, Random random)
  20.         {
  21.             if (M <= 0 || N <= 0) throw new Exception("Аргумент метода должен быть положительным!");
  22.             int[,] Array = new int[M, N];
  23.             for (int i = 0; i < M; i++)
  24.                 for (int j = 0; j < N; j++)
  25.                     Array[i, j] = random.Next(0, 11);
  26.             return Array;
  27.         }
  28.         /// <summary>
  29.         /// Multiply matrixs
  30.         /// </summary>
  31.         /// <param name="A">The first one</param>
  32.         /// <param name="B">The second</param>
  33.         /// <param name="fl"></param>
  34.         /// <returns></returns>
  35.         public static int[,] MatrixMult(int[,] A, int[,] B, out bool fl)
  36.         {
  37.             int[,] C = new int[A.GetUpperBound(0)+1, B.GetUpperBound(1)+1];
  38.             fl = false;
  39.             if (A.GetUpperBound(1) != B.GetUpperBound(0))  return C;
  40.             else
  41.             {
  42.                 fl = true;
  43.                 for (int row = 0; row <= A.GetUpperBound(0); row++)
  44.                     for (int col = 0; col <= B.GetUpperBound(1); col++)
  45.                         for (int inner = 0; inner <= A.GetUpperBound(1); inner++)
  46.                             C[row, col] += A[row, inner] * B[inner, col];
  47.                 return C;
  48.             }
  49.         }
  50.         /// <summary>
  51.         /// ...
  52.         /// </summary>
  53.         /// <param name="A"></param>
  54.         /// <returns></returns>
  55.         public static string MatrixToString(int[,] A)
  56.         {
  57.             string st = "";
  58.             for (int row = 0; row <= A.GetUpperBound(0); row++)
  59.             {
  60.                 for (int col = 0; col <= A.GetUpperBound(1); col++)
  61.                     st += A[row, col].ToString() + " ";
  62.                 st += "\n";
  63.             }
  64.             return st;
  65.         }
  66.         /// <summary>
  67.         /// String to int
  68.         /// </summary>
  69.         /// <param name="name"></param>
  70.         /// <param name="num"></param>
  71.         /// <returns></returns>
  72.         public static int StrToInt(string name, string num)
  73.         {
  74.             int b;
  75.             Console.WriteLine("Enter number of {0} in the {1} matrix.", name, num);
  76.             while (!int.TryParse(Console.ReadLine(), out b))
  77.                 Console.WriteLine("Please enter number of {0} again.", name);
  78.             return b;
  79.         }
  80.         /// <summary>
  81.         /// Idk why i did it.
  82.         /// </summary>
  83.         private static void JustDoIt()
  84.         {
  85.             int M, N;
  86.             bool fl;
  87.             int[,] A, B,C;
  88.             string a, b, c;
  89.             do
  90.             {
  91.                 Random random = new Random();
  92.                 try
  93.                 {
  94.                     M = StrToInt("rows", "first");
  95.                     N = StrToInt("columns", "first");
  96.                     A = CreateArray(M, N, random);
  97.                     M = StrToInt("rows", "second");
  98.                     N = StrToInt("columns", "second");
  99.                     B = CreateArray(M, N, random);
  100.                     C = MatrixMult(A, B, out fl);
  101.                     a = MatrixToString(A);
  102.                     b = MatrixToString(B);
  103.                    
  104.                 }catch(Exception ex)
  105.                 {
  106.                     Console.WriteLine(ex.Message);
  107.                     Console.WriteLine("Press Esc to exit.");
  108.                     continue;
  109.                 }
  110.                 if (fl)
  111.                 {
  112.                     c = MatrixToString(C);
  113.                     Console.WriteLine("Matrix A{0}{1}{0}Matrix B{0}{2}{0}AxB=C{0}Matrix C{0}{3}", "\n", a, b, c);
  114.                 }
  115.                 else
  116.                     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);
  117.                 Console.WriteLine("Do you want to clear screen?\nPress Y if you want else press any other key.");
  118.                 if (Console.ReadKey().Key == ConsoleKey.Y) Console.Clear();
  119.                 Console.WriteLine();
  120.                 Console.WriteLine("Press Esc to exit.");
  121.             } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
  122.         }
  123.             static void Main()
  124.         {
  125.             JustDoIt();
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement