Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace MatrixHelp
- {
- public class Matrix
- {
- public Matrix(string path)
- {
- ReadMatrixFromFile(path);
- }
- private double[][] matrix;
- public int RowsCount
- {
- get
- {
- return matrix.Length;
- }
- }
- public int ColumnsCount
- {
- get
- {
- return matrix[0].Length;
- }
- }
- public void ReadMatrixFromFile(string path)
- {
- string[] strings = File.ReadAllLines(path);
- string[][] strMatrix = Array.ConvertAll<String, String[]>(strings, s => s.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries));
- matrix = Array.ConvertAll<String[], Double[]>(
- strMatrix, strArr => Array.ConvertAll<String, Double>(
- strArr, s => Double.Parse(s)
- )
- );
- }
- public void WriteMatrix()
- {
- int elementLength = 5;
- for (int i = 0; i < RowsCount; i++)
- {
- for (int j = 0; j < ColumnsCount; j++)
- {
- string element = matrix[i][j].ToString();
- if (element.Contains(","))
- element = element.Remove(element.IndexOf(',') + 2);
- Console.Write(new String(' ', elementLength - element.Length) + element);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- public void SwapRows(int index1, int index2)
- {
- double[] row1 = matrix[index1];
- matrix[index1] = matrix[index2];
- matrix[index2] = row1;
- }
- public void AddRowTo(int targetIndex, int addingRowIndex, double multiplier)
- {
- for (int i = 0; i < ColumnsCount; i++)
- {
- matrix[targetIndex][i] += multiplier * matrix[addingRowIndex][i];
- }
- }
- public void MultiplyRow(int rowIndex, double multiplier)
- {
- for (int i = 0; i < ColumnsCount; i++)
- {
- matrix[rowIndex][i] *= multiplier;
- }
- }
- }
- }
- using System;
- namespace MatrixHelp
- {
- public class Program
- {
- public static void Main(String[] args)
- {
- var matrix = new Matrix("matrix");
- while (true)
- {
- matrix.WriteMatrix();
- switch (Console.ReadLine())
- {
- case "s":
- HandleSwap(matrix);
- break;
- case "a":
- HandleAddRow(matrix);
- break;
- case "m":
- HandleMultiplication(matrix);
- break;
- case "d":
- HandleDivision(matrix);
- break;
- }
- }
- }
- private static void HandleSwap(Matrix matrix)
- {
- Console.WriteLine("Enter two indexes:");
- string[] input = Console.ReadLine().Split(' ');
- int index1 = Int32.Parse(input[0]) - 1;
- int index2 = Int32.Parse(input[1]) - 1;
- matrix.SwapRows(index1, index2);
- }
- public static void HandleAddRow(Matrix matrix)
- {
- Console.WriteLine("Enter target row index, adding row index and multiplier:");
- string[] input = Console.ReadLine().Split(' ');
- int targetRowIndex = Int32.Parse(input[0]) - 1;
- int addingRowIndex = Int32.Parse(input[1]) - 1;
- double multiplier = Double.Parse(input[2]);
- matrix.AddRowTo(targetRowIndex, addingRowIndex, multiplier);
- }
- public static void HandleMultiplication(Matrix matrix)
- {
- Console.WriteLine("Enter row index and multiplier.");
- string[] input = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- int rowIndex = Int32.Parse(input[0]) - 1;
- double multiplier = Double.Parse(input[1]);
- matrix.MultiplyRow(rowIndex, multiplier);
- }
- public static void HandleDivision(Matrix matrix)
- {
- Console.WriteLine("Enter row index and divider.");
- string[] input = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- int rowIndex = Int32.Parse(input[0]) - 1;
- double multiplier = 1.0 / Double.Parse(input[1]);
- matrix.MultiplyRow(rowIndex, multiplier);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement