填空题【说明】 以下代码实现了当用户退出界面时,判断TextEdit中的文字是否发生改变,弹出对话框判断,让用户选择是否保存文件或取消退出界面操作。阅读下面的代码,将其补充完整。 【代码7-1】 Begin VB.Form Forml //...窗体描述(略) Begin VB.TextBox TextEdit Height = 1830 Left = 180 Tablndex = 0 Text = "TextEdit" Top = 360 Width = 3885 End //...窗体描述(略) End 【代码7-2】 Dim txtchange As Boolean Dim myval As String Private Sub Form Load () TextEdit.Text: "CIU, 中国软考联盟!" txtchange = False End Sub 【代码7-3】 Private Sub TextEdit_Change () Static notchange As Boolean (1) notchange = Tree End Sub Private Sub Form_Unload ( Cancel As Integer ) Dim myval As String If (2) Then myval = MsgBox ( "保存文件的更改吗?", vbYesNoCancel, "提示信息" ) If (3) Then MsgBox "保存成功" End End If If (4) Then End If (5) Then Cancel = 1 End If End Sub
填空题[说明1]下面[C程序代码1]的设计意图是:计算1~100各数的平方。运行该段代码后,没有得到应有的运算结果。[C程序代码1]
填空题阅读以下函数说明和C语言函数,将应填入 (n) 处的字句写在对应栏内。 [说明] 该程序从正文文件test.txt中读入一批整数,并将它们按照递增的顺序存放在一个链表中。其中,函数struct Link*insertChain(struct Link*head,structLink*k)用来寻找结点k在链表head中的插入位置,并插入该结点。 [C程序] #include <stdio.h> #include <stdlib.h> struct Link int number; struct Link *next; struct Link *insertChain(struct Link *head,struct Link *k); VOid main() struct Link *ptr, *head; FILE *fd; int hum; if((fd=fopen("test.txt","r"))==NULL) print("Cannot open this file!/n"); return; head=NULL; while(fscanf(fd,"%d",hum)==1) ptr= (1) ; if(!ptr) return; ptr->number=hum; ptr->next=NULL; head=insertChain(head,ptr); (2) ; return; struct Link *insertChain(struct Link *head,struct Link *k) struct Link *ptr,*u; ptr=head; while(ptr && k && k->number>ptr->number) u=ptr; (3) if(ptr == head) head=k; else (4) ; (5) ; return head;
填空题[说明]某绘图系统存在Point、Line和Square3种图元,它们具有Shape接口,图元的类图关系如图8-5所示。现要将Circle图元加入此绘图系统以实现功能扩充。已知某第三方库已经提供了XCircle类,且完全满足系统新增的Circle图元所需的功能,但XCircle不是由Shape派生而来的,它提供的接口不被系统直接使用。[C++代码1]既使用了Xcircle,又遵循了Shape规定的接口,既避免了从头开发一个新的Circle类,又可以不修改绘图系统中已经定义的接口。[C++代码2]根据用户指定的参数生成特定的图元实例,并对其进行显示操作。该绘图系统定义的接口与XCircle提供的显示接口及其功能如表8-2所示。[C++程序代码1]classCircle:public(1)Private:(2)m_circle;public:voiddisplay()m_circle.(3);[C++程序代码2]classFactorypublic:(4)getShapeInstance(inttype)//生成特定类实例switch(type)case0:returnnewPoint;case1:returnnewRectangle;case2:returnnewLine;case3:returnnewCircle;default:returnNULL;;voidmainintargc,char*argv[])if(argc!=2)cout<<"errorparameters!"<<end1;return;inttype=atoi(argv[1]):Factoryfactory;Shape*s;s=factory.(5);if(s==NULL)cout<<"Errorgettheinstance!"<<end1;return;s->display();(6);return;
填空题阅读以下函数说明和C语言函数,回答问题。 [说明] 函数Node *difference(A,B)用于求两个集合之差C=A-B,即当且仅当e是A中的一个元素,但不是B中的元素时,e是C中的元素。集合用有序链表实现,用一个空链表表示一个空集合,表示非空集合的链表根据元素之间按递增排列。执行C=A-B之后,表示集合A和B的链表不变,若结果集合C非空,则表示其链表根据元素之值按递增排列。函数append()用于在链表中添加节点。 [C函数] typedef struct node int element; struct node *link; Node; Node *A,*B,*C; Node *append(last,e) Node *last; int e; last->link=(Node *)malloc(sizeof(Node)); last->link->element=e; return(last->link); Node *difference(A,B) Node *A,*B; Node *c,*last; C=last=(Node *)malloc(sizeof(Node)); while( (1) ) if(A->element<B->element) last=append(last,A->element); A=A->link: else if( (2) ) A:A->link; B:B->link; elSe (3) ; while( (4) ) last=append(last,A->element); A=A->link: (5) ; last=c; c=c->link; free(last); return(c);
填空题[说明]如图8-7所示的流程图用于从数组K中找出一切满足:K(I)+K(J)=M的元素对(K(I),K(J))(1≤I≤J≤N)。假定数组K中的N个不同的整数已按由小到大的顺序进行排列,M是给定的常数。[流程图]在如图8-7所示的流程图中,比较“K(I)+K(J)=M?”最少执行次数约为______。
填空题【说明2.1】 以下C语言函数用二分插入法实现对整型数组a中n个数的排序功能。 【函数2.1】 void fun1 (int a[]) int i,j,k,r,x,m; for(i=2;i<=n;i++) (1) ; k=1;r=i-1; while(k<=r) m=(k+r)/2; if(x<a[m])r=m-1; else (2) ; for(j=i-1;j>=k;j--) a[j+l]=a[j]; (3) ; 【说明2.2】 以下程序可以把从键盘上输入的十进制数(long型)以二~十六进制形式输出。 【程序2.2】 #include<stdio.h> main() charb[16]='0','l','2','3 ,4,'5','6','7','8','9','A','B','C','D','E','F'; int c[64],d,i=0,base; long n; printf("enter a number:/n"); scanf("%1d", printf("enter new basc:/n"); scanf("%d", do c[i]= (4) ; i++; n=n/base; while(n!=0); printf("transmite new base:/n"); for(--i;i>=0;--i) d=c[i]; printf("%c", (5) );
填空题
阅读以下函数说明和C语言函数,将应填入{{U}} (n) {{/U}}的字句写在答题纸的对应栏内。
[说明1] 函数int fun1(int m, int
n)的功能是:计算并返回正整数m和n的最大公约数。 [函数1] int fun1(int m,
int n) { while ({{U}} (1) {{/U}})
{ if (m>n) m=m-n;
else n=n-m; } {{U}}(2) {{/U}};
} [说明2] 函数long
fun2(char*str)的功能是:自左至右顺序取出非空字符串str中的数字字符形成一个十进制整数(最多8位)。
例如,若字符串str的值为“f3g8d5.ji2e3p12fkp”,则函数返回值为3852312。
[函数2] long fun2(char *str) {
int i=0; long k=0; char *p=str;
while (*p!='\0'
++i; }
{{U}} (5) {{/U}}; } return
k; }
填空题[说明] 函数Node*difference(A,B)用于求两个集合之差C=A-B,即当且仅当e是A中的一个元素,但不是B中的元素时,e是C中的元素。集合用有序链表实现,用一个空链表表示一个空集合,表示非空集合的链表根据元素之值按递增排列。执行C=A-B之后,表示集合A和B的链表不变,若结果集合C非空,则表示其链表根据元素之值按递增排列。函数append()用于在链表中添加结点。 [函数] typedef struct node int element; struct node *link; Node; Node *A, *B, *C; Node *append(last,e) Node *last; int e; last->link= (Node *)malloc (sizeof (Node)); last->link->element=e; return(last->link); Node *difference (A,B) Node *A, *B; Node *c, *last; C=last= (Node *)malloc(sizeof (Node)); while( (1) ) if(A->element <B->element) last=append (last,A->element); A=A->link; else if( (2) ) A=A->link; B=B->link; else (3) ; while( (4) ) last=append(last,A->element); A=A->link; (5) ; last=C; C=C->link; free(last); return (C);
填空题【说明】
下面一段程序从给定的数组b中找出值最小的元素,并输出该元素的数组下标、内存地址minaddr以及元素值本身。函数findmin负责在给定的数组中查找最小值,并返回最小值所在的内存地址,函数有三个参数:array是所要处理的数组;size是数组的大小;index负责从函数返回具有最大值的元素在数组中的数组下标。请填充空白,使其成为一个合乎要求的完整程序。
【程序】
//程序开始
#include<iostream.h>
int *findmin(int *array, int size, int
void main()
{
/****** 变量定义部分 ***********/
int b[10] = {34, 34, 23, 89, 1, 2, 13, 42, 5, 54};
{{U}} (1) {{/U}};
int idx;
/******* 寻找最小值 ************/
minaddr = findmin(b, sizeof(b)/sizeof(int), idx);
/******* 输出最小值的有关信息 ************/
cout << "Min value index: "<<idx<<end1
<<"Min value address: "<<minaddr<<end1
<<"Min value: "<<{{U}} (2) {{/U}}<<end1;
}
/******* 函数findmin的定义部分 ************
int *findmin(int *array, int size, int //max 是当前具有最小值的数组元素下标值
for(int i = 1;{{U}} (3) {{/U}}; i++)
{
if(array[i]<{{U}} (4) {{/U}})
min = i;
}
{{U}} (5) {{/U}};
return array + min;//返回最小值所在内存地址
}
填空题[函数说明3.1] 假设以带头结点的单循环链表作非递减有序线性表的存储结构。函数deleteklist(LinkList head)的功能是删除表中所有数值相同的多余元素,并释放结点空间。 [函数3.1] Void deleteklist (LinkList head) LinkNode *p,*q; p=head->next; while (p!=head) q=p->next; while( (1) ) (2) ; free(q); q=p->next; p=p->next; [函数说明3.2] 已知一棵完全二叉树存放于一个一维数组T[n]中,T[n]中存放的是各结点的值。下面的程序的功能是:从T[0]开始顺序读出各结点的值,建立该二叉树的二叉链表表示。 [函数3.2] #include<istream.h> typedef struct node int data; stuct node leftChild,rightchild; BintreeNode; typedef BintreeNode *BinaryTree; void ConstrucTree(int T[] , int n, int i, BintreeNode *&ptr) if(i>=n) (3) ; /*置根指针为空*/ else ptr=(BTNode*) malloc(sizeof(BTNode)) ptr->data=T[i]; ConstrucTree (T,n, 2*i+1, (4) ); ConstrucTree (T,n, (5) ,ptr->rightchild); main(void) /*根据顺序存储结构建立二叉链表*/ Binarytree bitree; int n; printf("please enter the number of node: /n%s"; n); int *A=(int*)malloc (n*sizeof (int)) ; for (int i=0; i<n; i++) scanf("%d" ,A+i); /*从键盘输入结点值*/ for (int i=0; i<n; i++) printf("%d",A[i]); ConstructTree (A,n, 0,bitree);
填空题[说明] 下面程序定义了一个类Point及其成员函数,然后从类Point派生出类Circle及其成员函数。 [Java程序] class Mainjava public static void main (String arg[]) Circle c=new Circle(2.5,3.7,4.3); System.out.println(" X coordinate is "+c.getX() +"/nRadius is: "+c.get Radius()); (1) ; //设置新的圆心(2,2) Point pRef=c; System.out.println("/n Circle printed as a point is : " +"["+ (2) +"."+ (3) +"]"): class Point protected double x,y; public Point (double a, double b) x = a; y = b;) void setPoint (double a, double b) x = a; y = b; final double getX () return x; final double getY () return y; class Circle (4) protected double radius; public Circle(double r, double a, double b) (5) ; radius=r ; void setRadius (double r) radius=r; final double getRadius () return radius; final double area () return 3.14159*radius*radius;
填空题【说明】将A,B,C,D,E,F这6个变量排成如图(a)所示的三角形,这6个变量分别取[1,6]中的整数,且均不相同。求使三角形三条边上的变量之和相等的全部解。图(b)就是一个解。程序引入变量a,b,c,d,e,f,并让它们分别顺序取1~6的整数,在它们互不相同的条件下,测试由它们排列成的如图(a)所示的三角形三条边上的变量之和是否相等,如果相等即为一种符合要求的排列,就输出它们。【程序】voidmain(){inta,b,c,d,e,f;for(a=1;a<=6;a++)for(b=1;b<=6;b++){if({{U}}(1){{/U}})continue;for(c==1;c<=6;c++){if({{U}}(2){{/U}})continue;for(d=1;d<=6;d++){if({{U}}(3){{/U}})continue;for(e=1;e<=6;e++){if({{U}}(4){{/U}})continue;f=21-(a+b+c+d+e);if({{U}}(5){{/U}}){printf("%6d",a);printf("%4d%4d",b,f);printf("%2d%4d%4d",c,d,e);scanf("%*c");/*按回车键,继续找解*/}}}}}}
填空题【说明】
编写字符界面的Application程序,接收依次输入的10个整型数据,每个数据一行,将这些数据按升序排序后从系统的标准输出设备输出。
【Java代码】
import java.iO.*;
import java.util.* ;
public class compositor
{
public static void main ( String args[] )
{
final int NUMBER=10;
Vector dataVector=new Vector ();
try
{
BufferedReader br=new BufferedReader (
{{U}}(1) {{/U}}InputStreamReader ( System.in ));
System.out.println ("请输入"+NUMBER+"个整数");
for (int i=0; i<NUMBER; i++ )
{
int temp=Integer.parselnt ( br.{{U}} (2) {{/U}});
int low=0, high=i-1, mid=0;
while ({{U}} (3) {{/U}})
{
System.out.println ( low+","+mid+","+high );
{{U}}(4) {{/U}};
if ((( Integer ) dataVectOr.get( mid )) .intValue () ==temp )
{
data Vector.insertElementAt ( new Integer ( temp ), mid );
break;
}
else if ((( Integer ) dataVector.get ( mid )) .intValue ( ) >temp )
{
high=mid-1;
}
else
{
{{U}}(5) {{/U}};
}
}
if ( low>high )
{
dataVector, insertElementAt ( new Integer ( temp ), iow );
}
}
//输出
System.out.println ( "/n升序的排序结果为; ");
for (int i=0; i<NUMBER; i++ )
{
System.out.print ( dataVector.get( i ) .toString () +"/t" );
}
}
catch ( NumberFormatException nfe)
{
System.out.println ( nfe.toString ());
System.out.println ( "整数格式输入错误。");
}
catch ( IOException ioe )
{
System.out.println ( ioe.toString ());
}
}
}
填空题[流程图说明]下列流程图用泰勒(Taylor)展开式sinx=x-x3/3!+x5/5!-x7/7!+…+(-1)n*x2n+1/(2n+1)!+…计算并打印sinx的近似值。其中用ε(>0)表示误差要求。[流程图]
填空题[说明2] 有两个进程(编号分别为0和1)需要访问同一个共享资源。为了解决竞争条件(race condition)的问题,需要实现一种互斥机制,使得在任何时刻只能有一个进程访问该共享资源。以下[C程序代码2]给出了一种实现方法。 [C程序代码2] int flag[2]; /*flaq数组, 初始化为FALSE*/ Enter_Critical_Section (int my_task_id, int other_task_id) while (flag [other_task_id]==TRUE); /*空循环语句*/ flag[my_task_id]=TRUE; Exit_Critical_Section (int my_task_id, int other_task_id) flag[my_task id]=FALSE; 当一个进程要访问临界资源时,就可以调用[C程序代码2]给出的这两个函数。[C程序代码3]给出了进程0的一个例子。 [C程序代码3] Enter_Critical_Section(0,1) ; …使用这个资源… Exit_Critical_Section (0,1) ; …做其他的事情…
填空题【说明8.1】
以下程序的功能是:生成20个200~300之间的随机整数,输出其中能被5整除的数并求出它们的和。
【程序代码8.1】
Private Sub Command1_Click()
For i=1 To 20
x=Int({{U}} (1) {{/U}}*200+100)
If{{U}} (2) {{/U}}=0 Then
Print x
S=S+{{U}} (3) {{/U}}
End If
Next i
Print"Sum=";S
End Sub
【说明8.2】
程序8.2运行后,单击窗体,则在窗体上显示的内容是:a={{U}} (4) {{/U}}和b={{U}} (5) {{/U}}。
【程序代码8.2】
Private Sub Form_Click()
Dim a As Integer,b As Integer
a=20:b=50
p1 a,b
p2 a,b
p3 a,b
Print"a=";a,"b=";b
End Sub
Sub p1(x As Integer, ByValy As Integer)
x=x+l0
y=y+20
End Sub
Sub p2(ByVal×As Integer, y As Integer)
x=x+l0
y=y+20
End Sub
Sub p3(ByVal×As Integer, ByVal y As Integer)
x=x+10
y=y+20
End Sub
填空题[说明] 八皇后问题是指求解如何在国际象棋8*8棋盘上无冲突地放置八枚皇后棋子。因为在国际象棋里,皇后的移动方式是横竖交叉,所以在任意一个皇后所在位置的水平、竖赢和斜45。线上都不能有其他皇后棋子的存在。一个完整无冲突的八皇后棋子分布称为八皇后问题的一个解。本程序实现了八枚皇后棋子在8*8棋盘上无冲突的放置。 [函数] #include<math. h> #include<stdio.h> #define MAX 8 /*棋子数及棋盘大小MAX*MAX*/ int board [MAX]; /*印出结果*/ void show_result() int i; for (i=0;i<MAX;i++) printf("(%d, %d)",i,board [i]); printf("/n"); /*检查是否在同一直横斜线上有其他棋子*/ int check_cross (int n) int i; for(i=0; i<n; i++) if( (1) )return 1; return 0; /*放棋子到棋盘上*/ void put_chess (int n) int i; for (i=0; i<MAX; i++) board [n] =i; if( (2) ) if( (3) )show_result(); /*找到其中一种放法了,印出结果*/ else (4) ; void main() clrscr(); puts("The possible placements are:"); (5) ; puts("/n Press any key to quit…"); getch(); return:
填空题[说明]某绘图系统中有两个画矩形的程序DP1和DP2。程序DP1用函数draw_a_line(x1,y1,x2,y2)画一条直线,程序DP2则用函数drawline(x1,x2,y1,y2)画一条直线。当实例化矩形时,确定使用DP1还是DP2。为了适应变化,包括“不同类型的形状”和“不同类型的画图程序”,将抽象部分与实现部分分离,使它们可以独立地变化。若将“抽象部分”对应“形状”,“实现部分”对应“画图”,与一般的接口(抽象方法)和具体实现不同,则将这种应用称为Bridge(桥接)模式。图8-10显示了该系统与矩形绘制相关的各个类之间的关系。系统始终只处理3个对象:Shape对象、Drawing对象,以及DP1或DP2对象。以下是Java语言实现,能够正确编译通过。[Java代码]//DP1.java文件publicclassDP1staticpublicvoiddrawaline(doublex1,doubley1,doublex2,doubley2)...//省略具体实现//DP2.java文件publicclassDP2staticpublicvoiddrawline(doublex1,doubley1,doublex2,doubley2)...//省略具体实现//Drawing.java文件abstractpublicclassDrawing(1)publicvoiddrawLine(doublex1,doubley1,doublex2,doubley2);//V1Drawing.java文件publicclassV1DrawingextendsDrawingpublicvoiddrawLine(doublex1,doubley1,doublex2,doubley2)DP1.drawaline(x1,y1,x2,y2);//V2Drawing.java文件publicclassV2DrawingextendsDrawingpublicvoiddrawLine(doublex1,doubley1,doublex2,doubley2)//画一条直线(2);//Shape.java文件abstractpublicclassShape(abstractpublicvoiddraw();private(3)dp;Shape(Drawingdp)_dp=dp;protectedvoiddrawLine(doublex1,doubley1,doublex2,doubley2)(4);//Rectangle.java文件publicclassRectangle(5)privatedouble_x1,_x2,_y1,_y2;publicRectangle(Drawingdp,doublex1,doubley1,doublex2,doubley2)(6);_x1=x2;_x2=x2;_y1=y1;_y2=y2;publicvoiddraw()…//省略具体实现
填空题[程序5.1说明]
下列程序运行时,当单击窗体后,从键盘输入一个字符,判断该字符是字母字符、数字字符还是其他字符,并做相应的显示。窗体上无任何控件,并禁用Ase和Chr函数,Select Case语句中禁用枚举值。
[Visual Basic代码]
Private Sub Form Load ()
Dim x As String*1
X={{U}} (1) {{/U}} (“请输入单个字符”,“字符”)
Select Case UCase ({{U}} (2) {{/U}})
Case {{U}}(3) {{/U}}。
Print X+“是字母字符”
Case {{U}}(4) {{/U}}
Print X+ “是数字字符”
Case Else
Print X+“是其他字符”
End Select
[程序5.2说明]
在窗体上画一个名称为Timer1的计时器和一个名称为Labe11的标签。当运行程序后,将在标签中显示当前时间的数字时钟(包括时:分:秒)。
[Visual Basic代码]
Private Sub Fom_Load ()
Timer1.Interval=1000
EndSub
Private Sub Timer1_ timer() {{U}}(5) {{/U}}
EndSub