Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #define MAXWORD 100
- #define NKEYS 11
- struct key {
- char *word;
- int count;
- int score;
- } mas[] = {
- "auto", 0,1,
- "break", 0,1,
- "case", 0,1,
- "char", 0,1,
- "const", 0,1,
- "continue", 0,1,
- "default", 0,1,
- "unsigned", 0,1,
- "void", 0,1,
- "volatile", 0,1,
- "while", 0,1,
- };
- struct key arr[1000];
- int gethash(char *slovo, int line);
- int getword(char *, int);
- int binsearch(char *, struct key *, int);
- /* подсчет ключевых слов Си */
- int main()
- {
- int x=0;
- int hash[10];
- int j;
- int help;
- int huint;
- int temp;
- for (int z=0; z<=1000; z++)
- {
- arr[z].count=0;
- arr[z].score=0;
- /*printf("%d",arr[z].count);*/
- }
- for(x=0; x<=10; x++)
- {
- hash[x]=0;
- j=0;
- huint=strlen(mas[x].word);
- while(huint > 0)
- {
- help=mas[x].word[j];
- hash[x]= hash[x] + help;
- j++;
- huint--;
- }
- temp=hash[x];
- arr[temp].score=1;
- arr[temp].word=mas[x].word;
- printf ("%d\n", hash[x]);
- }
- int line;
- char slovo[MAXWORD];
- int p;
- while(getword(slovo, MAXWORD) != -1)
- {
- line=strlen(slovo);
- printf("%s\t", slovo);
- printf("%d\n", gethash(slovo,line));
- p=gethash(slovo,line);
- if (p<=1000 && arr[p].score!=0)
- {
- arr[p].count++;
- }
- }
- for (int z=400; z<=800; z++)
- {
- if (arr[z].score!=0)
- {
- printf("mas[%d]=%d ",z,arr[z].score);
- printf("mas[%d]=%d ",z,arr[z].count);
- printf("mas[%d]=%s \n",z,arr[z].word);
- }
- }
- }
- #define BUFSIZE 100
- char buf[BUFSIZE]; /* буфер для ungetch */
- int bufp = 0; /* след, свободная позиция в буфере */
- int getch(void) /* взять (возможно возвращенный) символ */
- {
- return (bufp > 0) ? buf[--bufp] : getchar();
- }
- void ungetch(int c)
- {
- if (bufp >= BUFSIZE)
- printf ("ungetch: слишком много символов\n");
- else
- buf[bufp++] = c;
- }
- int getword (char *word, int lim)
- {
- int c;
- char *w = word;
- while (isspace(c = getch()))
- ;
- if (c != EOF)
- *w++ = c;
- if (!isalpha(c)) {
- *w = '\0';
- return c;
- }
- for ( ; --lim > 0; w++)
- if (!isalnum(*w = getch())) {
- ungetch(*w);
- break;
- }
- *w = '\0';
- return word[0];
- }
- int gethash(char *slovo, int line)
- {
- int hash;
- int j=0;
- int help;
- while(line>0)
- {
- help=slovo[j];
- hash= hash+ help;
- j++;
- line--;
- }
- return hash;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement