Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Lab3
  9. {
  10.     class Program
  11.     {
  12.         public static void Main(string[] args)
  13.         {
  14.             List<int> compressed = Compress(Console.ReadLine());
  15.             Console.WriteLine(string.Join(", ", compressed));
  16.             string decompressed = Decompress(compressed);
  17.             Console.WriteLine(decompressed);
  18.             var file1 = File.Open("test1.txt", FileMode.Open);
  19.             var file2 = File.Open("test2.txt", FileMode.Open);
  20.             Console.WriteLine($"Size of compressed file: {file1.Length} byte");
  21.             Console.WriteLine($"Size of decompressed file: {file2.Length} byte");
  22.             file1.Dispose();
  23.             file2.Dispose();
  24.             Console.ReadKey();
  25.         }
  26.  
  27.         public static List<int> Compress(string uncompressed)
  28.         {
  29.             Dictionary<string, int> dictionary = new Dictionary<string, int>();
  30.             for (int i = 0; i < 256; i++)
  31.                 dictionary.Add(((char)i).ToString(), i);
  32.  
  33.             string w = string.Empty;
  34.             List<int> compressed = new List<int>();
  35.  
  36.             foreach (char c in uncompressed)
  37.             {
  38.                 string wc = w + c;
  39.                 if (dictionary.ContainsKey(wc))
  40.                 {
  41.                     w = wc;
  42.                 }
  43.                 else
  44.                 {
  45.                     compressed.Add(dictionary[w]);
  46.                     dictionary.Add(wc, dictionary.Count);
  47.                     w = c.ToString();
  48.                 }
  49.             }
  50.  
  51.             if (!string.IsNullOrEmpty(w))
  52.                 compressed.Add(dictionary[w]);
  53.             var array = new StringBuilder();
  54.             foreach(var el in compressed)
  55.             {
  56.                 array.Append($"{el.ToString()} ");
  57.             }            
  58.             File.WriteAllText("test1.txt", array.ToString());
  59.             return compressed;
  60.         }
  61.  
  62.         public static string Decompress(List<int> compressed)
  63.         {
  64.             Dictionary<int, string> dictionary = new Dictionary<int, string>();
  65.             for (int i = 0; i < 256; i++)
  66.                 dictionary.Add(i, ((char)i).ToString());
  67.  
  68.             string w = dictionary[compressed[0]];
  69.             compressed.RemoveAt(0);
  70.             StringBuilder decompressed = new StringBuilder(w);
  71.  
  72.             foreach (int k in compressed)
  73.             {
  74.                 string entry = null;
  75.                 if (dictionary.ContainsKey(k))
  76.                     entry = dictionary[k];
  77.                 else if (k == dictionary.Count)
  78.                     entry = w + w[0];
  79.  
  80.                 decompressed.Append(entry);
  81.  
  82.                 dictionary.Add(dictionary.Count, w + entry[0]);
  83.  
  84.                 w = entry;
  85.             }
  86.             File.WriteAllText("test2.txt", decompressed.ToString());
  87.             return decompressed.ToString();
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement