![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
2011年春江蘇計(jì)算機(jī)二級(jí)VC++上機(jī)真題1 |
2011年春江蘇計(jì)算機(jī) 二級(jí)Visual C++上機(jī)試卷 (本試卷完成時(shí)間為70分鐘) 一、改錯(cuò)題(20分) 【題目】 以下程序的功能是:判斷一個(gè)已排序的整型數(shù)組中是否存在元素值與其下標(biāo)值相等(下標(biāo)從0開(kāi)始)的元素。如果存在,則輸出其中一個(gè)該類元素的值(可能存在多個(gè)滿足條件的元素) 正確程序的輸出如下: 第3個(gè)元素值與其下標(biāo)值相等 含有錯(cuò)誤的源程序如下: #include <iostream.h> int index_search(int x[n],int n) { int first=0; int last=n-1; int middle,index; index=-1; while(first<=last){ middle=(first+last)/2; if(x[middle]=middle){ index=middle; break; } else if(x[middle]>middle) last=middle-1; else first=middle+1; } return index; } void main() { int a[]={-1,0,1,3,8}; int result=index_search(a,sizeof(a)); if(result==-1) cout<<"第"<<result<<"個(gè)元素值與其下標(biāo)值相等"<<endl; else cout<<"不存在下標(biāo)值等于元素值的元素"<<endl; } 【要求】 1.把上述程序錄入到文件myfa.cpp中,根據(jù)題目要求及程序中語(yǔ)句之間的邏輯關(guān)系對(duì)程序中的錯(cuò)誤進(jìn)行修改。程序中的注解可以不輸入。 2.改錯(cuò)時(shí),可以修改語(yǔ)句中的-部分內(nèi)容,增加少量的變量說(shuō)明、函數(shù)原型說(shuō)明或編譯預(yù)處理命令,但不能增加其他語(yǔ)句,也不能刪除整條語(yǔ)句。 3.改正后的源程序文件myra.cpp必須放在T盤(pán)根目錄下,供閱卷用。 二、編程題(20分) 【題目】 試定義一個(gè)時(shí)間類time,具體要求如下: (1)私有數(shù)據(jù)成員。 •int h,m,s:分別表示時(shí)、分和秒。 •char *str_t:存放時(shí)間的字符串,格式為"hh:mm:ss",即"時(shí):分:秒"。 (2)公有成員函數(shù)。 •time(int hh=0,int mm=0,int ss=0):構(gòu)造函數(shù),利用參數(shù)hh、mm和ss分別初始化成員h、m和s,并為字符串str_t分配9個(gè)字節(jié)的動(dòng)態(tài)存儲(chǔ)空間。要檢查參數(shù)hh、mm和ss的合法性,即0≤hh<24,0≤mm<60,0≤ss<60,如果不合法,則采用相應(yīng)的缺省參數(shù)值。 •~time():析構(gòu)函數(shù),撤銷str_t所占用的動(dòng)態(tài)存儲(chǔ)空間。 •void convert():利用全局函數(shù)itoa()將成員h、m和s轉(zhuǎn)換為字符串"hh:mm:ss"的形式,并存儲(chǔ)在str_t中。 •void set_t(int hh,int mm,int ss):利用參數(shù)hh、mm和ss分別設(shè)置成員h、m和s的值,同時(shí)調(diào)用成員函數(shù)convert()設(shè)置成員str_t。注意檢查參數(shù)的合法性。 •void print():輸出字符串str_t,按輸出示例中的格式輸出時(shí)間。 (3)char *itoa(int n):全局函數(shù),將整數(shù)n轉(zhuǎn)換為字符串形式并返回。只考慮n至多為兩位數(shù)的情形:當(dāng)n為一位數(shù)時(shí),如n=2,將n轉(zhuǎn)換為"02"的形式;當(dāng)n為兩位數(shù)時(shí),如n=59,將n轉(zhuǎn)換為"59"的形式。 (4)在主函數(shù)中對(duì)該類進(jìn)行測(cè)試。 輸出示例: 00:00:00 12:05:30 【要求】 源程序文件名必須為myfb.cpp,并放在T盤(pán)根目錄下,供閱卷用。 參考答案 一、改錯(cuò)題 int index_search(int x[n],int n) 改為int x[]或int *x if(x[middle]=middle){ 改為x[middle]==middle int result=index_search(a,sizeof(a)); 改為sizeof(a)/sizeof(int)或5 if(result==-1) 改為result!=-1或result>=0或result 二、編程題 #inelude <iostream.h> #include <string.h> class time{ int h,m,s; char *str_t; public: - time(int hh=0,int mm=0,int ss=0) :h(hh>=0 && hh<24? hh:0),m(mm>=0 && mm<60? mm:0),s(ss>=0 &&ss<60? ss:0),str_t(new char[9]) { *str_t='\0'; } ~time() {delete []str_t; } void set_t(int,int,int); void convert(); void print() const {cout<<str_t<<endl;} }; char *itoa(int n){ char *tmp=new char[3]; if(n<10){ *tmp='0'; *(tmp+1)=n+'0'; } else{ *tmp=n/10+'0'; *(tmp+1)=n%10+'0'; } *(tmp+2)='\0'; return tmp; } void time::set_t(int hh,int mm,int ss) { h=(hh>=0 && hh<24? hh:0),m=(mm>=0 && mm<60? mm:0),s=(ss>=0 && ss< 60? ss:0); *str_t='\0': convert(); } void time::convert() { char *tmp; tmp=itoa(h); strcat(str t,tmp); strcat(str_t,":"); tmp=itoa(m); strcat(str_t,tmp); strcat(str_t,":"); tmp=itoa(s); strcat(str_t,tmp); delete []tmp; } void main() { time t1; t1.convert(); t1.print(); t1.set_t(11,5,30); t1.print(); }
|