Guest User

LosujMacierz

a guest
May 6th, 2023
221
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. namespace NamespaceName
  3. {
  4.     public class ClassName
  5.     {
  6.         public static void PrintMatrix(int[,] A)
  7.         {
  8.             int m = A.GetLength(0);
  9.             int n = A.GetLength(1);
  10.             for(int i = 0; i < m;i++)
  11.             {
  12.                 for(int j = 0; j < n;j++)
  13.                     Console.Write("{0} ",A[i,j]);
  14.                 Console.WriteLine();
  15.             }
  16.         }
  17.         public static void Transpose(int[,] A)
  18.         {
  19.             int m = A.GetLength(0);
  20.             int n = A.GetLength(1);
  21.             int temp;
  22.             if(n == m)
  23.             {
  24.                 for(int i = 0;i < n;i++)
  25.                     for(int j = i + 1; j < n; j++)
  26.                     {
  27.                         temp = A[i,j];
  28.                         A[i,j] = A[j,i];
  29.                         A[j,i] = temp;
  30.                     }
  31.             }
  32.         }
  33.         public static void ElemSymPolys(int[] x,int[] c)
  34.         {
  35.             int n = x.Length;
  36.             int i,j;
  37.             c[0] = 1;
  38.             for(i = 1;i <= n;i++)
  39.                 c[i] = 0;
  40.             for(i = 1;i <= n;i++)
  41.                 for(j = i;j>=1;j--)
  42.                     c[j] += c[j-1]*x[i-1];
  43.             Array.Reverse(c);
  44.                
  45.         }
  46.         public static void AddRows(int i,int j,int m,int[,] A)
  47.         {
  48.             int rowCount = A.GetLength(0);
  49.             for(int k = 0;k < rowCount;k++)
  50.                 A[i,k] += m*A[j,k];
  51.         }
  52.         public static void AddCols(int i,int j,int m,int[,] A)
  53.         {
  54.             int colCount = A.GetLength(1);
  55.             for(int k = 0;k < colCount;k++)
  56.                 A[k,i] += m*A[k,j];
  57.         }
  58.         public static void SwapRows(int i,int j,int[,] A)
  59.         {
  60.             int rowCount = A.GetLength(0);
  61.             int temp;
  62.             for(int k = 0;k < rowCount;k++)
  63.             {
  64.                 temp = A[i,k];
  65.                 A[i,k] = A[j,k];
  66.                 A[j,k] = temp;
  67.             }
  68.         }
  69.         public static void SwapCols(int i,int j,int[,] A)
  70.         {
  71.             int colCount = A.GetLength(1);
  72.             int temp;
  73.             for(int k = 0;k < colCount;k++)
  74.             {
  75.                 temp = A[k,i];
  76.                 A[k,i] = A[k,j];
  77.                 A[k,j] = temp;
  78.             }
  79.         }
  80.         public static void Main(string[] args)
  81.         {
  82.             char esc;
  83.             int i,j,n,w,m;
  84.             int d;
  85.             int[,] A;
  86.             int[] x;
  87.             int[] c;
  88.             Random r = new Random();
  89.             do
  90.             {
  91.                 Console.WriteLine("Podaj stopien macierzy kwadratowej jaką chcesz wygenerowac");
  92.                 int.TryParse(Console.ReadLine(),out n);
  93.                 A = new int[n,n];
  94.                 x = new int[n];
  95.                 c = new int[n+1];
  96.                 for(int k = 0;k < n;k++)
  97.                     x[k] = -10+r.Next(21);
  98.                 ElemSymPolys(x,c);
  99.                 d = -1;
  100.                 for(int k = 1;k <= n;k++)
  101.                 {
  102.                     A[0,n-k] = d*c[k-1];
  103.                     d = -d;
  104.                 }
  105.                 for(i = 1;i < n;i++)
  106.                    for(j = 0;j < n;j++)
  107.                      A[i,j] = (j+1 == i ?1:0);
  108.                 /*PrintMatrix(A);
  109.                 foreach(int p in x)
  110.                     Console.Write("{0} ",p);
  111.                 Console.WriteLine(); */
  112.                 do
  113.                 {
  114.                     Console.WriteLine("0. Wyjscie");
  115.                     Console.WriteLine("1. Transpozycja macierzy");
  116.                     Console.WriteLine("2. Zamiana wierszy i kolumn");
  117.                     Console.WriteLine("3. Dodawanie wierszy i kolumn");
  118.                     Console.WriteLine("4. Wyswietlenie macierzy");
  119.                     int.TryParse(Console.ReadLine(),out w);
  120.                     switch(w)
  121.                     {
  122.                         case 0:
  123.                         {
  124.                             break;
  125.                         }
  126.                         case 1:
  127.                         {
  128.                             Transpose(A);
  129.                             break;
  130.                         }
  131.                         case 2:
  132.                         {
  133.                             Console.WriteLine("Podaj numery wierszy i kolumn ktore chcesz zamienic");
  134.                             int.TryParse(Console.ReadLine(),out i);
  135.                             int.TryParse(Console.ReadLine(),out j);
  136.                             i--;j--;
  137.                             SwapRows(i,j,A);
  138.                             SwapCols(i,j,A);
  139.                             break;
  140.                         }
  141.                         case 3:
  142.                         {
  143.                             Console.WriteLine("Podaj numery wierszy i kolumn ktore chcesz dodac");
  144.                             int.TryParse(Console.ReadLine(),out i);
  145.                             int.TryParse(Console.ReadLine(),out j);
  146.                             Console.WriteLine("Podaj mnoznik");
  147.                             int.TryParse(Console.ReadLine(),out m);
  148.                             i--;j--;
  149.                             AddRows(i,j,-m,A);
  150.                             AddCols(j,i,m,A);
  151.                             break;
  152.                         }
  153.                         case 4:
  154.                         {
  155.                             PrintMatrix(A);
  156.                             break;
  157.                         }
  158.                         default:
  159.                         {
  160.                             Console.WriteLine("Brak operacji");
  161.                             break;
  162.                         }
  163.                     }
  164.             }
  165.             while(w != 0); 
  166.                 esc = (char)Console.ReadKey().Key;
  167.             }
  168.             while(esc != (char)ConsoleKey.Escape);
  169.         }
  170.     }
  171. }
  172.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment