鱼C论坛

 找回密码
 立即注册
查看: 2783|回复: 5

老师布置的作业c语言

[复制链接]
发表于 2014-6-10 23:52:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
输入一段短文,数里面的单词,然后把每个单词出现的次数输出来。。。
找出出现次数最多的那个单词和其出现的次数。。。


急。。。急。。。急。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-6-11 12:26:00 | 显示全部楼层
咋没人理我啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-6-11 13:18:52 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>

  3. enum { MAX_SIZE = 20, MAX_WORDS = 200 };
  4. struct MyTable {
  5.         char strs[MAX_WORDS][MAX_SIZE];
  6.         int occur[MAX_WORDS];
  7.         int cur_pos;
  8. };
  9. void init( struct MyTable* ptable );
  10. void destroy( struct MyTable* ptable );
  11. void input( struct MyTable* ptable );
  12. void output_all( const struct MyTable* ptable );
  13. void output_max( const struct MyTable* ptable );

  14. int main(void) {
  15.         struct MyTable table;
  16.         init( &table );

  17.         input( &table );
  18.         output_all( &table );
  19.         output_max( &table );

  20.         destroy( &table );
  21.         return 0;
  22. }

  23. void init( struct MyTable* ptable ) {
  24.         ptable->cur_pos = 0;
  25. }
  26. void destroy( struct MyTable* ptable ) {}

  27. void input( struct MyTable* ptable ){
  28.         char tmp[MAX_SIZE];
  29.         puts("请输入短文:");
  30.         while( scanf("%s", tmp) != EOF ) {
  31.                 int i;
  32.                 for( i=0;i<ptable->cur_pos;++i )
  33.                         if( strcmp( tmp, ptable->strs[i]) ==0 ) {
  34.                                 ++(ptable->occur[i]);
  35.                                 break;//break for
  36.                         }
  37.                 if( i==ptable->cur_pos ) {//not occur before
  38.                         strcpy( ptable->strs[i], tmp );
  39.                         ptable->occur[i] = 1;
  40.                         ++(ptable->cur_pos);
  41.                 }
  42.         }
  43. }

  44. void output_all( const struct MyTable* ptable ) {
  45.         int i;
  46.         for( i=0;i<ptable->cur_pos;++i )
  47.                 printf("单词 %s 出现了%d次\n",ptable->strs[i], ptable->occur[i]);
  48. }

  49. void output_max( const struct MyTable* ptable ) {
  50.         if( ptable->occur > 0  ) {
  51.                 int i, max_index, max_occur;
  52.                 for( i=0, max_index =0, max_occur = -1;i<ptable->cur_pos;++i )
  53.                         if( ptable->occur[i] > max_occur ) {
  54.                                 max_occur = ptable->occur[i];
  55.                                 max_index = i;
  56.                         }
  57.                 printf("单词 %s 出现次数最多,出现了%d次\n",ptable->strs[max_index], ptable->occur[max_index]);
  58.         }
  59. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-6-30 15:06:43 | 显示全部楼层

回帖奖励 +1 鱼币

数里面的单词
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-7-1 10:08:59 | 显示全部楼层
这个很难吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-7-4 20:22:42 | 显示全部楼层
课后习题在哪,怎么没找到?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 11:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表