How to fix this structure?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris R.

    #1

    How to fix this structure?

    Hi everyone. I am trying to finish my homework, but I seem not to
    figure out how to fix this structure that seems to make wrong output.
    What this program should do is use structure to save the word and line
    number on which it was in array pointer using malloc and print them
    out alphabetized (no duplicates).

    Most of it works, except the words seem to be corrupted. Here is a
    code, if anyone can help me it would help so much since I've been
    working on this for a really long time. I tried even changing '\0' to
    NULL, but then program errored out.


    Here is my code:



    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>


    typedef struct list
    {
    int linenum;
    char word[30];
    } LIST;



    void main()
    {
    LIST *p[1000], *holder;
    int line = 1;
    int pcounter = 0;
    char buffer[40];
    int buffercounter = 0;
    char letter = ' ';
    int inword = 0;
    int i;
    int compare;
    int shouldstore = 1;
    int x,y;


    while (letter != EOF)
    {
    letter = getchar();
    if ( (letter >= 'a' && letter <= 'z') || (letter >=
    'A' && letter <= 'Z') )
    {
    letter = toupper(letter) ;
    inword = 1;
    buffer[buffercounter] = letter;
    buffercounter++ ;
    }
    else if (letter == '\n')
    {
    line++;
    if (inword == 1)
    {
    inword = 0;
    buffercounter++ ;
    buffer[buffercounter] = '\0';
    buffercounter = 0;
    if (pcounter == 0)
    {
    p[pcounter] = (LIST*) malloc (sizeof(LIST));
    strcpy( p[pcounter]->word , buffer);
    p[pcounter]->linenum = line;
    pcounter++;
    }
    else
    {
    for (i = 0; i < pcounter; i++)
    {
    compare = strcmp(p[i]->word, buffer);
    if (compare == 0)
    {
    shouldstore = 0;
    }
    }
    if (shouldstore == 0)
    {
    shouldstore = 1;
    }
    else
    {
    p[pcounter] = (LIST*) malloc (sizeof(LIST));
    strcpy( p[pcounter]->word, buffer);
    p[pcounter]->linenum = line;
    pcounter++;
    }
    }

    }
    else
    {
    buffercounter = 0;
    }
    }
    else
    {
    if (inword == 1)
    {
    inword = 0;
    buffercounter++ ;
    buffer[buffercounter] = '\0';
    if (pcounter == 0)
    {
    p[pcounter] = (LIST*) malloc (sizeof(LIST));
    strcpy( p[pcounter]->word , buffer);
    p[pcounter]->linenum = line;
    pcounter++;
    }
    else
    {
    for (i = 0; i < pcounter; i++)
    {
    compare = strcmp(p[i]->word, buffer);
    if (compare == 0)
    {
    shouldstore = 0;
    }
    }
    if (shouldstore == 0)
    {
    shouldstore = 1;
    }
    else
    {
    p[pcounter] = (LIST*) malloc (sizeof(LIST));
    strcpy( p[pcounter]->word, buffer);
    p[pcounter]->linenum = line;
    pcounter++;
    }
    }
    }
    else
    {
    buffercounter = 0;
    }
    }


    }

    for(x = 0; x < pcounter; x++)
    for(y = 0; y < pcounter-1; y++)
    {
    compare = strcmp(p[y]->word, p[y+1]->word);
    if( compare < 0)
    {
    holder = p[y+1];
    p[y+1] = p[y];
    p[y] = holder;
    }
    }

    for (x = 0; x < pcounter; x++)
    {
    printf("\nThe word %s was on line %d", p[x]->word, p[x]->linenum);
    }
    }
  • Al Bowers

    #2
    Re: How to fix this structure?



    Chris R. wrote:[color=blue]
    > Hi everyone. I am trying to finish my homework, but I seem not to
    > figure out how to fix this structure that seems to make wrong output.
    > What this program should do is use structure to save the word and line
    > number on which it was in array pointer using malloc and print them
    > out alphabetized (no duplicates).
    >
    > Most of it works, except the words seem to be corrupted. Here is a
    > code, if anyone can help me it would help so much since I've been
    > working on this for a really long time. I tried even changing '\0' to
    > NULL, but then program errored out.
    >
    >[/color]
    .... code snipped ....

    There are many errors, so I want take the time to try to fix your
    code. Along with the errors you have a design problem with the
    date type and with all the operations being done in one function, main.

    I would make the data type List which contains a pointer to an
    array of data type WORD as one member and a counter to keep a count
    on the number of words in the list as the other member. In the
    data type WORD you would store the word in on member and the other
    member would be the line number.

    Example:
    typedef struct WORD
    {
    unsigned linenum;
    char *name;
    } WORD;

    typedef struct LIST
    {
    WORD *word;
    unsigned count;
    } LIST;

    Now you should write fucntions that do simple operations
    on the LIST datatype. For example AddWordtoList() to add
    a word, PrintList() to print the list, FreeList() to free
    the allocations once you are finished with the list.
    You could use Standard C functions bsearch to search the
    list for dupes and function qsort to sort the list as you
    build it.

    An example of a possible definiion of AddWordtoList() .

    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>

    int cmp(const void *v1, const void *v2)
    {
    const WORD *w1 = v1;
    const WORD *w2 = v2;

    return strcmp(w1->name,w2->name);
    }

    int AddWordtoList(L IST *p, const char *word, unsigned linenr)
    {
    WORD *tmp,key;
    char *s, *s1;

    if((s = malloc(strlen(w ord)+1)) == NULL) return 0;
    strcpy(s,word);
    for(s1 = s; (*s1 = tolower((unsign ed char)*s1));s1++ ) ;
    key.name = s;
    if(p->word != NULL)
    if(bsearch(&key ,p->word,p->count,sizeof *p->word,cmp))
    { /* dupe */
    free(s);
    return 0;
    }
    tmp = realloc(p->word,(p->count+1)*(size of *tmp));
    if(tmp == NULL)
    {
    free(s);
    return 0;
    }
    p->word = tmp;
    p->word[p->count].name = s;
    p->word[p->count++].linenum = linenr;
    qsort(p->word,p->count,sizeof *p->word,cmp);
    return 1;
    }


    --
    Al Bowers
    Tampa, Fl USA
    mailto: xabowers@myrapi dsys.com (remove the x to send email)
    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


    Comment

    Working...