Здесь код(рабочий) для консоли, нужно перенести его в форму(на подобии самой консоли) помогите пожалуйста! Код | using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication85 { class GaussZeidel { public static double epsilon = 0.01; //точность вычисления public int n, k, N; //N -допустимое число итераций, n - размерность квадратной матрицы коэффицентов, k-количество итераций public double s, Xi, diff = 1; //s - сумма, величина погрешности public double[,] matrix; //матрица коэффицентов public double[] value; //матрица значений public double[] roots; //матрица корней public bool diagonal;
public GaussZeidel(double[,] matrix, double[] value, int N, int n, double[] roots) { this.matrix = matrix; this.N = N; this.value = value; this.n = n; this.roots = roots; }
public bool DiagonallyDominant() { for (int i = 0; i < n; i++) { double sum = 0; for (int j = 0; j < n; j++) { if (i != j) { sum += Math.Abs(matrix[i, j]); } } if (Math.Abs(matrix[i, i]) >= sum) { diagonal = true; break; } else { diagonal = false; } } return diagonal; }
public void algoritm() { k = 0; while ((k <= N) && (diff >= epsilon)) { k = k + 1; for (int i = 0; i < n; i++) { s = 0; for (int j = 0; j < n; j++) { if (i != j) { s += matrix[i, j] * roots[j]; } } Xi = (value[i] - s) / matrix[i, i]; diff = Math.Abs(Xi - roots[i]); roots[i] = Xi; } } } } }
|
Код | using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication85 { class Program { static void Main(string[] args) { do { try { Console.WriteLine("-----------------------------------------------------------\nSolving systems of linear equations by Gauss-Seidel method\n-----------------------------------------------------------\nEnter dimension of the matrix:"); int n = int.Parse(Console.ReadLine()); double[,] a = new double[n, n]; double[] b = new double[n]; double[] x = new double[n]; //нулевые приближения for (int i = 0; i < n; i++) { x[i] = 0; } Console.WriteLine("===========================================================\nTo enter data from the keyboard - press 1\nTo enter random data - press 2"); int key = int.Parse(Console.ReadLine()); switch (key) { case 1: { for (int i = 0; i < n; i++) //ввод коэффицентов { for (int j = 0; j < n; j++) { Console.WriteLine("Enter the element from position [" + (i+1) + "," + (j+1) + "]:"); a[i, j] = double.Parse(Console.ReadLine()); } } for (int j = 0; j < n; j++) //ввод значений { Console.WriteLine("Enter the value from position [" + (j+1) + "]:"); b[j] = double.Parse(Console.ReadLine()); } } break; case 2: { Random r = new Random(); for (int i = 0; i < n; i++) //ввод коэффицентов { for (int j = 0; j < n; j++) { a[i, j] = r.Next(-50, 50); Console.WriteLine("The element from position [" + (i+1) + "," + (j+1) + "]: "+a[i,j]); } } for (int j = 0; j < n; j++) //ввод значений { b[j] = r.Next(-50, 50); Console.WriteLine("The value from position [" + (j+1) + "]: " + b[j]); } } break; default: Console.WriteLine("Input error.\n==========================================================="); break; } Console.WriteLine("==========================================================="); GaussZeidel test = new GaussZeidel(a, b, 500, n, x); bool IsDiagonal = test.DiagonallyDominant(); if (IsDiagonal==true) { Console.WriteLine("The matrix has the property of diagonal dominance\n==========================================================="); } else { Console.WriteLine("The matrix does not have the property of diagonal dominance\n==========================================================="); } test.algoritm(); for (int j = 0; j < n; j++) { Console.WriteLine("X" + (j+1) + " = " + test.roots[j]); } Console.WriteLine("The number of iterations: " + test.k + "\n===========================================================\nPress Esc to exit or any key to continue"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } while (Console.ReadKey().Key != ConsoleKey.Escape); } } }
|
|