Code:
/*--------------main------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Word.c"
#define PREV '<'
#define NEXT '>'
#define FUNC '#'
#define DEF_SORT 1
#define END '\0'
#define MIN_UP 65
#define MAX_UP 90
#define MIN_LOW 97
#define MAX_LOW 122
#define MAX_INPUT_LENGTH 2
//============================= FUNCTIONS ===================================
static Boolean isLetter(int in)
{
if((in>=MIN_LOW && in<=MAX_LOW) || (in>=MIN_UP && in<=MAX_UP))
{
return TRUE;
}
return FALSE;
}
static void append(char in,char curr[MAX_WRD_LENGTH])
{
int i;
for(i=0;curr[i]!=END;i++);
curr[i] = in;
curr[i+1] = END;
}
int main(void) {
char str[MAX_INPUT_LENGTH];
gets(str);
char curr[MAX_WRD_LENGTH],c;
curr[0]= END;
int i,sort=DEF_SORT,num_of_next=0;
letter_p root = NULL;
linkedList_p words = createNewList();
for(i=0;i<strlen(str)+1;i++)
{
c = str[i];
if(isLetter(c))
{
append(c,curr);
insert(c,root,curr);
updateScores(words,curr);
printOption(words,curr,0);
continue;
}
else if(c==NEXT)
{
num_of_next++;
printOption(words,curr,num_of_next);
continue;
}
else if (c==PREV)
{
num_of_next--;
printOption(words,curr,num_of_next);
continue;
}
else if (c==FUNC)
{
c=str[i+1];
//..............change sorting.....
i++;
continue;
}
else
{
checkExit(curr,root,words);
word_p w = createNewWord(curr,root);
addLast(words,w,sort);
curr[0] = END;
num_of_next=0;
continue;
}
}
return EXIT_SUCCESS;
}
/*-------------------Word.h-------------*/
#ifndef targ51_Word_h
#define targ51_Word_h
#ifndef WORD_H_
#define WORD_H_
#include <string.h>
#define MAX_WRD_LENGTH 100
typedef enum {FALSE, TRUE} Boolean;
typedef struct letter_t* letter_p;
typedef struct word* word_p;
typedef struct linkedList* linkedList_p;
//=============================== FUNCTIONS =======================
void checkExit(char curr[MAX_WRD_LENGTH],letter_p root, linkedList_p list);
letter_p createNewLetter(char c);
letter_p getWordRoot(char curr[MAX_WRD_LENGTH],letter_p root);
void insert(char c,letter_p root,char curr[MAX_WRD_LENGTH]);
void deleteLetters();
word_p createNewWord(char curr[MAX_WRD_LENGTH],letter_p root);
int getWordScore(char curr[MAX_WRD_LENGTH],letter_p root);
void printOption(linkedList_p words,char curr[MAX_WRD_LENGTH],int num_of_next);
void updateScores(linkedList_p words,char curr[MAX_WRD_LENGTH]);
void deleteWord();
linkedList_p createNewList();
void addLast(linkedList_p list, word_p w, int sort);
void emptyList(linkedList_p list);
#endif /* WORD_H_ */
#endif
/*-------------Word.c---------------*/
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include "Word.h"
typedef struct letter_t
{
char _letter;
int _letter_score;
letter_p _right;
letter_p _down;
}letter_t;
typedef struct word
{
char _word[MAX_WRD_LENGTH];
int _word_score;
word_p _prev;
word_p _next;
}word;
typedef struct linkedList{
word_p _head;
word_p _tail;
} linkedList;
void checkExit(char curr[MAX_WRD_LENGTH],letter_p root, linkedList_p list)
{
if(strcmp(curr,"exit")==0)
{
deleteLetters(root);
emptyList(list);
exit(EXIT_SUCCESS);
}
}
/* --------------------- Letter Creation ---------------------- */
letter_p createNewLetter(char c)
{
letter_p res = (letter_p)malloc(sizeof(letter_t));
if(res==NULL)
{
free(res);
return NULL;
}
res->_letter = c;
res->_letter_score = 1;
res->_down = NULL;
res->_right = NULL;
return res;
}
/* -------------------- Letter Operations ----------------------- */
letter_p getWordRoot(char curr[MAX_WRD_LENGTH],letter_p root)
{
if(root==NULL) return NULL;
letter_p p = root;
while((p->_right)!=NULL)
{
p = p->_right;
if(p->_letter==curr[0]) return p;
}
return NULL;
}
void insert(char c,letter_p root,char curr[MAX_WRD_LENGTH])
{
int length = strlen(curr); //THIS IS WHERE I GET STUCK!!!! it tells me "implicit conversion loses integer precision"//
letter_p word_root = getWordRoot(curr,root);
if(word_root==NULL)
{
letter_p p=root;
if(p==NULL)
{
root = createNewLetter(c);
return;
}
while(p->_right!=NULL) p=p->_right;
p->_right=createNewLetter(c);
return;
}
letter_p pt = word_root;
int i = 0;
while(i<length)
{
if(pt->_down==NULL)
{
while(pt->_right!=NULL)
{
pt = pt->_right;
}
pt->_right=createNewLetter(c);
return;
}
pt=pt->_down;
i++;
}
if(pt->_letter==c)
{
pt->_letter_score++;
return;
}
else
{
while(pt->_right!=NULL) pt = pt->_right;
pt->_right=createNewLetter(c);
return;
}
}
void deleteLetters(letter_p root)
{
}
/* --------------------- Word Creation ------------------------ */
word_p createNewWord(char curr[MAX_WRD_LENGTH],letter_p root)
{
word_p res = (word_p)malloc(sizeof(word));
if(res==NULL) return NULL;
strcpy(res->_word,curr);
res->_word_score = getWordScore(curr,root);
return res;
}
/* -------------------- Word Operations ----------------------- */
int getWordScore(char curr[MAX_WRD_LENGTH],letter_p root)
{
int res=0,i;
letter_p p = getWordRoot(curr,root);
res+=p->_letter_score;
p=p->_down;
for(i=1;i<strlen(curr);i++)
{
if(p->_letter==curr[i])
{
res+=p->_letter_score;
p=p->_down;
continue;
}
p=p->_right;
while(p->_right!=NULL)
{
if(p->_letter==curr[i])
{
res+=p->_letter_score;
p=p->_down;
continue;
}
}
}
return res;
}
void printOption(linkedList_p words,char curr[MAX_WRD_LENGTH],int num_of_next)
{
if(words->_head==NULL) return;
word_p w = words->_head;
while(strncmp(curr,w->_word,strlen(curr))!=0)
{
w=w->_next;
}
int i=0;
while(i<num_of_next)
{
if(w->_next==NULL) return;
w=w->_next;
}
if(strncmp(curr,w->_word,strlen(curr))==0)
{
puts(w->_word);
return;
}
}
void updateScores(linkedList_p words,char curr[MAX_WRD_LENGTH])
{
if(words->_head==NULL)
{
return;
}
word_p w = words->_head;
while(w!=NULL)
{
if(strncmp(curr,w->_word,strlen(curr))==0)
{
w->_word_score++;
return;
}
w=w->_next;
}
}
void deleteWord()
{
}
/* --------------------- List Creation ------------------------ */
linkedList_p createNewList()
{
linkedList_p newList = (linkedList_p) malloc(sizeof(linkedList));
if(newList==NULL)
{
free(newList);
return NULL;
}
newList->_head = NULL;
newList->_tail = NULL;
return newList;
}
/* -------------------- List Operations ----------------------- */
//adds a new word (w) to the end of the list
void addLast(linkedList_p list, word_p w, int sort)
{
if (!list->_head)
{
list->_head = list->_tail = w;
return;
}
w->_prev = list->_tail;
list->_tail->_next = w;
list->_tail = w;
}
//deletes and frees all items in list
void emptyList(linkedList_p list)
{
while (list->_head != list->_tail)
{
list->_head = list->_head->_next;
free(list->_head->_prev);
}
if (list->_head)
free(list->_head);
list->_head = list->_tail = NULL;
}
i would appreciate any help really!