Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#define N 1024
#define WL 16
//komvos o pios periexei tin grammi kai tin stili,
//twn opiwn vriskete i leksi alla kai ena dikti
//se komvo fdnode, auto ginete giati mia leksei mporei
//na iparxei parapanw apo 1 fores se ena arxeio
typedef struct fdnode
{
int row;
int col;
struct fdnode *next;
}
FILEDETAILS;
//komvos o pios periexei to onoma tou arxeiou,
//ena dikti pou dixnei se allo arxeio kai allon ena,
//pou deixnei se ena komvo (fdnode)
typedef struct fnode
{
char fname[128];
struct fdnode *detnext;
struct fnode *next;
}
WORDFILES;
//komvos o pios periexei 4 pedia to onoma tis lekseis 2 diktes i opoioi douleuoun
//ws diadiko dentro kai dixnoun se alles lekseis kai enas allos diktis o opios dixnei
//se komvo (wnode)
typedef struct wnode
{
char wname[WL];
struct wnode *right, *left;
struct fnode *next;
}
WORDS;
int myhash(char [WL]);
int int_pow(int, int);
void insert(WORDS **, char [WL], char [64], int, int);
int main(void)
{
WORDS *wordmat[N]; //dilwnume ena pinaka typou wnode 1024 thesewn
FILE * fp;
int wslot, ftemp = 1, row = 1, col = 1, funcuse = 0;
char fname[64], sword[WL]; //pinakas pou tha exei tin leksei p tha psaxnume
char rowcon[101];
for (int i = 0; i < N; i++)
{
wordmat[i] = NULL;
}
//vazei to onoma ston pinaka fname k anigei to arxeio
sprintf(fname, "file%d.txt", ftemp);
fp = fopen(fname, "r");
while (fp != NULL)
{
printf("Diavasma %d arxeiou\n", ftemp);
//pernei kathe grammi tou arxeiou
while (fgets(rowcon, sizeof(rowcon), fp) != NULL)
{
//emfanizei tin grammi pou pire
fputs(rowcon, stdout);
//vazei ston deikti tin prwti leksi tis grammmis (diladi mexri to 1o keno)
char *word = strtok(rowcon, " ");
//ektipwnei kathe leksi pou iparxei stin grammi mexri
//na min iparxei kamia
while (word != NULL)
{
//ektipwnei kathe leksi
printf("%s | %s (%d,%d)\n", word, fname, row, col);
wslot = myhash(word);
insert(&wordmat[wslot], word, fname, row, col);
if (wordmat[wslot] == NULL) {}
col = col + strlen(word) + 1;
//pare tin epomeni meta to keno
word = strtok(NULL, " ");
}
//auksise to temp twn grammwn
row++;
//kane 1 tin stili
col = 1;
}
//klinei to arxeio
fclose(fp);
//auksani ton metriti gia na paei sto epomeno arxeio
//vazei to onoma ston pinaka fname k anigei to arxeio
ftemp++;
row = 1;
col = 1;
sprintf(fname, "file%d.txt", ftemp);
fp = fopen(fname, "r");
puts("\n");
}
//klinei to arxeio
fclose(fp);
puts("Dwse leksi gia anazitisi: ");
fflush(stdin);
gets(sword);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
int myhash(char word[WL])
{
int slot = 0;
int wordsize = strlen(word);
for (int k = 0; k < wordsize; k++)
{
//ipologizume to slot apo tin eksiswsi pou exume san dedomeno
//kai kalume to int pow pou einai mia sinartisi gia na ipologizei
//tin dinami se akeraio pernei san wrisma tin vasi kai tin dinami
//stin opoia to ipsonume
slot = slot + ((int_pow(2, (wordsize - k)) *word[k]));
}
slot = slot % 1024;
return slot;
}
//sinartisi pou ipologizei to apotelesma tis dinamis enos arithmou
//pernontas san orisma tin vasi kai tin dinami stin opoia ipsonume ton arithmo
int int_pow(int base, int exp)
{
//kanei to apotelesma 1
int result = 1;
//oso i dinami mas einai != 0 tote
while (exp)
{
//an exp%2 einai =1 tote
//kane to result *tin vasi mas
if (exp % 2)
result *= base;
//kane to exp/2
exp /= 2;
//polaplasiase tin vasi me ton eauto tis
base *= base;
//sinexise na epanalamvaneis oso isxiei i sinthiki
}
//epestrepse to apotelesma
return result;
}
void insert(WORDS **content, char word[WL], char fname[64], int row, int col)
{
WORDS *temp;
temp = (WORDS*) malloc(sizeof(WORDS));
if (temp != NULL)
{
strcpy(temp->wname, word);
temp->right = NULL;
temp->left = NULL;
temp->next = (WORDFILES*) malloc(sizeof(WORDFILES));
strcpy(temp->next->fname, fname);
temp->next->next = NULL;
temp->next->detnext = (FILEDETAILS*) malloc(sizeof(FILEDETAILS));
temp->next->detnext->row = row;
temp->next->detnext->col = col;
temp->next->detnext->next = NULL;
if (*content == NULL)
{
*content = temp;
}
else
{
if (strcmp(word, ((*content)->wname)) < 0)
{
insert(&((*content)->left), word, fname, row, col);
}
else
{
insert(&((*content)->right), word, fname, row, col);
}
}
}
}
So what i want is to make the function insert to insert extra nodes to the existing ones if the word is already saved, meaning i want to save the files name (if its from other file) and row and col where the word was found again. I messed up the pointers i think ...