Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- /*
- Przygotował: Dawid Grzeszuk
- Kierunek: Informatyka 3 semestr
- Środowisko: Visual Studio 2015
- Język programowania: C# */
- namespace D_GJ
- {
- class Macierz
- {
- private double[,] macierz;
- private int ilosc_rownan, kolumna, rownanie;
- public void wpisz_macierz()
- {
- Console.WriteLine("Podaj ilosc rownań: ");
- ilosc_rownan = Convert.ToInt16(Console.ReadLine());
- macierz = new double[ilosc_rownan, ilosc_rownan + 1];
- Console.Clear();
- for (int x = 0; x < ilosc_rownan; x++)
- {
- for (int y = 0; y < ilosc_rownan + 1; y++)
- {
- if (y < ilosc_rownan)
- {
- Console.WriteLine("Podaj A[{0},{1}]", x + 1, y + 1);
- }
- else
- {
- Console.WriteLine("Podaj B[{0}]", x + 1);
- }
- macierz[x, y] = Convert.ToDouble(Console.ReadLine());
- Console.Clear();
- }
- }
- }
- public void wyswietl_macierz()
- {
- for (int x = 0; x < ilosc_rownan; x++)
- {
- for (int y = 0; y < ilosc_rownan + 1; y++)
- {
- if (y < ilosc_rownan)
- {
- Console.Write("{0} ", macierz[x, y]);
- }
- else
- {
- Console.Write("| {0}", macierz[x, y]);
- }
- }
- Console.WriteLine();
- }
- }
- public void wyswietl_wyniki()
- {
- for (int x = 0; x < ilosc_rownan; x++)
- {
- Console.WriteLine("x{0}: {1} ",x+1,macierz[x,ilosc_rownan]);
- }
- }
- public void zaokraglij_macierz()
- {
- for (int x = 0; x < ilosc_rownan; x++)
- {
- for (int y = 0; y < ilosc_rownan + 1; y++)
- {
- macierz[x, y] = Math.Round(macierz[x, y],2);
- }
- }
- }
- public void metoda_gaussa()
- {
- for (kolumna = 0; kolumna < ilosc_rownan; kolumna++)
- {
- double max = macierz[kolumna, kolumna];
- rownanie = kolumna;
- for (int wiersz = kolumna; wiersz < ilosc_rownan; wiersz++)
- {
- if (Math.Abs(macierz[wiersz,kolumna]) > max)
- {
- max = Math.Abs(macierz[wiersz,kolumna]);
- rownanie = wiersz;
- }
- }
- if (Math.Abs(max) < 1E-20)
- {
- throw new Exception();
- }
- for (int x = kolumna; x < ilosc_rownan + 1; x++)
- {
- double zmienna_tymczasowa = macierz[kolumna,x];
- macierz[kolumna,x] = macierz[rownanie,x];
- macierz[rownanie, x] = zmienna_tymczasowa;
- }
- double pole = macierz[kolumna,kolumna];
- for (int x = kolumna; x < ilosc_rownan + 1; x++)
- {
- double zmienna_tymczasowa = macierz[kolumna,x] / pole;
- macierz[kolumna,x] = zmienna_tymczasowa;
- }
- for (int x = 0; x < ilosc_rownan; x++)
- {
- if (x != kolumna)
- {
- double g = macierz[x,kolumna];
- for (int y = kolumna; y < ilosc_rownan + 1; y++)
- {
- macierz[x,y] -= (g * macierz[kolumna,y]);
- }
- }
- }
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Macierz obiekt = new Macierz();
- obiekt.wpisz_macierz();
- Console.WriteLine("Macierz początkowa: ");
- obiekt.wyswietl_macierz();
- Console.WriteLine();
- try
- {
- obiekt.metoda_gaussa();
- obiekt.zaokraglij_macierz();
- Console.WriteLine("Macierz końcowa: ");
- obiekt.wyswietl_macierz();
- Console.WriteLine();
- Console.WriteLine("Wyniki: ");
- obiekt.wyswietl_wyniki();
- Console.WriteLine();
- }
- catch
- {
- Console.WriteLine("Macierz ukladu osobliwa");
- }
- finally
- {
- Console.WriteLine("\nProgram wykonal swoj kod");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement