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#参数化执行SQL语句,防止漏洞攻击本文以MySql为例【20151108非查询操作】
为什么要参数化执行SQL语句呢? 一个作用就是可以防止用户注入漏洞. 简单举个列子吧. 比如账号密码登入,如果不用参数, 写的简单点吧,就写从数据库查找到id和pw与用户输入一样的数据吧 sql:se ...
- java如何在eclipse编译时自动生成代码
用eclipse写java代码,自动编译时,如何能够触发一个动作,这个动作是生成本项目的代码,并且编译完成后,自动生成的代码也编译好了, java编辑器中就可以做到对新生成的代码的自动提示? 不生成代 ...
- 水池进水与放水问题:有一个水池,水池的容量是固定 的500L,一边为进水口,一边为出水口.........(多线程应用)
package demo2; class Pooll { /**1:有一个水池,水池的容量是固定 的500L,一边为进水口,一边为出水口. * 要求,进水与放水不能同时进行. 水 ...
- 修改msde登录方式,设置sa密码为空
md, 记不得msde怎么修改密码, 每次都要去baidu, 下了个鸟破软件,修改msde密码, 还流氓的安装了360, 写了个批处理,留在这里: net stop MSSQLSERVERreg ad ...
- QT实现HTTP JSON高效多线程处理服务器
QT实现HTTP JSON高效多线程处理服务器 Legahero QQ:1395449850 现在一个平台级的系统光靠web打天下是不太现实的了,至少包含APP和web两部分,在早期APP直接访问we ...
- jQuery的.html(),.text()和.val()的概述及使用
本节内容主要介绍的是如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法.jQuery中为我 ...
- zepto.js使用前注意
API:http://www.css88.com/doc/zeptojs_api/ 一.建议:不要从官网下载,而是从 Github 下载了源代码之后自己 Build 一个版本,这样你可以自行挑选适合的 ...
- android安装busybox
大家是否有过这样的经历,在命令行里输入adb shell,然后使用命令操作你的手机或模拟器,但是那些命令都是常见Linux命令的阉割缩水版,用起来很不爽.是否想过在Android上使用较完整的shel ...
- flash flex 程序出现错误 Error #2032
解决思路参考: http://www.cnblogs.com/enjoyprogram/archive/2012/06/21/2557615.html 有可能是这种情况: 状况:在安装flshbuil ...
- 作品-系统-[原生开发]新蓝景CRM管理系统
基于ThinkPHP开发 项目网址:http://www.xljchina.com.cn:8839/Admin/Login/login.html 开发性质:原生开发 系统类型:CRM