Advertisement
ArXen42

MatrixHelper

Apr 17th, 2016
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace MatrixHelp
  5. {
  6.     public class Matrix
  7.     {
  8.         public Matrix(string path)
  9.         {
  10.             ReadMatrixFromFile(path);
  11.         }
  12.         private double[][] matrix;
  13.  
  14.         public int RowsCount
  15.         {
  16.             get
  17.             {
  18.                 return matrix.Length;
  19.             }
  20.         }
  21.         public int ColumnsCount
  22.         {
  23.             get
  24.             {
  25.                 return matrix[0].Length;
  26.             }
  27.         }
  28.  
  29.         public void ReadMatrixFromFile(string path)
  30.         {
  31.             string[] strings = File.ReadAllLines(path);
  32.             string[][] strMatrix = Array.ConvertAll<String, String[]>(strings, s => s.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries));
  33.             matrix = Array.ConvertAll<String[], Double[]>(
  34.                     strMatrix, strArr => Array.ConvertAll<String, Double>(
  35.                             strArr, s => Double.Parse(s)
  36.                     )
  37.             );
  38.         }
  39.  
  40.         public void WriteMatrix()
  41.         {
  42.             int elementLength = 5;
  43.  
  44.             for (int i = 0; i < RowsCount; i++)
  45.             {
  46.                 for (int j = 0; j < ColumnsCount; j++)
  47.                 {
  48.                     string element = matrix[i][j].ToString();
  49.                     if (element.Contains(","))
  50.                         element = element.Remove(element.IndexOf(',') + 2);
  51.  
  52.                     Console.Write(new String(' ', elementLength - element.Length) + element);
  53.                 }
  54.  
  55.                 Console.WriteLine();
  56.             }
  57.  
  58.             Console.WriteLine();
  59.         }
  60.  
  61.         public void SwapRows(int index1, int index2)
  62.         {
  63.             double[] row1 = matrix[index1];
  64.             matrix[index1] = matrix[index2];
  65.             matrix[index2] = row1;
  66.         }
  67.  
  68.         public void AddRowTo(int targetIndex, int addingRowIndex, double multiplier)
  69.         {
  70.             for (int i = 0; i < ColumnsCount; i++)
  71.             {
  72.                 matrix[targetIndex][i] += multiplier * matrix[addingRowIndex][i];
  73.             }
  74.         }
  75.  
  76.         public void MultiplyRow(int rowIndex, double multiplier)
  77.         {
  78.             for (int i = 0; i < ColumnsCount; i++)
  79.             {
  80.                 matrix[rowIndex][i] *= multiplier;
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86. using System;
  87.  
  88. namespace MatrixHelp
  89. {
  90.     public class Program
  91.     {
  92.         public static void Main(String[] args)
  93.         {
  94.             var matrix = new Matrix("matrix");
  95.  
  96.             while (true)
  97.             {
  98.                 matrix.WriteMatrix();
  99.                 switch (Console.ReadLine())
  100.                 {
  101.                     case "s":
  102.                     HandleSwap(matrix);
  103.                     break;
  104.  
  105.                     case "a":
  106.                     HandleAddRow(matrix);
  107.                     break;
  108.  
  109.                     case "m":
  110.                     HandleMultiplication(matrix);
  111.                     break;
  112.  
  113.                     case "d":
  114.                     HandleDivision(matrix);
  115.                     break;
  116.                 }
  117.             }
  118.         }
  119.  
  120.         private static void HandleSwap(Matrix matrix)
  121.         {
  122.             Console.WriteLine("Enter two indexes:");
  123.  
  124.             string[] input = Console.ReadLine().Split(' ');
  125.             int index1 = Int32.Parse(input[0]) - 1;
  126.             int index2 = Int32.Parse(input[1]) - 1;
  127.  
  128.             matrix.SwapRows(index1, index2);
  129.         }
  130.  
  131.         public static void HandleAddRow(Matrix matrix)
  132.         {
  133.             Console.WriteLine("Enter target row index, adding row index and multiplier:");
  134.  
  135.             string[] input = Console.ReadLine().Split(' ');
  136.             int targetRowIndex = Int32.Parse(input[0]) - 1;
  137.             int addingRowIndex = Int32.Parse(input[1]) - 1;
  138.             double multiplier = Double.Parse(input[2]);
  139.  
  140.             matrix.AddRowTo(targetRowIndex, addingRowIndex, multiplier);
  141.         }
  142.  
  143.         public static void HandleMultiplication(Matrix matrix)
  144.         {
  145.             Console.WriteLine("Enter row index and multiplier.");
  146.             string[] input = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  147.             int rowIndex = Int32.Parse(input[0]) - 1;
  148.             double multiplier = Double.Parse(input[1]);
  149.  
  150.             matrix.MultiplyRow(rowIndex, multiplier);
  151.         }
  152.  
  153.         public static void HandleDivision(Matrix matrix)
  154.         {
  155.             Console.WriteLine("Enter row index and divider.");
  156.             string[] input = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  157.             int rowIndex = Int32.Parse(input[0]) - 1;
  158.             double multiplier = 1.0 / Double.Parse(input[1]);
  159.  
  160.             matrix.MultiplyRow(rowIndex, multiplier);
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement