结对项目—— 词频统计

 必做 1] 基于作业3的结果,读取一个较小的文本文件A_Tale_of_Two_Cities.txt,统计该文件中的单词的频率,并将统计结果输出到当前目录下的 Result1.txt 文件。 

  结对对象:刘雨恬

  博客网址:http://www.cnblogs.com/liuyutian/

  github链接:https://github.com/liuyutianlyt/EX_4.md

  贡献比例:1:1

  结对编程照片:

  预估时间:由于已经有了作业3的基础,我的预估时间是两三个小时。然而,在此次任务完成后,我实际共花了两个小时!

  这次的任务只是在作业3的基础上增加了一个输出流文件,和一个词频排序两大难点。

 1.输出流文件,我翻阅了以往的c++书和适当百度,得出了程序。

 2.最大的一个难点是词频按照大小排序,且词频相同的按照字典顺序排序。

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

struct WORD {                           /* 创建一个结构体 */
    int    count;
    char    s;
    void    exchange( Word &word )  /* 交换单词 */
    {
        string    tStr    = word.Str;
        int    tCount    = word.Count;
        word.Str    = Str;
        word.Count    = Count;
        Str        = tStr;
        Count        = tCount;
    }
};
} w[100];

bool isword( char a[] ) /* 判断是否是一个单词 */
{
    int i = 0;
    for ( i = 0; a[i] != ''; i++ )
        if ( (a[i] >= 'a' && a[i] <= 'z') || (a[i] >= '0' && a[i] <= '9') )
            return(true);
        else
            return(false);
}


int judge( char b[], int n )                            /* 判断该单词是否出现过 */
{
    if ( n > 0 )
        for ( int i = 0; i < n; i++ )
        {
            if ( !strcmp( b, &w[i].s ) )    /* 出现 */
            {
                w[i].count++;
                return(-1);
            }
        }
}


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( void )
{
    char result[500];

    char *ptr;
    ifstream file( "c://A_Tale_of_Two_Cities.txt" ); /* 读取 */
    if ( !file )
    {
        cout << "不能打开文件";
    }
    while ( !file.eof() )
    {
        file.getline( result, 500 );
    }
    file.close();
    int j = 0; /* 大写转小写 */
    while ( result[j] != '/0' && result[j + 1] != '/0' )
    {
        if ( result[j] >= 'A' && result[j] <= 'Z' )
        {
            result[j] = result[j] - 'A' + 'a';
            j++;
        }
    }
    cout << result;
    char *sep = " ";

    int i = 0;
    ptr = strtok( result, " " );            /* 利用strtok函数来分割result字符串中的单词 */
    while ( ptr != NULL )
    {
        if ( isword( p ) != false )
        {
            if ( judge( p, n ) != false )
            {
                w[n].s = *p;    /* 赋值给数组 */
                n++;
            }
        }
        ptr = strtok( NULL, " " );
    }
    int t = 0;
    ofstream outfile;                       /* 输出文件到result1 */
    outfile.open( "Result1.txt" )
    SortWordDown( w, count );
    while ( w[t].s )                        /* 输出统计结果 */
    {
        if ( strlen( w[t].s ) >= 4 )
        {
            outfile << w[t].s << "" << w[t].count << '
';
            t++;
        }
    }
    return(0);
}

  通过此次的团队合作让我明白的团队编程的重要性。互相指点,互相讨论,互相指点。最终一个程序的完成。写代码的正确性和效率都提高了。

原文地址:https://www.cnblogs.com/jitianmeng/p/5301828.html