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的更多相关文章

  1. cs11_c++_lab6

    expressions.hh #ifndef EXPRESSIONS_HH #define EXPRESSIONS_HH #include "environment.hh" #in ...

  2. cs11_c++_lab5待修改

    heap.hh #ifndef HEAP_HH #define HEAP_HH #include <iostream> #include <stdexcept> #includ ...

  3. cs11_c++_lab4b

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  4. cs11_c++_lab4a

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  5. cs11_c++_lab3

    Matrix.hh class Matrix { int row; int col; int *p; void copy(const Matrix &m); void clearup(); p ...

  6. cs11_c++_lab2

    Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...

  7. cs11_c++_lab1

    lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...

随机推荐

  1. pyside 移动窗口到屏幕中间

    由于计算机使用的尺寸不同,一台机器上设置的窗口位置固定参数往往会在另一台机器上表现欠佳 下面给出一个移动窗口到屏幕中心的示例 import sys from PySide import QtGui c ...

  2. Nginx-Lua模块的执行顺序

    一.nginx执行步骤 nginx在处理每一个用户请求时,都是按照若干个不同的阶段依次处理的,与配置文件上的顺序没有关系,详细内容可以阅读<深入理解nginx:模块开发与架构解析>这本书, ...

  3. jquery处理json对象

    在服务器端的php脚本: <?php $data['id'] = 1; $dat['name'] = "mary"; $da['red']= array_merge($dat ...

  4. js 中isArray

    es5中新加的方法Array.isArray是否是数值,低版本浏览器中可以这样修复 if (!Array.isArray) { Array.isArray = function(arg) { retu ...

  5. (转)php-curl响应慢(开发微信授权登陆时碰到的问题)

    最近在做一个php小项目的时候,发现curl调用微信的授权api.weixin.qq.com,经常是需要等待很久,但是有时候却很快. 刚开始以为是网络慢问题,没去注意.后面通过打上时间日志观察发现,慢 ...

  6. AlwaysOn添加高可用性自定义登陆用户的方法

    1.在主服务器添加自定义登陆用户,比如TestUser 2.在主服务器执行如下SQL,在master数据库创建存储过程sp_hexadecimal,sp_help_revlogin USE maste ...

  7. Nginx限速遇到的问题

    公司使用的是Nginx做文件服务器,最近服务器流量增大,老板提出要给每个客户端进行限速. 在Nginx中进行限速配置: http { limit_zone one $binary_remote_add ...

  8. C# 字符串 数据类型 判断 与特定规则验证

    验证字符串格式 1)判断字符串是否是常见数据类型,decimal,foalt,double,datetime,int等等 2)验证字符串符合特定规则    (1)邮箱地址,IP地址     (2)纯数 ...

  9. policy

    template <class Apolicy> class Host {   Apolicy direct_policy_use;   Apolicy <SomeInternalT ...

  10. Laravel 使用多个数据库的问题。

    这几天在使用Laravel 开发一个系统.这个系统连2个数据库.一个名为blog,一个名为center. center 数据库的作用是作为用户中心.可能会有其他几个系统相连,属于公用数据库.主要是用来 ...