Code:
//Hashtable Dictionary
//Hashes the words into the table position, then inserts them in the LL at that location
//After mistake found - insert into q and put the line it was found on in linequeue
//print out incorrect words and the times it appears in the file
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<malloc.h>
#define WORDSIZE 32
#define TABLESIZE 2000
#define HASH_VALUE 13
#define LINESIZE 520
#define CHECKFILE "photoshop_primer.txt"
#define TRUE 1
#define FALSE 0
int tablePosition;
char delim[]=" .,!?()\";:";
char line[LINESIZE]={'a','s','i','a','n'};
int lineCounter;
struct LIST table[TABLESIZE]; //table
//global methods
int hash(char string[]);
void checkMistakes();
/*typedef struct NODE Node;
typedef struct LIST List;
typedef struct QNODE QNode;
typedef struct QUEUE Queue;
*/
struct NODE
{
char word[WORDSIZE];
struct NODE *next;
};
struct LIST
{
struct NODE listTop;
};
struct LINEQUEUE
{
struct NODE lineTop;
struct NODE lineBottom;
};
struct QNODE
{
char word[WORDSIZE];
struct LINEQUEUE;
struct QNODE *next;
};
struct QUEUE
{
struct QNODE queueTop;
struct QNODE queueBottom;
}queue;
void enter(char word[])
{
/*get the word,
search for it
int found=FALSE;
struct QNODE current;
struct QNODE qnode;
current = queue.queueTop;
while(current.word[0]!='\0' && FALSE==found)
{
printf("%s",node->word);
//if(iter->code == key) return iter;
if(strcmp(current.word,string)==0)
found=TRUE;
else
current=current.next;
}
if not found
if(found!=TRUE)
create a new instance of it with blank array of lines
put into QNODE
strcpy(qnode.word,string);
queueBottom.next=&QNODE;
qnode.lines={0};
lines[lineCounter]=TRUE;
else
current is the same word
current.lines[lineCounter]=TRUE;
*/
}
void printQueue()
{
/*
int i;
start at beginning and print each word with lines
struct QNODE current=queue.queueTop;
while(!at the bottom)
printf("%s",current.word);
printf("%s","On Lines");
print the lines the word is mispelled on
*/
}
int hash(char string[])
{
int wordValue=0; //value of word
int hashValue; //position in table
int i=0;
char c=string[i];
//loops through the word and uses horners method
//for the polynomial function
while( (c!='\n') && (c!='\0')&& (i<WORDSIZE) )
{
wordValue = wordValue*HASH_VALUE+string[i];
i++;
c=string[i];
}
//printf("%s %i\n",string,wordValue);
//mods so the word will be in the table
hashValue = abs(wordValue%TABLESIZE);
return hashValue;
}
void insert(char string[])
{
int hashValue=0;
//printf("%i",hashValue);
//struct NODE *node;
struct NODE *newTop;
struct NODE *oldTop;
hashValue=hash(string); //gets the space in the array to insert word
/........canf(oldstring, "%30s", &newstring);*/
if(hashValue==188)
printf("%s",string);
//-----------------------------------------------
oldTop=(struct NODE*)malloc(sizeof(struct NODE));
newTop=(struct NODE*)malloc(sizeof(struct NODE));
if(oldTop==NULL && newTop==NULL)
{
oldTop=&(table[hashValue].listTop);
strcpy(newTop->word,string);
newTop->next=oldTop;
table[hashValue].listTop=*newTop; //puts the node at the top of list
}
else
perror("Out Of Memory");
//-----------------------------------------------*/
}
void checkMistakes()
{
char *token;
char word[WORDSIZE];
char lowerWord[WORDSIZE];
int hashValue;
int i=0;
int found=FALSE;
struct NODE *node;
lineCounter++; //increment the line currently reading
token=strtok(line,delim);
while(token!=NULL)
{
strcpy(word,token);
for(i=0;i<strlen(word) && word[i]!='\0';i++)
{
lowerWord[i]=(char)tolower(word[i]);
}
//puts a null character at the end of the word so array knows where word ends
lowerWord[i]='\0';
hashValue=hash(lowerWord);
printf("%i %s\n",hashValue,word);
node=&(table[hashValue].listTop);
if(table[hashValue].listTop.word[0]!='\0')
{
/*NODE struct book *iter;
for(iter=head; iter!=NULL; iter=iter->next)
*/
while(node->word[0]!='\0' && FALSE==found)
{
printf("%s",node->word);
//if(iter->code == key) return iter;
if(strcmp(node->word,lowerWord)==0)
found=TRUE;
else
if(node->next!=NULL && node->next->word[0]!='\0')
node=node->next;
else
break;
}
//search for the word in the list
/* while(node->word[0]!='\0' && found!=TRUE)
{
if(strcmp(node->word,word)==0)
//then found
found=TRUE;
else
//increment
node = &(node->next);
}
*/
//printf("%s %s\n",word,lowerWord);
}
if(found==TRUE)
{
printf("%s %s\n",word,lowerWord);
// enter(word);
}
token=strtok(NULL,delim);
}
}
int main(int argc, char *argv[])
{
int i;
int arg;
char string[WORDSIZE];
FILE* word;
FILE* check;
{
void insert();
void printQueue();
}
lineCounter=0;
arg=getopt(argc,argv,"d:");
if(-1!=arg)
word = fopen(optarg,"r");
else
printf("OOPS");
//sets the first character in each list to NULL to check for empty lists
for(i=0;i<TABLESIZE;i++)
table[i].listTop.word[0]='\0';
while(fgets(string,WORDSIZE,word))
{
//printf("%s",string);
insert(string);
}
checkMistakes();
/*
check= fopen(CHECKFILE,"r");
while(NULL!=fgets(line,LINESIZE,check))
{
checkMistakes();
}
*/
fclose(check);
fclose(word);
printQueue();
return EXIT_SUCCESS;
}
sorry about all the commenting out, but as I try things I comment out so I dont lose any code until I found the best.