Mouseartiom

sem5#2

May 18th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. //v2.0: размер слова не ограничен
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define HASHSIZE 101
  6.  
  7. static struct nlist *hashtab[HASHSIZE];
  8. unsigned hash(char *s);
  9. struct nlist *lookup(char *s);
  10. struct nlist *install(char *name);
  11. void man(int);
  12. char* eatword (FILE*, FILE*);
  13.  
  14. struct nlist {
  15.     struct nlist *next;
  16.     char *name;
  17.     unsigned int qty;
  18. };
  19.  
  20. int main(int argc, char * argv[]){
  21.     if (argc != 2) man(2);
  22.     FILE *fp, *tmpFile;
  23.     tmpFile = tmpfile();
  24.     if ((fp = fopen(argv[1], "r")) == NULL) man(3);
  25.     char *temp;
  26.     int wordNumber = 0, i = 0;
  27.     struct nlist *np, *next, *current;
  28.  
  29.     while(1){
  30.         if ((temp = eatword(fp, tmpFile)) == NULL) break;
  31.         if (!(np = lookup(temp))) {install(temp); wordNumber++;}
  32.     i++; }
  33.     i = 0;
  34.     printf("\nTotal %d words. Here is the list.\n\n", wordNumber);
  35.     printf("Word                              Repeats\n");
  36.     while (i <= HASHSIZE) {
  37.         if (hashtab[i] != NULL) {
  38.             current = hashtab[i];
  39.             while (1) {
  40.                 if (strcmp(current->name, "\n") != 0)
  41.                     printf("%-30s%20d\n", current->name, current->qty);
  42.                 if ((current = current->next) == NULL) break; }
  43.             }
  44.     i++; }
  45.     fclose(fp);
  46. }
  47.  
  48. char* eatword(FILE* file, FILE* tmpFile) {
  49.     char c, *word;
  50.     int i = 0, nCount = 0;
  51.     fpos_t pos;
  52.     fgetpos(tmpFile, &pos);
  53.     while ((c = fgetc(file)) == '\n') c = fgetc(file);
  54.     while (((c >= '0') && (c <= '9')) ||
  55.           ((c >= 'A') && (c <= 'Z')) ||
  56.           ((c >= 'a') && (c <= 'z'))) {
  57.         fputc(c, tmpFile);
  58.         c = fgetc(file);
  59.         i++; }
  60.     if (c == EOF) return NULL;
  61.     fputc('\0', tmpFile);
  62.     fsetpos(tmpFile, &pos);
  63.     word = malloc(i+1);
  64.     fgets(word, i+1, tmpFile);
  65.     fsetpos(tmpFile, &pos);
  66.     return word;
  67. }
  68.  
  69. struct nlist *lookup(char *s){
  70.     struct nlist *np;
  71.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  72.         if (strcmp(s, np->name) == 0) {
  73.             (np->qty)++;
  74.             return np; }
  75.     return NULL;
  76. }
  77.  
  78. struct nlist *install(char *name){
  79.     struct nlist *np;
  80.     unsigned hashval;
  81.     np = (struct nlist *) malloc(sizeof(*np));
  82.     if (np == NULL || (np->name = strdup(name)) == NULL) return NULL;
  83.     np->qty = 1;
  84.     hashval = hash(name);
  85.     np->next = hashtab[hashval];
  86.     hashtab[hashval] = np;
  87.     return np;
  88. }
  89.  
  90. unsigned hash(char *s){
  91.     unsigned hashval;
  92.     for (hashval = 0; *s != '\0'; s++)
  93.         hashval = *s + 31 * hashval;
  94.     return hashval % HASHSIZE;
  95. }
  96.  
  97. void man(int var) {
  98.  
  99.     if (var == 0) printf("Программа предназначена для работы с файлами.\nФайл выбирается при помощи аргумента.\nПример: ./a.out file1.txt\nВызов справки -?.\n");
  100.     if (var == 2) printf("Ошибка аргуметов. Укажите файлы при помощи аргумента. -? для справки.\n");
  101.     if (var == 3) printf("Файл не найден.\n");
  102.     if (var > 0) exit(0);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment