Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. //#include <thread>
  4. //#define _CRT_SECURE_NO_WARNINGS
  5. #define POCETZNAKU 62
  6. #define SOUBOR "random1.dat"
  7. //const char* SOUBOR = "random1.dat";
  8. //FILE *fp;
  9.  
  10.  
  11. int pocetVlaken = 1000;         /// TODO argument pri spusteni programu
  12.  
  13. char poleZnaku[POCETZNAKU] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  14. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  15. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
  16. void vytvorSoubor(int);
  17. int FileScan();
  18. void producenti(int, char*);
  19.  
  20. int main(int argc, char* argv[]) {
  21.  
  22.     //pocetVlaken = argv[1];
  23.    
  24.     vytvorSoubor(1000000);
  25.    
  26.     int velikostSouboru = FileScan();
  27.  
  28.     int velikostBloku = velikostSouboru / pocetVlaken;;
  29.     int posledniBlok = velikostSouboru % pocetVlaken;       // pricist k poslednimu vlaknu
  30.  
  31.     FILE *file;
  32.     file = fopen(SOUBOR, "r");
  33.     rewind(file);
  34.     int i;
  35.     for (i = 0; i < pocetVlaken; i++){
  36.         if (i == pocetVlaken)       //posledni vlakno
  37.         {
  38.             char *p = malloc(velikostBloku + posledniBlok);//new char[velikostBloku + posledniBlok];
  39.             for (int b = 0; b < velikostBloku + posledniBlok; b++)
  40.             {
  41.                 p[b] = fgetc(file);
  42.             }
  43.             producenti(velikostBloku  + posledniBlok, p);
  44.             // predani pole p vlaknu
  45.         }
  46.         else
  47.         {
  48.             char *p = malloc(velikostBloku);//[velikostBloku];
  49.             for (int b = 0; b < velikostBloku; b++)
  50.             {
  51.                 p[b] = fgetc(file);
  52.             }
  53.             printf("asdasdad %d", velikostBloku);
  54.             getchar();
  55.             producenti(velikostBloku, p);
  56.             // predani pole p vlaknu
  57.         }
  58.        
  59.        
  60.        
  61.     }
  62.     fclose(file);
  63.  
  64.  
  65.     //// vlakna
  66.     /*pthread_attr_t attr;
  67.  
  68.     pthread_attr_init(&attr);
  69.     pthread_attr_setdetachstate(
  70.         &attr, PTHREAD_CREATE_DETACHED);
  71.     pthread_create(NULL, &attr, &function, NULL);*/
  72.    
  73.  
  74.  
  75.  
  76.     return EXIT_SUCCESS;
  77. }
  78.  
  79. void vytvorSoubor(int velikost)
  80. {
  81.     FILE *file;
  82.     file = fopen(SOUBOR, "w");
  83.  
  84.     int i, j;
  85.    
  86.     for (i = 0; i<velikost; i++)
  87.     {
  88.         j = (int)rand() % POCETZNAKU;
  89.         putc(poleZnaku[j], file);               // pole Znaku = predem definovane znaky
  90.     }
  91.     printf("Vytvoren soubor");
  92.     fclose(file);
  93. }
  94.  
  95. int FileScan()
  96. {
  97.     FILE *file;
  98.     file = fopen(SOUBOR, "r");
  99.     char c;
  100.     int pocetZnaku = 0;
  101.     while (!feof(file))
  102.     {
  103.         c = fgetc(file);
  104.         pocetZnaku = pocetZnaku + 1;
  105.     }
  106.     printf("pocet znaku v souboru %d", pocetZnaku-1);
  107.     fclose(file);
  108.     return pocetZnaku-1;
  109. }
  110.  
  111. void producenti(int blok, char *p)
  112. {
  113.     //FILE *f;
  114.     //file = fopen(SOUBOR, "r");
  115.     int *cetnosti = calloc(POCETZNAKU, sizeof(int));        // alokace pole velikosti POCETZNAKU * INT a vyplni hodnoty na 0
  116.     for (int i = 0; i < blok; i++)
  117.     {
  118.         printf("%c %d\n", p[i], i);
  119.         for (int j = 0; j < POCETZNAKU; j++)
  120.         {
  121.             if (poleZnaku[j] == p[i])
  122.             {
  123.                 cetnosti[j] += 1;
  124.             }
  125.         }
  126.     }
  127.  
  128.     getchar();
  129.     for (int i = 0; i < POCETZNAKU; i++)        // tisk vysledku
  130.     {
  131.         printf("cetnosti %c %d \n", poleZnaku[i], cetnosti[i]);
  132.     }
  133. }
  134.  
  135. void* konzumer()
  136. {
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement