版本3和4间的区别
于2007-09-06 19:36:11修订的的版本3
大小: 2453
编辑: czk
备注:
于2007-09-06 19:43:54修订的的版本4
大小: 3874
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 96: 行号 96:

== 文件复制 ==
把输入一次一个字符地复制到输出
{{{#!cplusplus
#include <stdio.h>

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

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

== 文件复制第2版 ==
对于经验比较丰富的C语言程序员,可以把这个字符复制程序编写得更精炼一些。
{{{#!cplusplus
#include <stdio.h>

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

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

== 字符计数 ==
下列程序用于对字符进行计数,它与上面的复制程序类似。
{{{#!cplusplus
#include <stdio.h>

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

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


== 字符计数第2版 ==
{{{#!cplusplus
#include <stdio.h>

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

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


== 行计数 ==
接下来的这个程序用于统计输入中的行数。
{{{#!cplusplus
#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);
}
}}}

TableOfContents

1. 入门

1.1. 第一个C语言程序

写一个程序,在屏幕上输出hello, world

   1 #include <stdio.h>
   2 
   3 main()
   4 {
   5     printf("hello, world\n");
   6 }

1.2. 摄氏华氏温度对照表

使用公式℃=(5/9)(℉-32) 打印下列华氏温度与摄氏温度对照表

   1 #include <stdio.h>
   2 
   3 /* print Fahrenheit-Celsius table
   4    for fahr = 0, 20, ..., 300 */
   5 main()
   6 {
   7     int fahr, celsius;
   8     int lower, upper, step;
   9 
  10     lower = 0;      /* lower limit of temperature scale */
  11     upper = 300;    /* upper limit */
  12     step = 20;      /* step size */
  13 
  14     fahr = lower;
  15     while (fahr <= upper) {
  16         celsius = 5 * (fahr-32) / 9;
  17         printf("%d\t%d\n", fahr, celsius);
  18         fahr = fahr + step;
  19     }
  20 }

1.3. 温度对照表第2版

上述的温度转换程序存在两个问题。比较简单的问题是,由于输出的数不是右对齐的,所以输出的结果不是很美观。另一个较为严重的问题是,由于我们使用的是整型算术运算,因此经计算得到的摄氏温度值不太精确,例如,与0℉对应的精确的摄氏温度应该为-17.8℃,而不是-17℃。

   1 #include <stdio.h>
   2 
   3 /* print Fahrenheit-Celsius table
   4    for fahr = 0, 20, ..., 300; floating-point version */
   5 main()
   6 {
   7     float fahr, celsius;
   8     float lower, upper, step;
   9 
  10     lower = 0;      /* lower limit of temperatuire scale */
  11     upper = 300;    /* upper limit */
  12     step = 20;      /* step size */
  13 
  14     fahr = lower;
  15     while (fahr <= upper) {
  16         celsius = (5.0/9.0) * (fahr-32.0);
  17         printf("%3.0f %6.1f\n", fahr, celsius);
  18         fahr = fahr + step;
  19     }
  20 }

1.4. 温度对照表第3版

对于某个特定任务我们可以采用多种方法来编写程序。

   1 #include <stdio.h>
   2 
   3 /* print Fahrenheit-Celsius table */
   4 main()
   5 {
   6     int fahr;
   7 
   8     for (fahr = 0; fahr <= 300; fahr = fahr + 20)
   9         printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
  10 }

1.5. 温度对照表第4版

   1 #include <stdio.h>
   2 
   3 #define LOWER  0     /* lower limit of table */
   4 #define UPPER  300   /* upper limit */
   5 #define STEP   20    /* step size */
   6 
   7 /* print Fahrenheit-Celsius table */
   8 main()
   9 {
  10     int fahr;
  11 
  12     for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
  13         printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
  14 }

1.6. 文件复制

把输入一次一个字符地复制到输出

   1 #include <stdio.h>
   2 
   3 /* copy input to output; 1st version  */
   4 main()
   5 {
   6     int c;
   7 
   8     c = getchar();
   9     while (c != EOF) {
  10         putchar(c);
  11         c = getchar();
  12     }
  13 }

1.7. 文件复制第2版

对于经验比较丰富的C语言程序员,可以把这个字符复制程序编写得更精炼一些。

   1 #include <stdio.h>
   2 
   3 /* copy input to output; 2nd version  */
   4 main()
   5 {
   6     int c;
   7 
   8     while ((c = getchar()) != EOF)
   9         putchar(c);
  10 }

1.8. 字符计数

下列程序用于对字符进行计数,它与上面的复制程序类似。

   1 #include <stdio.h>
   2 
   3 /* count characters in input; 1st version */
   4 main()
   5 {
   6     long nc;
   7 
   8     nc = 0;
   9     while (getchar() != EOF)
  10         ++nc;
  11     printf("%ld\n", nc);
  12 }

1.9. 字符计数第2版

   1 #include <stdio.h>
   2 
   3 /* count characters in input; 2nd version */
   4 main()
   5 {
   6     double nc;
   7 
   8     for (nc = 0; gechar() != EOF; ++nc)
   9         ;
  10     printf("%.0f\n", nc);
  11 }

1.10. 行计数

接下来的这个程序用于统计输入中的行数。

   1 #include <stdio.h>
   2 
   3 /* count lines in input */
   4 main()
   5 {
   6     int c, nl;
   7 
   8     nl = 0;
   9     while ((c = getchar()) != EOF)
  10         if (c == '\n')
  11             ++nl;
  12     printf("%d\n", nl);
  13 }

Examples_from_TCPL (2008-02-23 15:37:07由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.