Chinaunix首页 | 论坛 | 博客
  • 博客访问: 548290
  • 博文数量: 179
  • 博客积分: 3845
  • 博客等级: 中校
  • 技术积分: 2003
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-16 21:25
文章分类
文章存档

2012年(74)

2011年(105)

分类: C/C++

2011-04-26 11:17:56

《C程序设计语言》中有这样一个关于单词计数的小程序:
 
#define IN 1 /*在单词内*/
#define OUT 0 /*在单词外*/
/*统计输入的单词数*/
main()
{
        int c, nw , state;
        state = OUT;
        nw = 0;
        while( (c = getchar() ) != EOF)
        {
                if( c== ' ' || c == '\n' || c == '\t')
                        state = OUT;
                else if( state == OUT)
                {
                        state = IN;
                        ++nw;
                }
        }
        printf("%d \ n",nw);
}
这里对单词的定义比较宽,它是任何其中不包含空字符、制表符和换行符的字符序列。
 
下面是其给出的两个练习题:
1、你准备怎样测试这个单词计数程序?如果程序出现错误,那么什么样的输入最有利于发现这些错误?

How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?

It sounds like they are really trying to get the programmers to learn how to do a unit test. I would submit the following:

0. input file contains zero words
1. input file contains 1 enormous word without any newlines
2. input file contains all white space without newlines
3. input file contains 66000 newlines
4. input file contains word/{huge sequence of whitespace of different kinds}/word
5. input file contains 66000 single letter words, 66 to the line
6. input file contains 66000 words without any newlines
7. input file is
/usr/dict contents (or equivalent)
8. input file is full collection of moby words
9. input file is binary (e.g. its own executable)
10. input file is
/dev/nul (or equivalent)

66000 is chosen to check for integral overflow on small integer machines.

Dann suggests a followup exercise 1-11a: write a program to generate inputs (0,1,2,3,4,5,6)
I guess it was inevitable that I'd receive a solution for this followup exercise! Here is Gregory Pietsch's program to generate Dann's suggested inputs:

#include #include int main(void){    FILE *f;    unsigned long i;    static char *ws = " \f\t\v";    static char *al = "abcdefghijklmnopqrstuvwxyz";    static char *i5 = "a b c d e f g h i j k l m "                      "n o p q r s t u v w x y z "                      "a b c d e f g h i j k l m "                      "n o p q r s t u v w x y z "                      "a b c d e f g h i j k l m "                      "n\n";    /* Generate the following: */    /* 0. input file contains zero words */    f = fopen("test0", "w");    assert(f != NULL);    fclose(f);    /* 1. input file contains 1 enormous word without any newlines */    f = fopen("test1", "w");    assert(f != NULL);    for (i = 0; i < ((66000ul / 26) + 1); i++)        fputs(al, f);    fclose(f);    /* 2. input file contains all white space without newlines */    f = fopen("test2", "w");    assert(f != NULL);    for (i = 0; i < ((66000ul / 4) + 1); i++)        fputs(ws, f);    fclose(f);    /* 3. input file contains 66000 newlines */    f = fopen("test3", "w");    assert(f != NULL);    for (i = 0; i < 66000; i++)        fputc('\n', f);    fclose(f);    /* 4. input file contains word/      *    {huge sequence of whitespace of different kinds}     *    /word      */    f = fopen("test4", "w");    assert(f != NULL);    fputs("word", f);    for (i = 0; i < ((66000ul / 26) + 1); i++)        fputs(ws, f);    fputs("word", f);    fclose(f);    /* 5. input file contains 66000 single letter words,     *    66 to the line      */    f = fopen("test5", "w");    assert(f != NULL);    for (i = 0; i < 1000; i++)        fputs(i5, f);    fclose(f);    /* 6. input file contains 66000 words without any newlines */    f = fopen("test6", "w");    assert(f != NULL);    for (i = 0; i < 66000; i++)        fputs("word ", f);    fclose(f);    return 0;}2、编写一个程序,以每行一个单词的形式打印输出

Write a program that prints its input one word per line.

#include int main(void){  int c;  int inspace;   inspace = 0;  while((c = getchar()) != EOF)  {    if(c == ' ' || c == '\t' || c == '\n')    {      if(inspace == 0)      {        inspace = 1;        putchar('\n');      }      /* else, don't print anything */    }    else    {      inspace = 0;      putchar(c);    }  }  return 0;}
阅读(2180) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~