⇤ ← 于2007-09-06 19:24:17修订的的版本1
30
备注:
|
2420
|
删除的内容标记成这样。 | 加入的内容标记成这样。 |
行号 2: | 行号 2: |
写一个程序,在屏幕上输出hello, world {{{#!cplusplus #include <stdio.h> main() { printf("hello, world\n"); } }}} == 摄氏华氏温度对照表 == 使用公式℃=(5/9)(℉-32) 打印下列华氏温度与摄氏温度对照表 {{{#!cplusplus #include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } } }}} == 温度对照表第2版 == 上述的温度转换程序存在两个问题。比较简单的问题是,由于输出的数不是右对齐的,所以输出的结果不是很美观。另一个较为严重的问题是,由于我们使用的是整型算术运算,因此经计算得到的摄氏温度值不太精确,例如,与0℉对应的精确的摄氏温度应该为-17.8℃,而不是-17℃。 {{{#!cplusplus #include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300; floating-point version */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperatuire scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } } }}} == 温度对照表第3版 == 对于某个特定任务我们可以采用多种方法来编写程序。 {{{#!cplusplus #include <stdio.h> /* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = 0; fahr <= 300; fahr = fahr + 20) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); } }}} == 温度对照表第4版 == {{{#!cplusplus #include <stdio.h> #define LOWER 0 /* lower limit of table */ #define UPPER 300 /* upper limit */ #define STEP 20 /* step size */ /* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); } }}} |
1. 第一个C语言程序
写一个程序,在屏幕上输出hello, world
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 }
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 }
4. 温度对照表第3版
对于某个特定任务我们可以采用多种方法来编写程序。
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 }