Advertisement
TheGoldsmith

BWT 2

Oct 28th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.63 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 BWT
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Transform();          
  14.             Console.Read();
  15.         }
  16.  
  17.         //Effettua la trasformata
  18.         private static void Transform()
  19.         {
  20.             char[] word;
  21.  
  22.             Console.WriteLine("Inserisci la parola. In caso vengano inserite lettere maiuscole,\nverranno trasformate in minuscole.");
  23.  
  24.             word = Console.ReadLine().ToCharArray();
  25.  
  26.             int n = word.Length, I = 0;
  27.  
  28.             ToLower(word, n);
  29.  
  30.             char[,] m = new char[n, n];
  31.             char[,] m1 = new char[n, n];
  32.  
  33.             char[] L = new char[n];
  34.  
  35.             Fill(word, m, n);
  36.  
  37.             Console.WriteLine("Permutazioni della parola:");
  38.             ShowM(m, n);
  39.  
  40.             Check(m, n);
  41.  
  42.             Console.WriteLine("Permutazioni in ordine lessicografico:");
  43.             ShowM(m, n);
  44.  
  45.             I = FindI(word, m, n);
  46.             L = FindL(word, m, n);
  47.  
  48.             ShowIandL(I, L);
  49.  
  50.             AntiTransform(m, m1, n, I, L);
  51.         }
  52.  
  53.         //Se presenti, trasforma le lettere maiuscole in minuscole
  54.         private static void ToLower(char[] word, int n)
  55.         {
  56.             for (int i = 0; i < n; i++)
  57.             {
  58.                 word[i] = Char.ToLower(word[i]);
  59.             }
  60.         }
  61.        
  62.         //Effettua le permutazioni della parola e riempie la matrice m
  63.         private static void Fill(char[] word, char[,] m, int n)
  64.         {  
  65.             int index = n - 1;
  66.  
  67.             for (int i = 0; i < n; i++)
  68.             {
  69.                 for (int j = 0; j < n; j++)
  70.                 {
  71.                     m[i, j] = word[index];
  72.  
  73.                     if (index == n - 1)
  74.                     {
  75.                         index = 0;
  76.                     }
  77.                     else
  78.                     {
  79.                         index++;
  80.                     }
  81.                 }
  82.                 index--;
  83.             }
  84.             Console.WriteLine();
  85.         }
  86.  
  87.         //Mostra i valori della matrice in console
  88.         private static void ShowM(char[,] m, int n)
  89.         {
  90.             for (int i = 0; i < n; i++)
  91.             {
  92.                 for (int j = 0; j < n; j++)
  93.                 {
  94.                     Console.Write(m[i, j]);
  95.                 }
  96.                 Console.WriteLine();
  97.             }
  98.             Console.WriteLine();
  99.         }
  100.  
  101.         //Trova i valori da ordinare e richiama Sort per ordinarli
  102.         private static void Check(char[,] m, int n)
  103.         {
  104.             int j;
  105.  
  106.             for (int i = 0; i < n; i++)
  107.             {
  108.                 for (j = i + 1; j < n; j++)
  109.                 {
  110.                     //Se le righe iniziano con la stessa lettera
  111.                     if (m[i, 0].CompareTo(m[j, 0]) == 0)
  112.                     {
  113.                         Check2(m, n, i, j);                      
  114.                     }
  115.                     //Se, alfabeticamente, la lettera nella colonna i si trova dopo la lettera nella colonna j
  116.                     else if (m[i, 0].CompareTo(m[j, 0]) > 0)
  117.                     {
  118.                         Sort(i, j, m, n);
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.  
  124.         //Trova i valori da ordinare che contengono una o più lettere uguali nella stessa posizione e chiama Sort per ordinarli
  125.         private static void Check2(char[,] m, int n, int i, int j)
  126.         {
  127.             for (int checkCounter = 1; checkCounter < n; checkCounter++)
  128.             {
  129.                 //Se, alfabeticamente, la lettera nella colonna i si trova dopo la lettera nella colonna j
  130.                 if (m[i, checkCounter].CompareTo(m[j, checkCounter]) > 0)
  131.                 {
  132.                     Sort(i, j, m, n);
  133.                     checkCounter = n;
  134.                 }
  135.                 //Se, alfabeticamente, la lettera nella colonna i si trova prima della lettera nella colonna j
  136.                 else if (m[i, checkCounter].CompareTo(m[j, checkCounter]) < 0)
  137.                 {
  138.                     checkCounter = n;
  139.                 }
  140.             }
  141.         }
  142.  
  143.         //Ordina le righe scelte
  144.         private static void Sort(int k, int j, char[,] m, int n)
  145.         {
  146.             char[] tempM = new char[n];
  147.  
  148.             for (int i = 0; i < n; i++)
  149.             {
  150.                 tempM[i] = m[k, i];
  151.                 m[k, i] = m[j, i];
  152.                 m[j, i] = tempM[i];
  153.             }
  154.         }
  155.        
  156.         //Trova il valore di I
  157.         private static int FindI(char[] word, char[,] m, int n)
  158.         {
  159.             int counter = 0;
  160.             int I = 0;
  161.  
  162.             for (int i = 0; i < n; i++)
  163.             {
  164.                 for (int j = 0; j < n; j++)
  165.                 {
  166.                     if (m[i, j].CompareTo(word[j]) == 0)
  167.                     {
  168.                         counter++;
  169.                     }
  170.                     else
  171.                     {
  172.                         j = n;
  173.                     }
  174.                     //Se tutte le lettere coincidono con quelle della parola
  175.                     if (counter == n - 1)
  176.                     {
  177.                         I = i;
  178.                         i = n;
  179.                         j = n;
  180.                     }
  181.                 }
  182.                 counter = 0;
  183.             }
  184.             return I;
  185.         }
  186.  
  187.         //Trova i valori di L
  188.         private static char[] FindL(char[] word, char[,] m, int n)
  189.         {
  190.             char[] L = new char[n];
  191.  
  192.             for (int i = 0; i < n; i++)
  193.             {
  194.                 L[i] = m[i, n - 1];
  195.             }
  196.  
  197.             return L;
  198.         }
  199.  
  200.         //Mostra i valori di I ed L in console
  201.         private static void ShowIandL(int I, char[] L)
  202.         {
  203.             Console.WriteLine("I: " + I);
  204.  
  205.             Console.Write("L: ");
  206.  
  207.             foreach (char c in L)
  208.             {
  209.                 Console.Write(c);
  210.             }
  211.  
  212.             Console.Write("\n\n");
  213.         }
  214.  
  215.         //Effettua l'antitrasformata
  216.         private static void AntiTransform(char[,] m, char[,] m1, int n, int I, char[] L)
  217.         {
  218.             int counter = 0;
  219.             int[] T = new int[n];
  220.  
  221.             char[] word = new char[n];
  222.  
  223.             for (int i = 0; i < n; i++)
  224.             {
  225.                 for (int j = 0; j < n; j++)
  226.                 {
  227.                     //La nuova parola da prendere come esempio è la riga i della matrice m
  228.                     word[j] = m[i, j];
  229.                 }
  230.  
  231.                 ReFill(word, m1, n, counter);
  232.                 T[i] = FindT(m1, m, n, counter);
  233.  
  234.                 counter++;
  235.             }
  236.  
  237.             Console.WriteLine("I valori della matrice m1 sono:");
  238.             ShowM(m1, n);
  239.  
  240.             Decode(L, T, n, I);
  241.         }
  242.  
  243.         //Riempie la matrice m1 con i valori modificati della matrice m
  244.         private static void ReFill(char[] word, char[,] m1, int n, int counter)
  245.         {
  246.             int index = n - 1;
  247.  
  248.             for (int j = 0; j < n; j++)
  249.             {
  250.                 m1[counter, j] = word[index];
  251.  
  252.                 if (index != n - 1)
  253.                 {
  254.                     index++;
  255.                 }
  256.                 else
  257.                 {
  258.                     index = 0;
  259.                 }
  260.             }
  261.         }
  262.  
  263.         //Trova i valori di T
  264.         private static int FindT(char[,] m1, char[,] m, int n, int count)
  265.         {
  266.             int T = 0;
  267.             int counter = 0;
  268.  
  269.             for (int i = 0; i < n; i++)
  270.             {
  271.                 for (int j = 0; j < n; j++)
  272.                 {
  273.                     if (m1[count, j].CompareTo(m[i, j]) == 0)
  274.                     {
  275.                         counter++;
  276.                     }
  277.                     else
  278.                     {
  279.                         j = n;
  280.                     }
  281.                     //Se tutte le lettere coincidono con quelle della parola
  282.                     if (counter == n - 1)
  283.                     {
  284.                         T = i;
  285.                         i = n;
  286.                         j = n;
  287.                     }
  288.                 }
  289.             }
  290.             return T;
  291.         }
  292.  
  293.         //Trova la parola usando L e T e la mostra in console
  294.         private static void Decode(char[] L, int[] T, int n, int I)
  295.         {
  296.             char[] S = new char[n];
  297.  
  298.             for (int i = n - 1; i > -1; i--)
  299.             {
  300.                 S[i] = L[I];
  301.                 I = T[I];
  302.             }
  303.             foreach (char c in S)
  304.             {
  305.                 Console.Write(c);
  306.             }
  307.         }
  308.     }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement