Exemplo n.º 1
0
 private static void Print(Gauss gauss)
 {
     Console.WriteLine();
     Console.WriteLine("Source Matrix");
     gauss.PrintMatrix();
     gauss.SolveMatrix();
     Console.WriteLine("Triangular matrix");
     gauss.PrintMatrix();
     Console.WriteLine("Answer:");
     foreach (var item in gauss.Answer)
     {
         Console.WriteLine("{0:f2}", item);
     }
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //выводим на экран информацию о интерфейсе программы
            Console.WriteLine("To read matrix elements");
            Console.WriteLine("from the keyboard enter 1");
            Console.WriteLine("from the file, enter 2");
            Console.WriteLine("for filling by random numbers 3");
            Console.Write("N: ");
            //пользователь выберает способ считывания элементов матрицы
            int select = int.Parse(Console.ReadLine());

            switch (select)
            {
            //ввод с клавиатуры
            case 1:
            {
                //вводим размер матрицы
                Console.WriteLine("Enter the number of dimension: ");
                uint size = uint.Parse(Console.ReadLine());
                //создаем экземпляр класса
                Gauss gauss = new Gauss(size, size);
                Console.WriteLine("Enter the elements of the matrix");
                Console.WriteLine("Enter the left side of equation" +
                                  "");
                for (int i = 0; i < gauss.Matrix.GetLength(0); i++)
                {
                    for (int j = 0; j < gauss.Matrix[i].Length; j++)
                    {
                        //считываем с экрана и переобразуем в число.
                        Console.Write("Enter an item [{0},{1}]: ", i, j);
                        gauss.Matrix[i][j] = double.Parse(Console.ReadLine());
                    }
                }
                Console.WriteLine("Enter the right side");
                for (int i = 0; i < size; i++)
                {
                    //считываем с экрана и переобразуем в число.
                    Console.Write("Enter an item [{0}]: ", i);
                    gauss.RightPart[i] = double.Parse(Console.ReadLine());
                }
                //выводим на экран исходную матрицу и треугольную и ответ
                Print(gauss);
            }
            break;

            case 2:
            {
                //вводим путь к файлу
                Console.Write("Enter the path to the file: ");
                string directory = Console.ReadLine();
                //объявляем экземпляр класса для работы с файлами


                StreamReader reader = new StreamReader(@directory);
                //считываем количество строк
                uint size = (uint)reader.ReadToEnd().Split('\n').Length;
                reader = new StreamReader(directory);
                Gauss  gauss = new Gauss(size, size);
                string line;
                int    k = 0;
                //считываем из файла элементы
                while ((line = reader.ReadLine()) != null)
                {
                    //в файле коэффициенты разделены пробелом и мы используя это переобразуем строку в массив чисел
                    string[] coefficients = line.Split(' ');
                    for (int i = 0; i < coefficients.Length - 1; i++)
                    {           //заполняем матрицу
                        gauss.Matrix[k][i] = double.Parse(coefficients[i]);
                    }
                    //заполняем правую часть системы уравнении
                    gauss.RightPart[k] = double.Parse(coefficients[coefficients.Length - 1]);
                    k++;
                }
                //закрываем поток считывания
                reader.Close();
                //выводим на экран матрицу и треугольную матрицу и ответы
                Print(gauss);
            }
            break;

            case 3:
            {
                //объявляем экземпляр класса для генерации случайных чисел
                Random random = new Random();
                //присваем случайный размер
                uint  size  = (uint)random.Next(3, 10);
                Gauss gauss = new Gauss(size, size);
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        //заполняем матрицу случайными коэффициентами от 0 до 10
                        gauss.Matrix[i][j] = random.Next(0, 10);
                    }
                    //заполняем правую часть уравнения коэеффициентами от 0 до 10
                    gauss.RightPart[i] = random.Next(0, 10);
                }
                //выводим на экран результат работы
                Print(gauss);
            }
            break;
            }

            Console.ReadKey();
        }