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; //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]);
- printf("\nnad timto radkem je prvotni nactenido bufferu\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]);
- printf("\n");
- while(j<(N-1))
- {
- buffer1[j%N]=buffer1[(j+1)%N];
- j++;
- }
- while(j==(N-1))
- {
- buffer1[j%N]=buffer2[(j+1)%N]; // PRESUNUTI PRVNIHO ZNAKU Z BUFFERU2 NA POSLEDNI ZNAK BUFFERU 1
- j++;
- }
- while ((j>(N-1)) && (j<((2*N)-1)))
- {
- buffer2[j%N]=buffer2[(j+1)%N];
- j++;
- }
- c=getchar();
- j++;
- c=buffer2[j%N];
- putchar (c);
- printf("\n");
- putchar(buffer1[0]);putchar(buffer1[1]);putchar(buffer1[2]);putchar(buffer2[0]);putchar(buffer2[1]);putchar(buffer2[2]);
- printf("\nnad timto radkem je posunuti o jeden\n");
- }
- unsigned int k=0, l=0;
- while(k < N)
- {
- putchar (buffer1[l%N]);
- k++;
- l++;
- printf ("K je : %d",k);
- while((k>=N) && k < (2 * (N-1)))
- {
- putchar (buffer2[l%N]);
- k++;
- l++;
- }
- }
- free(buffer1);
- free(buffer2);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- unsigned int N=(convert(argv[1]));
- komprimace(N);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement