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 BWT
- {
- class Program
- {
- static void Main(string[] args)
- {
- Transform();
- Console.Read();
- }
- //Effettua la trasformata
- private static void Transform()
- {
- char[] word;
- Console.WriteLine("Inserisci la parola. In caso vengano inserite lettere maiuscole,\nverranno trasformate in minuscole.");
- word = Console.ReadLine().ToCharArray();
- int n = word.Length, I = 0;
- ToLower(word, n);
- char[,] m = new char[n, n];
- char[,] m1 = new char[n, n];
- char[] L = new char[n];
- Fill(word, m, n);
- Console.WriteLine("Permutazioni della parola:");
- ShowM(m, n);
- Check(m, n);
- Console.WriteLine("Permutazioni in ordine lessicografico:");
- ShowM(m, n);
- I = FindI(word, m, n);
- L = FindL(word, m, n);
- ShowIandL(I, L);
- AntiTransform(m, m1, n, I, L);
- }
- //Se presenti, trasforma le lettere maiuscole in minuscole
- private static void ToLower(char[] word, int n)
- {
- for (int i = 0; i < n; i++)
- {
- word[i] = Char.ToLower(word[i]);
- }
- }
- //Effettua le permutazioni della parola e riempie la matrice m
- private static void Fill(char[] word, char[,] m, int n)
- {
- int index = n - 1;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- m[i, j] = word[index];
- if (index == n - 1)
- {
- index = 0;
- }
- else
- {
- index++;
- }
- }
- index--;
- }
- Console.WriteLine();
- }
- //Mostra i valori della matrice in console
- private static void ShowM(char[,] m, int n)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- Console.Write(m[i, j]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- //Trova i valori da ordinare e richiama Sort per ordinarli
- private static void Check(char[,] m, int n)
- {
- int j;
- for (int i = 0; i < n; i++)
- {
- for (j = i + 1; j < n; j++)
- {
- //Se le righe iniziano con la stessa lettera
- if (m[i, 0].CompareTo(m[j, 0]) == 0)
- {
- Check2(m, n, i, j);
- }
- //Se, alfabeticamente, la lettera nella colonna i si trova dopo la lettera nella colonna j
- else if (m[i, 0].CompareTo(m[j, 0]) > 0)
- {
- Sort(i, j, m, n);
- }
- }
- }
- }
- //Trova i valori da ordinare che contengono una o più lettere uguali nella stessa posizione e chiama Sort per ordinarli
- private static void Check2(char[,] m, int n, int i, int j)
- {
- for (int checkCounter = 1; checkCounter < n; checkCounter++)
- {
- //Se, alfabeticamente, la lettera nella colonna i si trova dopo la lettera nella colonna j
- if (m[i, checkCounter].CompareTo(m[j, checkCounter]) > 0)
- {
- Sort(i, j, m, n);
- checkCounter = n;
- }
- //Se, alfabeticamente, la lettera nella colonna i si trova prima della lettera nella colonna j
- else if (m[i, checkCounter].CompareTo(m[j, checkCounter]) < 0)
- {
- checkCounter = n;
- }
- }
- }
- //Ordina le righe scelte
- private static void Sort(int k, int j, char[,] m, int n)
- {
- char[] tempM = new char[n];
- for (int i = 0; i < n; i++)
- {
- tempM[i] = m[k, i];
- m[k, i] = m[j, i];
- m[j, i] = tempM[i];
- }
- }
- //Trova il valore di I
- private static int FindI(char[] word, char[,] m, int n)
- {
- int counter = 0;
- int I = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (m[i, j].CompareTo(word[j]) == 0)
- {
- counter++;
- }
- else
- {
- j = n;
- }
- //Se tutte le lettere coincidono con quelle della parola
- if (counter == n - 1)
- {
- I = i;
- i = n;
- j = n;
- }
- }
- counter = 0;
- }
- return I;
- }
- //Trova i valori di L
- private static char[] FindL(char[] word, char[,] m, int n)
- {
- char[] L = new char[n];
- for (int i = 0; i < n; i++)
- {
- L[i] = m[i, n - 1];
- }
- return L;
- }
- //Mostra i valori di I ed L in console
- private static void ShowIandL(int I, char[] L)
- {
- Console.WriteLine("I: " + I);
- Console.Write("L: ");
- foreach (char c in L)
- {
- Console.Write(c);
- }
- Console.Write("\n\n");
- }
- //Effettua l'antitrasformata
- private static void AntiTransform(char[,] m, char[,] m1, int n, int I, char[] L)
- {
- int counter = 0;
- int[] T = new int[n];
- char[] word = new char[n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- //La nuova parola da prendere come esempio è la riga i della matrice m
- word[j] = m[i, j];
- }
- ReFill(word, m1, n, counter);
- T[i] = FindT(m1, m, n, counter);
- counter++;
- }
- Console.WriteLine("I valori della matrice m1 sono:");
- ShowM(m1, n);
- Decode(L, T, n, I);
- }
- //Riempie la matrice m1 con i valori modificati della matrice m
- private static void ReFill(char[] word, char[,] m1, int n, int counter)
- {
- int index = n - 1;
- for (int j = 0; j < n; j++)
- {
- m1[counter, j] = word[index];
- if (index != n - 1)
- {
- index++;
- }
- else
- {
- index = 0;
- }
- }
- }
- //Trova i valori di T
- private static int FindT(char[,] m1, char[,] m, int n, int count)
- {
- int T = 0;
- int counter = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (m1[count, j].CompareTo(m[i, j]) == 0)
- {
- counter++;
- }
- else
- {
- j = n;
- }
- //Se tutte le lettere coincidono con quelle della parola
- if (counter == n - 1)
- {
- T = i;
- i = n;
- j = n;
- }
- }
- }
- return T;
- }
- //Trova la parola usando L e T e la mostra in console
- private static void Decode(char[] L, int[] T, int n, int I)
- {
- char[] S = new char[n];
- for (int i = n - 1; i > -1; i--)
- {
- S[i] = L[I];
- I = T[I];
- }
- foreach (char c in S)
- {
- Console.Write(c);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement