06級CPP1試題及答案_第1頁
06級CPP1試題及答案_第2頁
06級CPP1試題及答案_第3頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、成上海大學(xué)2006? 2007學(xué)年冬季學(xué)期試卷績課程名:高級語言程序設(shè)計(一)課程號:08305001學(xué)分:4應(yīng)試人聲明:我保證遵守上海大學(xué)學(xué)生手冊中的上海大學(xué)考場規(guī)則,如有考試違紀、 作弊行為,愿意接受上海大學(xué)學(xué)生考試違紀、作弊行為界定及處分規(guī)定的紀律 處分。應(yīng)試人應(yīng)試人學(xué)號應(yīng)試人所在院系題號一 (20)二(20)三(20)四(20)五(20)得分得一、判斷題。對真命題在題后括號內(nèi)打“,對假命題打“X分(每小題1分,共20分)。1 .C+表 達 式5/2的結(jié)果為2.5。(X )2.C(V )+表達式double(5/2)的結(jié)果為2(即 2.0),類型為double3.C+表達式(double

2、)5/2的結(jié)果為勺2.5, 類型為double (V )(V )4 .C+ 表達式 5/2.0 的結(jié)果為 2.5, 類型為 double(V )5 . First, first, 1 st均是C +合法的標識符,可用作變量命名。(X)6.定義一個變量就意味著給該變量分配內(nèi)存空間。()7 ,定義變量double *p;則系統(tǒng)為指針變量 p分配sizeof (double) 字節(jié)的內(nèi)存空間。(X)8 .定義變量int a=5; double x;并執(zhí)行表達式語句 x= (double) a/2; 后,變量a本身的數(shù)據(jù)類型將被改成 double 型,在內(nèi)存中將占用sizeof (double) 字

3、節(jié)。9 .定義數(shù)組double array 10 ;系統(tǒng)將為該數(shù)組分配 10*sizeof (double)字節(jié)10素表示整個數(shù)組的所有元. 定義數(shù)組 double array 10 ; 后, array10(X)new double11. C+中,new是一個運算符(或稱為操作符)。運算表達式10 執(zhí) 行時,將在內(nèi)存的堆空間中,“切下”10*sizeof (double )字節(jié)連續(xù)的內(nèi)存空 間,并將該空間的首地址作為表達式的值;若“切下”內(nèi)存空 間的操作不成功, 則該表達式的值為NULL (空地址)。(V)12 ,程序中char *str=abcdefg ”;表示定義一個指針變量str(系統(tǒng)

4、分配能夠存放一個地址值的內(nèi)存空間),abcdefg ”表示系統(tǒng)分配8字節(jié)的內(nèi)存空間存 放 該字符串常量,用abcdefg本身表示存放該字符串的內(nèi)存空間首地址,并用該首地址值初始化指針變量stro( V)13 .程序中char str 80 =abcdefg ”; 其中abcdefg 不是字符串常量,并不表示地址值,而是用來初始化數(shù)組元素的,其中元素 str 7的值為串結(jié)束標志字符,0,。( V)14 .程序中執(zhí)行 char str 80 =abcdefg ” ; cout str;后將輸出一個地址值。(X)15 .程序中執(zhí)行int a 5=一個地址值。(0, 1,2, 3, 4 ; cout

5、a;后將輸出(/)()16 . 一個C +程序只能定義一個 main()函數(shù)17. 函數(shù)原型聲明中,可以僅寫出形式參數(shù)的類型,也可以寫出形式參數(shù)的名稱。例女口: void swap (int &, int &); 或 void swap ( int &a, int&b);( V )18. 調(diào)用函數(shù)時,系統(tǒng)將為所有的形式參數(shù)(包括引用型參數(shù))分配內(nèi)存空間。(X)19 .對于值返回的函數(shù)(如 double sqrt(double );)函數(shù)返回時,將創(chuàng)建一個臨時變量存放函數(shù)的返回值20.有兩個函數(shù),其形式參數(shù)相同、函數(shù)名相同,僅它們的返回類型不同,貝尼們構(gòu)成函數(shù)重載。(X)sum += *arra

6、y+;;的功能return sum;#i nclude using n amespace std;int mai n()double array10=1,2,3,4,5,6,7,8,9,10 , si, s2, s3; si = f1 (arrayf 5);s2 = f2 (array+5f 5);s3 = f2 (&array1, 5);cout si = si ” , s2 = s2 ” ,s3 = s3 T func(T夫 a, int n)T x, y;x = y = a 0;for(i nt i=l; i x) x = a i;if (a i y) y = a i;return x-

7、y;#include using n amespace std;int mai n() int a10 = 12, 66, 34, 8, 56, 34, 8, 99, 73, 50; doublex8 = 43.1, 76.2, 41, -22, 85.4,0, 146, 98; cout DI= func (a, 10) endl; cout D2 = func (x, 8) en dl;return 0;(1)寫出編譯系統(tǒng)自動生成的兩個模板函數(shù)的定義;int fun c(i ntint n)(int x, y;X = y = a 0;for(i nt i=0; i x) x = ai; i

8、f (a i y) y = ai;return x-y;double func (double文 a,double n)double x, y;X = y = a 0;for(i nt i=0; i x) x = ai; if (a i y) y = ai;return x-y;(2) 寫出程序運行輸出的結(jié)果;D1 = 91D2 = 168(3) 指出函數(shù)模板的功能。計算并返回數(shù)組元素最大值與最小值的差3.有如下程序,指出程序運行所輸出的結(jié)果(4分)#in elude using n amespacestd; int mai n() char str100 = SMART,for(p=str;

9、*p!=0; p+) cout p en dl;程序運行輸出的結(jié)果cout p-str en dl;*PSMARTreturn 0;7MARTARTRT4.有如下函數(shù)(4分)double f(double x) double a5 = 1,-2, 3,F,57double y = 0;for(i nt i=0; i5; i+)V = y*x + ai; return y;(1)寫出執(zhí)行語句1) ” , f (0) en dl;, f(1) cout jvo_display (Tcout a0;for (i nt i=(4 cout en dl;2. 函數(shù) int digit (int n)整數(shù)

10、,并返回。例如: int digit(i nt n)1 ; in; i+) cout 大 a, int n) r n ? a i;的功能是將整數(shù)n按其十進制數(shù)各位倒置生成一個新digit (123) 的結(jié)果為321。請完成函數(shù)定義。int m = 0while( n)m = m*10 + n %10;n /= ? 10 ;return () m ;3 .函數(shù) char *StrCpy (char , const char * source);的功能是將source為首地址的字符串復(fù)制到dest為首地址的字符串中,并返回目標字符串的首地址。char *StrCpy(char *dest, con

11、st char夫 source)int i;(9 ) for (i=0; source i ! =z 0 A ; i+ )dest i = source i;dest i = (11),0,;return (12) dest ;4.請完成程序(通過傳遞引用、傳遞指針交換實參數(shù)據(jù)的值)#i nclude &a, T &b) using n amespace std;template void swap_r(T T temp;T夫 b)template void swap_p(T (16) T temp ;temp =夫 a;*a = *b;夫 b = temp;int main()int a,

12、b; a=3; b=5;swap_r (17) ar(18)b);cout a b en dl;a=3; b=5;swap_p (19) & a,(20)&b );cout a b en dl;return 0;四、簡答題。有如下關(guān)于日期處理的程序,請簡要回答各小題(共 20分)。注:行號不是程序中的成分,是為了敘述方便而添加 的。#i nclude #i nclude using n amespace std;int isLeapyear(i nt year)(return year%4=0 & year%100!=0 | year%400=0;void NextDate(i nt &yea

13、rz int &mon th, i nt &day)(int days12=31,28,31,30,31,30,31,31,30,31,30,31; days1 += isLeapyear (year);day+;if(day daysm on th-1)(day = 1; mon th+;if(mo nth=13)(month = 1; year+;void NextNDate(int &year(for(i nt i=0; in; i+)z int &mon th, int &day, int n)NextDate(yearzmon th, day); void ShowDate(i nt

14、 year(cout setfill (z int mon th, int day)10 T) setw(2) month setw(2)vdayv H/yearsetfill(1 T )endl;int main()(int year=2007 f mon th=lr day=l r n=400, i;ShowDate(year, mon th, day);for(i=0; i days*m on th-l)(夫 month)+;夫 day = 1;if(*mo nth=13)(夫 year)+; *mon th = 1;01/01/2007程序中調(diào)用該修改后的函數(shù)語句(第 25, 3801

15、/02/2007行)則應(yīng)修改為:01/03/2007NextDate(&year, &month, &day);01/04/20073.將程序運行的輸出結(jié)果填入右表中(5分)01/05/200701/06/200702/10/20071. (6分)從鍵盤輸入一個正整數(shù)n,找出nl以內(nèi)被3除余2,且被5除余3,且被 7除余5的最大整數(shù)。若存在,則輸出該數(shù);否則輸出“不存在”。#i nclude using n amespace std;int mai n()int n, i;cout n;for( i=n; i; i一)(if(i%3=2 & i%5=3 & i%7=5) break;if(i0

16、)cout i en dl;elsecout 不存在=f A 1 & stri=T a * &stri a且sZ?且sc。三角形的周長為 len = a + b + c ; 面積為 Area =-b)(sc).#in clude int tria ngle(double a, double b, double c, double &lenzdouble &area)(int flag =1; double s = (a+b+c)/2;if(sa & sb & sc)(area = sqrt(s夫(s-a) 夫(s-b)* (s-c); le n = a + b +c;retur n flag;#in clude using n amespace std;int mai n()(double a, b, c, le nz area;int flag;a = 3; b = 4; c = 5;flag = triangle(a, b, c, lenz area);c

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論