Advertisement
Slapoguzov

Lab6_OP

Dec 24th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Lab6
  4. {
  5.     class MyProgram
  6.     {
  7.         private static Matrix myMat;
  8.  
  9.         static void Main()
  10.         {
  11.             try
  12.             {
  13.  
  14.                 enterArr();
  15.                 menu();
  16.             }
  17.             catch (FormatException)
  18.             {
  19.                 Console.WriteLine("Ошибка Ввода!");
  20.             }
  21.    
  22.  
  23.             Console.ReadKey();
  24.         }
  25.  
  26.         static void enterArr()
  27.         {
  28.             Console.WriteLine("Введите кол-во строк");
  29.             int ln = int.Parse(Console.ReadLine());
  30.             Console.WriteLine("Введите кол-во столбцов");
  31.             int cl = int.Parse(Console.ReadLine());
  32.             double[,] arr = new double[ln,cl];
  33.  
  34.             Console.WriteLine("Введите коэффициенты матрицы:");
  35.             for (int i = 0; i < ln; i++)
  36.             {
  37.                 for (int k = 0; k < cl; k++)
  38.                 {
  39.                     Console.Write("a[{0},{1}]: ", i, k);
  40.                     arr[i, k] = double.Parse(Console.ReadLine());
  41.                 }
  42.                 Console.WriteLine();
  43.             }
  44.  
  45.             myMat = new Matrix(arr, ln, cl);
  46.  
  47.         }
  48.  
  49.         static void menu()
  50.         {
  51.             Console.WriteLine("Выберите действие");
  52.             Console.WriteLine("1.Преобразовать к треугольному виду");
  53.             Console.WriteLine("2.Кол-во строк, среднее ариф. которых меньше заданой велечины");
  54.             Console.WriteLine("3.Вывести матрицу");
  55.             Console.WriteLine("4.Выход");
  56.  
  57.             switch (Convert.ToInt32(Console.ReadLine()))
  58.             {
  59.                 case 1: myMat.convertToTriangle(); menu(); break;
  60.                 case 2: Console.WriteLine("Введите значение:"); Console.WriteLine(myMat.average(Convert.ToDouble(Console.ReadLine()))); menu(); break;
  61.                 case 3: printArr(myMat); menu(); break;
  62.                 case 4: Environment.Exit(0); break;
  63.  
  64.             }
  65.         }
  66.  
  67.         static void printArr(Matrix inMat)
  68.         {
  69.             for (int i = 0; i < inMat.line; i++)
  70.             {
  71.                 for (int k = 0; k < inMat.column; k++)
  72.                 {
  73.                     Console.Write("{0} ", inMat.arr[i,k]);
  74.                 }
  75.                 Console.WriteLine();
  76.             }
  77.         }
  78.     }
  79.  
  80.     class Matrix
  81.     {
  82.         public int line;
  83.  
  84.         public int column;
  85.         public double[,] arr;
  86.  
  87.         public Matrix (double[,] inMat, int ln, int cl)
  88.         {
  89.             this.arr = inMat;
  90.             this.column = cl;
  91.             this.line = ln;
  92.  
  93.         }
  94.  
  95.         public void convertToTriangle()
  96.         {
  97.             for (int i = 0; i < column-1; i++)
  98.             {
  99.                 double multK = arr[i+1, i] / arr[i, i];
  100.                 if (double.IsInfinity(multK) || double.IsNaN(multK))
  101.                 {
  102.                     continue;
  103.                 }
  104.                 for (int k=i+1; k < line; k++)
  105.                 {    
  106.                     arr[k,i] = 0;
  107.  
  108.                     for (int j = 1; j < column; j++)
  109.                     {
  110.                         arr[k, j] -= arr[i, j] * multK;
  111.                     }
  112.  
  113.                 }
  114.             }
  115.         }
  116.  
  117.         public int average(double val)
  118.         {
  119.             double avr;
  120.             int n = 0;
  121.             double sum = 0;
  122.  
  123.             for (int i = 0; i < line; i++)
  124.             {
  125.                 sum = 0;
  126.                 for (int k = 0; k < column; k++)
  127.                 {
  128.                     sum += arr[i,k];
  129.                 }
  130.  
  131.                 avr = sum / column;
  132.                 if (avr < val) n = n +1;
  133.             }
  134.  
  135.             avr = sum / n;
  136.             return n;
  137.         }
  138.  
  139.  
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement