Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace EncryptedMatrix
- {
- using System;
- using System.Text;
- public class EncryptedMatrix
- {
- public static int[] numberCode;
- public static char[,] matrix;
- public static void Main()
- {
- string messageToConvert = Console.ReadLine();
- numberCode = new int[messageToConvert.Length];
- for (int currSymbol = 0; currSymbol < messageToConvert.Length; currSymbol++)
- {
- int lastDigitOfAscii = messageToConvert[currSymbol] % 10;
- numberCode[currSymbol] = lastDigitOfAscii;
- }
- StringBuilder codeResult = new StringBuilder();
- for (int currNumber = 0; currNumber < numberCode.Length; currNumber++)
- {
- if (numberCode[currNumber] % 2 == 0)
- {
- codeResult.Append(numberCode[currNumber] * numberCode[currNumber]);
- }
- else
- {
- codeResult.Append(AddTheNeihbourDigits(currNumber));
- }
- }
- char directionOfTheDiagonal = char.Parse(Console.ReadLine());
- matrix = new char[codeResult.Length, codeResult.Length];
- int rowToSubstractFrom = directionOfTheDiagonal == '\\' ? 0 : matrix.GetLength(0) - 1;
- FillTheMatrix(codeResult, rowToSubstractFrom);
- PrintTheMatrix();
- }
- private static void PrintTheMatrix()
- {
- StringBuilder result = new StringBuilder();
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- result.Append(matrix[row, col]);
- if (PositionIsNotInTheEnd(col, 1))
- {
- result.Append(" ");
- }
- }
- if (PositionIsNotInTheEnd(row, 0))
- {
- result.AppendLine();
- }
- }
- Console.WriteLine(result.ToString());
- }
- private static bool PositionIsNotInTheEnd(int rowOrCol, int dimension)
- {
- bool toReturn = false;
- if (rowOrCol + 1 != matrix.GetLength(dimension))
- {
- toReturn = true;
- }
- return toReturn;
- }
- private static void FillTheMatrix(StringBuilder codeNumber, int rowToSubstractFrom)
- {
- int index = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- int calculatedRow = Math.Abs(rowToSubstractFrom - row);
- if (col == index)
- {
- matrix[calculatedRow, col] = codeNumber[index];
- }
- else
- {
- matrix[calculatedRow, col] = '0';
- }
- }
- index++;
- }
- }
- private static int AddTheNeihbourDigits(int index)
- {
- int result = numberCode[index];
- int leftAdd = index - 1 >= 0 ? numberCode[index - 1] : 0;
- int rightAdd = index + 1 < numberCode.Length ? numberCode[index + 1] : 0;
- result += leftAdd + rightAdd;
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement