Advertisement
simonradev

EncryptedMatrix

Mar 31st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. namespace EncryptedMatrix
  2. {
  3.     using System;
  4.     using System.Text;
  5.  
  6.     public class EncryptedMatrix
  7.     {
  8.         public static int[] numberCode;
  9.         public static char[,] matrix;
  10.  
  11.         public static void Main()
  12.         {
  13.             string messageToConvert = Console.ReadLine();
  14.  
  15.             numberCode = new int[messageToConvert.Length];
  16.             for (int currSymbol = 0; currSymbol < messageToConvert.Length; currSymbol++)
  17.             {
  18.                 int lastDigitOfAscii = messageToConvert[currSymbol] % 10;
  19.  
  20.                 numberCode[currSymbol] = lastDigitOfAscii;
  21.             }
  22.  
  23.             StringBuilder codeResult = new StringBuilder();
  24.             for (int currNumber = 0; currNumber < numberCode.Length; currNumber++)
  25.             {
  26.                 if (numberCode[currNumber] % 2 == 0)
  27.                 {
  28.                     codeResult.Append(numberCode[currNumber] * numberCode[currNumber]);
  29.                 }
  30.                 else
  31.                 {
  32.                     codeResult.Append(AddTheNeihbourDigits(currNumber));
  33.                 }
  34.             }
  35.            
  36.             char directionOfTheDiagonal = char.Parse(Console.ReadLine());
  37.             matrix = new char[codeResult.Length, codeResult.Length];
  38.  
  39.             int rowToSubstractFrom = directionOfTheDiagonal == '\\' ? 0 : matrix.GetLength(0) - 1;
  40.  
  41.             FillTheMatrix(codeResult, rowToSubstractFrom);
  42.  
  43.             PrintTheMatrix();
  44.         }
  45.  
  46.         private static void PrintTheMatrix()
  47.         {
  48.             StringBuilder result = new StringBuilder();
  49.             for (int row = 0; row < matrix.GetLength(0); row++)
  50.             {
  51.                 for (int col = 0; col < matrix.GetLength(1); col++)
  52.                 {
  53.                     result.Append(matrix[row, col]);
  54.  
  55.                     if (PositionIsNotInTheEnd(col, 1))
  56.                     {
  57.                         result.Append(" ");
  58.                     }
  59.                 }
  60.  
  61.                 if (PositionIsNotInTheEnd(row, 0))
  62.                 {
  63.                     result.AppendLine();
  64.                 }
  65.             }
  66.  
  67.             Console.WriteLine(result.ToString());
  68.         }
  69.  
  70.         private static bool PositionIsNotInTheEnd(int rowOrCol, int dimension)
  71.         {
  72.             bool toReturn = false;
  73.  
  74.             if (rowOrCol + 1 != matrix.GetLength(dimension))
  75.             {
  76.                 toReturn = true;
  77.             }
  78.  
  79.             return toReturn;
  80.         }
  81.  
  82.         private static void FillTheMatrix(StringBuilder codeNumber, int rowToSubstractFrom)
  83.         {
  84.             int index = 0;
  85.             for (int row = 0; row < matrix.GetLength(0); row++)
  86.             {
  87.                 for (int col = 0; col < matrix.GetLength(1); col++)
  88.                 {
  89.                     int calculatedRow = Math.Abs(rowToSubstractFrom - row);
  90.  
  91.                     if (col == index)
  92.                     {
  93.                         matrix[calculatedRow, col] = codeNumber[index];
  94.                     }
  95.                     else
  96.                     {
  97.                         matrix[calculatedRow, col] = '0';
  98.                     }
  99.                 }
  100.  
  101.                 index++;
  102.             }
  103.         }
  104.  
  105.         private static int AddTheNeihbourDigits(int index)
  106.         {
  107.             int result = numberCode[index];
  108.  
  109.             int leftAdd = index - 1 >= 0 ? numberCode[index - 1] : 0;
  110.             int rightAdd = index + 1 < numberCode.Length ? numberCode[index + 1] : 0;
  111.  
  112.             result += leftAdd + rightAdd;
  113.  
  114.             return result;
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement