C++課后習(xí)題答案2_習(xí)題和解答(第3版)_第1頁
C++課后習(xí)題答案2_習(xí)題和解答(第3版)_第2頁
C++課后習(xí)題答案2_習(xí)題和解答(第3版)_第3頁
C++課后習(xí)題答案2_習(xí)題和解答(第3版)_第4頁
C++課后習(xí)題答案2_習(xí)題和解答(第3版)_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、習(xí)題2及其解答2. 1選擇題1 .已知 int i=0, x=l,(a) y = 1 ; if( x = 0 ) (b) y = -1 ;if( x = 0 ) y =if ( x != 0 )0 ; else y = t;if( x 0 ) y=1 else y =(c) if( x = 0 )0;else y = 0 ;(d)else y = 1 ;if ( x = 0 )if ( x 0 ) y =-在下列選項(xiàng)使i的值變成1的語句是(c )(a) if ( x&y ) i+ ;(c) if( x|y ) i+ ;12.設(shè)有函數(shù)關(guān)系為y二01(b) if ( x=y ) i+ ;(d) i

2、f( !x )x0xoxoi+ ;,下面選項(xiàng)中能正確表示上述關(guān)系為3.假設(shè)i=2 ,執(zhí)行下列語句后i的值為(b ) switch( i ) case 1 : i + ;case 2 : i ;case 3 : + i ; break ;case 4 : i ;default : i + ;(a) 1(b) 2(c)3 (d) 4語句執(zhí)行時(shí)循環(huán)次4 .已知 int i=0 , x=0;下面 while數(shù)為(d )。while( !x & i 3 ) x+ ;i+ ; (a) 4(b) 3(c) 2(d) 15 .已知int i=3;下面do_while語句執(zhí)行時(shí)循環(huán)次數(shù)為(do i一;b )。c

3、outiendl; whileil= 1 );(a) 1(b) 2(c) 3(d)無限6 .下面for語句執(zhí)行時(shí)循環(huán)次數(shù)為(int i ; b )。for ( i=0 , j=5;i=j;) cout i j endl;i+; j;(a) 0(b) 5(c) 10(d)無限7.以下死循環(huán)的程序段是(b )o(a) int x; for( int x=0 ; x=0);(c) inta=5; while( a ) a一 ; ;(d) inti=3; for( ; i ;i );2.2閱讀下列程序,寫出執(zhí)行結(jié)果1.#include using namespace std; int main() i

4、nta, b, c, d, x;a=c=0;b=l;d=20;if( a ) d = d-10;else if( !b )if( !c )x = 15;else x = 25;cout d endl;)【解答】202.#include using namespace std; int main() int a =o, b = 1;switch( a ) case 0: switch( b ) case 0 : couta=b=z,bendl; break; case 1 : couta=b=bendl; break;)case 1 : a+; b+; cout,/a=,a,/ b=,/bend

5、l;)【解答】a= 0 b= 1 a= 1 b= 2 #includeusing namespace std;int main() int i = 1; while( i=10 )if( +i % 3 != 1 ) continue;else cout i endl; )【解答】47104.#include using namespace std;int main() int i = 0 , j = 5;do i+;尸;if ( i3 ) break; while ( j0 );cout i= i 1t,= j endl;【解答】4 j=15.#include using namespace

6、std;int main() int i, j;for( i=1, j=5; ij; i+ ) j; couti, t,jendl; )【解答】3 3 6.#include using namespace std;int main() int i, s = 0;for( i=0; i5; i+ )switch( i ) case 0: s += i; break;case 1: s += i; break;case 2: s += i; break;default: s += 2;coutusing namespace std;int main() int i, j, x = 0;for( i

7、=0; iv=3; i+ ) x+;for( j=0; jv=3; j+ ) if( j % 2 ) continue;x+;)x+;cout vv x二 vv x vv en dl;)【解答】x= 16【解答】6. 3 思考題語句使用方式使用場(chǎng)合if語句if (表達(dá)式)語句1;else語句2;需要對(duì)給定的條件進(jìn)行判斷,并根據(jù)判斷的結(jié)果選擇不同的操作。適用于復(fù)雜的條件表達(dá)式判斷。switch語句switch (表達(dá)式, case常量表達(dá)式1:語句1;case常量表達(dá)式2 :語句2;case常量表達(dá)式n;語句n;default : 語句 n +1;)根據(jù)整型表達(dá)式的不同值決定程序分支的情 況。適

8、用于判斷表達(dá)式簡(jiǎn)單,需要多個(gè)分支處理 的情況。i. c+中有什么形式的選擇控制語句?歸納它們語法形式、應(yīng)用場(chǎng)合。根據(jù)一個(gè)實(shí)際問題使用不同的條件語句編程。演示程序:程序(1)此程序用if輸出等級(jí)對(duì)應(yīng)的分?jǐn)?shù)段 a-=90, b-(90, 80, c-(80, 70 , d- (70, 60, ey60#includeusing namespace std;int main() char gd;cout=,a* & gd=* a & gd=,e*) coutvz/lnvalid grade! please retry:;cingd;if (gd=, i gd=, a ) coutvnscored 9

9、0-100! n,z;else if(gd=b gd二二b) cout,/nscored 80-89! n,;else if (gd-c gd二二c) cout,nscored 70-79 !n,;else if(gd二二d gd二二d) cout,nscored 60-69!n”;else if(gd=e gd=e) cout,nscore under 60!n;else cout=90, b-(90, 80 ,c-(80, 70 , d-(70, 60, e-60#includeusing namespace std;int main() char gd;coutz,enter the g

10、rade:”;cingd;直到輸入有效等級(jí),否則程序不繼續(xù)運(yùn)行while。(gd=a & gd二e) i i (gd=a &gd=e) coutzzlnvalid grade! please retry:;cingd;switch(gd) case a:case a : cout,znscored 90to0!n;break;case b:case b : cout,znscored 80-89 n;break;case c:case c :coutvnscored 70-79!nz,;break;case d:case d :coutnscored 60-69!n;break;case e:

11、case e :coutvnscore under 60!n/z;break; defa ul t: coutr語句的執(zhí)行杭程3根據(jù)一個(gè)實(shí)際問題,用不同的循環(huán)語句編程,分析其優(yōu)缺點(diǎn)【解答】4.用if語句和got。語句組織循環(huán),改寫思考題的第當(dāng)使用3小題編寫的程序。分析在什么情況下可以適goto語句。goto語句實(shí)現(xiàn)從語句結(jié)構(gòu)部向外的必要跳轉(zhuǎn),【解答】在不破壞程序基本流程控制的情況下,可以適當(dāng)使用即按特定條 件結(jié)束結(jié)構(gòu)語句塊的執(zhí)行。程序略。5.有以下程序#include using namespace std;int main() char c;cinc; if (c=y : ; c=y)int

12、 a=l;elseint a=0; coutv/za=vvavve ndl;)編譯錯(cuò)誤為:error c2065: a : undeclared identifiercoutvv,za=,zvvavve ndl;,指示語句行 變量a沒有定義。請(qǐng)分析原因,并做出修改?!窘獯稹砍绦蛟趇f和else分支中分別聲明了各自的局部變量a,而執(zhí)行輸出語句時(shí),聲明的變量a作用域已經(jīng)結(jié)束,所以編譯器認(rèn)為輸出語句的a沒有定義。正確的程序是:#include using namespace std;int main() char c;int a;cinc;if(c=v | |c=,y,)a=l;elsea=o;co

13、ut,a=/zaendl; )6.有以下程序,希望判斷兩個(gè)輸入的整數(shù)是否相等。程序通過編譯,但不能達(dá)到預(yù)期結(jié)果。請(qǐng)分析程序能夠通過c+編譯而不能 得到期望結(jié)果的原因。#includeusing namespace std;int main() int a, b;cout,a:cina;cout,b:cinb;if ( a二b )couta,z 等于,zbendl;elsecouta,z 不等于,bendl; )程序運(yùn)行后,輸入a的值為4, b的值為9,顯示結(jié)果如下:a: 4b: 99等于9【解答】在if語句的判斷表達(dá)式(a=b)中,賦值號(hào)“二”應(yīng)該是邏輯等“=” 從語法上,c+格jif語句把a(bǔ)

14、二b這個(gè)賦值表達(dá)式視為邏輯表達(dá) 式,沒有編譯錯(cuò)誤。a二b的值決定于b0若b的輸入值不等于0, if作為邏輯真(true ),否則作為邏輯假(false )。所以,題目中輸入b的值雖然不等于a,但表達(dá)式a=b為邏輯true ,執(zhí)行了 if語句的第 1個(gè)分支。2. 4編程題1 .輸入某學(xué)生成績(jī),若成績(jī)?cè)?5分以上輸出very good,若成績(jī)?cè)?0分到85分之間輸出“good,若成績(jī)低于60分輸出 “ no good o【解答】#includeusing namespace std;int main() double score;cout ”please input score:;cin scor

15、e;if ( score=85 ) cout ”very good!z/ ;else if ( score=60 ) cout good!”; else cout ”no good!”; )2 .輸入三個(gè)整數(shù),按從小到大的順序輸出它們的值。解答】#includeusing namespace std;int main() int a, b, c, t;cout a, b, c二;cin a b c;if(ab) t=a; a=b; b=t; if(ac) t=a; a=c; c=t; if(bc) t=b; b=c; c=t; cout a t b t c endl;)3 .輸入三角形的三條邊

16、,判別它們能否形成三角形,若能,則判斷是等邊、等腰、還是一般三角形解答】 #includeusing namespace std;int main() double a, b, c ;cout a, b, c =;cin a b c ;if ( a+b c & b+c a & c+a b ) if ( a = b & b = c )cout ” 等邊三角形! endl;else if ( a = b a = c i i b = c )cout 等腰三角形! endl;else cout 一般三角形! endl;elsecout 不能形成三角形! endl ;4 . 輸入百分制成績(jī) a (優(yōu)秀)

17、并把它轉(zhuǎn)換成五級(jí)轉(zhuǎn)換公式為90100b (良好)grade (級(jí)別)c (中等)d (合格)e (不合格)808970 7960 690 59解答】#include using namespace std;int main() double score; char grade; cout ”score=;cin score;if ( score = 0 & score = 100 ) switch ( int( score ) /10 ) case 10grade 二,a,;case 9:卜“八,grade 二,b,;case 8:入grade 二,c,;case 7:,grade 二,d,;

18、case 6:case 5:case 4:case 3:case 2:case 1:case 0: grade =e; 11else cout 數(shù)據(jù)輸入錯(cuò)誤! “ endl;goto end;cout grade endl;end: ; /分號(hào)不能省5 .編程序模擬剪刀、石頭和紙游戲。游戲規(guī)則為:剪刀剪紙,石頭砸剪刀,紙包石頭。玩游戲者從健盤上輸入s (表示剪刀)或r (表示石頭)或p (表示紙),要求兩個(gè)游戲者交替輸入,計(jì)算機(jī)給出輸贏的信息解答】#includeusing namespace std;int main() char first, second;cout ”first inpu

19、t ( s,r or p ):;cin first;cout ”second input ( s, r or p ) :z/;cin second;switch ( first ) case s:switch ( second ) case s: cout ”scissor ties scissor. endl; goto end;case r : cout ”scissor is crushed by rock. endl; goto end;case p : cout ”scissor cuts paper. endl; goto end; default : cout ”second i

20、nput error!,z endl ; goto end;case r :switch ( second ) case s : cout ”rock crushes scissor. endl; goto end;case r : cout ”rock ties rock. endl; goto end;case p : cout ”rock is wrapped by paper. endl; goto end; default : cout ”second input error!” endl; goto end;)case p:switch ( second ) case s : co

21、ut ”paper is cut by scissor. endl; gotoend; case r : cout ”paper wraps the rock. endl; gotoend;case p : cout paper ties paper.,z endl; goto end; default : cout second input error!,z endl; goto end;default : cout ”first input error! endl; goto end;)end:; )6 .輸入一個(gè)整數(shù),輸出該整數(shù)的所有素?cái)?shù)因子。例如,輸入120,輸出為2、2、2、3和5解

22、答】#include using namespace std;int main() int m, i = 2;cout ”please inputcin m;while( i=m )if( m % i = 0 ) cout i ,“; m = m / i;) else i+;)a的平方根。7 .使用迭代公式xni(xn a/xn) /2 (n 0, 1,2, ;xoa/2)編程序求某一正整數(shù)【解答】#include#includeusing namespace std;int main() const double eps = le-8;double a, xo, x;cout ”please

23、 input a:;cin a;xo = a / 2;x = ( xo + a/xo )/2;while ( fabs ( x-x0 )eps ) xo = x; x =( xo + a/xo )/2;cout x endl; )8 .已知 x=0 , 10 , 20 ,180 ,求 sinx, cosx 和 tan x 的值?!窘獯稹?include#include#includeusing namespace std;int main() const double pi = 3.14159265;int i;double x, yl, y2, y3;cout setw(2) “x setw

24、(15) “sin(x) setw(15) “cos(x) setw(15) tg(x) endl;for( i=0; i=18; i+ ) x = i*10*pi/180;yl = sin( x );y2 = cos (x);y3 = yl/y2;cout setw(2) i setw(15) yl setw(15) y2 setw(15) y3 endl;9 . 在100至ij 200中找出同時(shí)滿足用3除余2,用5除余3和用7除余2的所有整數(shù)?!窘獯稹?includeusing namespace std;int main() int i;for( i=100; i7200; i+ ) if ( ( i % 3 = 2) & ( i % 5 = 3 ) & ( i % 7 = 2 ) cout i endl;10 .求100到999中的水仙花數(shù)。所謂水仙花數(shù)是指一個(gè)三位數(shù),它的每位數(shù)字的立方之和等于該數(shù)。例如,因?yàn)?53=1+5 3 +3 3 ,所以153為水仙花數(shù)。【解答】#includeusing namespace std;int main() int i, a, b, c;for( i=100; i二999; i+ ) a = i/100;b = ( i-a*100 ) / 10;c = i - a*100 - b*1

溫馨提示

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

評(píng)論

0/150

提交評(píng)論