C程序设计语言 (第二版) 课后答案第一章

上传人:公**** 文档编号:563482618 上传时间:2023-04-29 格式:DOCX 页数:24 大小:24.45KB
返回 下载 相关 举报
C程序设计语言 (第二版) 课后答案第一章_第1页
第1页 / 共24页
C程序设计语言 (第二版) 课后答案第一章_第2页
第2页 / 共24页
C程序设计语言 (第二版) 课后答案第一章_第3页
第3页 / 共24页
C程序设计语言 (第二版) 课后答案第一章_第4页
第4页 / 共24页
C程序设计语言 (第二版) 课后答案第一章_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《C程序设计语言 (第二版) 课后答案第一章》由会员分享,可在线阅读,更多相关《C程序设计语言 (第二版) 课后答案第一章(24页珍藏版)》请在金锄头文库上搜索。

1、Chapter 1Exercise 1-1Run the “hello world” program on your system. Experiment with leaving out parts of the program, to see what error message you get.#include int main()printf(hello, ); printf(world);printf(n);return 0;Exercise 1-2Experiment to find out what happens when printfs argument string con

2、tains c, where c is some character not list above.Exercise 1-3Modify the temperature conversion program to print a heading above the table. #includeint main()float fahr, celsius;float lower, upper, step;lower = 0;upper = 300;step = 20;fahr = lower;printf(Fahrenheit temperatures and their centigrade

3、or Celsius equivalentsn); while (fahr = upper)celsius = (5.0/9.0) * (fahr-32.0); printf(%3.0f %6.1fn, fahr, celsius);fahr = fahr + step;return 0;Exercise 1-4Write a program to print the corresponding Celsius to Fahrenheit table. #includeint main()float fahr, celsius;float lower, upper, step;lower =

4、0;upper = 300;step = 20;celsius = lower;while (celsius= upper)fahr = 9.0*celsius/5.0+32; printf(%3.0f %6.1fn, celsius,fahr);celsius = celsius + step;return 0;Exercise 1-5Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.#includeint main()fl

5、oat fahr, celsius;float lower, upper, step;lower = 0;upper = 300;step = 20;fahr = upper;printf(Fahrenheit temperatures and their centigrade or Celsius equivalentsn); while (fahr = lower)celsius = (5.0/9.0) * (fahr-32.0); printf(%3.0f %6.1fn, fahr, celsius);fahr = fahr - step;return 0;Exercise 1-6Ver

6、ify that the expression getchar() != EOF is 0 or 1.#include main()int c;c =(getchar() != EOF);printf(%d,c);return 0;Exercise 1-7Write a program to print the value ofEOF . #include int main() printf(%d,EOF); return 0;Exercise 1-8Write a program to count blanks, tabs, and newlines. #include int main(v

7、oid)int blanks, tabs, newlines;int c;int done = 0;int lastchar = 0; blanks = 0;tabs = 0; newlines = 0;printf(输入0査看结果n);while(done = 0)c = getchar();if(c = ) +blanks;if(c = t)+tabs;if(c = n)+newlines;if(c = 0)if(lastchar != n)+newlines;done = 1;lastchar = c;printf(空格的个数为:%dn制表符的个数为:%dn换行符的个数为:%dn, bl

8、anks, tabs, newlines);return 0;Exercise 1-9Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.#include int main(void)int c;int inspace;inspace = 0;while(c = getchar() != EOF)if(c = )if(inspace = 0)inspace = 1;putchar(c);if(c != )inspace = 0

9、;putchar(c);return 0;Exercise 1-10Write a program to copy its input to its output, replacing each tab by t , each backspace by b , and each backslash by . This makes tabs and backspaces visible in an unambiguous way.#includeint main()int c;while(c=getchar()!=EOF)if (c=t) printf(t);if (c=b) printf(b)

10、;if (c=)printf();if (c!=t)if (c!=b)if (c!=)putchar(c);return 0;Exercise 1-11How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?Exercise 1-12Write a program that prints its input one word per line.#include int main(void)int c;int asd;asd =

11、0;while(c = getchar() != EOF)if(c = | c = t | c = n)if(asd = 0)asd = 1;putchar(n);elseasd = 0; putchar(c);return 0;Exercise 1-13Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challen

12、ging. #include #define MAXWORDLEN 10int main(void)int c;int inspace = 0;long lengtharrMAXWORDLEN + 1;int wordlen = 0;int firstletter = 1;long thisval = 0; long maxval = 0;int thisidx = 0;int done = 0;for(thisidx = 0; thisidx = MAXWORDLEN; thisidx+)lengtharrthisidx = 0;while(done = 0)c = getchar();if

13、(c = | c = t | c = n | c = EOF)if(inspace = 0)firstletter = 0; inspace = 1;if(wordlen 0)thisval = +lengtharrwordlen - 1; if(thisval maxval) maxval = thisval;elsethisval = +lengtharrMAXWORDLEN; if(thisval maxval)maxval = thisval;if(c = EOF)done = 1;else if(inspace = 1 | firstletter = 1) wordlen = 0;firstletter = 0;inspace = 0; +wordlen;for(thisval = maxval; thisval 0; thisval-)printf(%4d | , thisval);for(thisidx = 0; thisidx = thisval)printf(* );elseprintf( ); printf

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号