资源预览内容
第1页 / 共24页
第2页 / 共24页
第3页 / 共24页
第4页 / 共24页
第5页 / 共24页
第6页 / 共24页
第7页 / 共24页
第8页 / 共24页
第9页 / 共24页
第10页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 contains 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 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 = 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()float 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-6Verify 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(void)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, blanks, 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;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);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 = 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 challenging. #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(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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号