zoukankan      html  css  js  c++  java
  • 词频统计改进版1

    需求分析:从控制台读取文件并对文件中的词频进行统计,文件可以是自己输入也可以是指定目录自动读入。

    功能一:从命令行读入文章:

    功能二:手动输入单词并统计词频:

    代码实现:

    case 1:
            {
                fp=fopen("d:\2.txt","w");
                ch=getchar();
                while(ch!='
    ')
                {
                    fputc(ch,fp);
                    ch=getchar();
                }
                fclose(fp);
                fp=fopen("d:\2.txt","r");
            }
            break;
        sum=0;
        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++;
                sum++;
            }
            
        }
        fclose(fp);
        map<string,int>::iterator m;
        cout<<"总共出现的单词数:"<<endl;
        cout<<sum<<endl;
        cout<<"每个词出现的频数如下:"<<endl;
        for(m=list.begin(),i=1;m!=list.end();i++,m++)
        {
            if(m->first=="")
            continue;
    
            cout<<left;
            cout<<setw(15)<<m->first<<setw(10)<<m->second;
            if(i%5==0)
            cout<<endl;
        }
        cout<<endl;

    功能三:通过指定路径进行文件中单词词频统计

    代码实现:

    case 2:
            {
                cout<<"请输入所要统计词频的文章路径及文件名:"<<endl;
                cin>>str;
                fp=fopen(str,"r");
            }    
        sum=0;
        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++;
                sum++;
            }
            
        }
        fclose(fp);
        map<string,int>::iterator m;
        cout<<"总共出现的单词数:"<<endl;
        cout<<sum<<endl;
        cout<<"每个词出现的频数如下:"<<endl;
        for(m=list.begin(),i=1;m!=list.end();i++,m++)
        {
            if(m->first=="")
            continue;
    
            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/cipintongji1.git

  • 相关阅读:
    eclipse中编译出现错误undefined reference to `_sbrk'
    STM32L431驱动带UC1698芯片调试记录
    IAR里面STM32工程使用printf
    STM32L431仿真卡在HAL_InitTick(TICK_INT_PRIORITY);
    电信NB-IOT的温湿度采集器开发记录
    程序运行之ELF文件的段
    linux c编程:进程控制(二)_竞争条件
    ubuntun下安装Fiddler
    程序运行之目标文件(一)
    linux c编程:进程控制(一)
  • 原文地址:https://www.cnblogs.com/ziyoujay/p/5868882.html
Copyright © 2011-2022 走看看