我是靠谱客的博主 危机心锁,这篇文章主要介绍C primer Plus(6th edition )chapter 8学习笔记,现在分享给大家,希望可以做个参考。

``复习题

  1. putchar(getchar())是一个有效的表达式:使程序读取下一个输入字符并打印出来;
    getchar(putchar())是无效表达式,因为getchar()不需要参数,而putchar()需要一个参数;
  2. putchar(‘H’)显示字符H;putchar(’07’):如果系统是ASCII,则发出一声警报;
    putchar(’n’):把光标移至下一行开始;putchar(’b’)退后一格;
  3. count < essay > essayct 或者 count < essayct < essay
  4. 都不是有效命令;
  5. EOF是getchar()和scanf()返回的信号(一个特殊值),表明函数检测到文件结尾。
  6. C的标准I/O库把不同的文件映射为统一的流来处理;
  7. 数值会跳过空格和换行符,但字符输入不会。假设有以下代码:
    int score;
    char grade;
    printf(“Enter the score.n”);
    scanf("%s",&score);
    printf(“Enter the letter grade.n”);
    grade = getchar();
    编程练习题
    8.1 设计一个程序,统计在读到文件结尾之前读取的字符数。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h> int main(void) //这个程序以EOF为程序输入终止条件,但只有在输入Enter后继续输入Ctrl+Z程序才会输出结果 //目前我无法解释原因,我想的是getchar()读到^z就应该输出结果,但是并没有 { int ch; int ct; while ((ch = getchar()) != EOF) ct++; printf("%d characters are read.n", ct) ; return 0; }

8.2 编写一个程序,在遇到EOF之前,把输入作为字符流读取。程序要打印每个输入的字符及其相应的ASCII十进制。注意,在ASCII序列中,空格字符前面的字符都是非打印字符,要特殊处理这些字符。如果非打印字符是换行符或制表符,则分别打印n或t。否则,使用控制祖父表示法。例如,ASCII的1是Ctrl+A,可显示为^A。注意A的ASCII值是Ctrl+A的值加上64.其他非打印字符也有类似的官司。除每次遇到换行符打印新的一行之外每行打印10对值。(注意:不同的操作系统其控制字符可能不同。)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h> int main(void) //这题没理解,代码搬运的,懵逼! { char ch; int i; for(i=1; (ch=getchar()) != EOF; i++) { if (ch >= ' ' || ch == 'n' || ch == 't') printf("%-5c",ch); else printf("^%-4c",ch+64); printf("%-5d",ch); if(i%8 == 0) printf("n"); } return 0; }

8.3 编写一个程序,在遇到EOF之前,把输入作为字符流读取。改程序要报告输入中的大写字母和小写字母的个数。假设大小写字母数值是连续的。或者使用ctype.h库中合适的分类函数更方便。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h> #include<ctype.h> int main(void) { int ch; unsigned lct = 0; unsigned uct = 0; unsigned oct = 0; while((ch = getchar()) != EOF) { if (isupper(ch)) uct++; else if (islower(ch)) lct++; else oct++; } printf("%lu uppercase characters readn",uct); printf("%lu lowercase charactera readn",lct); printf("%lu other characters readn",oct); return 0; }

4、 编写一个程序,把输入作为字符流读取,直到遇到EOF。令其报告每个单词的平均字母数。不要将空白字符记为单词中的字母。实际上标点符号也不应该计算,但现在不必考虑这一点(如果您想做的好一些,可以考虑使用ctype.h中的ispunct()函数)。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h> #include <ctype.h> #include <stdbool.h> #define STOP '|' int main (void) { char c; //读入字符 char prev; //前一个读入字符 long n_chars = 0L; //字符数 int n_lines = 0; //行数 int n_words = 0; //单词数 int p_lines = 0; //不完整的行数 bool inword = false; //标志:如果c在一个单词中,则inword等于true printf("Enter text to be analyzed (| to terminate): n"); prev='n'; //用于识别完整的行 while ((c=getchar())!=STOP) { n_chars++; //统计字符 if (c=='n') n_lines++; //统计行数 if(!isspace(c) && !inword)//如果c不是空白字符,且c不在一个单词里 { inword = true;//开始一个新单词 n_words++; //统计单词 } if(isspace(c) && inword) inword = false;//到达单词尾部 prev = c; //保存字符值 } if(prev!='n') p_lines = 1; printf("characters = %ld,words = %d,lines = %d ", n_chars,n_words,n_lines); printf("partial lines = %dn",p_lines); return 0; }

5、修改程序清单8.4中的猜测程序,使其使用更智能的猜测策略。例如,程序最初猜50,让其询问用户该猜测值是大是小还是正确。如果该猜测值小,则下一次猜测值为50和100的中值,即75.如果75大,则下一次猜测75和50的中值,等等。使用这种二分搜索策略。起码如果用户没有欺骗,该程序很快会获得正确答案。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h> int main (void) { int guess,max=100,min=1; char response; printf("Pick an integer from 1 to 100.I will try to guess "); printf("it.nRespond with a b if my guess is big and with"); printf("nan l if it is little.n"); printf("Also,Respond a y if it is right.n"); printf("Uh...is your number %d?n", guess = ( max + min ) / 2 ); while ((response=getchar())!='y') { if(response=='b') { max=guess-1; printf("Well,then,is it %d?n",guess = ( max + min ) / 2 ); } else if (response=='l') { min=guess+1; printf("Well,then,is it %d?n",guess = ( max + min ) / 2 ); } else printf("Sorry,I understand only y or n.n"); while(getchar() != 'n'); } printf("I know I cloud do it.n"); return 0; }

6、修改程序清单8.8中的get_first()函数,使其返回遇到的第一个非空白字符,在一个简单的程序中测试该函数。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h> char get_first(void); int main (void) { char ch; while ((ch=get_first())!=EOF) { putchar(ch); } return 0; } char get_first(void) { int ch; while (isspace(ch=getchar())); /*获取一个字符并赋给ch,如果是空白字符则被丢弃*/ while (getchar()!='n');/*跳过本行剩余部分*/ return ch; }

7、修改第7单中的练习8,使菜单选项由字符代替数字进行标记。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h> #include<ctype.h> char get_first(void); //b.加班 #define TIME 40 //加班(超过TIME小时) = #define ADD 1.5 //ADD倍的时间 //c.税率 #define LIMIT1 300 //前LIMIT1美元为RATE1 #define RATE1 0.15 #define LIMIT2 150 //下一个LIMIT2美元为RATE2 #define RATE2 0.20 #define RATE3 0.25 //余下的位RATE3 int main(void) { double basic,hours,gross,tax; printf("Enter the number corresponding to the desired pay rate or action:n"); printf("1) $8.75/hrttt2) $9.33/hrn"); printf("3) $10.00/hrttt4) $11.20/hrn"); printf("5) quitn"); switch( get_first() ) { case '1': basic = 8.75; break; case '2': basic = 9.33; break; case '3': basic = 10.00; break; case '4': basic = 11.20; break; default: printf("quitn"); return(0); //退出程序 } printf("you have select $%.2lfn",basic); printf("input the work hours of a week:"); scanf("%lf",&hours); if (hours > 40) hours = 40 + (hours - 40) * 1.5; gross = hours * basic; printf("gross income:tt%lfn",gross); if (gross <= LIMIT1) tax = gross * RATE1; else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2; else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3; printf("tax:ttt%lfn",tax); printf("net income:tt%lfn",gross - tax); return(0); } char get_first(void) //得到字符串中的第一个非空字符 { int ch; while( isspace( ch = getchar() ) ); while ( getchar() != 'n'); return ch; }

8、编写一个程序,显示一个菜单,为您提供加法、减法、乘法或除法的选项。获得您的选择后,该程序请求两个数,然后执行您选择的操作。该程序应该只接受它所提供的菜单选项。它应该使用float类型的数,并且如果用户未能输入数字应允许其重新输入。在除法的情况中,如果用户输入0作为第二个数,该程序应该提示用户输入一个新的值。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h> char get_choice(void); char get_first(void); float get_float(void); int main(void) { char choice; float num1,num2; while((choice=get_choice())!='q') { printf("Enter first number:"); num1 = get_float(); printf("Enter second number:"); num2 = get_float(); while( choice == 'd' && num2 == 0) { printf("Enter a number other than 0:"); num2 = get_float(); } switch (choice) { case 'a': printf("%.2f + %.2f = %.2fn",num1, num2, num1 + num2); break; case 's': printf("%.2f - %.2f = %.2fn",num1, num2, num1 - num2); break; case 'm': printf("%.2f * %.2f = %.2fn",num1, num2, num1 * num2); break; case 'd': printf("%.2f / %.2f = %.2fn",num1, num2, num1 / num2); break; default: break; } } printf("Bye.n"); return 0; } char get_choice(void) /*显示选项,并返回选项*/ { char choice; printf("Enter the operation of your choice:n"); printf("a.add s.subtract:n"); printf("m.multiply d.dividen"); printf("q.quitn"); choice = get_first(); while( choice != 'a' && choice != 's' && choice != 'm' && choice != 'd' && choice != 'q') { printf("Please respond with a,s,m,d, or q.n"); choice=get_first(); } return choice; } char get_first(void) { int ch; while( isspace( ch = getchar() ) ); while ( getchar() != 'n'); return ch; } float get_float(void) { float num; char str[40]; while ((scanf("%f",&num))!=1) { gets(str); printf("%s is not a number.n",str); printf("Please enter a number such as 2.5, -1.78E8, or 3:"); } while (getchar()!='n'); /*不要忘记跳过多余的输入部分*/ return num; }

最后

以上就是危机心锁最近收集整理的关于C primer Plus(6th edition )chapter 8学习笔记的全部内容,更多相关C内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部