统计文本行单词数

前天同学请我帮她同学用C++写个程序,主要用于统计文本行的字符数,单词数和行数。做得过程中收获颇多,成品亦简洁易懂,故将此发表,大家一起来评论....

 

   1:  #include<iostream>
   2:  #include<string>
   3:  using namespace std;
   4:   
   5:  bool rowCount(int &Chars,int &Words){   //计算单行文本狭义字符数、单词数并累加到总字符数和单词数中
   6:      string str;            //用于存储单行文本
   7:      getline(cin,str);    //获得整行内容,包括空格
   8:      int rowWords=0;        //用于存储单行中单词数
   9:   
  10:      if((str[0]>='a'&&str[0]<='z'||str[0]>='A'&&str[0]<='Z'||str[0]>='0'&&str[0]<='9')&&str.length()==1) {
  11:          str.append(".");
  12:      } //识别并规范只有一个狭义字符(不包括分隔符)的文本行
  13:   
  14:      for(int i=0;i<str.length();i++) {
  15:          if((!(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z'||str[i]>='0'&&str[i]<='9'))) {
  16:              i==0&&--Chars+5||rowWords++;
  17:          } //判断并计算单行文本中单词数:若某行第一个字符为分隔符,则减去其在字符数中的占位
  18:   
  19:          else if(i==str.length()-1) {
  20:              str.append(".");
  21:          } //识别并规范末尾无分隔符的文本行
  22:      }
  23:   
  24:      Chars+=str.length()-rowWords;
  25:      Words+=rowWords;
  26:      //返回对该文本行是否为空行的判断值:若为空行,返回True
  27:      return str.empty();
  28:  }
  29:   
  30:  void main(){
  31:      //分别用于存储所有文本行的狭义字符数(不包括分隔符),单词数,行数
  32:      int Chars=0,Words=0,Rows=0;      
  33:   
  34:      cout<<"\n请输入多行文本,以空行结束:\n";
  35:      
  36:      while(!rowCount(Chars,Words)) {    
  37:          Rows++;    
  38:      } //循环调用rowCount函数,并通过该函数返回值判断是否结束
  39:   
  40:      cout<<"字符数:"<<Chars<<endl
  41:          <<"单词数:"<<Words<<endl
  42:          <<"行数:"<<Rows<<endl;
  43:  }

 

继发上文后,同学的同学表示不太理解类,故我又用char[]仿照上文将rowCount()方法重写了一遍 完整程序如下:

 

   1:  /**
   2:  *Name:0621String.cpp
   3:  *Author:San
   4:  *Date:2011.6.20
   5:  *Description:通过输入多行文本,以空行结束,改程序实现统计所有文本中的狭义字符数(不包括分隔符),单词数,行数。(本程序使用字符数组 char[])。
   6:  **/
   7:   
   8:  #include<iostream>
   9:  #include<cstring>
  10:  using namespace std;
  11:   
  12:  bool rowCount(int &Chars,int &Words){
  13:      char sptr[37001];            //用于存储单行文本
  14:      cin.getline(sptr,37000);    //获得整行内容,包括空格(最多支持37000个字符)
  15:      int rowWords=0,length=-1;     //分别用于存储单行中单词数和文本长度          
  16:   
  17:      if((sptr[0]>='a'&&sptr[0]<='z'||sptr[0]>='A'&&sptr[0]<='Z'||sptr[0]>='0'&&sptr[0]<='9')&&sptr[1]==NULL) {
  18:          strcat(sptr,".");
  19:      } //识别并规范只有一个狭义字符(不包括分隔符)的文本行    
  20:   
  21:      while(sptr[++length]!=NULL);    //获取sptr的内容长度
  22:   
  23:      for(int i=0;sptr[i]!=NULL&&length!=0;i++) {
  24:          if(!(sptr[i]>='a'&&sptr[i]<='z'||sptr[i]>='A'&&sptr[i]<='Z'||sptr[i]>='0'&&sptr[i]<='9')) {
  25:              i==0&&--Chars+5||rowWords++;
  26:          } //判断并计算单行文本中单词数:若某行第一个字符为分隔符,则减去其在字符数中的占位
  27:          else if(i==length-1) {
  28:              strcat(sptr,".");
  29:          } //识别并规范末尾无分隔符的文本行
  30:      }
  31:      sptr[length]==NULL||length++;        //使length的值与sptr的内容长度保持一致
  32:      Chars+=length-rowWords;
  33:      Words+=rowWords;
  34:      return sptr[0]==NULL;                //返回对该文本行是否为空行的判断值:若为空行,返回True
  35:   
  36:  }
  37:   
  38:  void main(){
  39:      //分别用于存储所有文本行的狭义字符数(不包括分隔符),单词数,行数
  40:      int Chars=0,Words=0,Rows=0;
  41:      cout<<"\n请输入多行文本,以空行结束:\n";
  42:      while(!rowCount(Chars,Words)) {
  43:          Rows++;
  44:      } //循环调用rowCount函数,并通过该函数返回值判断是否结束
  45:   
  46:      cout<<"字符数:"<<Chars<<endl
  47:          <<"单词数:"<<Words<<endl
  48:          <<"行数:"<<Rows<<endl;
  49:  }
posted @ 2011-06-22 03:58  Wave San  阅读(813)  评论(11编辑  收藏  举报