wangchao.net.cn
| 導購 | 订阅该频道 | 在线投稿
分享到:
 
 
當前位置: 王朝網路 >> c/c++ >> 讀取英文單詞,按順序排列(C++學習題目)

讀取英文單詞,按順序排列(C++學習題目)

字體: ||
本文來源: 互聯網
  讀取英文單詞,按順序排列(C++學習題目)

  題目:

  1. Implement a function that counting word frequency. It reads an English article from an user-specified txt file(article.txt) and counts their number. Those exclusive words should not be counted. Output the words and counts to 2 files. One(3_1_1out.txt) is in lexicographic order, and the other (3_1_2out.txt) is in descending frequency order.

  – Exclusive words:

  – Using map

  代碼:

  #pragma warning(disable:4786)

  #include <string>

  #include <iostream>

  #include <fstream>

  #include <functional>

  #include <algorithm>

  #include <vector>

  #include <map>

  #include <set>

  using namespace std;

  typedef map<string, int>::value_type sival_type;

  vector<string> *retrieve_text(string file_name)

  {

   ifstream artcile_file( file_name.c_str(), ios::in );

   if (!artcile_file) {

   cout << "Conn't open " << file_name.c_str() << " !" << endl;

   exit (1);

   }

   vector<string> *lines_of_text = new vector<string>;

   string textline;

   while ( getline(artcile_file, textline, '\n'))

   {

   //cout << " " << textline << '\n';

   lines_of_text->push_back(textline);

   }

   return lines_of_text;

  }

  void strip_caps( vector<string> *text_file )

  {

   string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );

  

   vector<string>::iterator iter = text_file->begin();

   for ( ; iter != text_file->end(); ++iter )

   {

   string::size_type pos = 0;

   while ( (pos = (*iter).find_first_of( caps, pos ))

   != string::npos )

   {

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

   }

   } //end of for

  }

  vector<string> *separate_words( const vector<string> *text_file )

  {

   string filter("abcdefghijklmnopqrstuvwxy");

  

   //包含獨立的單詞集合

   vector<string> *words = new vector<string>;

  

   short line_pos = 0;

   for ( ; line_pos <text_file->size(); ++line_pos )

   {

   string textline = (*text_file)[line_pos];

  

   // 用來遍曆所有的字母

   string::size_type pos = 0;

   // 單詞的開始位置

   string::size_type prev_pos = textline.find_first_of(filter);

   // 單詞末尾的下一空格位置

   string::size_type temp_pos = textline.find_first_of(filter);

  

   // 一個小開關,其值爲TURE時,prev_pos指向單詞開始的位置

   bool onoff = false;

  

   while ( (pos = textline.find_first_of(filter, pos))

   != string::npos )

   {

   if ( onoff )

   {

   prev_pos = temp_pos - 1;

   // 將onoff值改爲false,使單詞開始的位置不會改變

   onoff = false;

   }

  

   ++pos;

  

   if ( (pos - temp_pos) != 1 )

   {

   // 爲下一次的賦值做准備

   onoff = true;

   // 將分離出的單詞輸入words

   words->push_back(

   textline.substr( prev_pos, temp_pos - prev_pos ));

   }

  

   temp_pos = pos;

   } // end of while

  

   // 輸入最後一個單詞,除非這一段沒有找到任何字母

   if ( prev_pos != string::npos )

   {

   words->push_back(

   textline.substr( prev_pos, temp_pos - prev_pos ));

   }

   } // end of for

  

   return words;

  }

  map< string, int > *appear_total( const vector<string> *words )

  {

   // 創建單詞排除集合

   set<string> exclusion_set;

   ifstream exclusion_file( "pkg95.txt", ios::in );

   if (!exclusion_file) {

   cout << "Conn't open pkg95.txt !" << endl;

   exit (1);

   }

   string textline;

   while ( getline(exclusion_file, textline, '\n'))

   {

   //cout << " " << textline << '\n';

   exclusion_set.insert(textline);

   }

   map<string, int> *word_map = new map<string, int>;

  

   // 開始向word_map中記錄數據

   vector<string>::const_iterator iter = words->begin();

   for ( ; iter != words->end(); ++iter )

   {

   // 如果少于3個字符或在排除集合中存在,則不輸入到map中

   if ( (*iter).size() < 3 || exclusion_set.count( *iter ) )

   {

   continue;

   }

  

   // 如果count()返回0,則單詞不存在,加入它

   if ( !word_map->count(*iter) )

   {

   word_map->insert( sival_type( (*iter), 1 ) );

   }

   else

   {

   //將單詞的出現次數加1

   (*word_map)[ (*iter) ] += 1;

   }

   } //end of for

  

   return word_map;

  }

  multimap< int, string, greater<int> > * multimap_total( map<string, int> *text_map )

  {

   multimap<int, string, greater<int> > *word_map =

   new multimap< int, string, greater<int> >;

   map< string, int >::iterator map_siter = text_map->begin();

   for ( ; map_siter != text_map->end(); ++map_siter )

   {

   word_map->insert(make_pair((*map_siter).second, (*map_siter).first));

   }

   {

   string ofile("3_1_2out.txt");

  

   ofstream outfile( ofile.c_str() );

   if (!outfile)

   {

   cerr << "error: unable to open output file: "

   << ofile << endl;

   }

  

   multimap< int, string, greater<int> >::iterator map_siter = word_map->begin();

  

   for ( ; map_siter != word_map->end(); ++map_siter )

   {

   outfile << (*map_siter).second;

  

   for ( int n = 0; n < 15 - (*map_siter).second.size(); ++n )

   {

   outfile << ' ';

   }

  

   outfile << "出現 " << (*map_siter).first << "\t次" << endl;

   } // end of for

  

   cout << "程序已將處理結果寫入3_1_2out.txt,該文件保存在當前目錄"

   << endl;

   }

   return word_map;

  }

  void map_output( map<string, int> *text_map )

  {

   string ofile("3_1_1out.txt");

  

   ofstream outfile( ofile.c_str() );

   if (!outfile)

   {

   cerr << "error: unable to open output file: "

   << ofile << endl;

   }

  

   map< string, int >::iterator map_siter = text_map->begin();

   for ( ; map_siter != text_map->end(); ++map_siter )

   {

   outfile << (*map_siter).first;

  

   for ( int n = 0; n < 15 - (*map_siter).first.size(); ++n )

   {

   outfile << ' ';

   }

  

   outfile << "出現 " << (*map_siter).second << "\t次" << endl;

   } // end of for

  

   cout << "程序已將處理結果寫入3_1_1out.txt,該文件保存在當前目錄"

   << endl;

  }

  int main()

  {

  

   vector<string> *text_file = retrieve_text("article.txt");

   strip_caps(text_file);

   vector<string> *words = separate_words(text_file);

   map< string, int > *text_map = appear_total(words);

   map_output( text_map );

   multimap_total(text_map);

  

   return 0;

  }

  程序執行結果

  3_1_1out.txt

  article 出現 2 次

  counted 出現 1 次

  counting 出現 1 次

  counts 出現 2 次

  descending 出現 1 次

  english 出現 1 次

  exclusive 出現 1 次

  file 出現 1 次

  files 出現 1 次

  frequency 出現 2 次

  function 出現 1 次

  implement 出現 1 次

  lexicographic 出現 1 次

  output 出現 1 次

  reads 出現 1 次

  specified 出現 1 次

  txt 出現 4 次

  user 出現 1 次

  word 出現 1 次

  words 出現 2 次

  3_1_2out.txt

  txt 出現 4 次

  article 出現 2 次

  counts 出現 2 次

  frequency 出現 2 次

  words 出現 2 次

  counted 出現 1 次

  counting 出現 1 次

  descending 出現 1 次

  english 出現 1 次

  exclusive 出現 1 次

  file 出現 1 次

  files 出現 1 次

  function 出現 1 次

  implement 出現 1 次

  lexicographic 出現 1 次

  output 出現 1 次

  reads 出現 1 次

  specified 出現 1 次

  user 出現 1 次

  word 出現 1 次

  附PKG95.TXT 文件內容:

  different

  necessary

  need

  needed

  needing

  newest

  next

  no

  nobody

  non

  none

  not

  nothing

  now

  nowhere

  of

  off

  often

  new

  old

  older

  oldest

  on

  once

  one

  only

  open

  again

  among

  already

  about

  above

  against

  alone

  after

  also

  although

  along

  always

  an

  across

  and

  another

  ask

  asking

  asks

  backed

  away

  should

  show

  came

  all

  almost

  before

  began

  back

  backing

  be

  became

  because

  becomes

  been

  at

  behind

  being

  best

  better

  between

  big

  showed

  ended

  ending

  both

  but

  by

  asked

  backs

  can

  cannot

  number

  numbers

  case

  few

  find

  finds

  cases

  clearly

  her

  herself

  come

  could

  did

  here

  beings

  fact

  far

  felt

  become

  first

  for

  four

  from

  full

  fully

  furthers

  gave

  general

  generally

  get

  gets

  gives

  facts

  go

  going

  good

  goods

  certain

  certainly

  clear

  great

  greater

  greatest

  group

  grouped

  grouping

  groups

  got

  has

  have

  having

  he

  further

  furthered

  had

  furthering

  itself

  faces

  highest

  him

  himself

  his

  how

  however

  if

  important

  interests

  into

  is

  it

  its

  anyone

  anything

  anywhere

  are

  area

  areas

  around

  as

  seconds

  see

  seem

  seemed

  seeming

  seems

  sees

  right

  several

  shall

  she

  enough

  even

  evenly

  over

  part

  parted

  parting

  parts

  per

  down

  place

  places

  point

  pointed

  pointing

  points

  possible

  present

  presented

  presenting

  ends

  high

  mrs

  much

  must

  my

  myself

  presents

  down

  problem

  problems

  put

  puts

  quite

  will

  with

  within

  rather

  really

  room

  rooms

  said

  same

  right

  showing

  shows

  side

  sides

  since

  small

  smaller

  smallest

  so

  some

  somebody

  someone

  something

  somewhere

  state

  states

  such

  sure

  take

  taken

  than

  that

  the

  their

  then

  there

  therefore

  these

  thought

  thoughts

  three

  through

  thus

  to

  today

  together

  too

  took

  toward

  turn

  turned

  turning

  turns

  two

  still

  under

  until

  up

  others

  upon

  us

  use

  used

  uses

  very

  want

  wanted

  wanting

  wants

  was

  way

  we

  well

  wells

  went

  were

  what

  when

  where

  whether

  which

  while

  who

  whole

  year

  years

  yet

  you

  everyone

  everything

  everywhere

  young

  younger

  youngest

  your

  yours

  ever

  works

  every

  everybody

  face

  other

  our

  out

  just

  interesting

  high

  might

  keep

  keeps

  give

  given

  higher

  kind

  knew

  know

  known

  knows

  large

  largely

  last

  later

  latest

  least

  less

  needs

  never

  newer

  let

  lets

  like

  likely

  long

  high

  longer

  longest

  made

  make

  making

  man

  many

  may

  me

  member

  members

  men

  more

  in

  interest

  interested

  most

  mostly

  mr

  opened

  opening

  new

  opens

  or

  perhaps

  order

  ordered

  ordering

  orders

  differ

  differently

  do

  does

  done

  downed

  downing

  downs

  they

  thing

  things

  think

  thinks

  this

  those

  ways

  why

  without

  work

  worked

  working

  would

  during

  each

  early

  either

  end

  though

  still

  whose

  saw

  say

  says

  them

  second

  any

  anybody
 
熱門點閱
 
 
 
 
標籤: C++單詞學習按順序排列英文讀取題目
讀取英文單詞,按順序排列(C++學習題目) 題目: 1. Implement a function that counting word frequency. It reads an English article from an user-specified txt file(article.txt) and counts their number. Those exclusive words should not be counted. Output the words and counts to 2 files. One(3_1_1out.txt) is in lexicographic order, and the other (3_1_2out.txt) is in descending frequency order. – Exclusive words: – Using map 代碼: #pragma warning(disable:4786) #include <string> #include <iostream> #include <fstream> #include <functional> #include <algorithm> #include <vector> #include <map> #include <set> using namespace std; typedef map<string, int>::value_type sival_type; vector<string> *retrieve_text(string file_name) { ifstream artcile_file( file_name.c_str(), ios::in ); if (!artcile_file) { cout << "Conn't open " << file_name.c_str() << " !" << endl; exit (1); } vector<string> *lines_of_text = new vector<string>; string textline; while ( getline(artcile_file, textline, '\n')) { //cout << " " << textline << '\n'; lines_of_text->push_back(textline); } return lines_of_text; } void strip_caps( vector<string> *text_file ) { string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ); vector<string>::iterator iter = text_file->begin(); for ( ; iter != text_file->end(); ++iter ) { string::size_type pos = 0; while ( (pos = (*iter).find_first_of( caps, pos )) != string::npos ) { (*iter)[ pos ] = tolower( (*iter)[pos] ); } } // end of for } vector<string> *separate_words( const vector<string> *text_file ) { string filter("abcdefghijklmnopqrstuvwxy"); // 包含獨立的單詞集合 vector<string> *words = new vector<string>; short line_pos = 0; for ( ; line_pos <text_file->size(); ++line_pos ) { string textline = (*text_file)[line_pos]; // 用來遍曆所有的字母 string::size_type pos = 0; // 單詞的開始位置 string::size_type prev_pos = textline.find_first_of(filter); // 單詞末尾的下一空格位置 string::size_type temp_pos = textline.find_first_of(filter); // 一個小開關,其值爲TURE時,prev_pos指向單詞開始的位置 bool onoff = false; while ( (pos = textline.find_first_of(filter, pos)) != string::npos ) { if ( onoff ) { prev_pos = temp_pos - 1; // 將onoff值改爲false,使單詞開始的位置不會改變 onoff = false; } ++pos; if ( (pos - temp_pos) != 1 ) { // 爲下一次的賦值做准備 onoff = true; // 將分離出的單詞輸入words words->push_back( textline.substr( prev_pos, temp_pos - prev_pos )); } temp_pos = pos; } // end of while // 輸入最後一個單詞,除非這一段沒有找到任何字母 if ( prev_pos != string::npos ) { words->push_back( textline.substr( prev_pos, temp_pos - prev_pos )); } } // end of for return words; } map< string, int > *appear_total( const vector<string> *words ) { // 創建單詞排除集合 set<string> exclusion_set; ifstream exclusion_file( "pkg95.txt", ios::in ); if (!exclusion_file) { cout << "Conn't open pkg95.txt !" << endl; exit (1); } string textline; while ( getline(exclusion_file, textline, '\n')) { //cout << " " << textline << '\n'; exclusion_set.insert(textline); } map<string, int> *word_map = new map<string, int>; // 開始向word_map中記錄數據 vector<string>::const_iterator iter = words->begin(); for ( ; iter != words->end(); ++iter ) { // 如果少于3個字符或在排除集合中存在,則不輸入到map中 if ( (*iter).size() < 3 || exclusion_set.count( *iter ) ) { continue; } // 如果count()返回0,則單詞不存在,加入它 if ( !word_map->count(*iter) ) { word_map->insert( sival_type( (*iter), 1 ) ); } else { // 將單詞的出現次數加1 (*word_map)[ (*iter) ] += 1; } } // end of for return word_map; } multimap< int, string, greater<int> > * multimap_total( map<string, int> *text_map ) { multimap<int, string, greater<int> > *word_map = new multimap< int, string, greater<int> >; map< string, int >::iterator map_siter = text_map->begin(); for ( ; map_siter != text_map->end(); ++map_siter ) { word_map->insert(make_pair((*map_siter).second, (*map_siter).first)); } { string ofile("3_1_2out.txt"); ofstream outfile( ofile.c_str() ); if (!outfile) { cerr << "error: unable to open output file: " << ofile << endl; } multimap< int, string, greater<int> >::iterator map_siter = word_map->begin(); for ( ; map_siter != word_map->end(); ++map_siter ) { outfile << (*map_siter).second; for ( int n = 0; n < 15 - (*map_siter).second.size(); ++n ) { outfile << ' '; } outfile << "出現 " << (*map_siter).first << "\t次" << endl; } // end of for cout << "程序已將處理結果寫入3_1_2out.txt,該文件保存在當前目錄" << endl; } return word_map; } void map_output( map<string, int> *text_map ) { string ofile("3_1_1out.txt"); ofstream outfile( ofile.c_str() ); if (!outfile) { cerr << "error: unable to open output file: " << ofile << endl; } map< string, int >::iterator map_siter = text_map->begin(); for ( ; map_siter != text_map->end(); ++map_siter ) { outfile << (*map_siter).first; for ( int n = 0; n < 15 - (*map_siter).first.size(); ++n ) { outfile << ' '; } outfile << "出現 " << (*map_siter).second << "\t次" << endl; } // end of for cout << "程序已將處理結果寫入3_1_1out.txt,該文件保存在當前目錄" << endl; } int main() { vector<string> *text_file = retrieve_text("article.txt"); strip_caps(text_file); vector<string> *words = separate_words(text_file); map< string, int > *text_map = appear_total(words); map_output( text_map ); multimap_total(text_map); return 0; } 程序執行結果 3_1_1out.txt article 出現 2 次 counted 出現 1 次 counting 出現 1 次 counts 出現 2 次 descending 出現 1 次 english 出現 1 次 exclusive 出現 1 次 file 出現 1 次 files 出現 1 次 frequency 出現 2 次 function 出現 1 次 implement 出現 1 次 lexicographic 出現 1 次 output 出現 1 次 reads 出現 1 次 specified 出現 1 次 txt 出現 4 次 user 出現 1 次 word 出現 1 次 words 出現 2 次 3_1_2out.txt txt 出現 4 次 article 出現 2 次 counts 出現 2 次 frequency 出現 2 次 words 出現 2 次 counted 出現 1 次 counting 出現 1 次 descending 出現 1 次 english 出現 1 次 exclusive 出現 1 次 file 出現 1 次 files 出現 1 次 function 出現 1 次 implement 出現 1 次 lexicographic 出現 1 次 output 出現 1 次 reads 出現 1 次 specified 出現 1 次 user 出現 1 次 word 出現 1 次 附PKG95.TXT 文件內容: different necessary need needed needing newest next no nobody non none not nothing now nowhere of off often new old older oldest on once one only open again among already about above against alone after also although along always an across and another ask asking asks backed away should show came all almost before began back backing be became because becomes been at behind being best better between big showed ended ending both but by asked backs can cannot number numbers case few find finds cases clearly her herself come could did here beings fact far felt become first for four from full fully furthers gave general generally get gets gives facts go going good goods certain certainly clear great greater greatest group grouped grouping groups got has have having he further furthered had furthering itself faces highest him himself his how however if important interests into is it its anyone anything anywhere are area areas around as seconds see seem seemed seeming seems sees right several shall she enough even evenly over part parted parting parts per down place places point pointed pointing points possible present presented presenting ends high mrs much must my myself presents down problem problems put puts quite will with within rather really room rooms said same right showing shows side sides since small smaller smallest so some somebody someone something somewhere state states such sure take taken than that the their then there therefore these thought thoughts three through thus to today together too took toward turn turned turning turns two still under until up others upon us use used uses very want wanted wanting wants was way we well wells went were what when where whether which while who whole year years yet you everyone everything everywhere young younger youngest your yours ever works every everybody face other our out just interesting high might keep keeps give given higher kind knew know known knows large largely last later latest least less needs never newer let lets like likely long high longer longest made make making man many may me member members men more in interest interested most mostly mr opened opening new opens or perhaps order ordered ordering orders differ differently do does done downed downing downs they thing things think thinks this those ways why without work worked working would during each early either end though still whose saw say says them second any anybody
󰈣󰈤
 
  免責聲明:本文僅代表作者個人觀點,與王朝網路無關。王朝網路登載此文出於傳遞更多信息之目的,並不意味著贊同其觀點或證實其描述,其原創性以及文中陳述文字和內容未經本站證實,對本文以及其中全部或者部分內容、文字的真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,並請自行核實相關內容。
 
 
 
王朝美圖
國際車展_外場車模
最美诠釋_漂亮女孩
氣質模特_美麗絕倫
美麗小清新時尚女生
Endless Rain
暮色中的西湖
落霞椰風
勇登黃石寨
 
2006-01-09 11:13:17  简体版  手機版  編輯
 轉載本文
 UBB代碼 HTML代碼
複製到剪貼板...
 
 
 
 
 
 
 
 
 
 
© 2005- 王朝網路 版權所有