c程序設(shè)計語言第二版答案_第1頁
c程序設(shè)計語言第二版答案_第2頁
c程序設(shè)計語言第二版答案_第3頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、c 程序設(shè)計語言第二版答案篇一: c 語言程序設(shè)計現(xiàn)代方法 第二版習題答案】answers to selected exercises2. was #2 (a) the program contains one directive (#include) and four statements (three calls of printf and one return).(b) parkinsons law: work expands so as to fill the time available for its completion.3. was #4 #include stdio.h in

2、t main(void) int height = 8, length = 12, width = 10, volume; volume = height * length * width;printf(dimensions: %dx%dx%dn, length, width, height); printf(volume (cubic inches): %dn, volume); printf(dimensional weight (pounds): %dn, (volume + 165) / 166);return 0;4. was #6 heres one possible progra

3、m:#include stdio.hint main(void)int i, j, k;float x, y, z;printf(value of i: %dn, i);printf(value of j: %dn, j); printf(value of k: %dn, k);printf(value of x: %gn, x); printf(value of y: %gn, y);printf(value of z: %gn, z);return 0;when compiled using gcc and then executed, this program produced the

4、following output:value of i: 5618848value of j: 0value of k: 6844404value of x: 3.98979e-34value of y: 9.59105e-39 value of z: 9.59105e-39 the values printed depend on many factors, so the chance that youll get exactly these numbers is small.5. was #10 (a) is not legal because 100_bottles begins wit

5、h a digit.8. was #12 there are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.answers to selected programming projects4. was #8; modified#include stdio.hint main(void)float original_amount, amount_with_tax; printf(enter an amount: );scanf(%f, original_amount); amount_with_tax = original_amo

6、unt * 1.05f;printf(with tax added: $%.2fn, amount_with_tax); return 0;the amount_with_tax variable is unnecessary. if we remove it, the program is slightly shorter:#include stdio.hint main(void)float original_amount; printf(enter an amount: ); scanf(%f, original_amount);printf(with tax added: $%.2fn

7、, original_amount * 1.05f); return 0;chapter 3answers to selected exercises2. was #2(a) printf(%-8.1e, x);(b) printf(%10.6e, x);(c) printf(%-8.3f, x);(d) printf(%6.0f, x);5. was #8 the values of x, i, and y will be 12.3, 45, and .6, respectively. answers to selected programming projects1. was #4; mo

8、dified #include stdio.h int main(void)int month, day, year; printf(enter a date (mm/dd/yyyy): ); scanf(%d/%d/%d, month, day, year);printf(you entered the date %d%.2d%.2dn, year, month, day); return 0;3. was #6; modified #include stdio.hint main(void) int prefix, group, publisher, item, check_digit;

9、printf(enter isbn: );scanf(%d-%d-%d-%d-%d, prefix, group, publisher, item, check_digit);printf(gs1 prefix: %dn, prefix); printf(group identifier: %dn, group); printf(publisher code: %dn, publisher); printf(item number: %dn, item); printf(check digit: %dn, check_digit);/* the five printf calls can be

10、 combined as follows: printf(gs1 prefix: %dngroup identifier: %dnpublisher code: %dnitem number: %dncheck digit: %dn, prefix, group, publisher, item, check_digit);*/return 0;chapter 4answers to selected exercises2. was #2 not in c89. suppose that i is 9 and j is 7. the value of (-i)/j could be eithe

11、r or ., depending on the implementation. on the other hand, the value of -(i/j) is always ,regardless of the impleme ntati on. in c99, on the other hand, the value of (-i)/j must be equal to the value of -(i/j).9. was #6(a) 63 8(b) 3 2 1(c) 2 -1 3(d) 0 0 013. was #8 the expression +i is equivalent t

12、o (i += 1). the value of both expressions is i after the increment has been performed. answers to selected programming projects2. was#4#include stdio.hint main(void)int n;printf(enter a three-digit number: );scanf(%d, n);printf(the reversal is: %d%d%dn, n % 10, (n / 10) % 10, n / 100); return 0;chap

13、ter 5answers to selected exercises2. was #2(a) 1(b) 1(c) 1(d) 14. was #4 (i j) - (i j)6. was #12 yes, the statement is legal. when n is equal to 5, itdoes nothing, since 5 is not equal to-9.10. was #16 the output isonetwosince there are no break statements after the cases.answers to selected program

14、ming projects2. was #6【篇二: c 語言與程序設(shè)計 -第 2 章課后習題參考答案】txt> 關(guān)鍵字是注釋空白符八進制常量是三字符序列字符串常量是括號是2.2 c 編譯器可將以下每一個源字符串分解為哪些記號?不必考 慮記號組合是否合法(1) x+y x, +, +, y(2) -0xabl -, 0xabl(3) 2.89e+12l 2.89e+12l(5) x*2 x, *, *, 2(6) x?/ x?/(7) a?ba, ?, b(8) x-+=y x, -, +=, y(9) intx=+10 intx, =, +, 10(10) stringfoo stri

15、ng, foo 這道題當時改的時候有幾個小題改得有錯誤,注意!2.3 以下哪些不是標識符,為什么? 標識符由字母、數(shù)字和下劃線組成,但首字符必須是字母或下劃線。 4th 不是,以數(shù)字開頭; sizeof 不是 ( 標準 c 的關(guān)鍵字 )_limit 是_is2 是 xyshould 是 x*y 不是, * 非法 o_no_o_no 是 temp-2 不是, - 非法 isnt 不是, 非法 enum 不是 ( 標準 c 的關(guān)鍵字。注:關(guān)鍵字也稱為保存字,是被系統(tǒng) 賦予特定含義并有專門用途的標識符。關(guān)鍵字不能作為普通標識符, 但可以作為宏名。所有預處理均發(fā)生在識別這些關(guān)鍵字之前。 )2.4 在以

16、下表示中,哪些是合法常數(shù),哪些是非法常數(shù)?對于合法 常數(shù),指出其類型;對于非法常數(shù),說明其錯誤原因。2l 合法, long 長整型不合法,單引號組中的單引號前需要轉(zhuǎn)義字符.12 合法, double 雙精度浮點型0x1ag 不合法, g 不是 16 進制數(shù)中的符號,也不表示任何類型33333 合法, int 整形a 合法,字符串常量合法,字符串常量0.l 合法, long double 長雙精度浮點型e20 不合法,缺少尾數(shù)局部0377ul 合法, unsigned long 無符號長整型18 不合法,存在非 8 進制位0xa 不合法,不符合十六進制字符碼表示規(guī)那么 xhh0x9cfu 合法,

17、 unsigned int 無符號整形45 合法, char 字符型1.e-5 合法, double 雙精度浮點型0 合法, char 字符型3. f 合法, float 浮點型34 不合法,缺少轉(zhuǎn)義符合法, char 字符型 p35 ,雙引號作為字符常量時既可用圖形符號 也可用轉(zhuǎn)義序列表示a 合法, char 字符型2.6 以下的變量聲明語句中有什么錯誤?(1) int a; b = 5; 第一個分號改為逗號 int a, b=5;(2) doubel h; 關(guān)鍵字錯誤 double h;(3) int x = 2.3; 類型錯誤 float x = 2.3;(4) const long y

18、; 需要賦初值 const long y = 0;(5) float a = 2.5*g; g 未定義變量 int g = 1; float a = 2.5*g;(6) int a = b = 2; b 未定義變量 int a = 2, b = 2;2.7 設(shè)變量說明為: int a = 1, b = 2, c = 3, d; double x = 2.0; y = 7.7; 請給出以下表達式的值。(1) +a*b- 4(2) !a+b/c 0(3) a=-b+c true(4) d=a+,a*=b+1d 為 1, a 為 6(5) d=y+=1/x y 為 8.2, d 為 8(6) abx

19、=yfalse(7) x=(int)y/b+x 為 3.0(8) a-?+a:+aa 為 1(9) a+xa+a 108(10) a=0,-a,a+=(a+)-a 表達式結(jié)果為 -1,a 的值為 02.8 設(shè) i 和 j 是 int 類型, a 和 b 是 double 類型,以下表達式哪些是 錯誤的,為什么?(1) a=b=c 錯誤, c 未定義且邏輯錯誤aA045正確(3) 7+i*-j/3 正確(4) 39/-+i-+29%j 正確(5) a*+-b 錯誤, + 需要左值a|bAi錯誤,a號左側(cè)類型為double(7) i*j%a 錯誤, %右側(cè)類型為 double(8) i/j2 正確

20、(9) a+=i+=1+2 正確(10) int(a+b) 正確, vc+ 下可運行2.9 下面代碼的執(zhí)行結(jié)果是什么?char a = 1, b = 2, c = 3;printf(%d,%d,%d,%dn,sizeof(c), sizeof(a), sizeof(c=a), sizeof(a+b+7.7);結(jié)果: 1,4,1,82.10 設(shè)變量說明為:unsigned short x = 1, y = 2, z = 4, mask = 0xc3, w; short v;請給出以下表達式的值。(1) xx 0(2) v=x -2(3) w=xAx 65535(4) x|yx|z 5(5) w=

21、y|z,(w3)+(w1) 60(6) w=x|yx|zyAmaskx 113(7) v=-1,v=1-2(8) v=x|x -1(9) w=xAy 65532(10) x|y|z2 32.11 寫一個表達式,將整數(shù) k 的高字節(jié)作為結(jié)果的低字節(jié),整數(shù) p 的低字節(jié)作為結(jié)果的高字節(jié),拼成一個新的整數(shù)。表達式為: 32 位k 24 | (k 2558)8 | (p 25516)8 | p 2416 位(k 8) | (p 8)2.12 寫一個表達式,將整數(shù) x 向右循環(huán)移位 n 位。表達式為: 32 位x(32-(n%32) | x(n%32)16 位x(16-(n%16) | x(n%16)2

22、.13 寫一個表達式,將整數(shù) x 從第 p 位開始的向右 n 位 p 從右至 左編號為 015翻轉(zhuǎn)即 1 變 0,0 變 1,其余各位保持不變。表達式為:xA(0)(p+1-n) (unsigned short)0)(16-p-1)或:x八(0(16-n)(p+1 -n)2.15 表達式 v = (v-1) 能實現(xiàn)將 v 最低位的 1 翻轉(zhuǎn)。比方 v=108 ,其 二進制表示為 01101100 ,那么 v = (v-1) 的結(jié)果是 01101000 。用這一 方法,可以實現(xiàn)快速統(tǒng)計 v 的二進制中 1 的位數(shù),只要不停地翻轉(zhuǎn) v 的二進制數(shù)的最低位的 1 ,直到 v 等于 0 即可。請用該方

23、法重寫例 2-18 。 #includestdio.hint main(void)unsigned char data, backup, t = 0;int parity = 0;data = getchar();backup = data;while(data)t+;data = (data -1);data = backup | (parity7)八(t7);printf(the data is %#xn, backup);printf(parity-check code is %#xn, data);return 0;2.16 寫一個表達式,其結(jié)果是a、b 和 c 這 3 個數(shù)中最大的一

24、個。表達式為:ab?ac?a:c:bc?b:c 或(ab)?(ac?a:c):(bc?b:c) 或(ab)?(ac)?a:c):(bc)?b:c)2.18 寫一個表達式,如果整數(shù) a 能被 3 整除且個位數(shù)字是 5 ,那么結(jié) 果為非 0 ,否那么為 0。 表達式為:a%3 ? 0 : (a%10=5 ? 1 : 0)2.19 定義一個枚舉類型 enum month ,用來描述一年 12 個月:一 月jan、二月feb、十二月dec,并編寫一個程序,根據(jù)用戶輸入的年份,輸出該年各月的英文名及天數(shù)。#includestdio.henum year jan, feb, mar, apr, may,

25、jun, jul, aug, sep, oct, nov,dec;int main(void)2.20 設(shè)變量說明為: float a; double b; char c; int x; 試將以下表 達式中隱含的類型轉(zhuǎn)換改為用強制類型轉(zhuǎn)換運算符轉(zhuǎn)換的表達式。(1) x = (int)(a - (float)(int)c + a)(2) b * (double)x + (double)( (int)c - (int)0 )(3) ( x 0 ) ? (double)a : benum yaer month; int year_num, year_days=365; char *month_nam

26、e =january, february, march, april, may, june, july, august, september, october, november, december; int month_days = 31,28,31,30,31,30,31,31,30,31,30,31; scanf(%d, year_num); if(!(year_num%4) year_num%100 | !(year_num%400) month_days1=29, year_days=366; printf(n%dn, year_days); for(month=jan; month

27、 = dec;month+) printf(%s,%dn, month_namemonth,month_daysmonth); return 0;【篇三: c 語言程序設(shè)計學習指導第二版 答案】.c4.c5.d6.a7.b8.c9.a10.d 11.b 12.c 13.a14.d 15.b 16.b第二章一、1. d2.a3.c4.d5.a6.b7.a8.b9.a 10.d 11.b 12.c 13.c14.d 15.c 16.b 17.d 18.a 19.c 20.b 21.b 22.d 23.a 24.b25. b 26.b27.b第三章一、1. d2.c3.a4.c5.a6.c7.c8.

28、d 9.a 10.d 11.a13.c 14.c 15.a 16.b 17.a 18.a 19.c 20.a 21.b 22.d 23.b 24.b25.d26. d 27.c 28.c第四章一、1. b2.c3.d4.d5.d6.c7.b8.d9.c 10.d 11.b 12.a 13.c14.c 15.b 16.ab 17.d1.double fun(int m) double y=0;y=sin(m)*10;return(y);2. float fun ( float h )return (long)( h * 100 + 0.5 )/ 100.0;3. double fun(doubl

29、e m) float n;n=(5.0/9.0)*(m-32);return n; 4. char fun(char c)c=c+32;return c;146f 12.b、1.d2.c3.c4.c5.a6.a7.c8.a9.b 10.b 11.a 12.b 13.c14.c 15.d 16.a 17.d 18.a 19.a 20.a 21.b 22.d 23.c 24.b 25. fun(int n)int bw,sw,gw;bw=n/100;sw=(n-bw*100)/10;gw=n%10;if(n=bw*bw*bw+sw*sw*sw+gw*gw*gw) return 1;el

30、se return 0;2. float fun(float x) float y;if (x0 x!=-3.0) y=x*x+x+6;else if(x=0 x10.0 x!=2.0 x!=3.0) y=x*x-5*x+6;else y=x*x-x-1;return y;3. double y(float x) double z;if(x10) z=exp(x);else if(x-3) z=log(x+3);else z=sin(x)/(cos(x)+4); return(z); 4.int fun(int x) int k;k=x*x; if(k%10=x)|(k%100=x) retu

31、rn 1;else return 0; 第六章1.c2.c3.d4.b5.c6.a7.a8.a9.d 10.a 11.d 12.c 13.c14.c 15.c 16.a 17.a 18.b 19.a 20.d 21.b 22.c 23.c 24.d 25.b26.b27. c 28.a1. 位置 1:r!=0 【或】 0!=r 【或】 r位置 2:r=m%n 【或】 r=m-m/n*n位置 3:n位置 4:gcd,lcm 【或】 n,lcm2. 位置 1:k=0位置 2:n%10 【或】 n-n/10*10 【或】 n-10*(n/10)位置 3:while(n0) 【或】 while(0n)

32、 【或】 while(n!=0) 【或】 while(0!=n) 位置 4:printf(n)3. 位置 1:x!=0 【或】 x或】位置 2:else 【或】 else if(x%2=1) 【或】 else if(x%2!=0) if(x%2) 位置 3:scanf(%d,x)位置 4:av2=s2/j4. 位置 1:n=0位置 2:i=300 【或】 i300 【或】 300=i 【或】 300i位置 3:i%7=0|i%17=0 【或】 !(i%7)|!(i%17)【或】 !(i%17)|!(i%7) 【或】 !(i%7i%17)或】 i%17=0|i%7=0位置 4:n%5=0 【或】

33、 !(n%5) 【或】 n/5*5=n5. 位置 1:s=0位置 2:i+=2 【或】 i=i+2 【或】 i=2+i 【或】 i+,i+位置 3:j=i 【或】 i=j 【或】 ji+1 【或】 i+1j 【或】 j1+i 【或】1+ij 位置 4:f=f*j 【或】 f=j*f三、1. 位置 1:#include math.h 【或】 #include math.h位置 2:float s=0,t=1,p=1; 【或】 float s=0,p=1,t=1; 【或】 float p=1,s=0,t=1; 【或】 float p=1,t=1,s=0; 【或】 float t=1,p=1,s=0

34、; 【或】 float t=1,s=0,p=1;位置 3:while(fabs(t)1e-4) 【或】 while(0.0001fabs(t) 【或】 while(1e-4fabs(t)【或】 while(fabs(t)0.0001)位置 4:printf(pi=%fn,s*4); 【或】 printf(pi=%fn,4*s);2. 位置 1:printf(%8.0f,f1);【或】 printf(%f,f1);【或】printf(%8f,f1);位置 2:for(i=1;i20;i+)【或】 for(i=1;20i;i+)【或】for(i=2;i=20;i+) 【或】for(i=2;20=i

35、;i+) 【或】for(i=1;i=19;i+)【或】 for(i=1;19=i;i+)位置 3:f1=f2;位置 4:f2=f3;3. 位置 1:long k=1;位置 2: scanf(%ld,n);位置 3: n/=10; 【或】 n=n/10;4. 位置 1:scanf(%d,n);位置 2:for(i=1;i=n;i+) 【或】 for(i=1;n=i;i+) 【或】for(i=1;in+1;i+)【或】for(i=1;n+1i;i+)位置 3:s+=1.0/t;【或】s=s+1.0/(float)t;【或】s=1.0/(float)t+s;【或】 s=s+1.0/t;【或】 s=1.0/t+s;【或】s+=1.0/(float)t;【或】s+=1.0/(double)t;【或】 s=s+1.0/(double)t; 【或】 s=1.0/(double)t+s;5. 位置 1:sum=1.0; 【或】 sum=1;位置 2:s2=1.0; 【或】 s2=1;位置 3:for(k=4;k=n;k+) 【或】 for(k=4;n=k;k+) 【或】 for(k=4;kn+1;k+) 【或】 for(k=4;k1+n;k+) 【或】 for(k=4;n+1k;k+) 【或】 for(k=4;1+n

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論