zoukankan      html  css  js  c++  java
  • 作业4:结对项目—— 词频统计

    1. 目标

    • 代码复审练习
    • 结对练习
    • 编写单元测试

    2. 要求

    • [必做 1] 基于作业3的结果,读取一个较小的文本文件A_Tale_of_Two_Cities.txt,统计该文件中的单词的频率,并将统计结果输出到当前目录下的 Result1.txt 文件。 (第一阶段初稿完成该要求)

      结对对象纪杨阳的博客地址 :http://home.cnblogs.com/u/yangyangji/

      编写系统的Github 链接:https://github.com/yangyangji/4

      双方的贡献比例: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++;
                
            }
    
            ofstream Result1; 
            Result1.open("Result1.txt"); //表示你要把内容输出到"Result1.txt"这个文件里 如果没有这个文件,会自动创建这个文件
            Result1<< test[n].p << ":" << test[n].count << '
    ';//这里是你想要输出的内容
           
            
            q=strtok(NULL,delim);
        }
        return 0;
    }

    实验结果如下:

     

    实验总结:

    分工合作,利用各自的优点,节省了时间,提高了效率,易于发现对方的错误。

  • 相关阅读:
    sscanf 函数
    hdu 1232 畅通工程 解题报告
    hdu 2048 神、上帝以及老天爷 解题报告
    hdu 2049 不容易系列之(4)——考新郎 解题报告
    hdu 2013 蟠桃记 解题报告
    hdu 2093 考试排名 解题报告
    分糖果 的解题报告
    hdu 2017 字符串统计 解题报告
    表的连接方式:NESTED LOOP、HASH JOIN、SORT MERGE JOIN【转】
    sort_area_size设置【转】
  • 原文地址:https://www.cnblogs.com/yeyuexin/p/5301569.html
Copyright © 2011-2022 走看看