C++程序設(shè)計基礎(chǔ)習(xí)題及其解答_第1頁
C++程序設(shè)計基礎(chǔ)習(xí)題及其解答_第2頁
C++程序設(shè)計基礎(chǔ)習(xí)題及其解答_第3頁
C++程序設(shè)計基礎(chǔ)習(xí)題及其解答_第4頁
C++程序設(shè)計基礎(chǔ)習(xí)題及其解答_第5頁
已閱讀5頁,還剩48頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、(a) *p2 = b ;(b) p1 = a ;(a) *p2 = b ;(b) p1 = a ;選擇題1 及其解答1.一個最簡單的C+隹序,可以只有一個(c )(a) 庫函數(shù) (b)自定義函數(shù)(c) main 函數(shù)(d) 空函數(shù)2.用C+鈉言編制的源程序要變?yōu)槟繕?biāo)程序必須要經(jīng)過( d )。(a) 解釋(b)3. C+ 程序中的簡單語句必須以匯編( b ) 結(jié)束。(c) 編輯(d) 編譯(a) 冒號 (b)分號(c) 空格(d) 花括號. 有說明 int a=0; double x=; 以下語句中, ( c ) 屬于編譯錯誤。(a) x=a/x;(b) x=x/a;(c) a=a%x;(d)

2、 x=x*a;.執(zhí)行C+隹序時出現(xiàn)的“溢出”錯誤屬于 (c ) 錯誤。(a) 編譯 (b) 連接(c) 運行(d) 邏輯.下列選項中,全部都是 C+鐵鍵字的選項為(c ) o(a) while IF Static(b) break char go(c) sizeof case extern(d) switch float integer TOC o 1-5 h z .按C+而識符的語法規(guī)定,合法的標(biāo)識符是(a )。(a) _abc(b) new(c) 無(d)“age. C+叫句中,兩個標(biāo)識符之間(a ) 不能作為C+勺分隔符。數(shù)字(b) ;(c) :(d) +9下列正確的八進(jìn)制整型常量表示是

3、( b )。0a0(b) 015(c) 080(d) 0 x1010下列錯誤的十六進(jìn)制整型常量表示是( c )。0 x11(b) 0 xaf(c) 0 xg(d) 0 x1f11 在下列選項中,全部都合法的浮點型數(shù)據(jù)的選項為( b )。(b)-1e+515. 2e-4(c)-12345. e-5(d) (1+4)8e+2下列正確的字符常量為( d )。(a)“a”(b) name(c) a(d) 101 .下列選項中,(d )不能交換變量a和b的值。(a) t=b; b=a; a=t;(c) t=a; a=b; b=t;a=a+b; b=a-b; a=a - b;(d) a=b; b=a;14

4、關(guān)于下列語句敘述錯誤的是( a )int i=10, *p=&i;p 的值為 10(c) *p 表示變量 i 的值p 指向整型變量i(d) p 的值是變量i 的地址15有以下變量說明,下面不正確的賦值語句是( b )int a=5, b=10, c; int *p1 = &a, *p2 = &b;(d) c = *p1 *( *p2 ) ;p2 = p1 ;16有以下變量說明,下面正確的語句是int a=10, b; int &pa=a, &pb=b;(a) &pb = a;17執(zhí)行下面語句序列后,int a = 5 , b = 3 , t ;(b) pb = pa;a和b的值分別為(c) p

5、b = &pa;)。(d) *pb = *pa;int &ra = a ;int &rb = b ;t = ra ; ra = rb ; rb = t ;(a) 3 和 3在下列運算符中,(a) =在下列運算符中,(a) !20設(shè)int i=1, j=2;(a) 121 設(shè) int i=1, j=2;1( d(b) *=3 和 5) 優(yōu)先級最高。( d ) 優(yōu)先級最低。(b) &則表達(dá)式(b) 2則表達(dá)式(b) 222在下列表達(dá)式選項中,( c )(a) +(a+)(b) a+b23已知 int i=0, j=1, k=2;(a) 024. 執(zhí)行下列語句后,int x, y ;x = y =

6、1; + x |(a) 不確定(c) +i+j+i+j是正確。5 和3(c) !=的值為( c )(c) 3的值為( d )(c) 3(c) a+b則邏輯表達(dá)式+i|-j&+k(b) 1(c) 2x 的值是 ( d ), y 的值是( c )+ y ;(b) 0(c) 1(d) *(d) 4(d) 45 和5(d) :(d) a+b的值為 ( b )(d) 3(d) 225.設(shè)x為整型變量,不能正確表達(dá)數(shù)學(xué)關(guān)系1 x5的C+鍵輯表達(dá)式是(a )(a) 1 x 5(c) 1x & x5已知 int x=5; 執(zhí)行下列語句后,x += x -= x * x;(a) 25(b) 40設(shè) int a=

7、1, b=2, c=3, d=4;a b a : c d c : d(a) 1(b) 2以下逗號表達(dá)式的值為( d )x=2|x=3|x=4(d) !(x=5)x 的值為( c )。(c)40(d) 20則以下條件表達(dá)式的值為 ( a )。3(d) 4( x = 4 * 5, x * 5 ), x + 25(c) 100(d) 45(a) 25(b) 20把下列數(shù)學(xué)表達(dá)式寫成C+算術(shù)表達(dá)式i2. xxx(ax+b)+c+d+e1 TOC o 1-5 h z x y1 cos 4822+ab+b2)ln (1+|ab|10)4.abx2cot (1_22)6. lg(ax【解答】1/(1 + 1

8、/(1 + 1/(x + y)x * ( x * ( x * ( a * x + b ) + c ) + d ) + elog( 1 + pow( fabs( ( a + b )/( a b ) ), 10)sqrt( 1 + 2 * cos( 48 * 180 )1/tan( ( 1 - x*x )/( 1 + x*x)或者 cos( ( 1 - x*x )/( 1 + x*x ) )/sin( ( 1 - x*x )/( 1 + x*x )6. log10( a * a + a * b + b * b )用邏輯表達(dá)式表示下列條件1. i被j整除3. 1 x10. y 100,10,并且 y

9、.坐標(biāo)點(x, y )落在以(10, 20.三條邊a, b和c構(gòu)成三角形2. n是小于正整數(shù)k的偶數(shù)4. x , y其中有一個小于 z10,100)為圓心,以35為半徑的圓內(nèi)8.年份Year能被4整除,但不能被100整除或者能被400整除 【解答】1. i%j = 02. (nk) & (n%2 = 0)3. 1=x & x104. xz|y=-100 & y=10 & y=100 )sqrt(pow(x-10),2) + pow(y-20),2)c & b+ca & c+ab(year%4 = 0) & (year%100!=0)|(year%400=0)閱讀下列程序,寫出執(zhí)行結(jié)果.#inc

10、lude using namespace std;int main() int a = 1, b = 2;bool x, y;cout (a+)+(+b) endl;cout a % b b;y = a- & b;cout x endl;cout y endl;)【解答】4201#include using namespace std;int main() int x,y,z,f;x = y = z = 1;f = -x | y- & z+;cout x = x endl;cout y = y endl;cout z = z endl;cout f = f endl;)【解答】x=0y=0z=

11、2f=1#include #includeusing namespace std;int main() int a=123;int &ra=a;int *pa=&a;coutsetw(5)decasetw(5)octrasetw(5)hex*paendl;)【解答】123 173 7b思考題.什么叫數(shù)據(jù)類型變量的類型定義有什么作用【解答】數(shù)據(jù)“類型”是對數(shù)據(jù)的抽象。類型相同的數(shù)據(jù)有相同的表示形式、存儲格式以及相關(guān)的操作。定義 一個變量時,計算機根據(jù)變量的類型分配存儲空間,并以該類型解釋存放的數(shù)據(jù)。.普通數(shù)據(jù)類型變量和指針類型變量的定義、存儲、使用方式上有何區(qū)別請編寫一個程序驗證之。 【解答】變

12、量類型定義存儲使用方式數(shù)據(jù)類型標(biāo)識符數(shù)據(jù)值通過名訪問即直接訪問對變量內(nèi)容操作指針類型*標(biāo)識符地址值通過指針變量的地址值間址訪問對象驗證程序:#includeusing namespace std; int main() int a,b,c;coutabc;入一個三位整數(shù),將它反向輸出。【解答】#include using namespace std;int main() int x,i,j,k;cout x;i = x/100;j = x/10 %10;k = x%10;cout k j i endl;.輸入平面上某點橫坐標(biāo) x和縱坐標(biāo)y,若該點在由圖表示的方塊區(qū)域內(nèi),則輸出 1;否則,輸出0

13、圖 正方形【解答】#include using namespace std;int main() double x,y,b;cout x y;b = ( -2=x ) & ( x=2 ) & ( -2=y ) & ( y=2 ); cout b endl;. 輸入三個整數(shù),求出其中最小數(shù)(要求使用條件表達(dá)式)【解答】#include using namespace std;int main() int a,b,c,temp,min;cout a b c;temp = ( ab ) a:b;min = ( tempc ) temp:c;cout min= min = 0 )if( x != 0

14、)if( x = 0 ) y = 0 ;if( x 0 ) y = 1 ;else y = -1;else y = 0;(c) if( x = 0 )(d) y = -1 ;if( x 0 ) y = -1 ; if( x = 0 )else y = 0 ;if( x 0 ) y = -1 ;else y = 1 ;else y = 1 ;3 假設(shè)i=2 ,執(zhí)行下列語句后i 的值為( b ) 。switch( i ) case 1 : i + ;: i - ;: + i ; break ;: - i ;default : i + ;(d) 4)。(d) 1J(d) 無限 TOC o 1-5 h

15、 z (a) 1(b) 2(c) 3已知int i=0 , x=0; 下面 while 語句執(zhí)行時循環(huán)次數(shù)為( dwhile( !x & i 3 ) x+ ; i+ ; (a) 4(b) 3(c) 2已知int i=3; 下面 do_while 語句執(zhí)行時循環(huán)次數(shù)為( b )do i-; coutiendl;while( i!= 1) ;(a) 1(b) 2(c) 3下面for 語句執(zhí)行時循環(huán)次數(shù)為( b ) 。int i;for ( i=0, j=5;i=j; ) cout i j endl;i+; j-;(d) 無限(a) 0(b) 5(c) 10以下死循環(huán)的程序段是(b ) 。int x

16、; for( int x=0 ; x=0 ) ;int a=5 ; while( a ) a- ; ;int i=3 ; for( ; i ; i - ) ;閱讀下列程序,寫出執(zhí)行結(jié)果#includeusing namespace std;int main() int a,b,c,d,x;a = c = 0; b = 1; d = 20;if( a ) d = d-10;else if( !b )if( !c)x = 15;else x = 25;cout d endl;)【解答】20!.#includeusing namespace std;int main()int a = 0, b =

17、1;switch (a) case 0: switch( b ) case 0 : couta=a b=bendl; break;case 1 : couta=a b=bendl; break;)case 1: a+; b+; couta=a b=bendl;)【解答】a= 0 b= 1a= 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 7.using namespace std;i

18、nt main() int i = 0 , j = 5;do i+; j-;if ( i3 ) break; while ( j0 );cout i= i t j= j endl;【解答】i= 4 j= 1#includeusing namespace std;int main() int i,j;for( i=1, j=5; ij; i+ ) j-; coutitjendl;【解答】33#includeusing namespace std;int main() int i, s = 0;for( i=0; i5; i+ )switch( i ) case 0: s += i; break;

19、s += i; break;s += i; break;default: s += 2;couts=sendl;【解答】s= 7#includeusing namespace std;int main()int i, j, x = 0;for( i=0; i=3; i+ ) x+;for( j=0; j=3; j+ ) if( j % 2 ) continue;x+;x+;cout x= x endl;【解答】x= 16思考題1. C+鐘有什么形式的選擇控制語句歸納它們語法形式、應(yīng)用場合。根據(jù)一個實際問題使用不同的條 件語句編程?!窘獯稹空Z句使用方式使用場合if語句if(表達(dá)式)語句1;els

20、e語句2;需要對給定的條件進(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á)式的不同值決定程序分支的情況。適用于判斷表達(dá)式簡單,需要多個分支處理的情況。演示程序:程序(1)什么叫循環(huán)控制歸納比較 C+鐘各種循環(huán)控制語句的語法、循環(huán)條件和循環(huán)結(jié)束條件的表示形式及執(zhí)行流程【解答】循環(huán)控制是在特定的條件下,程序重復(fù)執(zhí)行一些特定動作。語句語法執(zhí)行流程使用場合while語句while(表達(dá)式)循環(huán)體;循環(huán)條

21、件:表達(dá)式值為非 0(真)循環(huán)結(jié)束條件:表達(dá)式值為 0(假)do-while語句do循環(huán)體while(表達(dá)式);循環(huán)條件:表達(dá)式值為非 0(真)循環(huán)結(jié)束條件:表達(dá)式值為0(假)程序中常用于根據(jù)條件執(zhí)行操作而不需關(guān)心循環(huán)次數(shù)的情況。先判斷形式循環(huán),條件不成立 時不進(jìn)入循環(huán)體。程序中常用于根據(jù)條件執(zhí)行 操作而不需關(guān)心循環(huán)次數(shù)。后判斷形式循環(huán),至少執(zhí)行 1 次循環(huán)體。一般情況,while語句和do while語句可以互換使用。for語句for(表達(dá)式1;表達(dá)式2;表達(dá)式3) 循環(huán)體;(1)表達(dá)式1稱為初始化表達(dá)式,不是 循環(huán)體執(zhí)行部分。(2)表達(dá)式3稱為后置表達(dá)式,作為循環(huán)體的最后一個執(zhí)行表達(dá)式。(

22、3)循環(huán)條件:表達(dá)式2值為非0 (真) 循環(huán)結(jié)束條件:表達(dá)式2值為0 (假)for語句稱為步長循環(huán)語句, 通常用于確定循環(huán)次數(shù)的情況。由于語句的3個表達(dá)式均可以 缺省,也可以用于條件循環(huán),即循 環(huán)次數(shù)不確定的情況。.根據(jù)一個實際問題,用不同的循環(huán)語句編程,分析其優(yōu)缺點 【解答】略。.用if語句和goto語句組織循環(huán),改寫思考題的第3小題編寫的程序。分析在什么情況下可以適當(dāng)使用goto語句。【解答】在不破壞程序基本流程控制的情況下,可以適當(dāng)使用goto語句實現(xiàn)從語句結(jié)構(gòu)內(nèi)部向外的必要跳轉(zhuǎn),即按特定條件結(jié)束結(jié)構(gòu)語句塊的執(zhí)行。程序略。.有以下程序#include using namespace st

23、d; int main() char c;cinc;if(c=y|c=Y)int a=1;elseint a=0;couta=aendl; 編譯錯誤為:error C2065: a: undeclared identifier,指示變量 a沒有定義。請分析原因,并做出修改。【解答】變量a的定義不應(yīng)該放在if-else語句體中。說明語句和執(zhí)行語句的執(zhí)行時機不同。變量說明要求在編譯時定義存儲空間,而 if-else 是執(zhí)行語句,程序運行后才執(zhí)行。正確的程序是:#includeusing namespace std;int main() char c;int a;cinc;if(c=y|c=Y)a=

24、1;elsea=0;couta=aendl; 6. 有以下程序,希望判斷兩個輸入的整數(shù)是否相等。程序通過編譯,但不能達(dá)到預(yù)期結(jié)果。請分析程序能夠通過C+褊譯而不能得到期望結(jié)果的原因。#includeusing namespace std;int main() int a,b;couta;coutb; if( a=b ) couta 等于 bendl;elsecouta 不等于 bendl; 程序運行后,輸入 a 的值為 4, b 的值為9,顯示結(jié)果如下:a: 4 b: 9 9 等于 9 【解答】在if語句的判斷表達(dá)式(a=b)中,賦值號“=”應(yīng)該是邏輯等“=。從語法上,C+曲勺if語句把a=b

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

26、score=85 ) cout =60 ) cout Good!;else cout No good!;2 輸入三個整數(shù),按從小到大的順序輸出它們的值?!窘獯稹?includeusing namespace std;int main() int a, b, c, t;cout 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輸入三角形的三條邊,判別它們能否形成三角形,若能,則判斷是等邊、等腰、還是一般三角形?!窘獯稹?includeusing namespa

27、ce std;int main() double a, b, c ;cout 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 | b = c )cout 等腰三角形! endl;else cout 一般三角形! endl;elsecout 不能形成三角形! endl ;4輸入百分制成績,并把它轉(zhuǎn)換成五級分制,轉(zhuǎn)換公式為: TOC o 1-5 h z A (優(yōu)秀)90100B (良好)8089grade (級別)C (中等) 70 79D (合格)60

28、69E (不合格)059【解答】#includeusing namespace std;int main() double score; char grade;cout score;if ( score = 0 & score = 100 ) switch ( int( score ) /10 ) case 10:case 9: grade = a; break;case 8: grade = b; break;case 7: grade = c; break;case 6: grade = d; break;case 5:case 4:case 3:case 2:case 1: case 0

29、: grade = e; break;else cout 數(shù)據(jù)輸入錯誤! endl;goto end;cout grade endl;end: ; 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 input error! endl ; goto end;case r:switch ( second ) case s: cout Rock crushes sciss

30、or. 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: cout Paper is cut by scissor. endl; goto end;case r: cout Paper wraps the rock. endl; goto end;case

31、p: cout Paper ties paper. endl; goto end;default : cout second input error! endl; goto end;default : cout First input error! endl; goto end;end: ;6. 輸入一個整數(shù),輸出該整數(shù)的所有素數(shù)因子。例如,輸入120,輸出為2、 2、 2、 3和 5【解答】#includeusing namespace std;int main() int m,i = 2;cout m;while( i=m )if( m % i = 0 ) cout i ,;m = m /

32、 i; else i+; .使用迭代公式xii函a/xj/2 (n 0,1,2,;凡a/2)編程序求某一正整數(shù)a的平方根。 【解答】#include#includeusing namespace std;int main() const double eps = 1e-8;double a,x0,x;cout a;x0 = a / 2;x = ( x0 + a/x0 )/2;while( fabs( x-x0 )eps ) x0 = x; x =( x0 + a/x0 )/2; cout x endl; .已知 X=0 ,10 , 20 ,,180 ,求 sin x,c0sx 和 tan x

33、的值?!窘獯稹?include#include#includeusing namespace std;int main() const double pi = 3.;int i;double x,y1,y2,y3;cout setw(2) x setw(15) sin(x) setw(15) cos(x) setw(15) tg(x) endl;for( i=0; i=18; i+ ) x = i*10*pi/180;y1 = sin( x );y2 = cos(x);y3 = y1/y2;cout setw(2) i setw(15) y1 setw(15) y2 setw(15) y3 e

34、ndl;在 100到 200 中找出同時滿足用 3 除余2,用5 除余 3 和用 7 除余 2 的所有整數(shù)?!窘獯稹?includeusing namespace std;int main() int i;for( i=100; i=200; i+ ) if ( ( i % 3 = 2) & ( i % 5 = 3 ) & ( i % 7 = 2 ) ) cout i endl;求 100 到 999 中的水仙花數(shù)。所謂水仙花數(shù)是指一個三位數(shù),它的每位數(shù)字的立方之和等于該數(shù)。例如,因為153=1 3+53+33 ,所以 153 為水仙花數(shù)。【解答】#includeusing namespace

35、 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*10;if ( i = a*a*a + b*b*b + c*c*c ) cout i endl;求 1000 之內(nèi)的所有完數(shù)。所謂完數(shù)是指一個數(shù)恰好等于它的所有因子之和。例如,因為6=1+2+3,所以 6 為完數(shù)?!窘獯稹?includeusing namespace std;int main() int i,j,s;for( i=1; i=1000; i+ ) s = 0;for( j=1;

36、ji; j+ )if ( i % j = 0 ) s = s + j;if ( i = s ) cout i endl;編一程序顯示由符號組成的三角形圖案。例如,程序運行后,屏幕顯示: How many lines用戶輸入:5屏幕顯示:What character用戶輸入:*則輸出如下圖案。* * * * * * * * * * * * * * * * * * * * *【解答】#includeusing namespace std;int main() int i,j,k,n;char ch;coutn;coutch;for( i=1; i=n; i+ ) for( k=1; k=n-i;

37、k+ ) cout ;for( j=1; j=2*i-1; j+ ) cout ch ;cout endl;13.已知XYZ+YZZ=532其中X, 丫和Z為數(shù)字,編一程序求出X, 丫和Z的值。【解答】#includeusing namespace std;int main() int x,y,z,i;for( x=1; x=9; x+ )for( y=1; y=9; y+ )for( z=0; z=9; z+ ) i = 100*x + 10*y + z + 100*y + 10*z + z;if ( i = 532 )coutx=xty=ytz=zendl;3 及其解答選擇題1以下正確的函

38、數(shù)原型為( d )(a) f1( int x; int y );(c) void f1( int x, y );2 有函數(shù)原型void fun2( int );int a = 21; fun2( a );int b = 100; fun2( &b );3有函數(shù)原型void fun3( int * );double x = ; fun3( &x );int b = 100; fun3( &b );4有函數(shù)原型void fun4( int & );int a = ; fun4( &a );int b = 100; fun4( b );5有聲明void fun5( int * & ); int a

39、, *p = &a;(b) void f1( x, y );(d) void f1( int, int );下面選項中,不正確的調(diào)用是 ( c )(b) int a = 15; fun2( a*3 );(d) fun2( 256 );下面選項中,正確的調(diào)用是( c )(b) int a = 15 ; fun3( a* );(d) fun3( 256 );下面選項中,正確的調(diào)用是( c )(b) int a = 15; fun4( a* );(d) fun4( 256 ) ;下面選項中,正確的調(diào)用是( b )。(a) fun5( &a );6有聲明(b) fun5( p );(c) fun5(

40、*a );(d) fun5( *p ) ;int fun6( int ),(*pf)(int) = fun6;下面選項中,正確的調(diào)用是( c )。int a=15; int n=fun6(&a);int a = 15; cout(&pf)(a);(d) cout *pf( 256 );void ,以下敘述正確的是( c )cout(*pf)( 256 );7.在VB,若定義一個函數(shù)的返回類型為int main()函數(shù)不執(zhí)行任何操作(d) 函數(shù)不能修改實際參數(shù)的值局部變量(d) 函數(shù)調(diào)用(b) 共享程序代碼(d) 節(jié)省存貯空間(a) 函數(shù)返回值需要強類型轉(zhuǎn)換(c) 函數(shù)本身沒有返回值8函數(shù)參數(shù)的

41、默認(rèn)值不允許為( c )。(a) 全局常量 (b) 直接常量9使用重載函數(shù)編程序的目的是( a )。(a) 使用相同的函數(shù)名調(diào)用功能相似的函數(shù)(c) 提高程序的運行速度10下列的描述中( b ) 是錯誤的。使用全局變量可以從被調(diào)用函數(shù)中獲取多個操作結(jié)果局部變量可以初始化,若不初始化,則系統(tǒng)默認(rèn)它的值為0當(dāng)函數(shù)調(diào)用完后,靜態(tài)局部變量的值不會消失全局變量若不初始化,則系統(tǒng)默認(rèn)它的值為011下列選項中,( c ) 的具有文件作用域。(a) 語句標(biāo)號 (b) 局部變量 (c) 全局變量 (d) 靜態(tài)變量閱讀下列程序,寫出執(zhí)行結(jié)果#includeusing namespace std;#includei

42、nt f( int ) ;int main()int i;for( i = 0; i 3; i + )cout f( i ) endl;int f( int a )int b = 0 , c = 1;b +; c+;return int( a + pow( double(b), 2 ) + c );#includeusing namespace std;void func(int a, int b, int c = 3, int d = 4 ); func( 10, 15, 20, 30 );func( 10, 11, 12 );func( 12, 12 );void func( int a,

43、 int b, int c, int d ) coutatbtctd endl;【解答】 TOC o 1-5 h z 101520301011124121234#includeusing namespace std;void func( int, int, int * ) ;int main()int x, y, z;func( 5, 6, &x );func( 7, x, &y );func( x, y, &z );cout x , y , z endl;void func( int a , int b , int *c )b += a ; *c = b - a ;【解答】, 6, 6#in

44、cludeusing namespace std;void func( int, int, int & );int main()int x=0 , y=1 , z=2;func( 1 , 2 , x );func( x + y , y , y );func( z , x + y , z );cout x , y , z endl ;void func( int a , int b , int &c )b += a ; c = b - a ;【解答】, 1, 3#includeusing namespace std;void func( int *, int *, int *& ) ;int m

45、ain()int a=10, b=20;int *p=&a, *q=&b;func( p, q, p );cout *p= *p ,*q= *q endl;void func( int *t1 , int *t2 , int *& rt )*t1 += 5 ; *t2 += 5 ;t = t1 ;rt += 5 ;cout *t1= *t1 ,*t2= *t2 ,*rt= *rt endl;【解答】*t1=20,*t2=25,*rt=20*p=20,*q=25#includeusing namespace std;int f2( int, int );int f1( int a , int b

46、 )int c ;a += a ; b += b ;c = f2( a+b , b+1 );return c;int f2( int a , int b )int c ;= b % 2 ;eturn a + c;int sub( int, int );int sub( int, int ); int a = 3 , b = 4;cout f1( a , b ) endl;【解答】15#includeusing namespace std;int age( int n ) int f;if( n = 1 ) f = 10 ;else f = age( n-1 ) + 2;return f ;in

47、t main() cout age : age( 5 ) endl;【解答】age:18#includeusing namespace std;int f1( int a, int b ) return a + b ; int f2( int a, int b ) return a - b ; int f3( int( *t )( int, int ), int a, int b ) return ( *t )( a, b ) ; int main() int ( *p )( int, int );p = f1 ;cout f3( p, 4, 8 ) endl;p = f2 ;cout f3(

48、 p, 8, 4 ) endl;【解答】124#include using namespace std;int main()int a = 1 ;int main() int m = 1, n = 2, f;f = sub( m, n );cout a t f endl;f = sub( m, n ) ;cout a t f endl;int sub( int c, int d ) static int m = 2, n = 5 ;cout m t n t endl;a = + a ; c = m + ; d = n +;return c + d ;【解答】25273639思考題1函數(shù)的作用是

49、什么如何定義函數(shù)什么叫函數(shù)原型【解答】函數(shù)的兩個重要作用:( 1 )任務(wù)劃分,把一個復(fù)雜任務(wù)劃分為若干小任務(wù),便于分工處理和驗證程序正確性;( 2)軟件重用, 把一些功能相同或相近的程序段, 獨立編寫成函數(shù), 讓應(yīng)用程序隨時調(diào)用, 而不需要編寫雷同的代碼。函數(shù)的定義形式:類型 函數(shù)名 ( 形式參數(shù)表 )語句序列函數(shù)原型是函數(shù)聲明,告訴編譯器函數(shù)的接口信息:函數(shù)名、返回數(shù)據(jù)類型、接收的參數(shù)個數(shù)、參數(shù)類型和參數(shù)順序,編譯器根據(jù)函數(shù)原型檢查函數(shù)調(diào)用的正確性。2 什么叫函數(shù)值的返回類型什么叫函數(shù)的類型如何通過指向函數(shù)的指針調(diào)用一個已經(jīng)定義的函數(shù)請寫一個驗證程序說明。【解答】1 )函數(shù)的返回類型是函數(shù)返

50、回的表達(dá)式的值得類型;2 )函數(shù)類型是指函數(shù)的接口,包括函數(shù)的參數(shù)定義和返回類型;3 )若有functionTypefunctionName;Pn(x)sh(1 shx)sh2x sh3xsh(t)3 仁3m 125 Z5512 ph(f(a) f(b)RandRandRandx(2n 1雙信)(n 1)pn 2(x) /n1f(a1ih)Jdxx 21 x2 dx sin xdx 2,10000 );0cout t3= t3 endl; 8.編寫一個程序,包含三個重載的display函數(shù)和一個主函數(shù)。要求第一個函數(shù)輸出前面用字符串“ a double :引導(dǎo),第二個函數(shù)輸出一個int值,前面

51、用字符串“ a intdouble值,函數(shù)輸出一個char字符值,前面用字符串“ a char :”引導(dǎo),在主函數(shù)中分別用 變量作為實參調(diào)用 display函數(shù)?!窘獯稹?includedouble、引導(dǎo),第三個int 和 char 型using namespace std;void display( double d ) cout a double: d endl;void display( int i ) cout a int: i endl;void display( char c ) cout a char: c endl;int main() double d = ; int i =

52、 100; char c = a;display( d );display( i );display( c );.使用重載函數(shù)編程序分別把兩個數(shù)和三個數(shù)從大到小排列。【解答】#includeusing namespace std;void sort( double x,double y );void sort( double x,double y,double z ); sort( ,79 );sort(,);void sort(double x,double y) if ( xy ) cout x t y endl;else cout y t x endl;void sort( double

53、 x,double y,double z ) double t; TOC o 1-5 h z if( yz ) t = y;y= z; z=t; if( xz ) t = x;x= z; z=t; if( xy ) t = x;x= y ;y=t; cout x t y t z t endl;.給定求組合數(shù)公式為:0n m!,編一程序,輸入m和n的值,求cn的值。注意優(yōu)化算 C m mn!(m n)!法,降低溢出可能。要求主函數(shù)調(diào)用以下函數(shù)求組合數(shù):nn.2n(save)一int Fabricate( int m, int n ) ;Cm ave jJ dev JJ i 輸入一個表小星n. n

54、期幾的數(shù),然后輸出相應(yīng)的英文單詞。要求使用指針數(shù)組實現(xiàn)。【解答】#includeusing namespace std;int main() char *weekday7 = sunday, monday, tuesday, Wednesday, thursday, friday, saturday ;int d;cout d;if( d=0 & d=6 )cout d - *( weekday + d ) endl; else cout input error! endl;編寫函數(shù):(1)在一個二維數(shù)組中形成如以下形式的n階矩陣:111112 11113 2 1114 3 2 1 15 4

55、3 2 1(2)去掉靠邊元素,生成新的 n-2階矩陣;(3)求矩陣主對角線下元素之和;(4)以方陣形式輸出數(shù)組。在main函數(shù)中調(diào)用以上函數(shù)進(jìn)行測試。【解答】#include using namespace std;void create( int *&app, int n );void del( int *&app, int *&bpp, int n );int maindiagonal( int *&app, int n );void output( int *&app, int );int main() int *ap = NULL, *bp = NULL, n;cout n;creat

56、e( ap,n );cout n形成矩陣:n;output( ap, n );cout 去掉靠邊元素生成的矩陣:n;del( ap,bp,n );output( bp,n-2 );cout 主對角線元素之和: maindiagonal( ap, n ) nosi和s2是兩個結(jié)構(gòu)類型變量,若要賦值s1 只能接受相同類型的數(shù)據(jù)成員(c) 同一結(jié)構(gòu)類型的變量閱讀下列程序,寫出執(zhí)行結(jié)果。(c) (*p).no s1=s2合法,則它們的說明應(yīng)該是(c結(jié)構(gòu)中的成員相同(d) 存儲字節(jié)長度一樣的變量#include using namespace std;struct Data int n ;double

57、score ; ;int main() Data a3 = 1001,87,1002,72,1003,90 , *p = a ;cout n endl ;cout n endl ;cout n+ endl ;cout (*p).n+ endl ;【解答】1001100210031004#include using namespace std;struct Employee char name 20 ;char sex ; ;void fun( Employee *p ) if( (*p).sex = m )cout (*p).name endl ;int main() Employee emp

58、5 = Liming, m, Wangxiaoping, f, Luwei, m ;int i ;for( i=0; i3; i+ ) fun( emp+i ) ;【解答】LimingLuwei#include using namespace std;struct Node char * s ;Node * q ; ;int main() Node a = Mary, a+1 , Jack, a+2 , Jim, a ;Node *p = a ;cout s endl ;cout q-s endl ;cout q-q-s endl ;cout q-q-q-s endl ;【解答】MaryJac

59、kJimMary思考題1分析以下說明結(jié)構(gòu)的語句struct Node int data;allpii.salaryNode error;ame allpii.id有關(guān)程序如下:#includeusing namespace std;struct personame;cout alli.id;cout alli.salary ;void Sort(person all, int pi, const int n) int i,j;int t;alaryallpij+1.salary)ametallpii.idtallpii.salaryendl;3 有以下結(jié)構(gòu)說明和遍歷單向鏈表的函數(shù)。 函數(shù)內(nèi)有錯

60、誤嗎是什么性質(zhì)的錯誤請上機驗證你的分析。struct Node int data; Node * next; ;void ShowList( Node *head ) while( head ) cout date next編程題.使用結(jié)構(gòu)類型表示復(fù)數(shù)。設(shè)計程序輸入兩個復(fù)數(shù),可以選擇進(jìn)行復(fù)數(shù)的 -、x或+運算,并輸出結(jié)果。【解答】#include #include using namespace std;struct complex double re,im;int main() complex a,b,c; char oper; TOC o 1-5 h z cout ;cout ;cout

溫馨提示

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

評論

0/150

提交評論