Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MatrixOp10._06._2016
- {
- class Program
- {
- static void Main(string[] args)
- {
- int rows = int.Parse(Console.ReadLine());
- int[][] matrix = new int[rows][];
- List<string> operations = new List<string>();
- matrix = ReadMatrix(rows, matrix);
- int indexOperation = 0;
- do
- {
- operations.Add(Console.ReadLine().ToLower());
- indexOperation++;
- } while (operations[indexOperation - 1] != "end");
- for (int i = 0; i < operations.Count; i++)
- {
- string[] currOperation = operations[i].Split(' ');
- switch (currOperation[0])
- {
- case "remove":
- matrix = ProcessMethod(matrix, currOperation);
- break;
- case "swap":
- matrix = SwapMethod(matrix, currOperation);
- break;
- case "insert":
- matrix = InsertMethod(matrix, currOperation);
- break;
- }
- }
- PrintMatrix(rows, matrix);
- }
- private static int[][] InsertMethod(int[][] matrix, string[] currOperation)
- {
- int[][] insertMatrix = matrix;
- int insertRow = int.Parse(currOperation[1]);
- List<int> temp = new List<int>();
- int l = insertMatrix[insertRow].Length;
- for (int i = 0; i < l; i++)
- {
- temp.Add(insertMatrix[insertRow][i]);
- }
- temp.Insert(0, int.Parse(currOperation[2]));
- insertMatrix[insertRow] = new int[temp.Count];
- insertMatrix[insertRow] = temp.ToArray();
- return insertMatrix;
- }
- private static int[][] SwapMethod(int[][] matrix, string[] currOperation)
- {
- int[][] swappedMatrix = matrix;
- int firstSwapRow = int.Parse(currOperation[1]);
- int secondSwapRow = int.Parse(currOperation[2]);
- int[] temp = new int[swappedMatrix[firstSwapRow].Length];
- temp = swappedMatrix[firstSwapRow];
- // firstSwapRow takes the secondSwapRow
- swappedMatrix[firstSwapRow] = new int[swappedMatrix[secondSwapRow].Length];
- swappedMatrix[firstSwapRow] = swappedMatrix[secondSwapRow];
- // secondSwapRow takes the firstSwapRow
- swappedMatrix[secondSwapRow] = new int[temp.Length];
- swappedMatrix[secondSwapRow] = temp;
- return swappedMatrix;
- }
- private static void PrintMatrix(int rows, int[][] matrix)
- {
- for (int printRow = 0; printRow < rows; printRow++)
- {
- Console.WriteLine(string.Join(" ", matrix[printRow]));
- }
- }
- private static int[][] ProcessMethod(int[][] matrixMethod, string[] currOperation)
- {
- int[][] processingMatrix = matrixMethod;
- switch (currOperation[2])
- {
- case "row":
- int processRow = int.Parse(currOperation[3]);
- if (currOperation[1] == "even")
- {
- processingMatrix[processRow] = processingMatrix[processRow].Where(x => x % 2 != 0).ToArray();
- }
- else if (currOperation[1] == "odd")
- {
- processingMatrix[processRow] = processingMatrix[processRow].Where(x => x % 2 == 0).ToArray();
- }
- else if (currOperation[1] == "negative")
- {
- processingMatrix[processRow] = processingMatrix[processRow].Where(x => x >= 0).ToArray();
- }
- else if (currOperation[1] == "positive")
- {
- processingMatrix[processRow] = processingMatrix[processRow].Where(x => x < 0).ToArray();
- }
- break;
- case "col":
- int processCol = int.Parse(currOperation[3]);
- for (int row = 0; row < processingMatrix.GetLength(0); row++)
- {
- if (processingMatrix[row].GetLongLength(0) >= processCol + 1)
- {
- if (currOperation[1] == "even")
- {
- List<int> tempEvenCol = new List<int>();
- tempEvenCol = processingMatrix[row].ToList();
- if (tempEvenCol[processCol] % 2 == 0)
- {
- tempEvenCol.Remove(tempEvenCol[processCol]);
- processingMatrix[row] = tempEvenCol.ToArray();
- }
- }
- else if (currOperation[1] == "odd")
- {
- List<int> tempOddCol = new List<int>();
- tempOddCol = processingMatrix[row].ToList();
- if (tempOddCol[processCol] % 2 != 0)
- {
- tempOddCol.Remove(tempOddCol[processCol]);
- processingMatrix[row] = tempOddCol.ToArray();
- }
- }
- else if (currOperation[1] == "positive")
- {
- List<int> tempPosCol = new List<int>();
- tempPosCol = processingMatrix[row].ToList();
- if (tempPosCol[processCol] > 0)
- {
- tempPosCol.Remove(tempPosCol[processCol]);
- processingMatrix[row] = tempPosCol.ToArray();
- }
- }
- else if (currOperation[1] == "negative")
- {
- List<int> tempNegCol = new List<int>();
- tempNegCol = processingMatrix[row].ToList();
- if (tempNegCol[processCol] < 0)
- {
- tempNegCol.Remove(tempNegCol[processCol]);
- processingMatrix[row] = tempNegCol.ToArray();
- }
- }
- }
- }
- break;
- }
- return processingMatrix;
- }
- private static int[][] ReadMatrix(int rows, int[][] matrix)
- {
- int[][] readMatrix = new int[rows][];
- for (int irow = 0; irow < rows; irow++)
- {
- readMatrix[irow] = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- }
- return readMatrix;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement