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

    需求分析:

     

        写一个程序对一篇英文文章中每个单词出现的次数进行统计,并按照首字母的顺序进行排列。

     

    代码设计:

     

        对每个单词出现的次数进行统计,并按照首字母的顺序进行排列,存储到map中。

    fp=fopen(str,"r");
        map<string,int>list;
        while(fgets(text,1000,fp)!=NULL)
        {    
             i=0;
            while(text[i]!='')
            {
                char s[40];
                int k=0;
                while((text[i]>='A'&&text[i]<='Z')||(text[i]>='a'&&text[i]<='z'))
                {
                    if(text[i]>='A'&&text[i]<='Z')
                    text[i]+='a'-'A';
                    s[k++]=text[i++];
                }
                s[k]='';//一个单词结束
                list[s]++;
                if(text[i]=='')break;//一行单词结束
                else i++;
            }
        }
        fclose(fp);

        对map中每个单词按照首字母的顺序显示出现的次数。

    map<string,int>::iterator m;
        cout<<"每个词出现的频数如下:"<<endl;
        for(m=++list.begin(),i=1;m!=list.end();i++,m++)
        {
            cout<<left;
            cout<<setw(15)<<m->first<<setw(10)<<m->second;
            if(i%5==0)
            cout<<endl;
        }
        cout<<endl;

        所统计的文章如下:

        统计结果如下:

     ssh:git@git.coding.net:ziyoujay/cipintongji.git

  • 相关阅读:
    Linux 磁盘分区
    curl
    Metasploit ms10_046_shortcut_icon_dllloader 利用
    Ettercap 入门
    Ettercap dos_attack
    Centos7/Debian 配置双网卡
    Centos7配置单网卡,多IP
    Ettercap MITM Arp Poisoning
    Ettercap DNS Spoofing
    java常用设计模式--工厂模式简单例子
  • 原文地址:https://www.cnblogs.com/ziyoujay/p/5844048.html
Copyright © 2011-2022 走看看