zoukankan      html  css  js  c++  java
  • 词频统计器

    功能:统计一篇英文txt文章中的单词出现次数

    代码如下:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 #include<fstream>
     5 #include<string>
     6 #include<map> 
     7 #include <iomanip>
     8 using namespace std;
     9 int main()
    10 {
    11     FILE *fp1;
    12     char text[1024];
    13     fp1=fopen("d:\2.txt","r");
    14     int i;
    15     map<string,int>my_map;
    16     while(fgets(text,1024,fp1)!=NULL)
    17     {   i=0;
    18         while(text[i]!='')
    19         {
    20             char s[30];
    21             int j=0;
    22             while((text[i]>='a'&&text[i]<='z')||(text[i]>='A'&&text[i]<='Z'))
    23             {
    24                 if(text[i]>='A'&&text[i]<='Z')
    25                 text[i]+='a'-'A';
    26                 s[j++]=text[i++];    
    27             }
    28             s[j]='';
    29             my_map[s]++;
    30             if(text[i]=='') break;
    31             else
    32             i++;
    33         }
    34         
    35     }
    36     fclose(fp1);
    37     map<string,int>::iterator it;
    38     for(it=++my_map.begin(),i=1;it!=my_map.end();it++,i++)
    39     {
    40         cout<<left;
    41         cout<<setw(10)<<it->first<<setw(10)<<it->second;
    42         if(i%4==0)
    43         cout<<'
    ';
    44     }
    45     cout<<'
    ';    
    46     return 0;
    47 }

    文章:

    输出结果:

     

  • 相关阅读:
    洛谷p1056
    __int64
    杭电2057
    4.4清北学堂Day1 主要内容:数论,数学
    递推的一点理解
    高精度减法
    高精度加法
    p1184高手之在一起
    对于rqy今天讲座的一些理解和看法吧
    PHP.21-商品信息管理
  • 原文地址:https://www.cnblogs.com/wangsen123/p/5844026.html
Copyright © 2011-2022 走看看