结对项目— 词频统计2(语言C++)

 

结对对象:季天梦

博客地址:http://www.cnblogs.com/jitianmeng/

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

比例:1:1 


 要求


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

  • 命令行格式: 提示符> Myapp.exe -f filename.txt -w word (PS:C++ 程序,Java 程序输出方式类似)
  • 解释:
    • 选项 -f 表示打开某一文件(filename.txt)
    • 选项 -w 表示统计其后单词(word)在打开的文件(filename.txt)中的频率。

源程序如下:

  1 #include <iostream>
  2 #include <cstring>
  3 #include <fstream>
  4 using namespace std;
  5 
  6 struct WORD {                           /* 创建一个结构体 */
  7     int    count;
  8     char    s;
  9     void    exchange( Word &word )  /* 交换单词 */
 10     {
 11         string    tStr    = word.Str;
 12         int    tCount    = word.Count;
 13         word.Str    = Str;
 14         word.Count    = Count;
 15         Str        = tStr;
 16         Count        = tCount;
 17     }
 18 };
 19 } w[100];
 20 
 21 bool isword( char a[] ) /* 判断是否是一个单词 */
 22 {
 23     int i = 0;
 24     for ( i = 0; a[i] != '\0'; i++ )
 25         if ( (a[i] >= 'a' && a[i] <= 'z') || (a[i] >= '0' && a[i] <= '9') )
 26             return(true);
 27         else
 28             return(false);
 29 }
 30 
 31 
 32 int judge( char b[], int n )                            /* 判断该单词是否出现过 */
 33 {
 34     if ( n > 0 )
 35         for ( int i = 0; i < n; i++ )
 36         {
 37             if ( !strcmp( b, &w[i].s ) )    /* 出现 */
 38             {
 39                 w[i].count++;
 40                 return(-1);
 41             }
 42         }
 43 }
 44 
 45 
 46 void SortWordDown( Word * words, int size )  /* 降序排序 */
 47 {
 48     for ( int i = 0; i < size; i++ )
 49     {
 50         for ( int j = 0; j < size - 1; j++ )
 51         {
 52             if ( words[j].Count < words[j + 1].Count )
 53             {
 54                 words[j].exchange( words[j + 1] );
 55             }
 56         }
 57     }
 58 }
 59 
 60 
 61 int main( void )
 62 {
 63     char result[500];
 64 
 65     char *ptr;
 66     ifstream file( "c://A_Tale_of_Two_Cities.txt" ); /* 读取 */
 67     if ( !file )
 68     {
 69         cout << "不能打开文件";
 70     }
 71     while ( !file.eof() )
 72     {
 73         file.getline( result, 500 );
 74     }
 75     file.close();
 76     int j = 0; /* 大写转小写 */
 77     while ( result[j] != '/0' && result[j + 1] != '/0' )
 78     {
 79         if ( result[j] >= 'A' && result[j] <= 'Z' )
 80         {
 81             result[j] = result[j] - 'A' + 'a';
 82             j++;
 83         }
 84     }
 85     cout << result;
 86     char *sep = " ";
 87 
 88     int i = 0;
 89     ptr = strtok( result, " " );            /* 利用strtok函数来分割result字符串中的单词 */
 90     while ( ptr != NULL )
 91     {
 92         if ( isword( p ) != false )
 93         {
 94             if ( judge( p, n ) != false )
 95             {
 96                 w[n].s = *p;    /* 赋值给数组 */
 97                 n++;
 98             }
 99         }
100         ptr = strtok( NULL, " " );
101     }
102     int t = 0;
103     char x;
104     cout<<"请输入需要统计的单词:"; 
105     cin>>x;                                        /*输入需要统计的单词*/
106     while (! strcmp(w[t].s,x))                     /*查询比较所需统计的单词*/   
107     {
108         t++;
109     }
110     cout<< w[t].s << "" << w[t].count << '\n';      /* 输出统计结果 */
111     return(0);
112 }

结果:(读取小文件,选择单词evil,before,going)


总结:由于前一个作业合作的基础,仔细看了必做二的内容,在我和搭档讨论了之后,决定在主函数中添加一个查找单词的代码。查找除了单次之后,由于之前已经统计过了函数出现的次数,所以可以显示出次数。在讨论过程中,我们基本没有什么分歧。

 

转载于:https://www.cnblogs.com/liuyutian/p/5328823.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值