2011年程序员考试考前密卷(二)-下午试题
- 
				●试题八 阅读以下说明及Visual Basic部分程序代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 本程序是一个可进行数制转换的应用程序,图1所示是其运行界面。txtDec为TextBox控件名,Lblkes为转换结果labe1控件名。  【程序代码】 Option Explicit Private Function convert(pintDec As Intege,pintS As Integer)As String Dim intCt As Integer,intR As Integer Dim strCov As String,strRes As String intR=pintDec Mod pintS Do While (1) strCov=strCov&Str(intR) (2) intR=pintDec Mod pintS Loop For intCt= (3) To 1 Step-1 strRes=strRes&Mid(strCov,intCt,1) Next intCt convert=strRes End Function Private Sub cmdQuit_Click()′退出 Unload Me End Sub Private Sub optBin_Click()′二进制 lblRes.Caption="转换结果:"& (4) End Sub Private Sub optHex_Click()′十六进制 lblRes.Caption="转换结果:"& (5) End Sub Private Sub optOct_Click()′八进制 lblRes.Caption="转换结果:"&Oct(Val(txtDe C.Text)) End Sub 
- 
				●试题七 阅读以下说明和Java代码,将解答写入答题纸的对应栏内。 【说明】 下面程序的功能是找出所有三位数中,个、十、百位数字的立方和等于该数本身的三位数并显示。在程序的每条横线处填写一个适当的词或语句,使程序的功能完整。 public (1) class cube_Root{ public static void (2) main(String args[]){ System.out.println("个、十、百位数字的立方和等于该数本身的三位数有:"); for(int n=100;n<1000;n++) { int a,b,c; (3) b=n/10%10; (4) if( (5) ) System.out.println(n); } } } 
- 
				●试题六 阅读以下说明和C++程序,将应填入(n)处的语句写在答题纸的对应栏内。 【说明】 以下程序的功能是计算三角形、矩形和正方形的面积并输出。 程序由4个类组成:类Triangle、Rectangle和Square分别表示三角形、矩形和正方形;抽象类Figure提供了一个纯虚拟函数getArea(),作为计算上述3种图形面积的通用接口 【C++程序】 #include #include class Figure{ public: virtual double getArea()=0:∥纯虚拟函数 }; class Rectangle: (1) { protected: double height; double width; public: Rectangle(){}; Rectangle(double height,double width){ this->height=height; this->width=width; } double getArea(){ return (2) ; } }; class Square: (3) { public: Square(double width){ (4) ; } }; class Triangle: (5) { double la; double Ib; double lc; public: Triangle(double la,double lb,double lc){ This->la=la;this->lb=lb;this->lc=lc; } double getArea(){ double s=(la+lb+±c)/2.0; return sqrt(s*(s-1a)*(s-1b)*(s-1c)); } }; void main(){ Figure*figures[3]={ new Triangle(2,3,3),new Rectangle(5,8),new SqUare (5) ); for (int i=0;i<3;i++){ cout<<"figures["<getArea()< } } 
- 
				●试题五 阅读以下应用说明及Visual Basic部分程序代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 在窗体上画一个名称为Combo1的组合框,画两个名称分别Label1、Label2及Caption属性分别为"城市名称"和空白的标签。程序运行后,当在组合框中输入一个新项后按回车键(ASCII码为13)时,如果输入的项在组合框的列表中不存在,则自动添加到组合框的列表中,并在Label2中给出提示"已成功添加输入项";如果存在,则在Label2中给出提示"输入项已在组合框中"。 【程序】 Private Sub Combo1_ (1) (KeyAscii As Integer) If KeyAscii= (2) Then For i=0 To (3) If Combo1.Text= (4) Then Label2.Caption="输入项已在组合框中" Exit Sub End If Next i Label2.Caption="已成功添加输入项" Combo1. (5) Combo1.Text End If End Sub 
- 
				●试题四 阅读以下说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 从文件IN.DAT中读取一篇英文文章存入到字符串数组XX中;请编写程序,其功能是:以行为单位把字符串中所有小写字母o左边的字符串内容移到该串的右边存放,然后把小写字母o删除,余下的字符串内容移到已处理字符串的左边存放。最后把已处理的字符串仍按行重新存入字符串数组XX中,最后调用函数WRITEDAT(),把结果XX输出到文件OUT5.DAT中。 例如:原文:You can create an index on any field. you have the correct record. 结果:n any field.Yu can create an index rd.yu have the crrect rec 原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。 【函数】 #include "stdio.h" #include "string.h" #include "conio.h" #include "ctype.h" #include "mem.h" unsigned char xx[50][80]; int maxline=0; int readdat(void); void writedat(void); /*将题目要求的字符串中所有小写字母o左边的字符串内容移到该串的右边存放,即 将串中"最后"一个字母o左右两侧的内容互换*/ void StrOR(void) { inti; char*p1,*p2,t[80]; for(i=0;i { t[0]=′/0′; p2=xx[i]; while(*p2)/*找到最后一个′o′*/ {if( (1) )p1=p2; p2++; } strcat(t,p1+1); *p1=′\\0′; strcat(t,xx[i]); p1=xx[i]; p2=t; while(*p2)/*删去字符′o′*/ {if( (2) ) (3) =*p2; p2++; } (4) ; } } void main() { clrscr(); if(readdat()) {printf("Can't open the file IN.DAT!\\n"); return; } StrOR(); writedat(); } int readdat(void) { FILE*fp; int i=0; char*p; if((fp=fopen("in.dat","r"))==NULL) return 1; while(fgets(xx[i],80,fp)!=NULL) {p=strchr(xx[i],′\\n′); if(p) *p=0; i++; } maxline= (5) ; fclose(fp); return 0; } void writedat(void) {FILE*fp; int i; fp=fopen("in.dat","w"); for(i=0;i {printf("%s\n",xx[i]); fprintf(fp,"%s\n",xx[i]); } fclose(fp); } } } 
- 
				●试题三 阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 函数move(int*a,int n)用于整理数组a[]的前n个元素,使其中小于0的元素移到数组的前端,大于0的元素移到数组的后端,等于0的元素留在数表中间。 令a[0]~a[low-1]小于0(初始为空);a[low]~a[i-1]等于0(初始为空);a[i]~a[high]还未考察,当前考察元素为a[i]。a[high+1]~a[n-1]大于0(初始为空)。 【函数】 move(int*a,int n) { int i,low,high,t; low=i=0;high=n-1; while( (1) ) if(a[i]<0) { t=a[i];a[i]=a[low];a[low]=t; (2) ;i++; } else if( (3) ) {t=a[i];a[i]=a[high];a[high]=t; (4) ; } else (5) ; } 
- 
				●试题二 阅读下列程序或函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。 【函数2.1说明】 函数strcmp()是比较两个字符串s和t的大小。若s<t函数返回负数;若s=t函数返回0;若s>t,函数返回正数。 【函数2.1】 int strcmp(char *s,char *t) { while(*s && *t && (1) ){ s++;t++; } return (2) ; } 【程序2.2说明】 在n行n列的矩阵中,每行都有最大的数,本程序求这n个最大数中的最小一个。 【程序2.2】 #includestdio.h #define N 100 int a[N][N]; void main() {int row,col,max,min,n; /*输入合法n(<100),和输入n×n个整数到数组a的代码略*/ for (row=0;row<n;row++){ for(max=a[row][0],col=1;col<n;col++) if( (3) )max=a[row][col]; if( (4) )min=max; else if( (5) )min=max; } printf ("The min of max numbers is %d\n",min); } 
- 
				●试题一 阅读下列算法说明和算法,将应填入(n)处的语句写在答题纸的对应栏内。 【说明】 为了减少直接插入排序关键字的比较次数,本算法使用了二分(折半)插入法对一个无序数组R[1..n]进行排序。排序思想是对一个待插入元素,先通过二分法(折半)找到插入位置,后移元素后将该元素插入到恰当位置(假设R[]中的元素互不相同)。 【算法】 1.变量声明 X:DataType i,j,low,high,mid,R0..n 2.每循环一次插入一个R[i] 循环:i以1为步长,从2到n,反复执行 ①准备 X<-R[i]; (1) ;high<-i-1; ②找插入位置 循环:当 (2) 时,反复执行 (3) 若X.key 则high<-mid-1 否则 (4) ③后移 循环:j以-1为步长,从 (5) ,反复执行 R[j+1]<-R[j] ④插入 R[low]<-X 3.算法结束 
 
          