Advertisement
isotonicq

metoda gaussa c#

Oct 20th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.02 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. /*
  8. Przygotował: Dawid Grzeszuk
  9. Kierunek: Informatyka 3 semestr
  10. Środowisko: Visual Studio 2015
  11. Język programowania: C# */
  12.  
  13. namespace D_GJ
  14. {
  15.     class Macierz
  16.     {
  17.         private double[,] macierz;
  18.         private int ilosc_rownan, kolumna, rownanie;
  19.  
  20.         public void wpisz_macierz()
  21.         {
  22.             Console.WriteLine("Podaj ilosc rownań: ");
  23.             ilosc_rownan = Convert.ToInt16(Console.ReadLine());
  24.             macierz = new double[ilosc_rownan, ilosc_rownan + 1];
  25.  
  26.             Console.Clear();
  27.  
  28.             for (int x = 0; x < ilosc_rownan; x++)
  29.             {
  30.                 for (int y = 0; y < ilosc_rownan + 1; y++)
  31.                 {
  32.                     if (y < ilosc_rownan)
  33.                     {
  34.                         Console.WriteLine("Podaj A[{0},{1}]", x + 1, y + 1);
  35.                     }
  36.                     else
  37.                     {
  38.                         Console.WriteLine("Podaj B[{0}]", x + 1);
  39.                     }
  40.                     macierz[x, y] = Convert.ToDouble(Console.ReadLine());
  41.                     Console.Clear();
  42.                 }
  43.             }
  44.         }
  45.  
  46.         public void wyswietl_macierz()
  47.         {
  48.             for (int x = 0; x < ilosc_rownan; x++)
  49.             {
  50.                 for (int y = 0; y < ilosc_rownan + 1; y++)
  51.                 {
  52.                     if (y < ilosc_rownan)
  53.                     {
  54.                         Console.Write("{0} ", macierz[x, y]);
  55.                     }
  56.                     else
  57.                     {
  58.                         Console.Write("| {0}", macierz[x, y]);
  59.                     }
  60.                 }
  61.                 Console.WriteLine();
  62.             }
  63.         }
  64.  
  65.         public void wyswietl_wyniki()
  66.         {
  67.             for (int x = 0; x < ilosc_rownan; x++)
  68.             {
  69.                Console.WriteLine("x{0}: {1} ",x+1,macierz[x,ilosc_rownan]);                
  70.             }
  71.         }
  72.  
  73.         public void zaokraglij_macierz()
  74.         {
  75.             for (int x = 0; x < ilosc_rownan; x++)
  76.             {
  77.                 for (int y = 0; y < ilosc_rownan + 1; y++)
  78.                 {
  79.                     macierz[x, y] = Math.Round(macierz[x, y],2);
  80.                 }
  81.             }
  82.         }
  83.  
  84.         public void metoda_gaussa()
  85.         {
  86.  
  87.             for (kolumna = 0; kolumna < ilosc_rownan; kolumna++)
  88.             {
  89.                 double max = macierz[kolumna, kolumna];
  90.                 rownanie = kolumna;
  91.  
  92.                 for (int wiersz = kolumna; wiersz < ilosc_rownan; wiersz++)
  93.                 {
  94.                     if (Math.Abs(macierz[wiersz,kolumna]) > max)
  95.                     {
  96.                         max = Math.Abs(macierz[wiersz,kolumna]);
  97.                         rownanie = wiersz;
  98.                     }
  99.                 }
  100.  
  101.                 if (Math.Abs(max) < 1E-20)
  102.                 {
  103.                     throw new Exception();
  104.                 }
  105.  
  106.                 for (int x = kolumna; x < ilosc_rownan + 1; x++)
  107.                 {
  108.                     double zmienna_tymczasowa = macierz[kolumna,x];
  109.                     macierz[kolumna,x] = macierz[rownanie,x];
  110.                     macierz[rownanie, x] = zmienna_tymczasowa;
  111.                 }
  112.  
  113.                 double pole = macierz[kolumna,kolumna];
  114.  
  115.                 for (int x = kolumna; x < ilosc_rownan + 1; x++)
  116.                 {
  117.                     double zmienna_tymczasowa = macierz[kolumna,x] / pole;
  118.                     macierz[kolumna,x] = zmienna_tymczasowa;
  119.                 }
  120.  
  121.                 for (int x = 0; x < ilosc_rownan; x++)
  122.                 {
  123.                     if (x != kolumna)
  124.                     {                
  125.                         double g = macierz[x,kolumna];
  126.                         for (int y = kolumna; y < ilosc_rownan + 1; y++)
  127.                         {
  128.                             macierz[x,y] -= (g * macierz[kolumna,y]);
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.     }
  135.  
  136.     class Program
  137.     {
  138.         static void Main(string[] args)
  139.         {
  140.             Macierz obiekt = new Macierz();
  141.             obiekt.wpisz_macierz();
  142.  
  143.             Console.WriteLine("Macierz początkowa: ");
  144.             obiekt.wyswietl_macierz();
  145.             Console.WriteLine();
  146.  
  147.             try
  148.             {
  149.                 obiekt.metoda_gaussa();
  150.                 obiekt.zaokraglij_macierz();
  151.                 Console.WriteLine("Macierz końcowa: ");
  152.                 obiekt.wyswietl_macierz();
  153.                 Console.WriteLine();
  154.  
  155.                 Console.WriteLine("Wyniki: ");
  156.                 obiekt.wyswietl_wyniki();
  157.                 Console.WriteLine();
  158.             }
  159.             catch
  160.             {
  161.                 Console.WriteLine("Macierz ukladu osobliwa");
  162.             }
  163.             finally
  164.             {
  165.                 Console.WriteLine("\nProgram wykonal swoj kod");
  166.             }            
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement