Advertisement
Guest User

Cprogram

a guest
Jul 30th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.63 KB | None | 0 0
  1. /*
  2.  * ask2.c
  3.  *
  4.  *  Created on: 15 Ιουλ 2014
  5.  *      Author: Ζαρκάδας
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. struct pos_node {
  13.     int x, y;
  14.     struct pos_node *next;
  15. };
  16. typedef struct pos_node pos_node;
  17.  
  18. struct file_node {
  19.     char filename[30];
  20.     struct file_node *next;
  21.     pos_node *coordinates;
  22. };
  23. typedef struct file_node file_node;
  24.  
  25. struct word_node {
  26.     char *word;
  27.     struct word_node *left, *right;
  28.     file_node *location;
  29. };
  30. typedef struct word_node word_node, *word_nodeptr;
  31.  
  32. FILE *f, *cur;
  33. char FileName[30];
  34. word_nodeptr HashTable[1031];
  35. int Table256[20];
  36.  
  37. int HashFunction(char *word) {
  38.     int n, i, sum;
  39.  
  40.     n = strlen(word);
  41.     sum = 0;
  42.     for (i = 0; i <= n - 1; i++)
  43.         sum = sum + (((Table256[i] % 1031) * (word[i] % 1031))) % 1031;
  44.     sum=sum%1031;
  45.     return sum;
  46. }
  47.  
  48. int ischar(char c) {
  49.     if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  50.         return 1;
  51.     return 0;
  52. }
  53.  
  54. void InsertFile(file_node *head, int x, int y) {
  55.     file_node *p,*k;
  56.     pos_node *last,*new;
  57.  
  58.     for(p=head;p->next!=NULL;p=p->next);
  59.     if (p->filename == FileName) {
  60.         for (last = p->coordinates; last->next!= NULL; last = last->next)
  61.             ;
  62.         new = malloc(sizeof(pos_node));
  63.         new->x = x;
  64.         new->y = y;
  65.         new->next = NULL;
  66.         last->next=new;
  67.     } else {
  68.         k = malloc(sizeof(file_node));
  69.         strcpy(k->filename,FileName);
  70.         k->next = NULL;
  71.         new = k->coordinates;
  72.         new = malloc(sizeof(pos_node));
  73.         new->x = x;
  74.         new->y = y;
  75.         new->next = NULL;
  76.  
  77.     }
  78. }
  79.  
  80. void InsertWord(word_nodeptr head, char *mold, int x, int y) {
  81.     int k;
  82.     word_nodeptr p;
  83.     if (head == NULL) {
  84.         p = malloc(sizeof(word_node));
  85.         p->word = malloc(sizeof(strlen(mold) + 1));
  86.         strcpy(mold, p->word);
  87.         head = p;
  88.         p->left = p->right = NULL;
  89.         p->location = malloc(sizeof(file_node));
  90.         strcpy(p->location->filename,FileName);
  91.         p->location->coordinates=malloc(sizeof(pos_node));
  92.         p->location->coordinates->x = x;
  93.         p->location->coordinates->y = y;
  94.         p->location->next=NULL;
  95.         p->location->coordinates->next=NULL;
  96.  
  97.     } else {
  98.  
  99.         k = strcmp(head->word, mold);
  100.         if (k == 0) {
  101.             InsertFile(head->location, x, y);
  102.         }
  103.         if (k == 1)
  104.             InsertWord(head->left, mold, x, y);
  105.         if (k == -1)
  106.             InsertWord(head->right, mold, x, y);
  107.     }
  108. }
  109.  
  110. void NewWord(char *mold, int x, int y) {
  111.     word_node *p;
  112.     int Hash;
  113.  
  114.     Hash = HashFunction(mold);
  115.  
  116.  
  117.     if (HashTable[Hash] == NULL) {
  118.         p = malloc(sizeof(word_node));
  119.         p->word = malloc(sizeof(strlen(mold) + 1));
  120.         strcpy(p->word,mold);
  121.         HashTable[Hash] = p;
  122.         p->left = p->right = NULL;
  123.         p->location = malloc(sizeof(file_node));
  124.         p->location->coordinates=malloc(sizeof(pos_node));
  125.         p->location->coordinates->x = x;
  126.         p->location->coordinates->y = y;
  127.         p->location->next=NULL;
  128.         strcpy(p->location->filename,FileName);
  129.         p->location->coordinates->next=NULL;
  130.     } else
  131.         InsertWord(HashTable[Hash], mold, x, y);
  132. }
  133.  
  134. void TextInput() {
  135.     char mold[20], c;
  136.     int x, y, i,x0,y0;
  137.  
  138.     x = y = 1;
  139.     while (!feof(cur)) {
  140.         c=fgetc(cur);
  141.         if (ischar(c)) {
  142.             i = 0;
  143.             x0=x;
  144.             y0=y;
  145.             for (; ischar(c) == 1; c = fgetc(cur)) {
  146.                 mold[i] = c;
  147.                 i++;
  148.             }
  149.             mold[i] = '\0';
  150.             NewWord(mold, x0, y0);
  151.             x=x+i;
  152.         }
  153.         if (c == '\n') {
  154.             x = 1;
  155.             y++;
  156.         } else if (c==' ')
  157.             x++;
  158.  
  159.     }
  160. }
  161.  
  162. int create256(int i) {
  163.     int res;
  164.     if (i == 0)
  165.         return (1);
  166.     else {
  167.         res = (256 * (create256(i - 1))) % 1031;
  168.         return (res);
  169.     }
  170. }
  171.  
  172. word_nodeptr find(word_nodeptr head, char *word){
  173.     int k;
  174.  
  175.     if (head==NULL) return NULL;
  176.     else {
  177.         k=strcmp(head->word,word);
  178.         if(k==0) return head;
  179.         else if (k==1) return find(head->left,word);
  180.         else return find(head->right,word);
  181.     }
  182. }
  183.  
  184. void test(){
  185.     char word[20];
  186.     int Hash;
  187.     word_node *p;
  188.     file_node *q;
  189.     pos_node *k;
  190.  
  191.     scanf("%s",word);
  192.     Hash=HashFunction(word);
  193.     if (HashTable[Hash]==NULL) printf("NOT FOUND");
  194.     else {
  195.         p=find(HashTable[Hash],word);
  196.         for(q=p->location;q!=NULL;q=q->next)
  197.             for(k=q->coordinates;k!=NULL;k=k->next)
  198.                 printf("%s (%d,%d)\n",q->filename,k->x,k->y);
  199.     }
  200. }
  201.  
  202. int main() {
  203.     int i;
  204.     word_node *p;
  205.     file_node *q;
  206.     pos_node *k;
  207.  
  208.  
  209.     for (i = 0; i <= 19; i++)
  210.         Table256[i] = create256(i);
  211.     for (i = 0; i <= 1030; i++)
  212.         HashTable[i] = NULL;
  213.     f = fopen("input.txt", "r");
  214.  
  215.     while (fscanf(f, "%s\n", FileName) != EOF) {
  216.         printf("%s\n",FileName);
  217.         cur = fopen(FileName, "r");
  218.         TextInput();
  219.         fclose(cur);
  220.     }
  221.     fclose(f);
  222.  
  223.     /*for(i=0;i<=1030;i++)
  224.         if (HashTable[i]!=NULL){
  225.             printf("%s\n",HashTable[i]->word);
  226.             for (q=HashTable[i]->location;q!=NULL;q=q->next)
  227.                 for(k=q->coordinates;k!=NULL;k=k->next)
  228.                     printf("%s %d %d\n",q->filename,k->x,k->y);
  229.         }*/
  230.  
  231.     test();
  232.  
  233.  
  234.  
  235.     return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement