Advertisement
isotonicq

Browary mają być zimne

Nov 17th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.95 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 INT
  8. {
  9.     class UserMatrix
  10.     {
  11.         private double?[,] Tab;
  12.         private int NrOfNodes;
  13.  
  14.         public UserMatrix()
  15.         {
  16.             Console.WriteLine("Podaj ilość węzłów:");
  17.             NrOfNodes = Convert.ToInt32(Console.ReadLine());
  18.             Tab = new double?[NrOfNodes, 2];
  19.             Console.Clear();
  20.         }
  21.  
  22.         public void FillTable()
  23.         {
  24.             for (int x = 0; x < NrOfNodes; x++)
  25.             {
  26.                 ShowTable();
  27.  
  28.                 for (int y = 0; y < 2; y++)
  29.                 {
  30.                     if (y == 0)
  31.                     {
  32.                         Console.WriteLine($"\nPodaj {x + 1} węzeł x: ");
  33.                         Tab[x, y] = Convert.ToDouble(Console.ReadLine());
  34.                     }
  35.                     else if (y == 1)
  36.                     {
  37.                         Console.WriteLine($"\nPodaj {x + 1} węzeł y: ");
  38.                         Tab[x, y] = Convert.ToDouble(Console.ReadLine());
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.  
  44.         public void ShowTable()
  45.         {
  46.             Console.Clear();
  47.  
  48.             for (int x = 0; x < NrOfNodes; x++)
  49.             {
  50.                 for (int y = 0; y < 2; y++)
  51.                 {
  52.                     if (Tab[x, y] == null)
  53.                     {
  54.                         Console.Write("- ");
  55.  
  56.                     }
  57.                     else
  58.                     {
  59.                         Console.Write("{0} ", Tab[x, y]);
  60.                     }
  61.                 }
  62.                 Console.WriteLine();
  63.             }
  64.         }
  65.  
  66.         public int ReturnNodes() => NrOfNodes;
  67.         public double?[,] ReturnMatrix() => Tab;
  68.     }
  69.  
  70.     class ProgramMatrix
  71.     {
  72.         private double[,] macierz;
  73.         private int NrOfNodes;
  74.         private double max;
  75.         private int rownanie;
  76.  
  77.         public ProgramMatrix(int nodes, double?[,] matrix)
  78.         {
  79.             macierz = new double[nodes, nodes + 1];
  80.             NrOfNodes = nodes;
  81.  
  82.             for (int x = 0; x < nodes; x++)
  83.             {
  84.                 macierz[x, 0] = 1;
  85.                 macierz[x, nodes] = matrix[x, 1].Value;
  86.             }
  87.  
  88.             for (int x = 0; x < nodes; x++)
  89.             {
  90.                 for (int y = 1; y < nodes; y++)
  91.                 {
  92.                     macierz[x, y] = macierz[x, y - 1]*matrix[x, 0].Value;
  93.                 }
  94.             }
  95.         }
  96.  
  97.         public void MakeMath()
  98.         {
  99.             for (int kolumna = 0; kolumna < NrOfNodes; kolumna++)
  100.             {
  101.                 double max = macierz[kolumna, kolumna];
  102.                 rownanie = kolumna;
  103.  
  104.                 for (int wiersz = kolumna; wiersz < NrOfNodes; wiersz++)
  105.                 {
  106.                     if (Math.Abs(macierz[wiersz, kolumna]) > max)
  107.                     {
  108.                         max = Math.Abs(macierz[wiersz, kolumna]);
  109.                         rownanie = wiersz;
  110.                     }
  111.                 }
  112.  
  113.                 if (Math.Abs(max) < 1E-20)
  114.                 {
  115.                     throw new Exception();
  116.                 }
  117.  
  118.                 for (int x = kolumna; x < NrOfNodes + 1; x++)
  119.                 {
  120.                     double zmienna_tymczasowa = macierz[kolumna, x];
  121.                     macierz[kolumna, x] = macierz[rownanie, x];
  122.                     macierz[rownanie, x] = zmienna_tymczasowa;
  123.                 }
  124.  
  125.                 double pole = macierz[kolumna, kolumna];
  126.  
  127.                 for (int x = kolumna; x < NrOfNodes + 1; x++)
  128.                 {
  129.                     double zmienna_tymczasowa = macierz[kolumna, x] / pole;
  130.                     macierz[kolumna, x] = zmienna_tymczasowa;
  131.                 }
  132.  
  133.                 for (int x = 0; x < NrOfNodes; x++)
  134.                 {
  135.                     if (x != kolumna)
  136.                     {
  137.                         double g = macierz[x, kolumna];
  138.                         for (int y = kolumna; y < NrOfNodes+ 1; y++)
  139.                         {
  140.                             macierz[x, y] -= (g * macierz[kolumna, y]);
  141.                         }
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.  
  147.         public void SwapElements(int x1, int y1, int x2, int y2)
  148.         {
  149.             double temp = macierz[x1, y1];
  150.             macierz[x1, y1] = macierz[x2, y2];
  151.             macierz[x2, y2] = temp;
  152.         }
  153.  
  154.         public void ShowTable()
  155.         {
  156.             for (int x = 0; x < NrOfNodes; x++)
  157.             {
  158.                 for (int y = 0; y < NrOfNodes + 1; y++)
  159.                 {
  160.                     Console.Write($"{macierz[x, y]} ");
  161.                 }
  162.                 Console.WriteLine();
  163.             }
  164.         }
  165.  
  166.         public void ShowResult()
  167.         {
  168.             double x, p;
  169.  
  170.             Console.WriteLine("\nPodaj x: ");
  171.             x = Convert.ToDouble(Console.ReadLine());
  172.             p = macierz[NrOfNodes - 1,NrOfNodes];
  173.  
  174.             for (int y = NrOfNodes - 2; y >= 0; y--)
  175.             {
  176.                 p = x * p + macierz[y,NrOfNodes];
  177.             }
  178.  
  179.             Console.WriteLine($"\nWynik to y={p}");
  180.         }
  181.  
  182.     }
  183.  
  184.     class Program
  185.     {
  186.         static void Main(string[] args)
  187.         {
  188.             var user = new UserMatrix();
  189.             user.FillTable();
  190.             var program = new ProgramMatrix(user.ReturnNodes(), user.ReturnMatrix());
  191.             Console.Clear();
  192.             Console.WriteLine("Macierz przed obliczeniami: ");
  193.             program.ShowTable();
  194.  
  195.             Console.WriteLine("\nMacierz po obliczeniach: ");
  196.             try
  197.             {
  198.                 program.MakeMath();
  199.                 program.ShowTable();
  200.                 program.ShowResult();
  201.             }
  202.             catch
  203.             {
  204.                Console.Clear();
  205.                Console.WriteLine("Macierz Osobliwa");
  206.             }
  207.  
  208.         }
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement