Advertisement
melnikovmaxim

BKV_2

Dec 16th, 2019
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #counts the number of words, source text write in count.txt file, has a bug
  2. #link https://yadi.sk/d/tJGgiAghdsp5WQ
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <malloc.h>
  9. #include <sys/time.h>
  10. #include <time.h>
  11.   struct timeval t;
  12. typedef struct word {
  13.     int count;
  14.     char *Word;
  15. } WORD;
  16.  
  17.  
  18. int cmpword_alpha(WORD *, WORD *);
  19. int cmpword_quant(WORD *, WORD *);
  20.  
  21. WORD *words[100000];
  22. char g[10000][100000],*gg[100000];
  23. int c = 0,b=0,u=1, kk=0, qqq=0;
  24. int totalw = 0;
  25. char *gw;
  26. char *getword(FILE *);
  27.  
  28. int main(int ac, char *av[])
  29. {
  30.     long int T1,T2,T3,T4,T5,T6;
  31.     gettimeofday(&t,NULL);
  32.     T1=t.tv_sec;
  33.     T2=t.tv_usec;
  34.     FILE *OUT1, *OUT2, *IN;
  35.     char *pword;
  36.     totalw = 0;
  37.     int k = 0, l=0, i=0;
  38.     OUT1 = fopen("qWord.txt","w");
  39.     OUT2 = fopen("qcount.txt", "w");
  40.     struct word *word = (struct word*)malloc(sizeof(struct word));
  41.         IN = fopen("count.txt", "r");
  42.         while ((pword = getword(IN))!=NULL)
  43.         {
  44.             totalw++;
  45.            
  46.  
  47.                 for (i=1;i<l;i++)
  48.                 if (strcmp(pword,word[i].Word)==0)
  49.                 {
  50.                     k=1;
  51.                     word[i].count++;
  52.                 }
  53.            
  54.             if ((k == 0) && (c>1))
  55.             {
  56.                 l++;
  57.                 word[l].Word = pword;
  58.                 word[l].count=1;
  59.             }
  60.             k = 0;
  61.         }
  62.  
  63.     qsort(word, l+1, sizeof(WORD),
  64.         (int(*)(const void *, const void *))cmpword_alpha);
  65.     for (i = 1; i < l; i++){
  66.         fprintf(OUT1,"%d - %s \n", word[i].count, word[i].Word);
  67.     }
  68.    
  69.     qsort(word, l+1, sizeof(WORD),
  70.         (int(*)(const void *, const void *))cmpword_quant);
  71.     for (i = 1; i < l; i++){
  72.         fprintf(OUT2,"%d - %s \n", word[i].count, word[i].Word);
  73.     }
  74.     fclose(OUT1);
  75.     fclose(OUT2);
  76.     gettimeofday(&t,NULL);
  77.     T3=t.tv_sec;
  78.     T4=t.tv_usec;
  79.     T5=T3-T1;
  80.     T6=T4-T2;
  81.     printf("Zatracheno %ld sec, %ld microsec \n", T5,T6);
  82.     /*
  83.     for (i=0;i<l+1;i++)
  84.     {
  85.         printf("%s",word[i].Word);
  86.     }
  87.     */
  88.     return 0;
  89. }
  90. char *getword(FILE *F)
  91. {
  92.     char *s;
  93.     if (c==0)
  94.     s=NULL;
  95.     if (b==0)
  96.     while (!feof(F))
  97.     {
  98.         b++;
  99.         fgets(g[b],10000,F);
  100.     }
  101.     if (c>1)
  102.     s=strtok(0," .,");
  103.     if ((s==NULL) && (kk<b))
  104.     {
  105.         kk++;
  106.         s=strtok(g[kk]," .,");
  107.     }
  108.     c++;
  109.     return s;
  110. }
  111. int cmpword_alpha(WORD *w1, WORD *w2)
  112. {
  113.     return (strcmp(((w1)->Word),((w2)->Word)));
  114. }
  115. int cmpword_quant(WORD *w1, WORD *w2)
  116. {
  117.     return (((w1)->count) - ((w2)->count));
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement