Advertisement
isotonicq

APR

Dec 7th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.15 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 APR
  8. {
  9.     class UserMatrix
  10.     {
  11.         double[] FirstTable;
  12.         double[] SecondTable;
  13.  
  14.         public UserMatrix()
  15.         {
  16.             Console.WriteLine("Podaj wielkość tablicy");
  17.             FirstTable = new double[Convert.ToInt32(Console.ReadLine())];
  18.             SecondTable = new double[FirstTable.GetLength(0)];
  19.             FillMatrix();
  20.         }
  21.  
  22.         private void FillMatrix()
  23.         {
  24.             for(int x=0; x < FirstTable.GetLength(0) ;x++)
  25.             {
  26.                 Console.Clear();
  27.                 Console.WriteLine($"Podaj {x + 1} liczbe w 1 tablicy:  ");
  28.                 FirstTable[x] = Convert.ToDouble(Console.ReadLine());
  29.                 Console.WriteLine($"Podaj {x + 1} liczbe w 2 tablicy:  ");
  30.                 SecondTable[x] = Convert.ToDouble(Console.ReadLine());
  31.                 Console.Clear();
  32.             }
  33.         }
  34.  
  35.         public void ShowTables()
  36.         {
  37.             Console.WriteLine("Pierwsza tablica:");
  38.             foreach (var item in FirstTable)
  39.             {
  40.                 Console.Write($"{item} ");
  41.             }
  42.             Console.WriteLine("\nDruga tablica:");
  43.             foreach (var item in SecondTable)
  44.             {
  45.                 Console.Write($"{item} ");
  46.             }
  47.         }
  48.  
  49.         public double[] returnFirstTable => FirstTable;
  50.         public double[] returnSecondTable => SecondTable;
  51.  
  52.     }
  53.  
  54.     class ProgramMatrix
  55.     {
  56.         double[,] table;
  57.         int nodes;
  58.         int level;
  59.         UserMatrix user;
  60.  
  61.         public ProgramMatrix()
  62.         {
  63.             user = new UserMatrix();
  64.             Console.WriteLine("Podaj ilość węzłów:");
  65.             nodes = Convert.ToInt32(Console.ReadLine());
  66.             Console.WriteLine("Podaj stopień wielomianu:");
  67.             level = Convert.ToInt32(Console.ReadLine());            
  68.             Console.Clear();
  69.             table = new double[nodes, nodes + 1];
  70.             Console.WriteLine("Macierz przed rozwiązaniem:");
  71.             FillTable();
  72.         }
  73.  
  74.         private double sum(double[]tab1,double[]tab2,int x,int y)
  75.         {
  76.             double result=0;
  77.  
  78.             if (x == nodes  )
  79.             {
  80.                 for (int i = 0; i < level; i++)
  81.                 {
  82.                     double t = (Math.Pow(tab1[i], y) *tab2[i]);
  83.                     result += t;
  84.                 }
  85.             }
  86.             else
  87.             {
  88.                 for (int i = 0; i < level; i++)
  89.                 {
  90.                     result += Math.Pow(tab1[i], x + y);
  91.                 }
  92.             }
  93.  
  94.             return result;
  95.         }
  96.  
  97.         private void FillTable()
  98.         {
  99.             for(int x=0;x<nodes;x++)
  100.             {
  101.                 for(int y=0;y<nodes+1;y++)
  102.                 {
  103.                     table[x, y] = sum(user.returnFirstTable,user.returnSecondTable,y,x);
  104.                 }
  105.             }
  106.             ShowTable();
  107.             ShowResult();
  108.         }
  109.  
  110.         private void ShowTable()
  111.         {
  112.             for (int x = 0; x < nodes; x++)
  113.             {
  114.                 for (int y = 0; y < nodes + 1; y++)
  115.                 {
  116.                     Console.Write($"{table[x,y]} ");
  117.                 }
  118.                 Console.WriteLine();
  119.             }
  120.         }
  121.  
  122.         private void ShowResult()
  123.         {
  124.             try
  125.             {
  126.                 table = GaussMethod.ExecuteMethod(table, nodes);
  127.                 Console.WriteLine("\nMacierz po rozwiazaniu:");
  128.                 ShowTable();
  129.  
  130.                 List<double> row = new List<double>();
  131.                 row.Add(table[0,3]);
  132.                 row.Add(table[1,3]);
  133.                 row.Add(table[2,3]);
  134.  
  135.                 Console.WriteLine($"\nS = {GaussMethod.CalculateS(user.returnFirstTable,user.returnSecondTable,row,level)}");
  136.             }
  137.             catch
  138.             {
  139.                 Console.WriteLine("\nMacierz osobliwa");
  140.             }
  141.         }
  142.     }
  143.  
  144.     public static class GaussMethod
  145.     {
  146.         static double max;
  147.         static int row;
  148.  
  149.         public static double[,] ExecuteMethod(double[,] table,int nodes)
  150.         {
  151.             for(int column=0;column<nodes;column++)
  152.             {
  153.                 max = table[column, column];
  154.                 row = column;
  155.  
  156.                 for (int x = column; x < nodes; x++)
  157.                 {
  158.                     if (Math.Abs(table[x,column]) > Math.Abs(max))
  159.                     {
  160.                         max = table[x,column];
  161.                         row = x;
  162.                     }
  163.                 }
  164.  
  165.                 if (Math.Abs(max) < 1e-30)
  166.                 {
  167.                     throw new Exception();
  168.                 }
  169.  
  170.                 for (int x = column; x < nodes + 1; x++)
  171.                 {
  172.                     double temp = table[column, x];
  173.                     table[column, x] = table[row, x];
  174.                     table[row, x] = temp;
  175.                 }
  176.  
  177.                 for (int x = column; x < nodes + 1; x++)
  178.                 {
  179.                     table[column,x] /= max;
  180.                 }
  181.  
  182.                 for (int x = 0; x < nodes ; x++)
  183.                 {
  184.                     if (x != column)
  185.                     {
  186.                         double z = table[x, column];
  187.                         for (int y = column; y < nodes + 1; y++)
  188.                         {
  189.                             table[x, y] = table[x, y] - z * table[column, y];
  190.                         }
  191.                     }
  192.                 }
  193.             }
  194.             return table;
  195.         }
  196.  
  197.         public static double CalculateS(double[] tab1, double[] tab2, List<double> row ,int level)
  198.         {
  199.             double result = 0;
  200.             for (int x = 0; x < level; x++)
  201.             {
  202.                 result += Math.Pow(((row[0] + row[1] * tab1[x] + row[2] * tab1[x]) - tab2[x]), 2);
  203.  
  204.             }
  205.  
  206.             return result;
  207.         }
  208.     }
  209.        
  210.  
  211.     class Program
  212.     {
  213.         static void Main(string[] args)
  214.         {
  215.             ProgramMatrix program = new ProgramMatrix();
  216.         }
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement