结对项目—— 词频统计

[必做 2] 读取小文本文件A_Tale_of_Two_Cities.txt 统计某一指定单词在该文本文件中出现的频率。

结对对象:刘雨恬

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

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

  贡献比例:1:1

  预估时间:由于已经有了上次作业的基础,我的预估时间是一个小时。在此次任务完成后,我确实也只花了1个小时!

   这次的任务只是在上次作业的基础上减少了一个输出流文件,但是需要查找指定单词的频率。因此首先我设置了一个字符型变量x;在与先前已统计好的数组,作比较,找到相同的便输出就好。

源代码:

#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;
    char x[8];
    cout<<"请输入需要统计的单词:"; 
    cin>>x;                                        /*输入需要统计的单词*/
    while (! strcmp(w[t].s,x))                     /*查询比较所需统计的单词*/   
    {
        t++;
    }
    cout<< w[t].s << "" << w[t].count << '
';      /* 输出统计结果 */
    return(0);
}

结果示例:

总结:这几次的作业感觉是那种层层递进的感觉,每次都是在前面的基础上去增加知识点,让我慢慢的学到了很多。这次的主要任务是去比较查找。我和我的搭一起翻阅书籍,在共同的努力下完成了这次对的作业。

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