• 作业4:结对项目—— 词频统计【必做2】


         结对对象:叶月欣 1413042001  博客地址: http://www.cnblogs.com/yeyuexin/

         Github链接https://github.com/yangyangji/ex.2

         

         双方贡献比例:  1:1

     

         结对编程照片:

    源程序

    #include<iostream>
    #include<fstream>
    #include<cstring>
    #include<string>
    using namespace std;
    
    struct Word{      //定义结构体
        int Count;//计数器
         Word() : Str(""), Count(0) {}  
        string Str; //字符串
        char *p;
    };
    
    
    void exchange(Word &word)  //函数,用于交换单词(排序单词)    
    {  
        string tStr = word.Str;  
        int tCount = word.Count;  
        word.Str = Str;  
        word.Count = Count;  
        Str = tStr;  
        Count = tCount;  
    }  
    
    Words test[100];
    
    void lwr(char x[])     //大写转小写
    {   int k = 0;
        while (x[k] != '')
        {
            if (x[k] >= 'A'&&x[k] <= 'Z')
                x[k] = x[k] + 32;
            k++;
        }
    }
    
    int identify(char a[])    //判断是否符合单词的定义
    {   int m=(strlen(a)>=4)?1:0;
        int n=(a[0]>='a'&&a[0]<='z')?1:0;
        if(!m||!n)
            return 0;
        else
            while(a)
            {   for(int i=1;;i++)
               {  
                   if(!(a[i]>='a'&&a[i]<='z')||!(a[i]>='0'&&a[i]<='9'))
                            return 0;
                   else
                            return 1;
               }
            }
    }
    
    void SortWordDown(Word * words, int size)  //以单词出现频率降序排列单词,words 单词数组,size 单词数量
    {  
        for(int i=0;i<size;i++)  
        {
            for(int j=0;j <size-1;j++)  
            { 
                if(words[j].Count<words[j+1].Count)  
                {  
                    words[j].exchange(words[j+1]);  
                }  
            }  
        }  
    }  
    
    int counting(char b[],int num)    //对出现次数计数
    {   for(int j=0;j<num;j++)
        {
    
            if(!strcmp(b,test[j].p))
                 test[j].count++;
            else
                return 0;
        }
    }
    
    
    int main()
    {   char c[200];
        ifstream fin("D:/A_Tale_of_Two_Cities.txt");   //从文档中获取字符串
        for(int f=0;;f++)
            fin>>c[f];
        fin.close();
        cin.get();
        lwr(c);
        const char *delim = ",”“.''!?";      //分割字符串
        char *q;
        int n=0;
        q = strtok(c, delim);
        SortWordDown(words, wCount);  
        while (q)
        {
            if (identify(q))
            {
                
                    strcpy(test[n].p,q);
                    n++;
                
            }
    
           
           q=strtok(NULL,delim);
        }
        cout<<"输入统计出现频数的指定单词:
    ";    
        char r;
        cin>>r;
        int t=0;
        while(!strcmp(test[n].p,r))     //找到指定单词
        {   
            t++;
        }
        cout<< test[n].p << ":" << test[n].count << '
    ';    //输出指定单词的统计结果
        return 0;
    }

    运行结果

    (1)测试“period”

    (2)测试“best”

    (3)测试“hope”

    总结

    在上次作业的基础上,去除掉输出文件流的部分,添加指定单词的指令即可。有了之前程序的完成,所以较为简单。

    指定单词指令如下图:

    测试数据来源:

    A_Tale_of_Two_Cities.txt 

    一同修改代码并且测试数据,高速高效完成。

  • 相关阅读:
    自动提示效果
    INF文件格式小结
    XP下IIS不能添加扩展名映射的BUG
    自动配置IE代理脚本
    基于多源数据画像的失败用例智能分析
    Hadoop中mapreduce作业日志是如何生成的
    从内存管理原理,窥探OS内存管理机制
    【伙伴故事】智慧厨电接入华为云+HarmonyOS,你的未来厨房长这样
    5步带你掌握工作流Activiti框架的使用
    华为云VSS漏洞扫描服务之开源组件漏洞检测能力
  • 原文地址:https://www.cnblogs.com/yangyangji/p/5328908.html
Copyright © 2020-2023  润新知