![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
2012秋江蘇計算機二級C++上機試卷第1套 |
2012秋江蘇省高校計算機等級考試 二級 C++上機試卷 一、改錯題(20分) 【題目】 以下程序的功能是:將一個整數(shù)轉(zhuǎn)換成相應(yīng)的字符串,例如,將整數(shù)1024轉(zhuǎn)換成字符串” 正確程序的輸入/輸出結(jié)果如下(下劃線部分為鍵盤輸入) 請輸入一個整數(shù):123 123 請輸入一個負整數(shù):-123 -123 含有錯誤的源程序如下: #include <iostream.h> #include <math.h> #define N 20; char *itoa(int x, char s[]) //將整數(shù)x轉(zhuǎn)換為字符串形式,結(jié)果存入數(shù)組s中 { int i=0,j=0; char c,tmp; if(x<0){ s[0]=’-‘; i=j=1; x=x*(-1); } if(x==0){ s[0]=” s[1]='\0'; } else{ while(x) { c=x%10; x/=10; s[i++]=c; } s[i+1]='\0'; for(int k=j;k<(j+i)/2;k++) //交換數(shù)組s中數(shù)字的先后順序 tmp=s[k],s[k]=s[i+j-k-1],s[i+j-k-1]=tmp; } return s; } void main() { int x; char s[N]; cout<<"請輸入一個整數(shù)"; cin>>x; cout<<itoa(x,s)<<endl; } 【要求】 1.打開T盤中myfa.txt文件,將其文本拷貝到文件myfa.cpp中(或?qū)⑸鲜龀绦蜾浫氲轿?/SPAN> 件myfa.cpp中),根據(jù)題目要求及程序中語句之間的邏輯關(guān)系對程序中的錯誤進行修改。程 序中的注解可以不輸入。 2.改錯時,可以修改語句中的一部分內(nèi)容,增加少量的變量說明、函數(shù)原型說明或編譯預(yù) 處理命令,但不能增加其他語句,也不能刪除整條語句。 3.將改正后的源程序文件myfa.cpp必須放在T盤根目錄下,供閱卷用。 二、編程題(20分) 【題目】 試定義一個類Array,實現(xiàn)由一個數(shù)組派生出另一個數(shù)組,派生規(guī)則如下:新數(shù)組的元素取值為原數(shù)組中相同位置元素的左、右兩個相鄰元素前后拼接后形成的整數(shù)(左鄰元素在前,右臨元素在后)。規(guī)定最左(右)列元素的左(右)臨元素為該元素所在行的最右(左)側(cè)的元素,具體要求如下: (1)私有數(shù)據(jù)成員 int a[3][4]:原數(shù)組 int b[3][4]:派生數(shù)組 (2)公有成員函數(shù) Array(int t[][4],int n):構(gòu)造函數(shù),利用參數(shù)t的前n行元素初始化數(shù)據(jù)成員a int nn(int t1,int t2):返回t1、t2拼接后形成的整數(shù)(t1在前,t2在后) void fun() :按題意生成新數(shù)組,并將結(jié)果存放到數(shù)據(jù)成員b中 void print():按矩陣形式輸出成員數(shù)組 (3)在主函數(shù)中對該類進行測試 輸出示例: 原數(shù)組: 41 67 34 0 69 24 78 58 62 64 5 45 派生數(shù)組 67 4134 670 3441 5824 6978 2458 7869 4564 625 6445 562 【要求】 源程序文件名必須為myfb.cpp,并放在T盤根目錄下,供閱卷用 一、改錯題 #define N 20; 修改為#define N 20\ s[0]=” c=x%10; 修改為 c=x%10+’ s[i+1]='\0'; 修改為 s[i]='\0'; 二、編程題 #include <iostream.h> #include <stdlib.h> class Array{ int a[3][4],b[3][4]; public: Array(int t[][4],int n) { for(int i=0;i<n;i++) for(int j=0;j<4;j++) a[i][j]=t[i][j]; } int nn(int t1,int t2) { if(t2==0) return t1*10; int n=1,t=t2; while(t){ n=n*10; t=t/10; } return t1*n+t2; } void fun() {for(int i=0;i<3;i++) for (int j=0;j<4;j++){ int u=j-1,d=j+1; u=u<0?3:u; d=d>3?0:d; b[i][j]=nn(a[i][u],a[i][d]); } } void print() { cout<<"原有數(shù)組"<<endl; for(int i=0;i<3;i++) for (int j=0;j<4;j++) cout<<a[i][j]<<'\t'; cout<<endl; cout<<"派生數(shù)組"<<endl; for(i=0;i<3;i++) for (int j=0;j<4;j++) cout<<b[i][j]<<'\t'; } }; void main() { int t[3][4]; for(int i=0;i<3;i++) for (int j=0;j<4;j++) t[i][j]=rand()%100; Array test(t,4); test.fun(); test.print(); } |