cs11_c++_lab7

时间:2021-12-21 07:35:16

wcount.cc

 #include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <ctype.h>
// So we don't have to type "std::" everywhere...
using namespace std; string processWord(string &word);
void processText(map<string, int>& wordCounts);
void outputWordsByCount(map<string, int>& wordCounts); unsigned total = ; int main()
{
map<string, int> wordCounts; // Process the text on console-input, using the skip-list.
processText(wordCounts); cout << "Total words are " << total << endl;
cout << "unique words are " << wordCounts.size() <<endl; // Finally, output the word-list and the associated counts.
outputWordsByCount(wordCounts);
} /*
* This helper-function converts a word to all lower-case, and then removes
* any leading and/or trailing punctuation.
*
* Parameters:
* word The word to process. It is passed by-value so that it can be
* manipulated within the function without affecting the caller.
*
* Return value:
* The word after all leading and trailing punctuation have been removed.
* Of course, if the word is entirely punctuation (e.g. "--") then the result
* may be an empty string object (containing "").
*/
string processWord(string &word)
{
/*****************************************/
/* TODO: Your implementation goes here! */
/*****************************************/ for(int i = ;i < word.length(); i++)
{
if(isalpha(word[i]))
{
word[i] = tolower(word[i]);
}
} int j = ;
for(; j < word.length(); j++)
{
if(isalpha(word[j]) || isdigit(word[j]))
break;
} int k = word.length()-;
for(; k >= ; k--)
{
if(isalpha(word[k]) || isdigit(word[k]))
break;
}
total++;
if(j > k)
{
return "";
}
else
{
return word.substr(j, k-j+);
}
} void processText(map<string, int>& wordCounts)
{
/*****************************************/
/* TODO: Your implementation goes here! */
/*****************************************/ string word;
while(cin >> word)
{
string new_word = processWord(word);
cout<<new_word<<endl;//log if(new_word.length() > )
{
wordCounts[new_word]++;
}
} /* for(auto i = wordCounts.begin(); i != wordCounts.end(); i++)
{
cout << i->first << " " << i->second << endl;
}
*/
} /*
* This helper-function outputs the generated word-list in descending order
* of count. The function uses an STL associative container to sort the words
* by how many times they appear. Because multiple words can have the same
* counts, a multimap is used.
*/
void outputWordsByCount(map<string, int>& wordCounts)
{
multimap<int, string, greater<int> > sortByCount;
map<string, int>::const_iterator wIter; for (wIter = wordCounts.begin(); wIter != wordCounts.end(); wIter++)
sortByCount.insert(pair<int, string>(wIter->second, wIter->first)); multimap<int, string>::const_iterator cIter;
for (cIter = sortByCount.begin(); cIter != sortByCount.end(); cIter++)
cout << cIter->second << "\t" << cIter->first << endl;
}

swcount.cc

 #include <iostream>
#include <map>
#include <set>
#include <string> // So we don't have to type "std::" everywhere...
using namespace std; void initSkipList(set<string>& skipList);
string processWord(string word);
void processText(set<string>& skipList, map<string, int>& wordCounts);
void outputWordsByCount(map<string, int>& wordCounts); int total = ;
int skipped = ; int main()
{
set<string> skipList;
map<string, int> wordCounts; // Initialize the skip-list.
initSkipList(skipList); // Process the text on console-input, using the skip-list.
processText(skipList, wordCounts); cout << "Total words are------------ " << total << endl;
cout << "unique words are------------ " << wordCounts.size() << endl;
cout << "skipped words are------------ " << skipped << endl; // Finally, output the word-list and the associated counts.
outputWordsByCount(wordCounts);
} /*
* This function initializes the skip-list of words.
*
* skipList = the set of words to skip
*/
void initSkipList(set<string>& skipList)
{
// Use a pre-specified skip-list. const char *swords[] = {
"a", "all", "am", "an", "and", "are", "as", "at",
"be", "been", "but", "by",
"did", "do",
"for", "from",
"had", "has", "have", "he", "her", "hers", "him", "his",
"i", "if", "in", "into", "is", "it", "its",
"me", "my",
"not",
"of", "on", "or",
"so",
"that", "the", "their", "them", "they", "this", "to",
"up", "us",
"was", "we", "what", "who", "why", "will", "with",
"you", "your", }; for (int i = ; swords[i] != ; i++)
skipList.insert(string(swords[i]));
} /*
* This helper-function converts a word to all lower-case, and then removes
* any leading and/or trailing punctuation.
*
* Parameters:
* word The word to process. It is passed by-value so that it can be
* manipulated within the function without affecting the caller.
*
* Return value:
* The word after all leading and trailing punctuation have been removed.
* Of course, if the word is entirely punctuation (e.g. "--") then the result
* may be an empty string object (containing "").
*/
string processWord(string word)
{
/*****************************************/
/* TODO: Your implementation goes here! */
/*****************************************/ for(int i = ;i < word.length(); i++)
{
if(isalpha(word[i]))
{
word[i] = tolower(word[i]);
}
} int j = ;
for(; j < word.length(); j++)
{
if(isalpha(word[j]) || isdigit(word[j]))
break;
} int k = word.length()-;
for(; k >= ; k--)
{
if(isalpha(word[k]) || isdigit(word[k]))
break;
} if(j > k)
{
return "";
}
else
{
total++;
return word.substr(j, k-j+);
} } void processText(set<string>& skipList, map<string, int>& wordCounts)
{
/***********************************/
/* TODO: Implement this function! */
/***********************************/ string word;
while(cin >> word)
{
string new_word = processWord(word); if(new_word.length() > )
{
if(skipList.find(new_word) == skipList.end())
wordCounts[new_word]++;
else
skipped++;
}
}
} /*
* This helper-function outputs the generated word-list in descending order
* of count. The function uses an STL associative container to sort the words
* by how many times they appear. Because multiple words can have the same
* counts, a multimap is used.
*/
void outputWordsByCount(map<string, int>& wordCounts)
{
multimap<int, string, greater<int> > sortByCount;
map<string, int>::const_iterator wIter; for (wIter = wordCounts.begin(); wIter != wordCounts.end(); wIter++)
sortByCount.insert(pair<int, string>(wIter->second, wIter->first)); multimap<int, string>::const_iterator cIter;
for (cIter = sortByCount.begin(); cIter != sortByCount.end(); cIter++)
cout << cIter->second << "\t" << cIter->first << endl;
}

相关文章