问题描述:
统计英文文单词数,具体要求:对于给定的一片英文文章,统计单词的个数、关键词的个数、空格的个数
标点符号的个数,同时还能将原来的关键词替换成新的词语
1 #include2 #include 3 #include 4 #include 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 ='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 array_s;//存放文章里面的内容 53 vector 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++;//关键字数+1104 direc_keyword.push_back(array_s.size());105 106 }107 num_word++;//单词数+1108 109 array_s.push_back(s);110 }111 }112 cout<<"单词数为:"< < >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<<"文件操作出错"< result;//存放替换关键词后的文件146 result = array_s;147 int input;148 string str;149 cout<<"替换关键词:"< >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