Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.32 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,pocet=1; //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.  
  71. /*putchar(buffer1[0]);putchar(buffer1[1]);putchar(buffer1[2]);putchar(buffer2[0]);putchar(buffer2[1]);putchar(buffer2[2]);*/
  72.  
  73.  
  74. while(strcmp(buffer1, buffer2) == 0)        //porovvnani buffer (jestli se bloky neopakuji
  75.   {
  76.   pocet++;                                  //pokud se tovnaji naèti do bufferu 2 dalsi blok znakù a znovu skoc na provnavani
  77.   printf("rovnaji se %d \n",N);
  78.   unsigned int ochrana=0;
  79.   while((c=getchar())!= EOF && ochrana!=N)
  80.     {
  81.     ochrana++;
  82.     i++;
  83.     c=getchar();
  84.     buffer2[i%N]=c;
  85.     }
  86.   }
  87.   printf("jsem tu %d \n",N);
  88.   putchar(pocet);
  89.   putchar(buffer1[i%N]);
  90.  
  91.   while((c = getchar()) != EOF)             //vytisknuti prvniho znaku a posunuti znaku o jeden a nacteni dalsiho znaku
  92.     {
  93.     unsigned int j=0;
  94.     putchar(buffer1[j%N]);
  95.     while(j<N)
  96.       {
  97.       buffer1[j%N]=buffer1[(j+1)%N];
  98.       j++;
  99.       }
  100.     while(j==N)
  101.       {
  102.        buffer1[(j-1)%N]=buffer2[j%N];     // PRESUNUTI PRVNIHO ZNAKU Z BUFFERU2 NA POSLEDNI ZNAK BUFFERU 1
  103.        j++;
  104.       }
  105.     while (j>N&&j<(2*N))
  106.       {
  107.       buffer2[j%N]=buffer2[(j+1)%N];
  108.       j++;
  109.       }
  110.     c=buffer2[(j-1)%N];
  111.       while(strcmp(buffer1, buffer2) == 0)
  112.         {
  113.         pocet++;
  114.         unsigned int ochrana=0;
  115.         while((c=getchar())!= EOF && ochrana<N)
  116.           {
  117.           ochrana++;
  118.           i++;
  119.           c=getchar();
  120.           buffer2[i%N]=c;
  121.           }
  122.         }
  123.   putchar(pocet);
  124.   putchar(buffer1[i%N]);
  125.  
  126.   }
  127. unsigned int k=0;
  128. while(k<(2*N))
  129.   {
  130.     if(k < N)
  131.       {
  132.       putchar (buffer1[i%N]);
  133.       k++;
  134.       i--;
  135.       }
  136.       else if(k < (2 * N))
  137.         {
  138.         putchar (buffer2[i%N]);
  139.         k++;
  140.         }
  141.   }
  142.  
  143. free(buffer1);
  144. free(buffer2);
  145. return 0;
  146. }
  147.  
  148.  
  149. int main(int argc, char *argv[])
  150. {
  151. unsigned int N=(convert(argv[1]));
  152. komprimace(N);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement