essential C ++ 3.1 title

#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<map>
#include<set>

using namespace std;

//
//0.declare function
void Initialization_word( set<string> &);
void make_map(const set<string> &,map<string,int> &,ifstream &);
void user_query(const map<string,int> &);
void display_word_count(const map<string,int> &,ofstream &);


int main()
{
set<string> checkcharacter;
map<string,int> word_count;

//1.enter the text
ifstream in_file("/home/zzm/Desktop/test.txt");
ofstream o_file("/home/zzm/otext.map");

if(!in_file ||!o_file)
{cerr << "Unable to open file -- bailing out:\n";
return -1;
}

Initialization_word( checkcharacter);
make_map(checkcharacter,word_count,in_file);
user_query(word_count);
display_word_count(word_count, o_file );
}


//2.initialization of the checkcharacter;
void Initialization_word( set<string> &check_word)
{
static string Excludecharacter[25]={"the","and","but","that","then","are","been","can","a","could","did","for","of","had","have","him","his","her","its","is","were","whice","when","with","would"};

check_word.insert(Excludecharacter,Excludecharacter+25);}

//3.make map

void make_map(const set<string> &check_map,map<string,int> &word_count,ifstream &in_file)
{
string word;
while(in_file >>word)
{
if(check_map.count(word))
continue;
word_count[word]++;
}
}

//4.display
void user_query(const map<string,int> &word_map)
{
string search_word;
cout << "Please enter a word to search: q to quit";
cin >> search_word;

while(search_word != "q" && search_word.size())
{
map<string,int>:: const_iterator it;
if((it = word_map.find(search_word)) != word_map.end())
{cout << "Found!" << it->first
<< "occurs" << it ->second
<< "times.\n";}
else {cout << search_word
<< "was not found in text.\n";}
cout << "\nAnother search?(q to quit)";
cin >> search_word;
}
}

 

//5.display word_count

void display_word_count(const map<string,int> &word_map,ofstream &os )
{
map<string,int>::const_iterator iter = word_map.begin(),end_it = word_map.end();
while(iter != end_it)
{ os << iter->first << "("
<< iter->second << ")"
<< endl;
++iter;}
os << endl;
}

c ++ books after school program theme 3.1, running under linux.

Guess you like

Origin www.cnblogs.com/zzm1/p/12170308.html