Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Nov 5, 2014
1 parent 56b8489 commit cf68ffe
Showing 1 changed file with 18 additions and 52 deletions.
70 changes: 18 additions & 52 deletions ch11/ex11_3_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,38 @@
#include <iostream>
#include <map>
#include <string>
#include <string.h>
#include <set>
#include <algorithm>

//! Exercise 11.4:
void
w_count_pro(std::map<std::string, int> &m);

void
ex11_3(void);

int main()
//! Exercise 11.4
void word_count_pro(std::map<std::string, int> &m)
{
std::map<std::string, int> m;
w_count_pro(m);
for(std::string word; std::cin >> word; /* */)
{
for(auto& ch : word) ch = std::tolower(ch);
std::remove_if(word.begin(), word.end(), ispunct);
++m[word];

return 0;
for (const auto &e : m)
std::cout << e.first << " : " << e.second <<"\n";
}
}



//! Exercise 11.3
void ex11_3()
{
std::map<std::string, std::size_t> word_count;
std::string word;
while(std::cin >> word && word != "@q")
for(std::string word; std::cin >> word; /* */)
{
++word_count[word];
for (const auto &e : word_count)
std::cout << e.first << " : " << e.second <<"\n";
for (const auto &elem : word_count)
std::cout << elem.first << " : " << elem.second <<"\n";
}

}

//! Exercise 11.4:
//! Extend your program to ignore case and punctuation.
//! For example, “example.” “example,” and “Example” should
//! all increment the same counter.
void
w_count_pro(std::map<std::string, int> &m)
int main()
{
std::string word;

while(std::cin >> word && word != "@q")
{
//! convert to lowercase
for(std::size_t i=0; i != word.size(); ++i)
{
word[i] = ::tolower(word[i]);
}


//! delete ',' or '.'
auto punc_iter = std::find_if(word.begin(), word.end(),[] (const char c)
{
return c == '.' || c == ',';
});

if(punc_iter != word.end())
word.erase(punc_iter);

//! read into the map m
++m[word];
std::map<std::string, int> m;
word_count_pro(m);

//! print contents of m
for (const auto &e : m)
std::cout << e.first << " : " << e.second <<"\n";
}
return 0;
}

0 comments on commit cf68ffe

Please sign in to comment.