help?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: help?

  1. #1
    Join Date
    Jul 2007
    Posts
    2

    help?

    basically...the program reads and sorts string values alphabetically.. all i need now is a sort of counter to display how many times a certain word shows up. been staring at it too long and for some reason havent figured it out, any help would be much appreciated. i believe i have to add a function to public Dblist then call it after the last if statement.."...wlist.Insert(readword)" <--right after this

    just cant figure how to do it and the teacher wrote a lot of the code so I am a little confused as to what exactly is going on and in what order...thank you in advance :)

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstddef>

    using namespace std;
    struct NodeType {
    string word;
    NodeType* prev;
    NodeType* next;
    };
    class Dblist
    {
    public:
    Dblist();
    bool Find(string word);
    bool IsEmpty() const;
    void InsertFirst(string newword);
    void Insert(string newword);
    void Display() const;
    private:
    NodeType * headptr;
    };
    Dblist::Dblist()
    {
    headptr = NULL;
    }
    bool Dblist::IsEmpty() const
    {
    return (headptr ==NULL);
    }
    bool Dblist::Find(string newword)
    {
    NodeType * currptr;

    currptr= headptr;
    while(currptr != NULL)
    {
    if((currptr->word) == newword)
    {
    return true;
    }
    else
    {
    currptr = currptr-> next;
    }
    }
    return false;
    }
    void Dblist::InsertFirst(string newword)
    {
    NodeType * newNode;

    newNode = new NodeType;
    newNode->word = newword;
    newNode->prev = NULL;
    newNode->next = NULL;
    headptr = newNode;
    }
    void Dblist::Insert(string newword)
    {
    NodeType * newNode;
    NodeType * currptr, *prevptr;
    currptr = headptr;
    prevptr = NULL;

    newNode = new NodeType;
    newNode->word = newword;

    while((currptr != NULL) && (currptr->word < newword))
    {
    prevptr = currptr;
    currptr = currptr->next;

    }
    if(prevptr == NULL)
    {
    newNode->prev = NULL;
    newNode->next = headptr;
    currptr->prev = newNode;
    headptr= newNode;
    }
    else if(currptr == NULL)
    {
    newNode->next=NULL;
    newNode->prev=prevptr;
    prevptr->next=newNode;
    }
    else
    {
    newNode->prev=currptr->prev;
    newNode->next=currptr;
    currptr->prev->next=newNode;
    currptr->prev=newNode;
    }
    }
    void Dblist::Display() const
    {

    NodeType * currptr;
    currptr= headptr;
    while(currptr!=NULL)
    {
    cout << currptr->word<<endl;
    currptr= currptr->next;
    }

    }
    int main()
    {
    Dblist wlist;
    string readword;
    ifstream inText;

    inText.open("book.txt");
    if(!inText)
    {
    cout<<"** can't open input file stud**"<<endl;
    return 1;
    }
    while(!inText.eof())
    {
    inText>>readword;
    while(readword[0] < 'A' || readword[0] > 'Z')
    {
    inText >> readword;
    }
    cout << "next word is " << readword << endl;
    if(wlist.IsEmpty())
    wlist.InsertFirst(readword);
    else
    {
    if(! wlist.Find(readword))
    wlist.Insert(readword);
    }
    }
    wlist.Display();

    return 0;
    }

  2. #2
    Join Date
    Jul 2007
    Posts
    25
    I will give you a tip.
    Since a list is used you hold each word inside a node of this list. You can add a counter inside the struct node to hold the number of words you encounter. Each time you encounter the same word you need to increment this counter. :-)

  3. #3
    Join Date
    Jul 2007
    Posts
    2
    thanks for the tip... is there any way you could be a little more specific though, I'm still having a lot of trouble. I can get a counter to output the number of times ANY word repeats which is 13 times I believe... but the 13 shows up after every word whereas I need it to be independent with each.

    does that make sense?

  4. #4
    Join Date
    Jul 2007
    Posts
    25
    Ok , I compiled your code and there are a few problems you need to take care. First of all you have to remove the while loop that compares capital letters. There is no point doing that and plus it is this that messes things up.
    The counter I assume you already put it inside the struct node:

    struct NodeType {
    string word;
    int counter;
    NodeType* prev;
    NodeType* next;
    };
    So all you have to do now is increment this counter by using the node pointer in the proper places. And these places would be:
    When you first insert a new word for the first time (Insertfirst routine)
    When you Find a word that already exists (Find routine)
    When you insert a word (Insert routine)

    I just added 3 lines of code and deleted your while loop with the capital letters and it works: See below copied from my linux session...
    Have a try and you will get it. Next thing is to give you the whole code and that would be cheating , right? :)

    manos@linux-w9gc:~> cat book.txt
    Word1 Word5 Word2
    Word2
    Word3 Word2 Word1
    Word3

    manos@linux-w9gc:~> ./list
    next word is Word1
    next word is Word5
    next word is Word2
    next word is Word2
    next word is Word3
    next word is Word2
    next word is Word1
    next word is Word3
    next word is Word3
    Word1 : count is 2
    Word2 : count is 3
    Word3 : count is 3
    Word5 : count is 1
    manos@linux-w9gc:~>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center