Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1099177
  • 博文数量: 165
  • 博客积分: 5957
  • 博客等级: 大校
  • 技术积分: 2015
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-24 15:04
文章分类

全部博文(165)

文章存档

2014年(10)

2013年(14)

2012年(9)

2011年(22)

2010年(17)

2009年(17)

2008年(26)

2007年(34)

2006年(16)

我的朋友

分类: C/C++

2008-03-15 09:39:29

这里列举一些代码的习惯比较,想想why可以这样做

1、copy input to output

   #include <stdio.h>

   /* copy input to output; 1st version */
   main()
   {
       int c;

       c = getchar();
       while (c != EOF) {
           putchar(c);
           c = getchar();
       }
   }


   #include <stdio.h>

   /* copy input to output; 2nd version */
   main()
   {
       int c;

       while ((c = getchar()) != EOF)
           putchar(c);
   }

EOF到底是什么,不妨自己动手看下
printf("%d",EOF);//-1

2、characters count

   #include <stdio.h>

   /* count characters in input; 1st version */
   main()
   {
       long nc;

       nc = 0;
       while (getchar() != EOF)
           ++nc;
       printf("%ld\n", nc);
   }


   #include <stdio.h>

   /* count characters in input; 2nd version */
   main()
   {
       double nc;

       for (nc = 0; gechar() != EOF; ++nc)
           ;
       printf("%.0f\n", nc);
   }

同理:Line Counting

   #include <stdio.h>

   /* count lines in input */
   main()
   {
       int c, nl;

       nl = 0;
       while ((c = getchar()) != EOF)
           if (c == '\n')
               ++nl;
       printf("%d\n", nl);
   }

3、Word Counting

   #include <stdio.h>

   #define IN 1 /* inside a word */
   #define OUT 0 /* outside a word */

   /* count lines, words, and characters in input */
   main()
   {
       int c, nl, nw, nc, state;

       state = OUT;
       nl = nw = nc = 0;
       while ((c = getchar()) != EOF) {
           ++nc;
           if (c == '\n')
               ++nl;
           if (c == ' ' || c == '\n' || c = '\t')
               state = OUT;
           else if (state == OUT) {
               state = IN;
               ++nw;
           }
       }
       printf("%d %d %d\n", nl, nw, nc);
   }

待续

阅读(2441) | 评论(0) | 转发(0) |
0

上一篇:zenoss安装

下一篇:vmstat详解

给主人留下些什么吧!~~