Thread: Auto-Complete Program HELP

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Auto-Complete Program HELP

    Hey guys,

    I really need you're help with this.
    The code below is kind of very long but i've been working with a friend on this for a long time now but i'm stuck and need to submit it in the following 3 hours.
    There's a problem here and it doesn't build properly.
    I'm actually doing an autocomplete program sorry if i don't have any info to give out more clearly and sorry if it's too much i would just appreciate a lot for any help possible.
    here's the code (there's the main,Word.h & Word.c)

    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!

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Why doesn't it build?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I think #include "Word.c" should be #include "Word.h"

    If you need help you should also include what the actual problem was. Do you get a compiler error when trying to build it?

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    I have both Word.h and Word.c
    if you look at line 230 you'll see my comment there.
    it just doesn't build it tells me "implicit conversion loses integer precision"//

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Pass the length of the array as a third parameter!!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    what do you mean?
    where to go?
    please be precise..
    and also thanks a lot for your help

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome.
    Code:
    void insert(char c,letter_p root,char curr[MAX_WRD_LENGTH], int length)
    {
        int length = strlen(curr);      //THIS IS WHERE I GET STUCK!!!! it tells me "implicit conversion loses integer precision"//
    ...
    instead of doing strlen, pass the length of the array as 3rd parameter to the function (since the length is fixed to MAX_WRD_LENGTH, isn't it?)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    how do i do that?

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You mean how you call it in main?
    You have
    Code:
    insert(c,root,curr);
    Change it to this
    Code:
    insert(c, root, curr, MAX_WRD_LENGTH);
    And in line 230 use this
    Code:
    void insert(char c,letter_p root,char curr[MAX_WRD_LENGTH], int length)
    {
        int length = strlen(curr);  // delete this line and let the function as it is
        ...
    }
    and in line 129 change the prototype of the function to
    Code:
    void insert(char c,letter_p root,char curr[MAX_WRD_LENGTH], int length);
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    linker command failed with exit code 1 (use -v to see invocation)
    no idea why all my issues we're gone as if it went very well

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Do what the other guy said. In main include the word.h file.

    Line 6 on the first post, you have
    Code:
    #include "Word.c"
    Change it to this
    Code:
    #include "Word.h"
    and report back!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    better thanks guys appreciate a lot

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Nice!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    thanks man appreciate it. I have so many assignments its crazy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Auto-Complete Program in c
    By YannB in forum C Programming
    Replies: 9
    Last Post: 01-15-2013, 01:01 AM
  2. Little help to complete my program..pls..
    By scorpio76 in forum C Programming
    Replies: 14
    Last Post: 02-24-2011, 05:58 AM
  3. c++ complete program
    By gaza rose in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2008, 01:47 PM
  4. auto log in program ....
    By twomers in forum C++ Programming
    Replies: 15
    Last Post: 01-16-2006, 06:39 AM
  5. Auto Complete ?
    By diediemustdie in forum C Programming
    Replies: 10
    Last Post: 11-18-2004, 01:24 AM