Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- unsigned int convert(char *hodnota)//funkce, ktera pretypuje N (vstup char a vystup unsigned int
- {
- unsigned int cislo=0,preteceni=0;
- unsigned int i=0;
- while(hodnota[i]!='\0')
- {
- if (hodnota[i]<'0' && hodnota[i]>'9') //osetreni, ze N je cislo (osetreni i pro to, ze cilo je kladne)
- {
- return 0;
- }
- hodnota[i]=hodnota[i]-'0'; //zaskani presne hodnoty cisla z nejake ascii hodnoty
- preteceni=cislo;
- cislo=cislo*10;
- cislo=cislo+hodnota[i]; //pokud uz proslo pripocte k 10tinasobku predchoziho cila prave nastene cislo
- i++;
- if(preteceni>cislo) //doslo k preteceni - jednicka vypadla ven a jede se od nuly
- {
- return 0; //kdyz vrati nulu - doslo k chybe (zde z duvodu preteceni)
- }
- }
- return cislo;
- }
- /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- int komprimace(unsigned int N)
- {
- printf("Komprimace %d \n",N);
- char *buffer1 = malloc((N+1) * sizeof(char));//definice bufferù a jejich dynamické alokování
- char *buffer2 = malloc((N+1) * sizeof(char));
- if(buffer1==NULL)
- {
- return 1;
- }
- if (buffer2==NULL)
- {
- return 1;
- }
- unsigned int i=0,pocet=1; //vlozeni pomocnych promenych (pocet je promena, do ktere se bude vkladat pocet shodujicich se blokù znakù)
- char c=0;
- while((c = getchar()) != EOF && i!=(2*N))
- {
- if(i < N) //prvotní nacteni do bufferu ze vstupu
- {
- buffer1[i%N] = c;
- i++;
- }
- else if(i < 2 * N)
- {
- buffer2[i%N] = c;
- i++;
- }
- }
- /*putchar(buffer1[0]);putchar(buffer1[1]);putchar(buffer1[2]);putchar(buffer2[0]);putchar(buffer2[1]);putchar(buffer2[2]);*/
- while(strcmp(buffer1, buffer2) == 0) //porovvnani buffer (jestli se bloky neopakuji
- {
- pocet++; //pokud se tovnaji naèti do bufferu 2 dalsi blok znakù a znovu skoc na provnavani
- printf("rovnaji se %d \n",N);
- unsigned int ochrana=0;
- while((c=getchar())!= EOF && ochrana!=N)
- {
- ochrana++;
- i++;
- c=getchar();
- buffer2[i%N]=c;
- }
- }
- printf("jsem tu %d \n",N);
- putchar(pocet);
- putchar(buffer1[i%N]);
- while((c = getchar()) != EOF) //vytisknuti prvniho znaku a posunuti znaku o jeden a nacteni dalsiho znaku
- {
- unsigned int j=0;
- putchar(buffer1[j%N]);
- while(j<N)
- {
- buffer1[j%N]=buffer1[(j+1)%N];
- j++;
- }
- while(j==N)
- {
- buffer1[(j-1)%N]=buffer2[j%N]; // PRESUNUTI PRVNIHO ZNAKU Z BUFFERU2 NA POSLEDNI ZNAK BUFFERU 1
- j++;
- }
- while (j>N&&j<(2*N))
- {
- buffer2[j%N]=buffer2[(j+1)%N];
- j++;
- }
- c=buffer2[(j-1)%N];
- while(strcmp(buffer1, buffer2) == 0)
- {
- pocet++;
- unsigned int ochrana=0;
- while((c=getchar())!= EOF && ochrana<N)
- {
- ochrana++;
- i++;
- c=getchar();
- buffer2[i%N]=c;
- }
- }
- putchar(pocet);
- putchar(buffer1[i%N]);
- }
- unsigned int k=0;
- while(k<(2*N))
- {
- if(k < N)
- {
- putchar (buffer1[i%N]);
- k++;
- i--;
- }
- else if(k < (2 * N))
- {
- putchar (buffer2[i%N]);
- k++;
- }
- }
- free(buffer1);
- free(buffer2);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- unsigned int N=(convert(argv[1]));
- komprimace(N);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement