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;
- namespace INT
- {
- class UserMatrix
- {
- private double?[,] Tab;
- private int NrOfNodes;
- public UserMatrix()
- {
- Console.WriteLine("Podaj ilość węzłów:");
- NrOfNodes = Convert.ToInt32(Console.ReadLine());
- Tab = new double?[NrOfNodes, 2];
- Console.Clear();
- }
- public void FillTable()
- {
- for (int x = 0; x < NrOfNodes; x++)
- {
- ShowTable();
- for (int y = 0; y < 2; y++)
- {
- if (y == 0)
- {
- Console.WriteLine($"\nPodaj {x + 1} węzeł x: ");
- Tab[x, y] = Convert.ToDouble(Console.ReadLine());
- }
- else if (y == 1)
- {
- Console.WriteLine($"\nPodaj {x + 1} węzeł y: ");
- Tab[x, y] = Convert.ToDouble(Console.ReadLine());
- }
- }
- }
- }
- public void ShowTable()
- {
- Console.Clear();
- for (int x = 0; x < NrOfNodes; x++)
- {
- for (int y = 0; y < 2; y++)
- {
- if (Tab[x, y] == null)
- {
- Console.Write("- ");
- }
- else
- {
- Console.Write("{0} ", Tab[x, y]);
- }
- }
- Console.WriteLine();
- }
- }
- public int ReturnNodes() => NrOfNodes;
- public double?[,] ReturnMatrix() => Tab;
- }
- class ProgramMatrix
- {
- private double[,] macierz;
- private int NrOfNodes;
- private double max;
- private int rownanie;
- public ProgramMatrix(int nodes, double?[,] matrix)
- {
- macierz = new double[nodes, nodes + 1];
- NrOfNodes = nodes;
- for (int x = 0; x < nodes; x++)
- {
- macierz[x, 0] = 1;
- macierz[x, nodes] = matrix[x, 1].Value;
- }
- for (int x = 0; x < nodes; x++)
- {
- for (int y = 1; y < nodes; y++)
- {
- macierz[x, y] = macierz[x, y - 1]*matrix[x, 0].Value;
- }
- }
- }
- public void MakeMath()
- {
- for (int kolumna = 0; kolumna < NrOfNodes; kolumna++)
- {
- double max = macierz[kolumna, kolumna];
- rownanie = kolumna;
- for (int wiersz = kolumna; wiersz < NrOfNodes; 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 < NrOfNodes + 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 < NrOfNodes + 1; x++)
- {
- double zmienna_tymczasowa = macierz[kolumna, x] / pole;
- macierz[kolumna, x] = zmienna_tymczasowa;
- }
- for (int x = 0; x < NrOfNodes; x++)
- {
- if (x != kolumna)
- {
- double g = macierz[x, kolumna];
- for (int y = kolumna; y < NrOfNodes+ 1; y++)
- {
- macierz[x, y] -= (g * macierz[kolumna, y]);
- }
- }
- }
- }
- }
- public void SwapElements(int x1, int y1, int x2, int y2)
- {
- double temp = macierz[x1, y1];
- macierz[x1, y1] = macierz[x2, y2];
- macierz[x2, y2] = temp;
- }
- public void ShowTable()
- {
- for (int x = 0; x < NrOfNodes; x++)
- {
- for (int y = 0; y < NrOfNodes + 1; y++)
- {
- Console.Write($"{macierz[x, y]} ");
- }
- Console.WriteLine();
- }
- }
- public void ShowResult()
- {
- double x, p;
- Console.WriteLine("\nPodaj x: ");
- x = Convert.ToDouble(Console.ReadLine());
- p = macierz[NrOfNodes - 1,NrOfNodes];
- for (int y = NrOfNodes - 2; y >= 0; y--)
- {
- p = x * p + macierz[y,NrOfNodes];
- }
- Console.WriteLine($"\nWynik to y={p}");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var user = new UserMatrix();
- user.FillTable();
- var program = new ProgramMatrix(user.ReturnNodes(), user.ReturnMatrix());
- Console.Clear();
- Console.WriteLine("Macierz przed obliczeniami: ");
- program.ShowTable();
- Console.WriteLine("\nMacierz po obliczeniach: ");
- try
- {
- program.MakeMath();
- program.ShowTable();
- program.ShowResult();
- }
- catch
- {
- Console.Clear();
- Console.WriteLine("Macierz Osobliwa");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement