2012年2月22日 星期三
Exercise #14
This assignment will give you practice with C++ input/output classes and file I/O. Use the following program specifications and use the following main() to test your program. You should use the WordFile and the Dictionary classes described below, and any others you wish.
Here is the WordFile class. Write the 4 member functions. Add any others you desire. The getNextWord() function should place the "next word to be read" in the buffer argument and return it. If getNextWord() fails, it should return a null pointer.
class WordFile {
private:
fstream File;
public:
WordFile(const char* filename);
void addWord(const char* word);
void goToTopOfFile();
char* getNextWord(char* buffer);
};
The Dictionary class should contain two members, an fstream object and an unsigned int, which stores the number of words in the Dictionary. The Dictionary constructor should use a WordFile& argument. This constructor should read the WordFile's file into memory. Use dynamic memory allocation to temporarily store the words, so they can be sorted. After sorting, write them out to the new Dictionary file and clean up.
Add four more members functions to the Dictionary class, getNumWords(), getDictionarySizeInBytes(), getMiddleWord() and find(). The function, getMiddleWord(), should return the word that's in the middle of the file. That is, if the file is 100 bytes, then the function should return the word that begins on or before byte 50.
Add a friend operator<<() to the Dictionary class. This function should print out the Dictionary.
Use the main() below for the final testing of your program. It, and the sample program run should give you more insight to the program. If you do not complete the program, turn in only the functions and the parts of the program that run, along with the output. Do not turn in a program that does not run.
Test your code thoroughly after you complete each function. Do not attempt the Dictionary class until you WordFile class is complete.
int main() {
WordFile Words("wordfile.txt");
char buffer[MaxWordSize];
// Read words into the Word file
cout << "Enter words for the Word file (“quit” to stop)\n";
while (cin.getline(buffer,MaxWordSize)&&strcmp(buffer,”quit”))
Words.addWord(buffer);
Dictionary Webster(Words);
// Print the Dictionary
cout << Webster << endl;
// Print the Dictionary size
cout << "Dictionary size = " << Webster.getDictionarySizeInBytes() << endl;
// Print the word in the middle of the dictionary
cout << "The middle word is " << Webster.getMiddleWord(buffer) << endl;
// Search for words in the Dictionary
cout << "Enter words to search for in the Dictionary (“\quit\” to stop)\n";
cin.clear();
while (cin.getline(buffer,MaxWordSize) && strcmp(buffer,"quit")) {
cout << buffer << " is ";
if (Webster.find(buffer)) cout << "definitely ";
else cout << "NOT ";
cout << "in the Dictionary\n";
}
return 0;
}
****** Sample Run ******
Enter words for the Word file (enter "quit" to stop)
chimpanzee
whale
bald eagle
tiger
zebra
mouse
horse fly
mountain goat
baboon
quit
Dictionary Words:
baboon
bald eagle
chimpanzee
horse fly
mountain goat
mouse
…
Dictionary size = 86 <- Note this could be a different size
The middle word is ??? <- this may vary
Enter words to search for in the Dictionary ("quit" to stop)
elephant
elephant is NOT in the Dictionary
goat
goat is NOT in the Dictionary
baboon
baboon is definitely in the Dictionary
zebra
zebra is definitely in the Dictionary
mountain goat
mountain goat is definitely in the Dictionary
dog
dog is NOT in the Dictionary
quit
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言