Advertisement
vaakata

MatrixOp10._06._2016

Jun 11th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MatrixOp10._06._2016
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int rows = int.Parse(Console.ReadLine());
  14.             int[][] matrix = new int[rows][];
  15.             List<string> operations = new List<string>();
  16.             matrix = ReadMatrix(rows, matrix);
  17.  
  18.             int indexOperation = 0;
  19.             do
  20.             {
  21.                 operations.Add(Console.ReadLine().ToLower());
  22.                 indexOperation++;
  23.             } while (operations[indexOperation - 1] != "end");
  24.  
  25.             for (int i = 0; i < operations.Count; i++)
  26.             {
  27.                 string[] currOperation = operations[i].Split(' ');
  28.  
  29.                 switch (currOperation[0])
  30.                 {
  31.                     case "remove":
  32.                         matrix = ProcessMethod(matrix, currOperation);
  33.                         break;
  34.  
  35.                     case "swap":
  36.                         matrix = SwapMethod(matrix, currOperation);
  37.                         break;
  38.  
  39.                     case "insert":
  40.                         matrix = InsertMethod(matrix, currOperation);
  41.                         break;
  42.                 }
  43.             }
  44.             PrintMatrix(rows, matrix);
  45.         }
  46.  
  47.         private static int[][] InsertMethod(int[][] matrix, string[] currOperation)
  48.         {
  49.             int[][] insertMatrix = matrix;
  50.             int insertRow = int.Parse(currOperation[1]);
  51.             List<int> temp = new List<int>();
  52.             int l = insertMatrix[insertRow].Length;
  53.             for (int i = 0; i < l; i++)
  54.             {
  55.                 temp.Add(insertMatrix[insertRow][i]);
  56.             }
  57.             temp.Insert(0, int.Parse(currOperation[2]));
  58.             insertMatrix[insertRow] = new int[temp.Count];
  59.             insertMatrix[insertRow] = temp.ToArray();
  60.             return insertMatrix;
  61.         }
  62.  
  63.         private static int[][] SwapMethod(int[][] matrix, string[] currOperation)
  64.         {
  65.             int[][] swappedMatrix = matrix;
  66.             int firstSwapRow = int.Parse(currOperation[1]);
  67.             int secondSwapRow = int.Parse(currOperation[2]);
  68.             int[] temp = new int[swappedMatrix[firstSwapRow].Length];
  69.             temp = swappedMatrix[firstSwapRow];
  70.             // firstSwapRow takes the secondSwapRow
  71.             swappedMatrix[firstSwapRow] = new int[swappedMatrix[secondSwapRow].Length];
  72.             swappedMatrix[firstSwapRow] = swappedMatrix[secondSwapRow];
  73.  
  74.             // secondSwapRow takes the firstSwapRow
  75.             swappedMatrix[secondSwapRow] = new int[temp.Length];
  76.             swappedMatrix[secondSwapRow] = temp;
  77.             return swappedMatrix;
  78.         }
  79.  
  80.         private static void PrintMatrix(int rows, int[][] matrix)
  81.         {
  82.             for (int printRow = 0; printRow < rows; printRow++)
  83.             {
  84.                 Console.WriteLine(string.Join(" ", matrix[printRow]));
  85.             }
  86.         }
  87.  
  88.         private static int[][] ProcessMethod(int[][] matrixMethod, string[] currOperation)
  89.         {
  90.             int[][] processingMatrix = matrixMethod;
  91.             switch (currOperation[2])
  92.             {
  93.                 case "row":
  94.                     int processRow = int.Parse(currOperation[3]);
  95.                     if (currOperation[1] == "even")
  96.                     {
  97.                         processingMatrix[processRow] = processingMatrix[processRow].Where(x => x % 2 != 0).ToArray();
  98.                     }
  99.                     else if (currOperation[1] == "odd")
  100.                     {
  101.                         processingMatrix[processRow] = processingMatrix[processRow].Where(x => x % 2 == 0).ToArray();
  102.                     }
  103.                     else if (currOperation[1] == "negative")
  104.                     {
  105.                         processingMatrix[processRow] = processingMatrix[processRow].Where(x => x >= 0).ToArray();
  106.                     }
  107.                     else if (currOperation[1] == "positive")
  108.                     {
  109.                         processingMatrix[processRow] = processingMatrix[processRow].Where(x => x < 0).ToArray();
  110.                     }
  111.                     break;
  112.  
  113.                 case "col":
  114.                     int processCol = int.Parse(currOperation[3]);
  115.                     for (int row = 0; row < processingMatrix.GetLength(0); row++)
  116.                     {
  117.                         if (processingMatrix[row].GetLongLength(0) >= processCol + 1)
  118.                         {
  119.                             if (currOperation[1] == "even")
  120.                             {
  121.                                 List<int> tempEvenCol = new List<int>();
  122.                                 tempEvenCol = processingMatrix[row].ToList();
  123.                                 if (tempEvenCol[processCol] % 2 == 0)
  124.                                 {
  125.                                     tempEvenCol.Remove(tempEvenCol[processCol]);
  126.                                     processingMatrix[row] = tempEvenCol.ToArray();
  127.                                 }
  128.                             }
  129.                             else if (currOperation[1] == "odd")
  130.                             {
  131.                                 List<int> tempOddCol = new List<int>();
  132.                                 tempOddCol = processingMatrix[row].ToList();
  133.                                 if (tempOddCol[processCol] % 2 != 0)
  134.                                 {
  135.                                     tempOddCol.Remove(tempOddCol[processCol]);
  136.                                     processingMatrix[row] = tempOddCol.ToArray();
  137.                                 }
  138.                             }
  139.                             else if (currOperation[1] == "positive")
  140.                             {
  141.                                 List<int> tempPosCol = new List<int>();
  142.                                 tempPosCol = processingMatrix[row].ToList();
  143.                                 if (tempPosCol[processCol] > 0)
  144.                                 {
  145.                                     tempPosCol.Remove(tempPosCol[processCol]);
  146.                                     processingMatrix[row] = tempPosCol.ToArray();
  147.                                 }                                
  148.                             }
  149.                             else if (currOperation[1] == "negative")
  150.                             {
  151.                                 List<int> tempNegCol = new List<int>();
  152.                                 tempNegCol = processingMatrix[row].ToList();
  153.                                 if (tempNegCol[processCol] < 0)
  154.                                 {
  155.                                     tempNegCol.Remove(tempNegCol[processCol]);
  156.                                     processingMatrix[row] = tempNegCol.ToArray();
  157.                                 }
  158.                             }
  159.                         }
  160.                     }
  161.                     break;
  162.             }
  163.             return processingMatrix;
  164.         }
  165.  
  166.         private static int[][] ReadMatrix(int rows, int[][] matrix)
  167.         {
  168.             int[][] readMatrix = new int[rows][];
  169.             for (int irow = 0; irow < rows; irow++)
  170.             {
  171.                 readMatrix[irow] = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  172.             }
  173.             return readMatrix;
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement