作业4 结对编程 词频统计

  • 要求:
  •  基于作业3的结果,读取一个较小的文本文件A_Tale_of_Two_Cities.txt,统计该文件中的单词的频率,并将统计结果输出到当前目录下的 Result1.txt 文件。 (第一阶段初稿完成该要求)
  • 命令行格式: 提示符> Myapp.exe -f filename.txt > Result.txt (PS:C++ 程序,Java 程序输出方式类似) filename.txt 为前面下载的文件名。
  • 解释:此外, 读取一个较大的文本文件Gone_with_the_wind.txt实验对比程序执行效率,做如下改进,比较改进前后程序执行时间。
    • 选项 -f 表示后面跟文件名
    • 输出格式规定(参考作业3中的示例):
      • 首先按照频率由高到低排序
      • 频率一样的词, 按照字典顺序排序
  • 我的结对对象是孙楷悦,她的博客地址为http://www.cnblogs.com/sunkaiyue
  •                                 github地址为https://github.com/apridelion/work4
  • 结对照片
  • #include<iostream>
    #include<fstream>
    #include<cstring>
    using namespace std;
    
    struct Num{
        int num;
        char *s;
    
    void    exchange( Word &word )  /* 交换单词 */
        {
            string    tStr    = word.Str;
            int    tCount    = word.Count;
            word.Str    = Str;
            word.Count    = Count;
            Str        = tStr;
            Count        = tCount;
        }
    }
    Num word[999];
    
    int Change(char str[999])//大写转小写
    {
        int a = 0;
        while (str[a] != '')//当str字符串未结束时将大写字母转小写
        {
            if (str[a] >= 'A'&&str[a] <= 'Z')
                str[a] = str[a] + 32;//因为大写字母与小写字母的ascll码值相差32,大写转小写要加上32
            a++;
        }
        return 0;
    }
    int Judge(char w[])//判断是否符合题意的单词
    {
         for (int e = 1; w[e] != '';)
        {
             if(strlen(w)<4)//判断单词字长是否超过以及等于4
               return -1;
             if (w[e] >= 'a' && w[e] <= 'z')//判断单词的首字符是否是字母
            return -1;
             if (!((w[e] >= 'a' && w[e] <= 'z') || (w[e] >= '0' && w[e] <= '9')))//判断单词中是否有非字母数字
                return -1;
            else
                e++;
        }
         return 0;
    }
    int Fre(char f[],int total )   //统计单词出现过的频率
    {
        if (total>0)
        for (int i = 0; i <total; i++)
        {
            if (!strcmp(f, word[i].s))  
            {
                word[i].num++;      
                return -1;
            }
        }
        return 0;
    }
    void SortWordDown( Word *words,int 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 main()
    {   char sentence[999];
       ifstream file("A_Tale_of_Two_Cities.txt"); //从文件中读取
        while (!file.eof())
        {
            file.getline(result,500);
        }
        file.close();
       
        const char *delim = ",“”.' '‘’!?";  //delim是用来定义分隔符的内容
        char *p= strtok(sentence, delim);//strtok函数根据分隔符分隔字符串
        int n=0;
        int c=0;
        while (p)
        {
           Change(p);
            if (Judge(p) != -1)
            {
                if (Fre(p, n)!=-1)
                {
                    word[n].s = p;
                    n++;
                }
            }
            p = strtok(NULL, delim);
        }
         int t = 0;
        ofstream outfile;                       /* 输出文件到result1 */
        outfile.open( "Result1.txt" )
        SortWordDown( w, count );
        while (word[c].s)        //输出统计结果
        {
            cout << word[c].s<< "" << word[c].num << '
    ';
            c++;
        }
        return 0;
    }
  • 结果
  • 这次程序相对于上次作业来说,多了排序和输出结果到文件。于是我和孙楷悦分工,她写排序部分,我写输出结果到文件部分。这样作业量大大减少,并且提高了作业速度。

  • 总结:第一次尝试结对方式写程序,对我来说是一次新的体验。两个人一起写程序难免会有分歧,这时候就需要电脑来证明。比较才会有更好的结果。结对多了点学习激情和热情。
原文地址:https://www.cnblogs.com/wumin2/p/5308896.html