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
|
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
#include <list>
#include <map>
#include <fstream>
using namespace std;
void clean_entry( const string& s1, string& s2 ) {
string::const_iterator start = s1.begin();
string::const_iterator finish = s1.begin();
start = find_if (s1.begin(), s1.end(),
[] (char c) -> bool { return (isalnum(c) > 0); });
// *************************************************************
// * There is an error in this line
// *************************************************************
finish = find_if(s1.begin(), s1.end(),
[] (char c) -> bool { return (!isalnum(c) > 0); });
s2 = s1.substr (start-s1.begin(), finish-start);
transform (s2.begin(), s2.end(), s2.begin(),::tolower);
}
void get_words( map <string, int >&m ) {
string s1, s2, line;
ifstream myfiles("prog4.d");
if (myfiles.is_open()) {
while (!myfiles.eof()) {
getline(myfiles, line) ;
line += '\n';
size_t index = 0;
// **************************************************************
// * <= is bad
// * vv
// * for (unsigned int i = 0; i <= line.size(); i++)
// *
// * line can only be indexed line.size()-1, <= would cause your
// * out_of_range error
// **************************************************************
for (unsigned int i = 0; i < line.size(); i++) {
if(line[i] == ' ' || line[i] == '\n' ) {
size_t len = i;
cout << "Space found at position: " << len << endl;
cout << "Index: " << index << " " << "Length afterwards: " << len-index << endl;
// ******************************************************
// * Check to see if there is length
// ******************************************************
if ( len-index > 0 ) {
// ******************************************************
// * You need to set the value of s1 here using the
// * varables index and len
// ******************************************************
//s1 =
cout << "Before: '" << s1 << "'" << endl;
// ******************************************************
// * Have to make a change to clean_entry
// ******************************************************
clean_entry(s1, s2);
cout << "After: '" << s2 << "'" << endl;
m[s2]++;
s1.clear();
s2.clear();
index = len;
}
}
}
}
myfiles.close();
}
else {
cout << "Could not open file" << endl;
}
}
void print_words( const map <string, int >& m ){
const int NO_ITEMS(3),ITEM_W(18);
int i(1);
for (map <string, int>::const_iterator it = m.begin(); it != m.end(); it++) {
if (i != NO_ITEMS) {
cout << left << setw(ITEM_W) << it->first << ": " << setw(3) << it->second << " ";
i++;
} else {
cout << left << setw(ITEM_W) << it->first << ": " << setw(3) << it->second << endl;
i = 1;
}
}
cout << endl;
cout << "Number of distinct words: " << m.size() << endl;
}
int main(void){
map<string,int> theMap;
get_words(theMap);
print_words(theMap);
return 0;
}
|