统计英文文章单词数相关内容

问题描述:

统计英文文单词数,具体要求:对于给定的一片英文文章,统计单词的个数、关键词的个数、空格的个数

标点符号的个数,同时还能将原来的关键词替换成新的词语

  1 #include<iostream>
  2 #include<vector>
  3 #include<fstream>
  4 #include<string> 
  5 using namespace std;
  6 
  7 //关键词
  8 #define keyword 11
  9 string k[keyword]={"do","end","for","if","printf","return","then","while","int","float","double"};
 10 
 11 //标点符号
 12 #define punctuation  6
 13 char *pun[]={".",",","!","?","'",";"};
 14 
 15 //判断是否为关键字
 16 bool IsKey(string s){  
 17     bool flags = false;
 18     for(int i=0;i<keyword;i++)
 19     {
 20          if(!strcmp(k[i].c_str(),s.c_str()))
 21          {
 22              flags = true;
 23              break;
 24          }
 25     }
 26     return flags;
 27 }
 28  
 29 //判断是否是字母
 30 bool IsLetter(char ch){  
 31     if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))){   
 32         return true;
 33     }else    return false;
 34 } 
 35 
 36 //判断是否为标点符号
 37 bool IsPunctuation(char ch){  
 38     bool flags = false;
 39     for(int i=0;i<punctuation;i++)
 40     {
 41         if(ch==pun[i][0]){   
 42             flags =  true; 
 43             break;
 44         }
 45     }
 46     return flags;
 47 }
 48 int num_word = 0;//单词的个数
 49 int num_keyword = 0;//关键词的个数
 50 int num_punctuation = 0;//标点符号的个数
 51 int num_blank = 0;//空格的个数
 52 vector<string>array_s;//存放文章里面的内容
 53 vector<int>direc_keyword;//存储关键词的位置
 54 void analyse(ifstream &in)
 55 { 
 56     int i;
 57     string s="";
 58     char ch;
 59     string temp;
 60     while((in.get(ch)))
 61     {   
 62           
 63         if(ch==' '){//当前字符是空格
 64         
 65             num_blank++;//空格数+1
 66 
 67             temp="";
 68             temp +=ch;
 69             array_s.push_back(temp);
 70         }else if(IsPunctuation(ch)){    //标点符号处理
 71     
 72             num_punctuation++;//标点符号的个数+1
 73 
 74             temp="";
 75             temp +=ch;
 76             array_s.push_back(temp);
 77                 
 78         }else if(IsPunctuation(ch)){    //标点符号处理
 79     
 80             num_punctuation++;//标点符号的个数+1
 81 
 82             temp="";
 83             temp +=ch;
 84             array_s.push_back(temp);
 85 
 86         }else if(ch=='\n'){//换行 ,不做处理继续往下操作
 87             temp="";
 88             temp +=ch;
 89             array_s.push_back(temp);
 90         
 91         }else if(IsLetter(ch)){   //关键字、单词的处理  
 92     
 93             s="";  
 94             while(IsLetter(ch))
 95             {  
 96                  s+=ch;    
 97                  in.get(ch);    
 98              } 
 99              in.seekg(-1,ios::cur);//文件指针(光标)后退一个字节    
100              
101              if(IsKey(s)){ //判断是否为关键字 查询关键字表; 
102 
103                   num_keyword++;//关键字数+1
104                   direc_keyword.push_back(array_s.size());
105               
106               }
107               num_word++;//单词数+1
108              
109               array_s.push_back(s);
110         }
111     }
112     cout<<"单词数为:"<<num_word<<endl;
113     cout<<"关键词数为:"<<num_keyword<<endl;
114     cout<<"空格数为:"<<num_blank<<endl;
115     cout<<"标点符号个数为:"<<num_punctuation<<endl;
116     cout<<endl;
117 
118     
119 }
120   
121 int main() 
122 {  
123     ifstream in; 
124     ofstream out;
125     int i;
126     cout<<"输入文件路径:";
127     char a[30];
128     cin>>a;//输入文件路径
129     in.open(a,ios::in); 
130     //in.open("D://test.txt",ios::in); 
131 
132     if(in.is_open()){
133         analyse(in);   
134         in.close();
135         //system("pause");
136     }else cout<<"文件操作出错"<<endl; 
137     string file="";
138 
139     //把原文件存放到字符串中
140     for(i=0;i<array_s.size();i++)
141     {
142         file += array_s[i];
143     }
144 
145     vector<string>result;//存放替换关键词后的文件
146     result = array_s;
147     int input;
148     string str;
149     cout<<"替换关键词:"<<endl;
150     for(i=0;i<direc_keyword.size();i++)
151     {
152         cout<<"换掉关键词["<<result[direc_keyword[i]]<<"]?"<<endl;
153         cout<<"输入1:换掉;0:不换掉;[input]:";
154         cin>>input;
155         switch(input)
156         {
157         case 1:
158             cout<<"请输入你要替换成的单词:";
159             cin>>str;
160             result[direc_keyword[i]] = str;
161             break;
162         case 0:
163             break;
164         default:
165             break;
166         }
167     }
168 
169     string resultstr="";//存放关键词替换后的字符串
170     for(i=0;i<result.size();i++)
171     {
172         resultstr += result[i];
173     }
174     cout<<resultstr<<endl;//输出替换后的字符串
175 
176     resultstr.c_str();//字符串转化为字符数组
177 
178     out.open("D:/result.txt");//将某些关键词替换后的字符串存到文件中
179 
180     if(out.is_open())
181     {
182         for(i=0;i<resultstr.length();i++)
183         {
184             out<<(resultstr.c_str()[i]);
185 
186         }
187 
188     }else cout<<"文件操作出错"<<endl; 
189     return 0;
190 }

posted @ 2015-12-24 12:24  指间ゝ繁华初逝的格调  阅读(458)  评论(0编辑  收藏  举报