Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- int main()
- {
- char symbol;
- char preInput;
- int count = 0;
- int j = 0;
- int symbolsCount = 0;
- int codedSymbols = 0;
- while (scanf(" %c", &symbol) != EOF)
- {
- if (symbol < 'A' || symbol > 'Z')
- {
- if (count < 3)
- {
- for (int i = 0; i < count; i++)
- {
- printf("%c", preInput);
- }
- printf("\n");
- }
- else if (count > 2 && count < 256)
- {
- printf("%c%d", preInput, count);
- printf("\n");
- }
- else
- {
- int series = count / 255;
- for (int i = 0; i < series; i++)
- {
- printf("%c%d", preInput, 255);
- }
- int trash = count - (series * 255);
- if (trash < 3)
- {
- for (int i = 0; i < trash; i++)
- {
- printf("%c", preInput);
- }
- }
- else
- {
- printf("%c%d", preInput, trash);
- }
- printf("\n");
- }
- fprintf(stderr, "Error: Neplatny symbol!\n");
- return 100;
- }
- if (j == 0)
- {
- preInput = symbol;
- j++;
- count++;
- symbolsCount++;
- continue;
- }
- if (preInput != symbol)
- {
- if (count < 3)
- {
- for (int i = 0; i < count; i++)
- {
- printf("%c", preInput);
- codedSymbols++;
- }
- }
- else if (count > 2 && count < 256)
- {
- printf("%c%d", preInput, count);
- if (count < 10)
- {
- codedSymbols += 2;
- }
- else if (count > 9 && count < 100)
- {
- codedSymbols += 3;
- }
- else
- {
- codedSymbols += 4;
- }
- }
- else
- {
- int series = count / 255;
- for (int i = 0; i < series; i++)
- {
- printf("%c%d", preInput, 255);
- codedSymbols += 4;
- }
- int trash = count / 255;
- if (trash < 3)
- {
- for (int i = 0; i < trash; i++)
- {
- printf("%c", preInput);
- codedSymbols++;
- }
- }
- else
- {
- printf("%c%d", preInput, trash);
- if (trash < 10)
- {
- codedSymbols += 2;
- }
- else if (trash > 9 && trash < 100)
- {
- codedSymbols += 3;
- }
- else
- {
- codedSymbols += 4;
- }
- }
- }
- preInput = symbol;
- count = 1;
- symbolsCount++;
- }
- else
- {
- count++;
- symbolsCount++;
- }
- }
- if (count < 3)
- {
- for (int i = 0; i < count; i++)
- {
- printf("%c", symbol);
- codedSymbols++;
- }
- }
- else if (count > 2 && count < 256)
- {
- printf("%c%d", symbol, count);
- if (count < 10)
- {
- codedSymbols += 2;
- }
- else if (count > 9 && count < 100)
- {
- codedSymbols += 3;
- }
- else
- {
- codedSymbols += 4;
- }
- }
- else
- {
- int series = count / 255;
- for (int i = 0; i < series; i++)
- {
- printf("%c%d", symbol, 255);
- codedSymbols += 4;
- }
- int trash = count - (series * 255);
- if (trash < 3)
- {
- for (int i = 0; i < trash; i++)
- {
- printf("%c", symbol);
- codedSymbols++;
- }
- }
- else
- {
- printf("%c%d", symbol, trash);
- if (trash < 10)
- {
- codedSymbols += 2;
- }
- else if (trash > 9 && trash < 100)
- {
- codedSymbols += 3;
- }
- else
- {
- codedSymbols += 4;
- }
- }
- }
- printf("\n");
- double comprese = (double)codedSymbols / (double)symbolsCount;
- fprintf(stderr, "Pocet vstupnich symbolu: %d\n", symbolsCount);
- fprintf(stderr, "Pocet zakodovanych symbolu: %d\n", codedSymbols);
- fprintf(stderr, "Kompresni pomer: %.2f\n", comprese);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement