Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. unsigned int convert(char *hodnota)//funkce, ktera pretypuje N (vstup char a vystup unsigned int
  7. {
  8. unsigned int cislo=0,preteceni=0;
  9. unsigned int i=0;
  10. while(hodnota[i]!='\0')
  11.   {
  12.   if (hodnota[i]<='0' && hodnota[i]>'9') //osetreni, ze N je cislo (osetreni i pro to, ze cilo je kladne)
  13.     {
  14.     return 0;
  15.     }
  16.   hodnota[i]=hodnota[i]-'0';    //zaskani presne hodnoty cisla z nejake ascii hodnoty
  17.  
  18.   preteceni=cislo;
  19.   cislo=cislo*10;
  20.   cislo=cislo+hodnota[i]; //pokud uz proslo pripocte k 10tinasobku predchoziho cila prave nastene cislo
  21.   i++;
  22.   if(preteceni>cislo)   //doslo k preteceni - jednicka vypadla ven a jede se od nuly   
  23.     {
  24.     return 0;       //kdyz vrati nulu - doslo k chybe (zde z duvodu preteceni)
  25.     }
  26.   }  
  27. return cislo;
  28. }
  29.  
  30.  
  31. /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  32. /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  33. /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  34. /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  35. /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  36. /*------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  37.  
  38.  
  39. int komprimace(unsigned int N)
  40. {
  41. printf("Komprimace %d \n",N);
  42. char *buffer1 = malloc((N+1) * sizeof(char));//definice bufferù a jejich dynamické alokování
  43. char *buffer2 = malloc((N+1) * sizeof(char));
  44.  
  45. if(buffer1==NULL)
  46. {
  47. return 1;
  48. }
  49.  
  50. if (buffer2==NULL)
  51. {
  52. return 1;
  53. }
  54. unsigned int i=0; //vlozeni pomocnych promenych (pocet je promena, do ktere se bude vkladat pocet shodujicich se blokù znakù)
  55. char c=0;
  56.  
  57. while((c = getchar()) != EOF && i!=(2*N))
  58.   {
  59.     if(i < N)               //prvotní nacteni do bufferu ze vstupu
  60.       {
  61.       buffer1[i%N] = c;
  62.       i++;
  63.       }
  64.       else if(i < 2 * N)
  65.         {
  66.         buffer2[i%N] = c;
  67.         i++;
  68.         }
  69.   }
  70. putchar(buffer1[0]);putchar(buffer1[1]);putchar(buffer1[2]);putchar(buffer2[0]);putchar(buffer2[1]);putchar(buffer2[2]);
  71. printf("\nnad timto radkem je prvotni nactenido bufferu\n");
  72.   while((c = getchar()) != EOF)             //vytisknuti prvniho znaku a posunuti znaku o jeden a nacteni dalsiho znaku
  73.     {
  74.     unsigned int j=0;
  75.     putchar(buffer1[j%N]);
  76.         printf("\n");
  77.     while(j<(N-1))
  78.       {
  79.       buffer1[j%N]=buffer1[(j+1)%N];
  80.       j++;
  81.       }  
  82.  
  83.         while(j==(N-1))
  84.       {
  85.       buffer1[j%N]=buffer2[(j+1)%N];     // PRESUNUTI PRVNIHO ZNAKU Z BUFFERU2 NA POSLEDNI ZNAK BUFFERU 1
  86.             j++;      
  87.       }
  88.  
  89.     while ((j>(N-1)) && (j<((2*N)-1)))
  90.       {
  91.       buffer2[j%N]=buffer2[(j+1)%N];
  92.       j++;
  93.       }
  94.         c=getchar();
  95.         j++;    
  96.         c=buffer2[j%N];
  97.         putchar (c);
  98.         printf("\n");
  99.     putchar(buffer1[0]);putchar(buffer1[1]);putchar(buffer1[2]);putchar(buffer2[0]);putchar(buffer2[1]);putchar(buffer2[2]);
  100.         printf("\nnad timto radkem je posunuti o jeden\n");
  101.     }  
  102.  
  103.   unsigned int k=0, l=0;
  104.   while(k < N)
  105.       {
  106.       putchar (buffer1[l%N]);
  107.       k++;
  108.       l++;
  109.       printf ("K je : %d",k);
  110.       while((k>=N) && k < (2 * (N-1)))
  111.         {
  112.         putchar (buffer2[l%N]);
  113.         k++;
  114.     l++;
  115.         }
  116.       }
  117.  
  118. free(buffer1);
  119. free(buffer2);
  120. return 0;
  121. }
  122.  
  123.  
  124. int main(int argc, char *argv[])
  125. {
  126. unsigned int N=(convert(argv[1]));
  127. komprimace(N);
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement