Advanced C++ assignment

0 comments

Your program should adhere to the following specifications:

  1. You are to use the Word and Dictionary classes defined below and write all member functions and any necessary supporting functions to achieve the specified result.
  2. The Word class should dynamically allocate memory for each word to be stored in the dictionary.
  3. The Dictionary class should contain an array of pointers to Word. Memory for this array must be dynamically allocated. You will have to read the words in from the file. Since you do not know the “word” file size, you do not know how large to allocate the array of pointers. You are to let this grow dynamically as you read the file in. Start with an array size of 8, When that array is filled, double the array size, copy the original 8 words to the new array and continue. You can see the expected behavior in the sample program output listed below.
  4. You can assume the “word” file is sorted, so your Dictionary::find() function must contain a binary search algorithm. You might want to save this requirement for later – until you get the rest of your program running.
  5. Make sure you store words in the dictionary as lower case and that you convert the input text to the same case – that way your Dictionary::find() function will successfully find “Four” even though it is stored as “four” in your Dictionary.
  6. Make sure you remove leading or trailing punctuation from a word, such as “nation,”.
  7. Do not use the string class for this assignment. All text data should be stored and managed as C-strings.

Warning: If you are using a Mac or Linux compiler, you may want to remove the r at the end of each line in both input files. You can do that like this:

strtok(line,”r”);

<strong><u>Word class</u></strong>
class Word 
{ 
   char* word_; 
public: 
   Word(const char* text = 0); 
   ~Word(); 
   const char* word() const; 
};

<strong><u>Dictionary class</u></strong>
class Dictionary 
{ 
   Word** words_; 
   unsigned int capacity_;   // max number of words Dictionary can hold 
   unsigned int numberOfWordsInDictionary_; 
   void resize(); 
   void addWordToDictionary(char* word); 
public: 
   Dictionary(const char* filename); 
   ~Dictionary(); 
   bool find(const char* word);
};

<strong><u>main()</u></strong>

int main()
{
   char buffer[MaxWordSize];
   Dictionary Websters(wordfile);
   ifstream fin(document);
   cout << "nSpell checking " << document << "nn";
   while (fin >> buffer) {
      // remove leading/trailing punctuation, change to lowercase
      if (cleanupWord(buffer)) {      
         if (!Websters.find(buffer)) {
            cout << buffer << " not found in the Dictionaryn";
         }
      }
   }
}

<strong><u>Program output</u></strong>

Dictionary resized to capacity: 16            <strong><--- This illustrates Dictionary resizing</strong>
Dictionary resized to capacity: 32
Dictionary resized to capacity: 64
Dictionary resized to capacity: 128
Dictionary resized to capacity: 256
Dictionary resized to capacity: 512
Dictionary resized to capacity: 1024
Dictionary resized to capacity: 2048
Dictionary resized to capacity: 4096
Dictionary resized to capacity: 8192
Dictionary resized to capacity: 16384
Dictionary resized to capacity: 32768

Spell checking /deanza/data/gettysburg.txt       <strong><--- spell checking begins</strong>

created not found in the Dictionary        <strong><--- words not found in the Dictionary</strong>
struggled not found in the Dictionary
consecrated not found in the Dictionary
por not found in the Dictionary
remember not found in the Dictionary
unfinished not found in the Dictionary
nobly not found in the Dictionary
advanced not found in the Dictionary
...

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}