Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ask2.c
- *
- * Created on: 15 Ιουλ 2014
- * Author: Ζαρκάδας
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- struct pos_node {
- int x, y;
- struct pos_node *next;
- };
- typedef struct pos_node pos_node;
- struct file_node {
- char filename[30];
- struct file_node *next;
- pos_node *coordinates;
- };
- typedef struct file_node file_node;
- struct word_node {
- char *word;
- struct word_node *left, *right;
- file_node *location;
- };
- typedef struct word_node word_node, *word_nodeptr;
- FILE *f, *cur;
- char FileName[30];
- word_nodeptr HashTable[1031];
- int Table256[20];
- int HashFunction(char *word) {
- int n, i, sum;
- n = strlen(word);
- sum = 0;
- for (i = 0; i <= n - 1; i++)
- sum = sum + (((Table256[i] % 1031) * (word[i] % 1031))) % 1031;
- sum=sum%1031;
- return sum;
- }
- int ischar(char c) {
- if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
- return 1;
- return 0;
- }
- void InsertFile(file_node *head, int x, int y) {
- file_node *p,*k;
- pos_node *last,*new;
- for(p=head;p->next!=NULL;p=p->next);
- if (p->filename == FileName) {
- for (last = p->coordinates; last->next!= NULL; last = last->next)
- ;
- new = malloc(sizeof(pos_node));
- new->x = x;
- new->y = y;
- new->next = NULL;
- last->next=new;
- } else {
- k = malloc(sizeof(file_node));
- strcpy(k->filename,FileName);
- k->next = NULL;
- new = k->coordinates;
- new = malloc(sizeof(pos_node));
- new->x = x;
- new->y = y;
- new->next = NULL;
- }
- }
- void InsertWord(word_nodeptr head, char *mold, int x, int y) {
- int k;
- word_nodeptr p;
- if (head == NULL) {
- p = malloc(sizeof(word_node));
- p->word = malloc(sizeof(strlen(mold) + 1));
- strcpy(mold, p->word);
- head = p;
- p->left = p->right = NULL;
- p->location = malloc(sizeof(file_node));
- strcpy(p->location->filename,FileName);
- p->location->coordinates=malloc(sizeof(pos_node));
- p->location->coordinates->x = x;
- p->location->coordinates->y = y;
- p->location->next=NULL;
- p->location->coordinates->next=NULL;
- } else {
- k = strcmp(head->word, mold);
- if (k == 0) {
- InsertFile(head->location, x, y);
- }
- if (k == 1)
- InsertWord(head->left, mold, x, y);
- if (k == -1)
- InsertWord(head->right, mold, x, y);
- }
- }
- void NewWord(char *mold, int x, int y) {
- word_node *p;
- int Hash;
- Hash = HashFunction(mold);
- if (HashTable[Hash] == NULL) {
- p = malloc(sizeof(word_node));
- p->word = malloc(sizeof(strlen(mold) + 1));
- strcpy(p->word,mold);
- HashTable[Hash] = p;
- p->left = p->right = NULL;
- p->location = malloc(sizeof(file_node));
- p->location->coordinates=malloc(sizeof(pos_node));
- p->location->coordinates->x = x;
- p->location->coordinates->y = y;
- p->location->next=NULL;
- strcpy(p->location->filename,FileName);
- p->location->coordinates->next=NULL;
- } else
- InsertWord(HashTable[Hash], mold, x, y);
- }
- void TextInput() {
- char mold[20], c;
- int x, y, i,x0,y0;
- x = y = 1;
- while (!feof(cur)) {
- c=fgetc(cur);
- if (ischar(c)) {
- i = 0;
- x0=x;
- y0=y;
- for (; ischar(c) == 1; c = fgetc(cur)) {
- mold[i] = c;
- i++;
- }
- mold[i] = '\0';
- NewWord(mold, x0, y0);
- x=x+i;
- }
- if (c == '\n') {
- x = 1;
- y++;
- } else if (c==' ')
- x++;
- }
- }
- int create256(int i) {
- int res;
- if (i == 0)
- return (1);
- else {
- res = (256 * (create256(i - 1))) % 1031;
- return (res);
- }
- }
- word_nodeptr find(word_nodeptr head, char *word){
- int k;
- if (head==NULL) return NULL;
- else {
- k=strcmp(head->word,word);
- if(k==0) return head;
- else if (k==1) return find(head->left,word);
- else return find(head->right,word);
- }
- }
- void test(){
- char word[20];
- int Hash;
- word_node *p;
- file_node *q;
- pos_node *k;
- scanf("%s",word);
- Hash=HashFunction(word);
- if (HashTable[Hash]==NULL) printf("NOT FOUND");
- else {
- p=find(HashTable[Hash],word);
- for(q=p->location;q!=NULL;q=q->next)
- for(k=q->coordinates;k!=NULL;k=k->next)
- printf("%s (%d,%d)\n",q->filename,k->x,k->y);
- }
- }
- int main() {
- int i;
- word_node *p;
- file_node *q;
- pos_node *k;
- for (i = 0; i <= 19; i++)
- Table256[i] = create256(i);
- for (i = 0; i <= 1030; i++)
- HashTable[i] = NULL;
- f = fopen("input.txt", "r");
- while (fscanf(f, "%s\n", FileName) != EOF) {
- printf("%s\n",FileName);
- cur = fopen(FileName, "r");
- TextInput();
- fclose(cur);
- }
- fclose(f);
- /*for(i=0;i<=1030;i++)
- if (HashTable[i]!=NULL){
- printf("%s\n",HashTable[i]->word);
- for (q=HashTable[i]->location;q!=NULL;q=q->next)
- for(k=q->coordinates;k!=NULL;k=k->next)
- printf("%s %d %d\n",q->filename,k->x,k->y);
- }*/
- test();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement