⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main5.c

📁 C++ Primer(第三版)的随书源代码
💻 C
字号:
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>

#include <iostream.h>
#include <fstream.h>

#include <stddef.h>
#include <ctype.h>

typedef pair<short,short>           location;
typedef vector<location,allocator>  loc;
typedef vector<string,allocator>    text;
typedef pair<text*,loc*>            text_loc;

extern vector<string,allocator> *retrieve_text();
extern text_loc *separate_words( const vector<string,allocator>* );
extern void display_text_locations( text_loc* );
extern void filter_text( vector<string,allocator>*, string filter );
extern void strip_caps(  vector<string,allocator>* );
extern void suffix_text( vector<string,allocator>* );
extern void suffix_s( string& );
extern void display_map_text( map<string,loc*,less<string>,allocator>*);
extern map< string, loc*, less<string>, allocator >*
       build_word_map( const text_loc *text_locations );

int main() 
{
	vector<string,allocator>   *text_file      = retrieve_text();
        text_loc 		   *text_locations = separate_words( text_file );

        string filt_elems( "\",.;:!?)(" );
        filter_text( text_locations->first, filt_elems );
        suffix_text( text_locations->first );	
	strip_caps(  text_locations->first );

	map<string,loc*,less<string>,allocator> *text_map = build_word_map( text_locations );

	ostream_iterator< string > output( cout, "\n" );

	cout << "----------- about to generate text read --------------\n";
        copy( text_file->begin(), text_file->end(), output );

	cout << "-------- about to generate word and location data ----\n";
	display_map_text( text_map );

	return 0;
}

vector<string,allocator>*
retrieve_text()
{
	string file_name;
	
	cout << "please enter file name: ";
	cin  >> file_name;

	ifstream infile( file_name.c_str(), ios::in );
	if ( !infile ) {
		cerr << "oops! unable to open file "
		     << file_name << " -- bailing out!\n";
		exit( -1 );
	}
	else cout << "\n";

	vector<string,allocator> *lines_of_text = new vector<string,allocator>;
        string textline;

        typedef pair<string::size_type, int> stats;
        stats maxline;
	int   linenum = 0;

        while ( getline( infile, textline, '\n' ))
	{
		cout << "line read: " << textline << "\n";

		if ( maxline.first < textline.length() )
		{
		     maxline.first = textline.length();
		     maxline.second = linenum;
		}

		lines_of_text->push_back( textline );
		linenum++;
	}

	cout << "\n";
        cout << "number of lines: " 
             << lines_of_text->size() << "\n";

	cout << "maximum length: "  
	     << maxline.first << "\n";

        cout << "longest line: "    
	     << (*lines_of_text)[ maxline.second ] << "\n";
	
	return lines_of_text;
}

text_loc*
separate_words( const vector<string,allocator> *text_file )
{
        // ok: now have all the lines. wish to grab the
        // individual words: look for blanks:

        vector<string,allocator>   *words     = new vector<string,allocator>;
	vector<location,allocator> *locations = new vector<location,allocator>;

        for ( short line_pos = 0; line_pos < text_file->size(); line_pos++ )
        {
		short  word_pos = 0;
                string textline = (*text_file)[ line_pos ];

                cout << "textline: " << textline << endl;

                string::size_type eol = textline.length();
                string::size_type pos = 0, prev_pos = 0;

                while (( pos = textline.find_first_of( ' ', pos )) != string::npos )
                {
                        cout << "eol: "  << eol << " "
			     << "pos: "  << pos << " "
			     << "line: " << line_pos << " "
			     << "word: " << word_pos << " "
                             << "substring: " 
                             << textline.substr( prev_pos, pos-prev_pos ) 
			     << "\n";

                        words->push_back( textline.substr( prev_pos, pos - prev_pos ));
			locations->push_back( make_pair( line_pos, word_pos ));

                        word_pos++; pos++; prev_pos = pos;
                }

	        cout << "last word on line substring: " 
                     << textline.substr( prev_pos, pos-prev_pos ) 
		     << "\n";

                words->push_back( textline.substr( prev_pos, pos - prev_pos ));
		locations->push_back( make_pair( line_pos, word_pos ));
        }
	
        return new text_loc( words, locations );
}

void 
display_text_locations( text_loc *text_locations )
{
        vector<string,allocator>   *text_words     = text_locations->first;
        vector<location,allocator> *text_locs      = text_locations->second;

        register int elem_cnt = text_words->size();

        if ( elem_cnt != text_locs->size() )
        {
	     cerr << "oops! internal error: word and position vectors "
		  << "are of unequal size\n"
		  << "words: " << elem_cnt << " "
		  << "locs: "  << text_locs->size() 
		  << " -- bailing out!\n";
	     exit( -2 );
	}

        for ( int ix = 0; ix < elem_cnt; ix++ ) 
        {
		cout << "word: " << (*text_words)[ ix ] << "\t"
		     << "location: (" 
		     << (*text_locs)[ix].first  << ","
		     << (*text_locs)[ix].second << ")"
		     << "\n";
	}

        cout << endl;
}

void 
filter_text( vector<string,allocator> *words, string filter )
{
	vector<string,allocator>::iterator iter = words->begin();
	vector<string,allocator>::iterator iter_end = words->end();

        if ( ! filter.size() )
             filter.insert( 0, "\".," );

        cout << "filter elements: " << filter << endl;

	while ( iter != iter_end )
	{
		cout << "filter_text: " << *iter << endl; 

                string::size_type pos = 0;
                while (( pos = (*iter).find_first_of( filter, pos )) != string::npos )
                {
                        cout << "found! : pos: " 
			     << pos << "\t" 
			     << (*iter)[pos] << endl;

			// this is wrong: erases from pos to npos
                        // (*iter).erase(pos);
                        (*iter).erase(pos,1);

			cout << "after: " << *iter << endl;
                }

		cout << "finished with word: " << *iter << endl;

		iter++;
	}
}

void
suffix_text( vector<string,allocator> *words )
{
        vector<string,allocator>::iterator iter = words->begin();
        vector<string,allocator>::iterator iter_end = words->end();

        while ( iter != iter_end )
        {
                cout << "suffix_text: " << *iter << endl;

		// if 3 or less characters, let it be
		if ( (*iter).size() <= 3 ) {
		     iter++; 
		     continue;
                }

		if ( (*iter)[ (*iter).size()-1 ] == 's' )
		     suffix_s( *iter );

		// additional suffix handling goes here ...

                iter++;
        }
}

void
suffix_s( string &word )
{
	cout << "suffix_s -- word passed in: " << word << endl;

	string::size_type spos = 0;
	string::size_type pos3 = word.size()-3;

        // "ous", "ss", "is"
	string suffixes( "oussis" );

        if ( ! word.compare( pos3, 3, suffixes, spos, 3 ) ||
             ! word.compare( pos3+1, 2, suffixes, spos+2, 2 ) ||
             ! word.compare( pos3+1, 2, suffixes, spos+4, 2 ))
        {
		cout << "suffix_s: found immutable suffix: "
		     << word << endl;
		return;
	}

	string ies( "ies" );
        if ( ! word.compare( pos3, 3, ies ))
	{
	     word.replace( pos3, 3, 1, 'y' );
	     cout << "suffix_s -- word returned: " << word << endl;

	     return; 
	}

	// erase ending 's'
	word.erase( pos3+2 );
	cout << "suffix_s -- word returned: " << word << endl;
}

void
strip_caps( vector<string,allocator> *words )
{
        vector<string,allocator>::iterator iter = words->begin();
        vector<string,allocator>::iterator iter_end = words->end();

	string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );

        while ( iter != iter_end )
        {
                cout << "strip_caps: " << *iter << endl;

                string::size_type pos = 0;
                while (( pos = (*iter).find_first_of( caps, pos )) != string::npos )
                {
                        cout << "found! : pos: "
                             << pos << "\t"
                             << (*iter)[pos] << endl;

                        (*iter)[ pos ] = tolower( (*iter)[pos] );

                        cout << "after: " << *iter << endl;
                }

                cout << "finished with word: " << *iter << endl;

                iter++;
        }
}

map< string, loc*, less<string>, allocator >*
     build_word_map( const text_loc *text_locations )
{
     map< string, loc*, less<string>, allocator > *word_map = new map< string, loc*, less<string>, allocator >;
     typedef map<string,loc*, less<string>, allocator>::value_type value_type;

     // iterate through the the words, entering the key/pair

     vector<string,allocator>   *text_words = text_locations->first;
     vector<location,allocator> *text_locs  = text_locations->second;

     register int elem_cnt = text_words->size();
     for ( int ix = 0; ix < elem_cnt; ++ix )
         {
                cout << "word: " << (*text_words)[ ix ] << "\t"
                     << "location: ("
                     << (*text_locs)[ix].first  << ","
                     << (*text_locs)[ix].second << ")"
                     << "\n";

                if ( ! word_map->count((*text_words)[ix] ))
                {  // not present, add it:
                   cout << "adding word: " << (*text_words)[ix] << endl;
                   loc *ploc = new vector<location,allocator>;
                   ploc->push_back( (*text_locs)[ix] );
                   word_map->insert( value_type( (*text_words)[ix], ploc ));
                }
                else (*word_map)[(*text_words)[ix]]->push_back( (*text_locs)[ix] );

                // enter word -- check if new or already entered.
                // if new, provide key/value -- allocate new instance
                // if already has entry, just add new value
         }

        cout << endl;
	return word_map;
}

void 
display_map_text( map<string,loc*,less<string>,allocator> *text_map )
{
	typedef map<string,loc*,less<string>,allocator> map_text;
	map_text::iterator iter = text_map->begin(), iter_end = text_map->end();
	
	while ( iter != iter_end ) {
		cerr << "word: " << (*iter).first << " ";

		int           loc_cnt = 0;
		loc          *text_locs = (*iter).second;
		loc::iterator liter     = text_locs->begin(), 
		  	      liter_end = text_locs->end();

		while ( liter != liter_end ) 
		{
			if ( loc_cnt ) 
			     cerr << ","; 
			else ++loc_cnt;
 
			// cerr << "(" << (*liter).first 
			//      << "," << (*liter).second << ")";
			cerr << "(" << (*liter).first 
			     << "," << (*liter).second << ")";

			++liter;
		}
		
		cerr << ")\n";
		++iter;
	}

        cerr << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -