词频统计

目标:

实现一个控制台程序,给定一段英文字符串,统计其中各个英文单词(4字符以上含4字符)的出现频率。 附加要求:读入一段文本文件,统计该文本文件中单词的频率。

读入文本文件上次的作业也做了所以这个不是太困难不像上次一样找资料学习,将文本中的数据移入数组中,由h数组记录相同的个数,用strcmp比较,相同则+1.单词之间用空格,.相隔。

#include<iostream>
#include<fstream>
using namespace std;
void main()
{   int n;
    char *a=new char[n];  
    int num=1;//单词个数
    ifstream in("D:\1.txt");
    if(!in) 

 cout<<"文件无法打开或不存在";
 }
     cin>>a[n]; //数据移入数组
     char *p=a; 
      while(*p!='') 
{      
     if(*p>='A'&&*p<='Z')   
{   
  *p=*p-'A'+'a';   
}  
  if(*p==' '||','||'.')  

     num++; 
   *p=''; 
 } 
 p++; 

int *h; 
h[0]=1;  
   for(int i=1;i<num;i++)
     h[i]=0;    
char *m=a;    
for(i=0;i<num-1;i++)
  {      
m+=strlen(m)+1;  
     char *z=a;  
  for(int j=0;j<num;j++) 
   {
     if(strcmp(m,z)==0)//比较单词  
 {    
  h[j]++;   
  break;   
  }    
else     
z+=strlen(z)+1;
   } 

m=a;
  for(i=0;i<num;i++) 
{  
if(h[i]&&*m!='')
cout<<m<<" 单词个数:"<<h[i]<<endl;  
m+=strlen(m)+1;
  } 
}

原文地址:https://www.cnblogs.com/ROUGH-TALE/p/5283290.html