c语言程序设计习题答案之序言[K&R]

之前已经写过序言中的理论部分内容,以下是每章节的习题答案,程序运行环境为vc++6.0。之所以选择vc++6.0是因为电脑配置的原因,虽然已经很OUT了,但对于程序来讲能跑就满足。

习题1-1:在自己的系统中运行“hello world”程序,再有意去掉程序中部分内容,看看会到到什么出错信息。

hello world
1 #include <stdio.h>
2 
3 /* K&R最为经典的程序设计 */
4 void main()
5 {
6     printf("hello world");
7 }

程序中将'\n'去掉,'\n'表示换行。有兴趣童鞋可将双引号去除或者程序末尾的分号去除再运行试试有无报错。

RT:

习题1-2:做个实验,但printf函数的参数字符中包含\c(其中c是上面的转义字符序列中未曾列出的某一个字符时,观察一下会出现什么情况。

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

此程序在编译时会有警告:warning C4129: 'c' : unrecognized character escape sequence。出现警告是因为'\c'不是转义字符,如果要转义就得使用C的转义序列,如果要单纯的输出'\'则需进行转义'\\',这样就可以输出'\'。

注意:'\n'只代表一个字符,类似于'\n'的转义字符序列为表示无法输入的字符或不可见字符提供了一种通用可扩充的机制。如:'\t'表示制表符;'\b'表示回退符;'\"'表示双引号;'\\'表示反斜杠本身。

习题1-3:修改温度转换程序,使之能在转换表的顶部打印一个标题。

View Code
 1 #include <stdio.h>
 2 #define MAXHIGH 300 /* 温度表的上限 */
 3 
 4 /* 在温度转换程序的顶部增加一个标题 */
 5 void main()
 6 {
 7     int low, temp, celsius;
 8     low = 0;     /* 温度表的下限 */
 9     temp = 20;   /* 步长 */
10     printf("The following is printed with degrees Celsius temperature Fahrenheit table:\n");
11     for (; low <= MAXHIGH; low += temp) {
12         celsius = 5 * (low-32) /9;  /* 转换后的温度 */
13         printf("%d\t%d\n", low, celsius);
14     }
15 }

RT:

习题1-4:编写一个程序打印摄氏温度转换为相应华氏温度的转换表。

View Code
 1 #include <stdio.h>
 2 #define MAXHIGH 300 /* 温度表的上限 */
 3 
 4 /* 打印华氏温度到摄氏温度的对照表 */
 5 void main()
 6 {
 7     float fahr, celsius;  /* 声明更具体的浮点型 */
 8     int low, temp;
 9     low = 0;     /* 温度表的下限 */
10     temp = 20;   /* 步长 */
11     celsius = low; /* 摄氏温度 */
12     while (celsius <= MAXHIGH) {
13         fahr = (9.0*celsius) / 5.0 + 32.0;
14         printf("%3.0f\t%6.1f\n", celsius, fahr);
15         celsius += temp;
16     }
17 }

RT:

由于此程序使用了float,编译过程中会出现警告,可忽略直接看结果。警告如下,11行:conversion from 'int' to 'float', possible loss of data;13行:conversion from 'double' to 'float', possible loss of data,int到float是窄到宽的转型不应该出现丢失数据的现象,不解,第二个也不明白为什么,如果有了解其机制的还望指点一下。

习题1-5:修改温度转换程序,要求以逆序(即按照从300度到0度的顺序)打印温度转换表。

View Code
 1 #include <stdio.h>
 2 
 3 /* 逆序打印摄氏温度到华氏温度的对照表 */
 4 void main()
 5 {
 6     int high, temp, celsius;
 7     high = 300;     /* 温度表的上限 */
 8     temp = 20;   /* 步长 */
 9     printf("fahr\tcelsius\n");
10     while (high >= 0) {
11         celsius = 5 * (high-32) / 9;
12         printf("%d\t%d\n", high, celsius);
13         high -= temp;
14     }
15 }

RT:

习题1-6:验证表达式getchar() != EOF的值是0还是1。

View Code
 1 #include <stdio.h>
 2 
 3 /* 验证getchar() != EOF的值 */
 4 void main()
 5 {
 6     char c;
 7     while ((c=getchar()) != EOF)
 8         ;
 9     printf("EOF value: %d\n", c);
10 }

RT:

习题中让验证getchar() != EOF的值是0还是1,其实这个值和你的开发环境有关,我使用的vc++6.0,它的环境即是vc98,STDIO.H头文件中默认值其实是:#define EOF     (-1),大家可以在你的开发环境的include文件下来查看此值,然后可以使用此程序进行验证。

注:结束程序则用【ctrl+z】键即可

习题1-7:编写一个打印EOF值的程序。

View Code
1 #include <stdio.h>
2 
3 /* 打印EOF的值 */
4 void main()
5 {
6     printf("EOF value: %d\n", EOF);
7 }

RT:

习题1-8:编写一个统计空格、制表符与换行符个数的程序。

View Code
 1 #include <stdio.h>
 2 
 3 /* count blanks,tabs,and newlines */
 4 void main()
 5 {
 6     int c, nb, nt, nl;
 7     nb = 0; /* number of blanks */
 8     nt = 0; /* number of tabs */    
 9     nl = 0; /* number of newlines */
10     while((c = getchar()) != EOF)
11         if (c == ' ')
12             ++nb;
13         else if (c == '\t')
14             ++nt;
15         else if (c == '\n')
16             ++nl;
17     printf("%d\t%d\t%d\n",nb, nt, nl);
18 }

RT:

习题1-9:编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替。

View Code
 1 #include <stdio.h>
 2 #define NONBLANK 'a'  /* 定义一个区别空格符的字符 */
 3 
 4 /* 将输入复制到输出,并将多个连续的空格用一个空格代替 */
 5 void main()
 6 {
 7     char c, nextchar;
 8     nextchar = NONBLANK; /* 用来保存下一个输入的字符是否为空字符 */
 9     while ((c=getchar()) != EOF) {
10         if (c == ' ') {
11             if (nextchar != ' ')
12                 putchar(c);
13         } else
14             putchar(c);
15         nextchar = c;
16     }
17 }

RT:

习题1-10:编写一个将输入复制到输出的程序,并将其中的制表符替换为\t,把回退符替换为\b,把反斜杠替换为\\。这样可以将制表符和回退符以可见的方式显示出来。

View Code
 1 #include <stdio.h>
 2 
 3 /*  releace tabs and backspace with visible characters */
 4 void main()
 5 {
 6     int c;
 7     while ((c = getchar()) != EOF) {
 8         if (c == '\t')
 9             printf("\\t");
10         else if (c == '\b')
11             printf("\\b");
12         else if (c == '\\')
13             printf("\\\\");
14         else
15             putchar(c);
16     }
17 }

程序中测试回退符无任何效果,还请高人指点下。

习题1-11:略...

习题1-12:编写一个程序,以每行一个单词的形式打印其输入。

View Code
 1 #include <stdio.h>
 2 
 3 /* 以每行一个单词的形式打印输入 */
 4 void main()
 5 {
 6     char c;
 7     while ((c=getchar()) != '\n' ) {
 8         if ((c>='a'&&c<='z' || (c>='A'&&c<='Z'))) {
 9             putchar(c);
10             printf("\n");
11         }
12     }
13 }

RT:

习题1-13:编写一个程序,打印输入中单词长度的直方图。水平方向的直方图比较容易,垂直方向的直方图要困难些。

View Code
 1 #include <stdio.h>
 2 #define IN 1     /* 在单词内部 */
 3 #define OUT 0    /* 在单词外部 */
 4 #define MAXLEN  15 /* 单词的最大长度 */
 5 #define MAXHIST 20 /* 直方图的最大长度 */
 6 #define MAXWORD 100 /* 限制输入单词的上限 */
 7 
 8 int wl[MAXWORD];    /* 用于存放单词长度的数组 */
 9 
10 /* 打印水平方向的直方图 */
11 void main()
12 {
13     char c;
14     int len = 0;    /* 当前单词的长度 */
15     int wc = 0; /* 统计单词的个数 */
16     int state = OUT;  /* 状态初始化为在单词外部 */
17     for (int k=0; k<MAXWORD; k++)
18         wl[MAXWORD] = 0;            /* 数组进行初始化 */
19     while ((c=getchar()) != EOF) {
20         if (c == ' ' || c == '\t' || c == '\n') {
21             if (state == IN) {
22                 if (len > MAXLEN)
23                     len = MAXLEN;
24                 wl[wc++] = len;
25             }
26             len = 0;
27             state = OUT;
28         } else if (state == OUT) {
29             state = IN;
30             len++;
31         } else
32             len++;
33     }
34     if (wc <= 0) {
35         printf("no words!\n");
36         return;
37     }else if (wc > MAXWORD)
38         wc = MAXWORD;
39     for (int j=0; j<wc; j++) {
40         while (wl[j] > 0) {
41             printf("*");
42             wl[j]--;
43         }
44         printf("\n");
45     }
46 }

RT:

先到这里吧,后续章节会陆续补上。都是本人亲测亲写的代码,如有错误还请不吝赐教。由于能力有限,还望各位看客手下留情,切勿拍砖哈!

后续答案

 

posted @ 2012-06-13 02:52  飘零de思绪  阅读(2787)  评论(2编辑  收藏  举报