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;
- namespace cramer
- {
- class Program
- {
- static void sorcsere(double[,] tomb, ref int sorindex, int k)
- {
- double temp;
- for (int j = 0; j < tomb.GetLength(1); j++)
- {
- temp = tomb[sorindex,j];
- tomb[sorindex, j] = tomb[k, j];
- tomb[k, j] = temp;
- }
- }
- static void oszlopcsere(double[,] tomb, ref int oszlopindex, int k)
- {
- double temp;
- for (int i = 0; i < tomb.GetLength(0); i++)
- {
- temp = tomb[i, oszlopindex];
- tomb[i, oszlopindex] = tomb[i, k];
- tomb[i, k] = temp;
- }
- }
- static void abszelemkereses(double[,] tomb, double max, ref int sorindex, ref int oszlopindex, int k)
- {
- for (int i = k+1; i < tomb.GetLength(0); i++)
- {
- for (int j = k; j < tomb.GetLength(1); j++)
- {
- if (max == Math.Abs(tomb[i, j]))
- {
- sorindex = i;
- oszlopindex = j;
- }
- }
- }
- }
- static double maxabszkereses(double[,] tomb, int k)
- {
- double max = 0;
- for (int i = k+1; i < tomb.GetLength(0); i++)
- {
- for (int j = k; j < tomb.GetLength(1); j++)
- {
- if (max < Math.Abs(tomb[i, j]))
- {
- max = Math.Abs(tomb[i, j]);
- }
- }
- }
- return max;
- }
- static void visszatoltes(double[,] tomb1, double[,] tomb2)
- {
- for (int i = 0; i < tomb1.GetLength(0); i++)
- {
- for (int j = 0; j < tomb1.GetLength(1); j++)
- {
- tomb1[i, j] = tomb2[i, j];
- }
- }
- }
- static void csere(double[,] tomb, double[] szabadtagok, int oszlopindex)
- {
- for(int i = 0; i < tomb.GetLength(1); i++)
- {
- tomb[i,oszlopindex-1] = szabadtagok[i];
- }
- }
- static double determinans(double[,] tomb)
- {
- double szorzat = 1;
- for (int i = 0; i < tomb.GetLength(1); i++)
- {
- szorzat *= tomb[i, i];
- }
- return szorzat;
- }
- static void kiir(double[,] tomb)
- {
- for (int i = 0; i < tomb.GetLength(0); i++)
- {
- for (int j = 0; j < tomb.GetLength(1); j++)
- {
- Console.Write("{0,6:0.00} ", tomb[i, j]);
- }
- Console.WriteLine();
- }
- }
- static int kinullazas(double[,] tomb, ref int sorindex, ref int oszlopindex)
- {
- double hanyados, tarolt;
- int elojel = 1;
- for (int k = 0; k < tomb.GetLength(0) - 1; k++)
- {
- abszelemkereses(tomb, maxabszkereses(tomb, k), ref sorindex, ref oszlopindex, k);
- if(tomb[sorindex ,oszlopindex] != 0)
- {
- if(k != sorindex)
- {
- elojel *= -1;
- sorcsere(tomb, ref sorindex, k);
- }
- if(k != oszlopindex)
- {
- elojel *= -1;
- oszlopcsere(tomb, ref oszlopindex, k);
- }
- for (int i = k + 1; i < tomb.GetLength(0); i++)
- {
- tarolt = tomb[i, k];
- for (int j = k; j < tomb.GetLength(1); j++)
- {
- hanyados = tarolt / tomb[k, k];
- tomb[i, j] = tomb[i, j] + tomb[k, j] * -(hanyados);
- }
- }
- }
- }
- return elojel;
- }
- static void Main(string[] args)
- {
- int oszlopindex, elojel;
- int sor=0, oszlop=0;
- double[,] feltoltheto = new double[4, 4];
- double[,] tomb1 = new double[4, 4] { // cserére szánt
- {1, -2, 1, 0},
- {2, 1, 0, -1},
- {0, -1, 1, 2},
- {-1, 1, -1, 3}
- };
- double[,] tomb2 = new double[4, 4] { // eredeti
- {1, -2, 1, 0},
- {2, 1, 0, -1},
- {0, -1, 1, 2},
- {-1, 1, -1, 3}
- };
- double[] szabadtagok = new double[4] {4, 3, 1, -7};
- double[] determinansok = new double[5];
- double[] megoldasok = new double[4];
- kiir(tomb1);
- Console.WriteLine();
- elojel = kinullazas(tomb1, ref sor, ref oszlop);
- determinansok[0] = elojel*determinans(tomb1);
- visszatoltes(tomb1, tomb2);
- for (oszlopindex = 1; oszlopindex < tomb1.GetLength(0)+1; oszlopindex++)
- {
- csere(tomb1, szabadtagok, oszlopindex);
- elojel = kinullazas(tomb1, ref sor, ref oszlop);
- kiir(tomb1);
- Console.WriteLine();
- determinansok[oszlopindex] = elojel*determinans(tomb1);
- visszatoltes(tomb1, tomb2);
- }
- for (int i = 0; i < tomb2.GetLength(0); i++)
- {
- megoldasok[i] = determinansok[i+1] / determinansok[0];
- Console.WriteLine("x{0}= {1}", i+1, megoldasok[i]);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement