1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#include <iostream>
#include <iomanip>
#include "string2.cpp"
using namespace std;
const int MAX_WORD_LENGTH = 254;
// The type definition below permits much easier prototyping of
// functions that pass arrays of strings.
typedef char String[MAX_WORD_LENGTH + 1]; // + 1 for null terminator.
void ReadWord(String Word, int MaxLength);
void WriteWords(String Word[],
int Count[],
int TotalWordCount,
int DistinctWordCount);
void StoreWord(String NewWord,
String Word[],
int Count[],
int &TotalWordCount,
int &DistinctWordCount,
int MaxWordCount);
/*************************** main ***********************************/
void main()
{
const int MAX_WORD_COUNT = 130;
int x;
String NewWord,
Word[MAX_WORD_COUNT + 1] = { "", "" }; // ALWAYS allow 1 extra slot!
int Count[MAX_WORD_COUNT + 1] = { 0 }, // Initialize array to zeros
DistinctWordCount = 0,
TotalWordCount = 0;
ReadWord(NewWord, MAX_WORD_LENGTH); // Get the first word
while ( NewWord[0] != 0) // While NewWord is not null string
{
StoreWord(NewWord, Word, Count, TotalWordCount,
DistinctWordCount, MAX_WORD_COUNT);
ReadWord(NewWord, MAX_WORD_LENGTH); // Get the next word
}
WriteWords(Word, Count, TotalWordCount, DistinctWordCount);
}
/************************ ReadWord *********************************
DESCRIPTION Reads a word from standard input and stores in the array
Word.
For the purposes of this routine, a "word" is any
contiguous sequence of non-blank characters.
PARAMETERS Word An array of char.
MaxWordLength: The maximum number of chars to store in
NewWord (not including the null terminator).
Chars beyond this number are discarded.
CALLS cin.get and cin.good, both in the iostream library.
isspace from ctype file.
NOTE Words longer than MaxWordLength are truncated.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void ReadWord(String NewWord, int MaxWordLength)
{
char Ch;
int K;
cin >> Ch; // Skip blanks, and get the first character of word
K = 0;
while (cin.good() && K < MaxWordLength && !isspace(Ch)) // Store chars
{
NewWord[K++] = Ch;
cin.get(Ch);
}
NewWord[K] = 0; // Add null terminator
while (cin.good() && !isspace(Ch)) // Discard tail end of
cin.get(Ch); // long words
}
/*************************** StoreWord ********************************
DESCRIPTION Adds a string NewWord (holding up to MAX_WORD_LENGTH
characters) to the end of the array Word.
PARAMETERS NewWord The string (array of char) to be added
Word An array of String (char[MAX_WORD_LENGTH])
DistinctWordCount The number of different words in the array
TotalWordCount The total number of word in the file
MaxWordCount The max number of words that can be stored
in the array Word. If DistinctWordCount ==
MaxWordCount, the function is exited.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void StoreWord(String NewWord,
String Word[],
int Count[],
int &TotalWordCount,
int &DistinctWordCount,
int MaxWordCount)
{
int i, k = 0;
while ((NewWord, Word[k]) > 0 && k < DistinctWordCount)
++k;
// Assert: k is NewWord's correct position in the ordered array Word
if ((NewWord, Word[k]) == 0) // NewWord is already there
{
++Count[k];
++TotalWordCount;
}
else if (DistinctWordCount < MaxWordCount) // Room for a new word
{
++DistinctWordCount; // If this line reached, found new word
++TotalWordCount;
for (i = DistinctWordCount - 1; i > k; --i) // Make room for
{ // NewWord
(Word[i], Word[i - 1]);
Count[i] = Count[i - 1];
}
(Word[k], NewWord); // Store NewWord in array
Count[k] = 1;
}
}
/*************************** WriteWords ********************************
DESCRIPTION Writes the strings in the array Word to standard output.
The words are left justified. After WORDS_PER_ROW words have
been written a new line is started.
PARAMETERS Word An array of String (char[MAX_WORD_LENGTH])
WordCount The number of words in the array
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void WriteWords(String Word[],
int Count[],
int TotalWordCount,
int DistinctWordCount)
{
const char *Header1 = " Word Count | ",
*Header2 = " | ",
*Separator = " | ";
const int WORD_FW = 17;
const int COUNT_FW = 4;
const int WORDS_PER_ROW = 3;
int C;
cout << "\n\n";
if (TotalWordCount == 0)
return;
for (C = 1; C <= WORDS_PER_ROW; ++C)
cout << Header1;
cout << endl;
for (C = 1; C <= WORDS_PER_ROW; ++C)
cout << Header2;
cout << endl;
int N = 0;
while (N < DistinctWordCount)
{
for (C = 1; C <= WORDS_PER_ROW && N < DistinctWordCount; ++C)
{
cout << setiosflags(ios::left) << setw(WORD_FW) << Word[N];
cout << setiosflags(ios::right) << setw(COUNT_FW) << Count[N];
cout << Separator;
cout << resetiosflags(ios::right);
++N;
}
cout << endl;
}
cout << "\nTotal Word Count : " << TotalWordCount << endl;
cout << "\nDistinct Word Count : " << DistinctWordCount << endl;
}
|