Advertisement
double_trouble

Перемножение матриц комплексных чисел

Mar 31st, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication14
  8. {
  9.  
  10.     class complex
  11.     {
  12.         int x, y;
  13.         public complex(int _x, int _y)
  14.         {
  15.             x = _x;
  16.             y = _y;
  17.         }
  18.  
  19.         public static complex operator*(complex a, complex b)
  20.         {
  21.             return new complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
  22.         }
  23.  
  24.         public static complex operator+(complex a, complex b)
  25.         {
  26.             return new complex(a.x + b.x, a.y + b.y);
  27.         }
  28.  
  29.         public override string ToString()
  30.         {
  31.             return String.Format("{0} : {1}", x, y);
  32.         }
  33.     }
  34.  
  35.     class comp_matrix
  36.     {
  37.         int h, w;
  38.         complex[,] x;
  39.         public comp_matrix(int _h, int _w)
  40.         {
  41.             x = new complex[_h, _w];
  42.             h = _h;
  43.             w = _w;
  44.         }
  45.  
  46.         public complex this[int i, int j]
  47.         {
  48.             set { x[i, j] = value; }
  49.             get { return x[i, j]; }
  50.         }
  51.  
  52.         public int getH
  53.         {
  54.             set { h = value; }
  55.             get { return h; }
  56.         }
  57.  
  58.         public int getW
  59.         {
  60.             set { w = value; }
  61.             get { return w; }
  62.         }
  63.  
  64.         public static comp_matrix operator*(comp_matrix a, comp_matrix b)
  65.         {
  66.             comp_matrix d = new comp_matrix(a.getH, b.getW);
  67.             for (int i = 0; i < a.getH; ++i)
  68.                 for (int j = 0; j < b.getW; ++j)
  69.                 {
  70.                     d[i, j] = new complex(0, 0);
  71.                     for (int k = 0; k < a.getW; k++)
  72.                     {
  73.                         d[i, j] = d[i, j] + a.x[i, k] * b.x[k, j];
  74.                     }
  75.                 }
  76.             return d;
  77.         }
  78.     }
  79.  
  80.     class Program
  81.     {
  82.         static void Main(string[] args)
  83.         {
  84.             int a, b, c;
  85.             string s = Console.ReadLine();
  86.             string[] f = s.Split(' ');
  87.             a = int.Parse(f[0]);
  88.             b = int.Parse(f[1]);
  89.             c = int.Parse(f[2]);
  90.             comp_matrix m1 = new comp_matrix(a, b);
  91.             comp_matrix m2 = new comp_matrix(b, c);
  92.  
  93.             string[] d = new string[2 * b];
  94.             for (int i = 0; i < a; i++)
  95.             {
  96.                 s = Console.ReadLine();
  97.                 d = s.Split(' ');
  98.                 for (int j = 0; j < b; j++)
  99.                 {
  100.                     m1[i, j] = new complex(int.Parse(d[j]), int.Parse(d[j * 2 + 1]));
  101.                 }
  102.                
  103.             }
  104.  
  105.             d = new string[2 * c];
  106.             for (int i = 0; i < b; i++)
  107.             {
  108.                 s = Console.ReadLine();
  109.                 d = s.Split(' ');
  110.                 for (int j = 0; j < c; j++)
  111.                 {
  112.                     m2[i, j] = new complex(int.Parse(d[j]), int.Parse(d[j * 2 + 1]));
  113.                 }
  114.  
  115.             }
  116.  
  117.  
  118.             comp_matrix ans = m1 * m2;
  119.             Console.WriteLine("***********************************************");
  120.             for (int i = 0; i < ans.getH; i++)
  121.             {
  122.                 for (int j = 0; j < ans.getW; j++)
  123.                 {
  124.                     Console.Write("{0, 10} ", ans[i, j]);
  125.                 }
  126.                 Console.WriteLine();
  127.             }
  128.  
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement