鱼C论坛

 找回密码
 立即注册
查看: 2537|回复: 15

C语言“统计字符、单词和行”问题中,实现统计单词出错

[复制链接]
回帖奖励 34 鱼币 回复本帖可获得 2 鱼币奖励! 每人限 1 次(中奖概率 50%)
发表于 2013-3-26 19:14:22 | 显示全部楼层 |阅读模式

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

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

x
  1. /*
  2.         时间:2013年3月26日14:41:19
  3.         程序功能:统计输入的字符、单词和行
  4. */
  5. #include <stdio.h>
  6. #include <stdbool.h> //为isspace()提供函数原型
  7. #include <ctype.h> //为bool、true和false提供宏定义
  8. #define STOP '|'

  9. int main(void)
  10. {
  11.         char c; //读入字符
  12.         char prev; //前一个读入字符
  13.         long n_chars = 0L; //字符数
  14.         int n_lines = 0; //行数
  15.         int n_words = 0; //单词数
  16.         int p_lines = 0; //不完整的行数
  17.         bool inword = false;

  18.         printf("Enter text to be analyzed (| to terminate): \n");
  19.         prev = '\n'; //用于识别完整的行
  20.         while ((c = getchar()) != STOP)
  21.         {
  22.                 n_chars++; //统计字符
  23.                 if (c == '\n')
  24.                         n_lines++; //统计行
  25.                 if (!isspace && !inword)
  26.                 {
  27.                         inword = true; //开始一个新单词
  28.                         n_words++; //统计单词
  29.                 }
  30.                 if (isspace || inword)
  31.                         inword = false; //到达单词的结尾
  32.                 prev = c;
  33.         }

  34.         if (prev != '\n')
  35.                 p_lines = 1;
  36.         printf("characters = %ld, words = %d, lines = %d,",
  37.                 n_chars, n_words, n_lines);
  38.         printf("partial lines = %d\n", p_lines);
  39.         return 0;
  40. }
复制代码

C语言“统计字符、单词和行”问题中,实现统计单词出错。上面是源代码。
这个是在vim中运行的结果:
173044av0tvooio080gatz.png
和预期的结果不一样,characters = 55, words = 0, lines = 3,partial lines = 0
characters = 55得到了正确的结果,lines = 3得到了正确的结果,partial lines = 0得到了正确的结果,但是在统计单词words的时候却运行不正确!???请问这是为什么?哪里出问题了吗?

上面是vim中的运行结果和源代码。下面也在VC++6.0中测试了下:
因为在VC++6.0中用isspace()函数编译的时候出错,所以我把代码改成了这样:
  1. /*
  2.         时间:2013年3月26日14:41:19
  3.         程序功能:统计输入的字符、单词和行
  4. */
  5. #include <stdio.h>
  6. //#include <stdbool.h> //为isspace()提供函数原型
  7. #include <ctype.h> //为bool、true和false提供宏定义
  8. #define STOP '|'

  9. int main(void)
  10. {
  11.         char c; //读入字符
  12.         char prev; //前一个读入字符
  13.         long n_chars = 0L; //字符数
  14.         int n_lines = 0; //行数
  15.         int n_words = 0; //单词数
  16.         int p_lines = 0; //不完整的行数
  17.         bool inword = false;

  18.         printf("Enter text to be analyzed (| to terminate): \n");
  19.         prev = '\n'; //用于识别完整的行
  20.         while ((c = getchar()) != STOP)
  21.         {
  22.                 n_chars++; //统计字符
  23.                 if (c == '\n')
  24.                         n_lines++; //统计行
  25.                 if (c != ' ' && c != '\n' && c != '\t')
  26.                 {
  27.                         inword = true; //开始一个新单词
  28.                         n_words++; //统计单词
  29.                 }
  30.                 if (c == ' ' || c == '\n' || c == '\t')
  31.                         inword = false; //到达单词的结尾
  32.                 prev = c;
  33.         }

  34.         if (prev != '\n')
  35.                 p_lines = 1;
  36.         printf("characters = %ld, words = %d, lines = %d,",
  37.                 n_chars, n_words, n_lines);
  38.         printf("partial lines = %d\n", p_lines);
  39.         return 0;
  40. }
复制代码
在VC++6.0中运行的结果是这样的:
V.png
这里也是统计单词的时候没有运行正确!!
并且words统计的时候统计了字符的个数,而不是单词!请问这个又是哪里错了?
统计单词我的思路是这样的,讲一个单词定义为不包含空白字符(也就是说,没有空格,制表符或换行符)的一系列字符。因此,“dtz”和“asdasd”是单词。一个单词以程序首次遇到非空白符开始,在下一个空白符出现时结束。检测非空白符最简单了的判断表达式是这样的:
  1. c != ' ' && c != '\n' && c != '\t'
复制代码
检测空白字符最简单明了的判断是:
  1. c == ' ' || c == '\n' || c == '\t'
复制代码

这个可以用ctype.h文件中的isspace()函数来解决。如果该函数的参数是空白字符,它就返回真。因此如果是c是空白字符,isspace为真;而如果c不是空白字符,!isspace为真。
之前说过,因为VC++6.0中用isspace函数时错误,所以改用了比较复杂的公式c != ' ' && c != '\n' && c != '\t'和c == ' ' || c == '\n' || c == '\t'来代替isspace,并在vim中用isspace代替比较复杂的公式运行。两个运行结果都和预期不一样,并且出现了不一样的结果。
请问这个是什么问题导致的?
这个程序错了吗?
我觉得思路是没有错的,因为这是CPP书上的,
如果错了要怎么写才能实现统计单词?

小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-3-26 19:17:52 | 显示全部楼层
不知道为什么有些字体有背景色了。。。:(
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-26 20:16:36 | 显示全部楼层

回帖奖励 +2 鱼币

计算单词的地方有一点小错误
代码:
  1. /*
  2.         时间:2013年3月26日14:41:19
  3.         程序功能:统计输入的字符、单词和行
  4. */
  5. #include <stdio.h>
  6. //#include <stdbool.h> //为isspace()提供函数原型
  7. #include <ctype.h> //为bool、true和false提供宏定义
  8. #define STOP '|'

  9. int main(void)
  10. {
  11.         char c; //读入字符
  12.         char prev; //前一个读入字符
  13.         long n_chars = 0L; //字符数
  14.         int n_lines = 0; //行数
  15.         int n_words = 0; //单词数
  16.         int p_lines = 0; //不完整的行数
  17.         bool inword = false;

  18.         printf("Enter text to be analyzed (| to terminate): \n");
  19.         prev = '\n'; //用于识别完整的行
  20.         while ((c = getchar()) != STOP)
  21.         {
  22.                 n_chars++; //统计字符
  23.                 if (c == '\n')
  24.                         n_lines++; //统计行
  25.                 if (c != ' ' && c != '\n' && c != '\t')
  26.                 {
  27.                         inword = true; //开始一个新单词
  28.                 }
  29.                 if (c == ' ' || c == '\n' || c == '\t' )
  30.                                 {
  31.                         inword = false; //到达单词的结尾                                               
  32.                         n_words++; //统计单词
  33.                                 }
  34.                 prev = c;
  35.         }

  36.         if (prev != '\n')
  37.                 {
  38.                 p_lines = 1;
  39.                                 n_words++;
  40.                 }
  41.         printf("characters = %ld, words = %d, lines = %d,",
  42.                 n_chars, n_words, n_lines);
  43.         printf("partial lines = %d\n", p_lines);
  44.                 _flushall();
  45.                 getchar();
  46.         return 0;
  47. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-26 20:17:30 | 显示全部楼层
结果:
1.jpg
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-28 14:25:44 | 显示全部楼层

回帖奖励 +2 鱼币

会贴 收鱼币
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-4-2 13:11:02 | 显示全部楼层
回帖拿钱~~
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-4-2 16:46:03 | 显示全部楼层
回帖收鱼币
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-4-3 12:56:55 | 显示全部楼层

回帖奖励 +2 鱼币

来收钱的。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-17 23:49:17 | 显示全部楼层
楼主加油,鱼C加油!我们都看好你哦!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-29 14:36:30 | 显示全部楼层
强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-5-29 23:25:25 | 显示全部楼层
学习学习。。。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-5-30 01:59:50 | 显示全部楼层
用函数应很好做的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 15:19:59 | 显示全部楼层
顶上去
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 15:20:31 | 显示全部楼层
拿鱼币
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 15:21:16 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-4 23:48:49 | 显示全部楼层
都是高手,向你们学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 02:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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