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 ...
随机推荐
- 初学Laravel
之前一直用开tp和ot,本来觉得学会一个tp便可走遍天下,tp的确强大.但后来听到很多同行的同学说他们的公司都开始转型往lv走了,我的同学没有学过lv,然而公司给足时间去让他们去学.当然,缺人可能是占 ...
- openjudge 螺旋加密
/*======================================================================== 25:螺旋加密 总时间限制: 1000ms 内存限 ...
- Amazon验证码机器算法识别
Amazon验证码识别 在破解Amazon的验证码的时候,利用机器学习得到验证码破解精度超过70%,主要是训练样本不够,如果在足够的样本下达到90%是非常有可能的. update后,样本数为2800多 ...
- linux 持续构建(自动部署) 重启动tomcat或进程的脚本
#!/bin/sh TOMCAT_PATH=`dirname "$0"` echo "TOMCAT_PATH is /usr/local/tomcat" PID ...
- Tomcat启动脚本
记录一个比较好的tomcat启动脚本,截取<OneinStack>,修改如下两个参数即可用. 使用之前修改下面2个参数: #Location of JAVA_HOME (bin files ...
- es6 ... 表达
... 包含0个或n个属性, { ...state.counters [id]: state.counters[id] - 1 } 只改变其中的id相对应的属性,其他保持不变
- Can't connect to MySQL server on localhost (0)
配置双主的时候,由于一台始终连不上另一台. 于是我使用root账号远程登录查看,填入账户密码点击连接,结果就提示这样的错误. 一开始以为是权限的问题,于是就授与所有权,结果还是报一样的错. 重新创建一 ...
- vscode配置
默认的挺难看的 颜色主题换成 Monokai Dimmed 用户设置 // 将设置放入此文件中以覆盖默认设置 { "editor.fontFamily": "Monaco ...
- vc++>>Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enable
用VC来连接远程MYSQL时,出现如标题一样的错误,网上搜索了此错误产生的原因,最后自己找到了解决办法. 此错误产生的原因: 异常原因在于服务器端的密码管理协议陈旧,使用的是旧有的用户密码格式存储:但 ...
- mybatis传递参数到mapping.xml
第一种方案 ,通过序号传递 DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id ...