Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. int main()
  5. {
  6.     char symbol;
  7.     char preInput;
  8.     int count = 0;
  9.     int j = 0;
  10.  
  11.     int symbolsCount = 0;
  12.  
  13.     int codedSymbols = 0;
  14.  
  15.     while (scanf(" %c", &symbol) != EOF)
  16.     {
  17.         if (symbol < 'A' || symbol > 'Z')
  18.         {
  19.             if (count < 3)
  20.             {
  21.                 for (int i = 0; i < count; i++)
  22.                 {
  23.                     printf("%c", preInput);
  24.                 }
  25.                 printf("\n");
  26.             }
  27.             else if (count > 2 && count < 256)
  28.             {
  29.                 printf("%c%d", preInput, count);
  30.                 printf("\n");
  31.             }
  32.             else
  33.             {
  34.                 int series = count / 255;
  35.                 for (int i = 0; i < series; i++)
  36.                 {
  37.                     printf("%c%d", preInput, 255);
  38.                 }
  39.  
  40.                 int trash = count - (series * 255);
  41.  
  42.                 if (trash < 3)
  43.                 {
  44.                     for (int i = 0; i < trash; i++)
  45.                     {
  46.                         printf("%c", preInput);
  47.                     }
  48.                 }
  49.                 else
  50.                 {
  51.                     printf("%c%d", preInput, trash);
  52.                 }
  53.                 printf("\n");
  54.             }
  55.             fprintf(stderr, "Error: Neplatny symbol!\n");
  56.             return 100;
  57.         }
  58.  
  59.         if (j == 0)
  60.         {
  61.             preInput = symbol;
  62.             j++;
  63.             count++;
  64.             symbolsCount++;
  65.             continue;
  66.         }
  67.  
  68.         if (preInput != symbol)
  69.         {
  70.             if (count < 3)
  71.             {
  72.                 for (int i = 0; i < count; i++)
  73.                 {
  74.                     printf("%c", preInput);
  75.                     codedSymbols++;
  76.                 }
  77.             }
  78.             else if (count > 2 && count < 256)
  79.             {
  80.                 printf("%c%d", preInput, count);
  81.                 if (count < 10)
  82.                 {
  83.                     codedSymbols += 2;
  84.                 }
  85.                 else if (count > 9 && count < 100)
  86.                 {
  87.                     codedSymbols += 3;
  88.                 }
  89.                 else
  90.                 {
  91.                     codedSymbols += 4;
  92.                 }
  93.             }
  94.             else
  95.             {
  96.                 int series = count / 255;
  97.                 for (int i = 0; i < series; i++)
  98.                 {
  99.                     printf("%c%d", preInput, 255);
  100.                     codedSymbols += 4;
  101.                 }
  102.                 int trash = count / 255;
  103.                 if (trash < 3)
  104.                 {
  105.                     for (int i = 0; i < trash; i++)
  106.                     {
  107.                         printf("%c", preInput);
  108.                         codedSymbols++;
  109.                     }
  110.                 }
  111.                 else
  112.                 {
  113.                     printf("%c%d", preInput, trash);
  114.                     if (trash < 10)
  115.                     {
  116.                         codedSymbols += 2;
  117.                     }
  118.                     else if (trash > 9 && trash < 100)
  119.                     {
  120.                         codedSymbols += 3;
  121.                     }
  122.                     else
  123.                     {
  124.                         codedSymbols += 4;
  125.                     }
  126.                 }
  127.             }
  128.  
  129.             preInput = symbol;
  130.             count = 1;
  131.             symbolsCount++;
  132.         }
  133.         else
  134.         {
  135.             count++;
  136.             symbolsCount++;
  137.         }
  138.     }
  139.  
  140.     if (count < 3)
  141.     {
  142.         for (int i = 0; i < count; i++)
  143.         {
  144.             printf("%c", symbol);
  145.             codedSymbols++;
  146.         }
  147.     }
  148.     else if (count > 2 && count < 256)
  149.     {
  150.         printf("%c%d", symbol, count);
  151.         if (count < 10)
  152.         {
  153.             codedSymbols += 2;
  154.         }
  155.         else if (count > 9 && count < 100)
  156.         {
  157.             codedSymbols += 3;
  158.         }
  159.         else
  160.         {
  161.             codedSymbols += 4;
  162.         }
  163.     }
  164.     else
  165.     {
  166.         int series = count / 255;
  167.         for (int i = 0; i < series; i++)
  168.         {
  169.             printf("%c%d", symbol, 255);
  170.             codedSymbols += 4;
  171.         }
  172.  
  173.         int trash = count - (series * 255);
  174.  
  175.         if (trash < 3)
  176.         {
  177.             for (int i = 0; i < trash; i++)
  178.             {
  179.                 printf("%c", symbol);
  180.                 codedSymbols++;
  181.             }
  182.         }
  183.         else
  184.         {
  185.             printf("%c%d", symbol, trash);
  186.             if (trash < 10)
  187.             {
  188.                 codedSymbols += 2;
  189.             }
  190.             else if (trash > 9 && trash < 100)
  191.             {
  192.                 codedSymbols += 3;
  193.             }
  194.             else
  195.             {
  196.                 codedSymbols += 4;
  197.             }
  198.         }
  199.     }
  200.  
  201.     printf("\n");
  202.  
  203.     double comprese = (double)codedSymbols / (double)symbolsCount;
  204.     fprintf(stderr, "Pocet vstupnich symbolu: %d\n", symbolsCount);
  205.     fprintf(stderr, "Pocet zakodovanych symbolu: %d\n", codedSymbols);
  206.     fprintf(stderr, "Kompresni pomer: %.2f\n", comprese);
  207.  
  208.     return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement