Advertisement
Dantesio

Untitled

Nov 14th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main()
  4.         {
  5.  
  6.         }
  7.     }
  8.  
  9.  
  10.     class MatrixMultiply
  11.     {
  12.         public int[,] Do(int[,] left, int[,] right)
  13.         {
  14.             if (CheckOnCorrect(left, right))
  15.                 throw new IndexOutOfRangeException();
  16.  
  17.             int aRows = left.GetLength(0); int aCols = left.GetLength(1);
  18.             int bRows = right.GetLength(0); int bCols = right.GetLength(1);
  19.  
  20.             int[,] result = new int[left.GetLength(0),right.GetLength(1)];
  21.  
  22.             for (int i = 0; i < aRows; ++i) // каждая строка A
  23.                 for (int j = 0; j < bCols; ++j) // каждый столбец B
  24.                     for (int k = 0; k < aCols; ++k)
  25.                         result[i,j] += left[i,k] * right[k,j];
  26.             return result;
  27.         }
  28.  
  29.         public bool CheckOnCorrect(int[,] left, int[,] right)
  30.         {
  31.             if ((left.GetLength(1) == right.GetLength(0)))
  32.                 return true;
  33.             else
  34.                 return false;
  35.         }
  36.     }
  37.  
  38.  
  39.     class MatrixSum
  40.     {
  41.         public int[,] Do(int[,] left, int[,] right)
  42.         {
  43.             if (CheckOnCorrect(left, right))
  44.                 throw new IndexOutOfRangeException();
  45.  
  46.             int[,] result = new int[left.GetLength(0),right.GetLength(1)];
  47.  
  48.             for (int i = 0; i < left.GetLength(0); ++i)
  49.                 for (int j = 0; j < left.GetLength(1); ++j)
  50.                     result[i,j] = left[i,j] + right[i,j];
  51.             return result;
  52.         }
  53.  
  54.         public bool CheckOnCorrect(int[,] left, int[,] right)
  55.         {
  56.             if ((left.GetLength(0) == right.GetLength(0)) && (left.GetLength(1) == right.GetLength(1)))
  57.                 return true;
  58.             else
  59.                 return false;
  60.         }
  61.     }
  62.  
  63.  
  64.     class IO
  65.     {
  66.         public int[,] ReadMatrix(string path)
  67.         {
  68.             string[] text = File.ReadAllLines(path);
  69.             int[,] result = new int[text.Length, text[0].Split(' ').Length];
  70.             string[] bufer = new string[1];
  71.  
  72.             for (int i = 0; i < text.Length; i++)
  73.             {
  74.                 bufer = text[i].Split(' ');
  75.                 for (int j = 0; j < bufer.Length; j++)
  76.                 {
  77.                     result[i,j] = int.Parse(bufer[j]);
  78.                 }
  79.             }
  80.  
  81.             return result;
  82.         }
  83.  
  84.         public int ReadInt(string path)
  85.         {
  86.             string number = File.ReadAllText(path);
  87.             int cons = int.Parse(number);
  88.             return cons;
  89.         }
  90.  
  91.         public void WriteData(string path, string data)
  92.         {
  93.             File.AppendAllText(path, data);
  94.         }
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement