cs11_c++_lab7
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;
}
cs11_c++_lab7的更多相关文章
- cs11_c++_lab6
expressions.hh #ifndef EXPRESSIONS_HH #define EXPRESSIONS_HH #include "environment.hh" #in ...
- cs11_c++_lab5待修改
heap.hh #ifndef HEAP_HH #define HEAP_HH #include <iostream> #include <stdexcept> #includ ...
- cs11_c++_lab4b
SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...
- cs11_c++_lab4a
SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...
- cs11_c++_lab3
Matrix.hh class Matrix { int row; int col; int *p; void copy(const Matrix &m); void clearup(); p ...
- cs11_c++_lab2
Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...
- cs11_c++_lab1
lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...
随机推荐
- C语言字符串处理函数
函数名: strcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include < ...
- 备忘录模式(Memento Pattern)
在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 备忘录模式主要思想是——利用备忘录对象来对保存发起人的内部状态,当发起人需要恢复原 ...
- js中java式的类成员
function Range(from,to,x){ //实例(对象)字段 this.x=x; } //类字段 Range.Y="类字段"; //类方法 Range.s=funct ...
- DataTable 批量插入SqlServer数据库 使用:SqlBulkCopy
简单使用: private void UpdateTitle(DataTable dt) { ) { using (SqlBulkCopy sbc = new SqlBulkCopy(SqlHelpe ...
- Linux-深入理解Socket异常
在各种网络异常情况的背后,TCP是怎么处理的?又是怎样把处理结果反馈给上层应用的?本文就来讨论这个问题.分为两个场景来讨论 建立连接时的异常情况 1 正常情况下 经过三次握手,客户端连接成功,服务端有 ...
- Unity浅析
在分析PRISM项目的时候, 发现里面用到了Unity 这个Component, 主要用于依赖注入的.由于对其不熟悉,索性分析了一下,记载在此,以作备忘. 任何事物的出现,总有它独特的原因,Unity ...
- linux centos6.5支持ipv6
1.用ifconfig查看有没有inet6 addr,我的这个已经支持了,如果不支持请看第二步. 2.vim /etc/sysconfig/network 把这句改成:NETWORKING_IPV6= ...
- bash shell,调用ffmpeg定期截图
#!/bin/bash #获取当前目录中所有m3u8文件,并 var=$(ls |grep '.m3u8'|cut -d '.' -f1) #死循环 = ] do #循环每个文件 for stream ...
- 解除svn版本控制
步骤1.去除目录下的所有.svn文件夹:两种方式: 方法1:搜索目录下所有.svn文件,删除: 方法2:复制下列文字到txt中,然后把扩展名改为reg,放到需要去除.svn的目录中,双击运行注册表即可 ...
- 黄聪:使用srvany.exe将任何程序作为Windows服务运行
srvany.exe是什么? srvany.exe是Microsoft Windows Resource Kits工具集的一个实用的小工具,用于将任何EXE程序作为Windows服务运行.也就是说sr ...