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
|
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <sstream>
#include <iomanip>
// in in in in string
// return a string with characters other than alpha characters
// in the line replaced with spaces, and with all upper case characters
// converted to lower case
std::string format( std::string line )
{
std::string result ;
// for each character in line, if it is an alpha character, add it to result
for( char& c : line )
{
if( std::isalpha(c) ) result += std::tolower(c) ;
else result += ' ' ;
}
return result ;
}
// reads words one by one into the array up to num_words words
// return the number of words that were read in
int get_words( std::istream& stm, std::string words_array[], int array_sz )
{
int num_words = 0 ; // number of words read so far
std::string line ;
// for each line in the input stream
while( num_words < array_sz && std::getline( stm, line ) )
{
// create an input string stream to read from the formatted line
std::istringstream str_stm( format(line) ) ;
std::string word ;
while( num_words < array_sz && str_stm >> word ) // for each word in the line
{
words_array[num_words] = word ; // add it to the words array
++num_words ; // and increment the count
}
}
return num_words ;
}
// return he position of the smallest element in the array
int pos_smallest_element( const std::string words[], int num_words )
{
int pos_smallest = 0 ;
for( int i = 1 ; i < num_words ; ++i )
if( words[i] < words[pos_smallest] ) pos_smallest = i ;
return pos_smallest ;
}
// sort in ascending order
void sort( std::string words[], int num_words )
{
if( num_words > 1 )
{
// bring the smallest element to the front
const int pos_smallest = pos_smallest_element( words, num_words ) ;
std::swap( words[0], words[pos_smallest] ) ;
// sort the remaining elements
sort( words+1, num_words-1 ) ;
}
}
// return the frequency of the word that occurs most often
int max_frequency( std::string words[], int num_words )
{
sort( words, num_words ) ;
int max_freq = 1 ;
int curr_freq = 1 ;
for( int i = 1 ; i < num_words ; ++i )
{
if( words[i] == words[i-1] ) ++curr_freq ;
else
{
if( curr_freq > max_freq ) max_freq = curr_freq ;
curr_freq = 1 ;
}
}
return max_freq > curr_freq ? max_freq : curr_freq ;
}
// print all the words that have occurred freq times
void print_words_with_frequency( const std::string words[], int num_words, int freq )
{
int curr_freq = 1 ;
for( int i = 1 ; i < num_words ; ++i )
{
if( words[i] == words[i-1] ) ++curr_freq ;
else
{
if( curr_freq == freq ) std::cout << words[i-1] << '\n' ;
curr_freq = 1 ;
}
}
if( curr_freq == freq ) std::cout << words[num_words-1] << '\n' ;
}
int main()
{
// for testing: zebra zebra zebra zebra zebra zebra zebra zebra zebra zebra (10)
// FOR TESTING: ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA (20)
const int MAX = 1000 ; // maximum number of words that we can read
std::string words[MAX] ;
std::ifstream file( __FILE__ ) ; // this file: modify as required. eg.
// std::ifstream file( "3.txt" ) ;
// for testing: ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant (20)
// FOR TESTING: ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA (30)
// FOR TESTING: ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT (40)
// for testing: zebra zebra zebra zebra zebra zebra zebra zebra zebra zebra (40)
// FOR TESTING: ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA (50)
// for testing: ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant ant (60)
const int nwords_read = get_words( file, words, MAX ) ;
// take it up from there
const int max_freq = max_frequency( words, nwords_read ) ;
std::cout << "these words occurred the most number of times (" << max_freq << "):\n" ;
print_words_with_frequency( words, nwords_read, max_freq ) ;
}
// for testing: zebra zebra zebra zebra zebra zebra zebra zebra zebra zebra (60)
// FOR TESTING: ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA ZEBRA (70)
// FOR TESTING: ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT ANT (80)
// for testing: zebra zebra zebra zebra zebra zebra zebra zebra zebra zebra (80)
|