Advertisement
kiruha98

Untitled

Apr 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #define MAXWORD 100
  5. #define NKEYS 11
  6.  
  7.  
  8. struct key {
  9. char *word;
  10. int count;
  11. int score;
  12. } mas[] = {
  13. "auto", 0,1,
  14. "break", 0,1,
  15. "case", 0,1,
  16. "char", 0,1,
  17. "const", 0,1,
  18. "continue", 0,1,
  19. "default", 0,1,
  20. "unsigned", 0,1,
  21. "void", 0,1,
  22. "volatile", 0,1,
  23. "while", 0,1,
  24. };
  25.  
  26. struct key arr[1000];
  27.  
  28.  
  29. int gethash(char *slovo, int line);
  30.  
  31.  
  32.  
  33. int getword(char *, int);
  34. int binsearch(char *, struct key *, int);
  35. /* подсчет ключевых слов Си */
  36. int main()
  37. {
  38.     int x=0;
  39.     int hash[10];
  40.    int j;
  41.    int help;
  42.     int huint;  
  43.     int temp;
  44.        
  45.      for (int z=0; z<=1000; z++)
  46.    {
  47.       arr[z].count=0;
  48.       arr[z].score=0;
  49.          
  50.          /*printf("%d",arr[z].count);*/
  51.     }
  52.    
  53.     for(x=0; x<=10; x++)
  54.     {
  55.         hash[x]=0;
  56.         j=0;
  57.        
  58.         huint=strlen(mas[x].word);
  59.        
  60.         while(huint > 0)
  61.         {
  62.             help=mas[x].word[j];
  63.            
  64.             hash[x]= hash[x] + help;
  65.                 j++;
  66.             huint--;
  67.         }
  68.        
  69.         temp=hash[x];
  70.  
  71.       arr[temp].score=1;
  72.         arr[temp].word=mas[x].word;
  73.        
  74. printf ("%d\n", hash[x]);
  75.     }
  76.    
  77.    
  78.    
  79.      
  80.  
  81.     int line;
  82. char slovo[MAXWORD];
  83. int p;
  84.  
  85. while(getword(slovo, MAXWORD) != -1)
  86. {
  87.     line=strlen(slovo);
  88.  
  89.    
  90.     printf("%s\t", slovo);
  91.    
  92.     printf("%d\n", gethash(slovo,line));
  93.    
  94.   p=gethash(slovo,line);
  95.  
  96.       if (p<=1000 && arr[p].score!=0)
  97.       {
  98.         arr[p].count++;
  99.       }
  100.     }
  101.    for (int z=400; z<=800; z++)
  102.    {
  103.       if (arr[z].score!=0)
  104.       {
  105.          printf("mas[%d]=%d ",z,arr[z].score);
  106.          printf("mas[%d]=%d ",z,arr[z].count);
  107.          printf("mas[%d]=%s \n",z,arr[z].word);
  108.       }
  109.        
  110.       }
  111. }
  112.  
  113.  
  114.    
  115.  
  116.    
  117.     #define BUFSIZE 100
  118. char buf[BUFSIZE]; /* буфер для ungetch */
  119. int bufp = 0; /* след, свободная позиция в буфере */
  120. int getch(void) /* взять (возможно возвращенный) символ */
  121. {
  122. return (bufp > 0) ? buf[--bufp] : getchar();
  123. }
  124. void ungetch(int c)
  125. {
  126. if (bufp >= BUFSIZE)
  127. printf ("ungetch: слишком много символов\n");
  128. else
  129. buf[bufp++] = c;
  130. }
  131.  
  132. int getword (char *word, int lim)
  133. {
  134. int c;
  135. char *w = word;
  136. while (isspace(c = getch()))
  137. ;
  138. if (c != EOF)
  139. *w++ = c;
  140. if (!isalpha(c)) {
  141. *w = '\0';
  142. return c;
  143. }
  144. for ( ; --lim > 0; w++)
  145. if (!isalnum(*w = getch())) {
  146. ungetch(*w);
  147. break;
  148. }
  149. *w = '\0';
  150. return word[0];
  151. }
  152.  
  153. int gethash(char *slovo, int line)
  154. {
  155.    
  156.     int hash;
  157.    
  158.         int j=0;
  159.        
  160.     int help;
  161.    
  162.        
  163.         while(line>0)
  164.         {
  165.             help=slovo[j];
  166.            
  167.             hash= hash+ help;
  168.                 j++;
  169.             line--;
  170.         }
  171.     return hash;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement