Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Metody Numeryczne zadanie INT1
- Przygotował: Dawid Grzeszuk
- Kierunek: Informatyka 3 semestr
- Środowisko: Visual Studio 2015
- Język programowania: C#
- */
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace INT
- {
- class UserMatrix
- {
- private readonly double?[,] _tab;
- private readonly int _NrOfNodes;
- private readonly int _columns;
- public UserMatrix()
- {
- Console.WriteLine("Podaj stopien wielomianu:");
- _NrOfNodes = Convert.ToInt32(Console.ReadLine());
- _columns = _NrOfNodes + 1;
- _tab = new double?[_columns, 2];
- Console.Clear();
- }
- public void FillTable()
- {
- for (int x = 0; x < _columns; 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();
- Console.WriteLine("x y");
- for (int x = 0; x < _columns; 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[,] _programTable;
- private double?[,] _userTable;
- private int _nodes;
- private double _max;
- private int _equation;
- private double Equation(double x, int n)
- {
- if (n == 0)
- return 1;
- else if (n == 1)
- return 2 * x;
- else
- return (((2 * n - 3 * x) * Equation(x, n - 1)) - ((x + 5 * n) * Equation(x, n - 2)));
- }
- public ProgramMatrix(int nodes, double?[,] matrix)
- {
- _programTable = new double[nodes + 1, nodes + 2];
- _userTable = matrix;
- _nodes = nodes;
- for (int x = 0; x < _nodes + 1; x++)
- {
- _programTable[x, 0] = 1;
- _programTable[x, 1] = 2 * _userTable[x, 0].Value;
- _programTable[x, _nodes] = _userTable[x, 1].Value;
- }
- for (int x = 0; x < _nodes + 1; x++)
- {
- for (int y = 2; y < _nodes; y++)
- {
- _programTable[x, y] = Equation(_userTable[x, 0].Value, y);
- }
- }
- }
- public void MakeMath()
- {
- for (int column = 0; column < _nodes; column++)
- {
- _max = _programTable[column, column];
- _equation = column;
- for (int x = column; x < _nodes+1; x++)
- {
- if (Math.Abs(_programTable[x, column]) > Math.Abs(_max))
- {
- _max = _programTable[x, column];
- _equation = x;
- }
- }
- if (Math.Abs(_max) < 1e-30)
- {
- throw new Exception();
- }
- for (int x = column; x < _nodes + 2; x++)
- {
- SwapElements(column, x, _equation, x);
- }
- for (int x = column; x < _nodes +2; x++)
- {
- _programTable[column, x] = _programTable[column, x] / _max;
- }
- for (int x = 0; x < _nodes+1; x++)
- {
- if (x != column)
- {
- double z = _programTable[x, column];
- for (int y = column; y < _nodes + 1; y++)
- {
- _programTable[x, y] = _programTable[x, y] - z * _programTable[column, y];
- }
- }
- }
- }
- }
- public void SwapElements(int x1, int y1, int x2, int y2)
- {
- double temp = _programTable[x1, y1];
- _programTable[x1, y1] = _programTable[x2, y2];
- _programTable[x2, y2] = temp;
- }
- public void ShowResult()
- {
- double x, p;
- bool choice = true;
- while (choice)
- {
- Console.WriteLine("\nPodaj x: ");
- x = Convert.ToDouble(Console.ReadLine());
- p = 0;
- for (int y = 0; y < _nodes; y++)
- {
- p = p + _programTable[y, _nodes] * Equation(x, y);
- }
- Console.WriteLine($"\nWynik to y={Math.Round(p,3)}");
- Console.WriteLine("Czy chcesz liczyć dalej? (t/n)");
- var option = Console.ReadLine();
- if (option == "t")
- {
- choice = true;
- }
- else if (option == "n")
- {
- choice = false;
- }
- else
- {
- Console.Clear();
- Console.WriteLine("Nacisnij t lub n");
- Console.ReadKey();
- ShowResult();
- }
- Console.Clear();
- }
- }
- public void ShowTable()
- {
- for (int x = 0; x < _nodes; x++)
- {
- for (int y = 0; y < _nodes + 1; y++)
- {
- Console.Write($"{Math.Round(_programTable[x, y], 3)} ");
- }
- Console.WriteLine();
- }
- }
- }
- 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();
- Console.WriteLine("\nNacisnij dowolny klawisz aby kontynuować");
- Console.ReadLine();
- Console.Clear();
- try
- {
- program.ShowResult();
- }
- catch
- {
- program.ShowResult();
- }
- }
- catch
- {
- Console.Clear();
- Console.WriteLine("Macierz Osobliwa");
- }
- finally
- {
- Console.WriteLine("Program wykonał swój kod");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment