My exercise yesterday: Design by Using C++

来源:互联网 发布:爱古兰手机版中阿软件 编辑:程序博客网 时间:2024/04/25 12:43

Yesterday, I wrote the code as below to seperate every word in a file,and then display. I wrote the sentence more C++ functions,and feel good:)

#include "iostream"
#include "fstream"
#include "vector"
#include "algorithm"

using namespace std;
vector<char> vec_ch;
vector< vector<char> > vecStr;
void process(char* ch)
{
 bool bWordBegin = false;
 while(*ch)
 {
  if(*ch != '/x20' && *ch != '/x0D' && *ch != '/x0A' && *ch != '/x09') //not delimerator
  {
   bWordBegin = true;
   vec_ch.push_back(*ch);
  }
  else
  { 
   if(bWordBegin)
   {
    vecStr.push_back(vec_ch);
   // vec_ch.empty();
    vec_ch.clear();
    bWordBegin = false;
   }
  }
  
  ch++;
 }
 if(bWordBegin)
 {
  vecStr.push_back(vec_ch);
   vec_ch.clear();
 }
}
void Output_char(char ch)
{
 cout << ch;
}

void Output( vector<char> v1)
{
 //for_each(v1.begin(), v1.end(), Output_char);
 copy(v1.begin(), v1.end(), ostream_iterator<char>(cout));
 cout << endl;

}
void main(void)

 char ch[500];
 ch[0] = '/r';
 ch[1] = '/n';
 ifstream in("test.txt");
 while(in.getline(ch, 100))
 {
  process(ch);
 // cout << ch <<endl;
 }
 vector< vector<char> >::iterator iterStr;
// typedef  void (* fp)(void) pfp;
// pfp pfp1;
// pfp1 = Output;
 for_each(vecStr.begin(), vecStr.end(), Output); //pfp1);

 
}