第1章-导言-习题1.8-1.12

前面习题比较简单,根据教材就可以做出来,下面记录从习题1.8开始。

1-8

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

初始化语句是有必要的,否则输入语句后按下Enter会出错。

我原先用C语言写C51单片机程序时,对于初值为0的变量通常是直接设置变量名就完事(C51编译器会默认赋值为0),这也给我一个提醒:对于一个新定义的变量,若有必要最好加上初值。

 

1-9

我的思路很简单,先由c=getchar()读取一个字符,如果是连续空格的话就不断读入到下一个非空格字符为止,不是的话就直接打印该字符。

 1 #include<stdio.h>
 2 
 3 /* 16/6/8 repacing string of blanks with a single blank */
 4 main()
 5 {
 6     int c;
 7     
 8     while ((c = getchar()) != EOF) {
 9         if (c == ' ') {
10             while ((c = getchar()) == ' ');
11             putchar(' ');
12             putchar(c);
13         }
14         else putchar(c);
15         
16     }
17 }
1-9.c

答案给出了三种写法,亮点是设置整型变量lastc来记录前一个输入字符,开始时将其初始化为符号常量NONBLANK(可通过define设置为任意的非空格字符)。下面给出答案的第三种写法(稍微难理解点)

 1 #include<stdio.h>
 2 #define NONBLANK 'a'
 3 
 4 /* 16/6/15 repacing string of blanks with a single blank */
 5 main()
 6 {
 7     int c;
 8     int lastc;
 9 
10     lastc = NONBLANK;
11     while ((c = getchar()) != EOF) {
12         if (c != ' ' || lastc != ' ')
13             putchar(c);
14         lastc = c;
15     }
16 }
1-9-a.c

 

1-10

 1 #include<stdio.h>
 2 
 3 /* 16/6/8 replace tab,backspace,backslash with the \* form */
 4 main()
 5 {
 6     int c;
 7 
 8     while ((c = getchar()) != EOF) {
 9         //if (c == '\t') putchar('\\t');
10         if (c == '\t') printf("\\t");
11         else if (c == '\b') printf("\\b");
12         else if (c == '\\') printf("\\\\");
13         else putchar(c);
14     }
15 }
1-10.c

刚开始使用类似putchar('\\t')的形式,输出结果如下

换成printf才达到预期效果,注意在C语言中反斜号是用'\\'来表示的,要想输出两个反斜号,需要使用printf输出字符串"\\\\"(应该是前面\\输出一个\,然后后面\\再输出一个\)。

 

1-11

查看答案即可,要点是满足边界条件的输入,比如没有输入、没有单词(只有空格、制表符或换行符)、每个单词各占一行(没有空格和制表符)、单词出现在一串空格之后。

 

1-12

在教材P14例程基础上写的,思路是当前读到的字符在单词里面(state=IN)的话就打印出来,一直到下一个空格/制表符/回车符为止,并打印回车符\n另起一行。

 1 #include<stdio.h>
 2 
 3 #define IN 1
 4 #define OUT 0
 5 
 6 /* 16/6/8 以每行一个单词的形式输出 */
 7 main()
 8 {
 9     char state = OUT;
10     char c;
11 
12     while ((c = getchar()) != EOF) {
13         if (c == ' ' || c == '\t' || c == '\n') {
14             state = OUT;
15         }
16         else if(state == OUT) {
17             state = IN;
18         }
19         while (state == IN) {
20                 putchar(c);
21                 c = getchar();
22                 if (c == ' ' || c == '\t' || c == '\n') {
23                     state = OUT;
24                     putchar('\n');
25                 }
26         }
27     }
28 }
1-12.c

答案写的更好一些,这里放上源代码体会一下。

 1 #include<stdio.h>
 2 
 3 #define IN 1
 4 #define OUT 0
 5 
 6 /* 16/6/15 以每行一个单词的形式输出 */
 7 main()
 8 {
 9     char state = OUT;
10     char c;
11 
12     while ((c = getchar()) != EOF) {
13         if (c == ' ' || c == '\t' || c == '\n') {
14             if (state == IN) {
15                 putchar('\n');
16                 state = OUT;
17             }
18         }
19         else if (state == OUT) {
20             state = IN;
21             putchar(c);
22         }
23         else putchar(c);
24     }
25 }
1-12-a.c

 

posted @ 2016-07-30 15:35  cust渔舟唱晚  阅读(131)  评论(0编辑  收藏  举报