填空题[说明] 以下程序的功能是计算三角形、矩形和正方形的面积并输出,程序由5个类组成:AreaTest是主类,类Trianlge、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算面积的抽象方法。 [程序] public class AreaTest public static void main(String args []) Figure [] figures= new Triangle (2,3,3) ,new Rectangle (5,8) ,new Square (5) ; for (int i=0; 1 System. out .println (figures [i] +"area=" +figures [i]. getArea ()); public abstract class Figure public abstract double getArea() ; public class Rectangle extends (1) double height; double width; public Rectangle(double height, double width) this .height=height; this.width=width; public String toString () return "Rectangle: height= "+height+" , width= "+width+":"; public double getArea () return (2) ; class Square extends (3) public Square(double width) (4) ; public string toString () return "Square: width= "+width+" : " ; class Triangle extends (5) double la; double lb; double lc; public Triangle(double la,double lb, double lc) this.la=la; this.lb=lb; this.lc=lc; public String toString() return "Triangle : sides= "+la+" , "+lb+" , " +lc+" : " ; public double getArea () double s= (la+lb+lc)/2.0; return Math. sqrt (s* (s-la) * (s-lb) * (s-lc)) ;
填空题阅读以下说明和流程图回答问题,将解答填入对应栏。 [说明] “直接插入法”排序是一种N2运算量的例程,只能用在N较小的时候,其方法是:挑出第二个数将它按与第一个数大小的顺序插入,然后挑出第三个数将它按大小顺序插入到前两个数中,如此下去,一直到最后一个也插入。 注:流程中循环开始的说明按照“循环变量:循环初值,循环终值,增量”格式描述。 [问题] 将流程图的(1)~(5)处补充完整。
填空题【说明】
设计一个日期类Date包括年、月、日等私有数据成员。要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。
在Date类中设计如下重载运算符函数:
Date operator + (int days) : 返回某日期加上天数得到的日期。
Date operator - (int days) : 返回某日期减去天数得到的日期。
int operator - (Date
//day_tab二维数组存放各月天数,第一行对应非闰年,第二行对应闰年class Date
{
int year, month, day //年,月,日
int leap(int); //判断是否闰年
int dton(Date month = m; day = d;}
void setday(intd){day = d;}
void setmonth(int m) {month = m;}
void setyear(int y) {year =y;}
int getday() {return day;}
int getmonth() {return month:}
int getyear() {return yea;}
Date operator + (int days) //+运算符重载函数
{
static Date date;
int number ={{U}} (1) {{/U}}
date = ntod(number)
return date
}
Date operator - (int days) //-运算符重载函数
{
staffs Date date;
int number={{U}} (2) {{/U}};
number - = days;
date = ntod(number)
return date;
}
int operator - (Date
return days;
}
void disp()
{
cout<<year<<"."<<month<<". "<<day<<endl;
}
};
int Date: :leap( int year)
if({{U}} (4) {{/U}}) //是闰年
return 1; //不是闰年
else
return0:
}
int Date:: dton( Date
for(y=1;y<=d. year;y++)
if({{U}} (5) {{/U}})days+ =366; //闰年时加366天
else days + = 365; //非闰年时加365天
for(m =0;m<d. month-1;m++)
if( (6) )
days += day_tab[1] [m];
else
days +=day_tab[0] [m];
days + = d. day;
return days;
}
Date Date::ntod(intn) //将从元0年0月0日的天数转换成日期
{
int y=1,m = 1,d,rest = n,lp;
while(1)
{ if(leap(y))
if(rest<= 366) break;
else rest - = 366;
else //非闰年
if(rest = 365 ) break;
else rest-=365;
y++;
}
y--;
Ip = leap(y);
while(1)
{
if(lp) //闰年
if(rest>day_tab[1] [m - 1]) rest -= day_tab[1] [m-1];
else break;
else //非闰
if(rest>day_tab[0] [m -1]) rest -= day_tab[0] [m-1];
else break;
m++;
}
d = rest;
return Date(y;m,d);
}
void main()
{
Date now(2003,10,1) ,then(2005,6,5);
cout<<" now:";now. disp();
cout<<" then:";then. disp();
cout<<"相差天数:"<<(then-now)<<endl;
Date dl = now + 1000,d2 = now - 1000;
cout<<"now + 1000:" ;d1. disp();
cout<<" now - 1000:" :d2. disp();
}
填空题[函数2.1说明] 求任意两个正整数的最大公约数的欧几里德算法。用辗转相除法求正整数m和n的最大公约数,并返回该公约数。[函数2.1] void func1(int m, int n) r=m% n;while(r<>0) (1) ; n=r; (2) ; return n; [函数2.2说明] 判断101~200之间有多少个素数,并输出所有素数。用一个数分别去除2到sqrt (这个数),如果能被整除,则表明此数不是素数,反之是素数。[函数2.2]void func2 ( ) int m, i, k, h=0,leap=1; printf ( "/n" ); for ( m=101;m<=200;m++ ) (3) ; for (i=2;i<=k; i++ ) if( (4) ) leap=0;break; if ( leap ) printf ( "%-4d",m ); (5) ; if ( h%10==0 ) printf ( "/n" ); leap=1; printf ( "/n The total is %d", h );
填空题【说明】]开发一个五笔编码查询系统,界面如图所示。五笔编码存放在word.txt文件中,它的格式是:<汉字>,<编码>。程序的任务需要以<汉字>为关键字去查询它所对应的编码。根据前面的设计,用两个ComboBox来完成。m_cmbZi存放单字,m_cmbWord存放<汉字>,<编码>序列。当用户输入汉字的时候,在m_cmbZi中查找到相应的序号,然后再对应到mcmbWord中,这样就完成了查找动作。把查找出来的序列显示出来,之后再把这个序列送到列表框中,在列表框中保存着最近几次查找的结果,以方便浏览。【VisualC++代码】//在m_cmbWord中查找位置、显示并加主到列表框voidCWMPropDlg::OnSelchangeCombozi()intnSel=m_cmbZi.GetCurSel();//获得输入汉字的序号if(-1!=nSel)(1);//查找mcmbWord中五笔编码m_cmbWord.GetWindowText(m_strLastHit);(2);//显示到标签上CRectrect;(3);intnCount=rect.Height()/m_listHitHist.GetltemHeight(0);if((4))m_listHitHist.DeleteString(0);m_listHitHist.InsertString(-1,m_strLastHit);m_cmbZi.SetEditSel(0,-1);(5);
填空题【说明】本应用程序是一个小写数字转换成大写格式的转换器,其运行窗口如图5所示。窗口中有两个标签(lblNumber和lblChinese)、两个文本框(txtNnmber和txtChinese)和一个命令按钮(cmdChange)。当单击按钮cmdChange时,将文本框txtNumber中数字转换成中文大写,并显示在文本框txtChinese中。例如,在文本框txtNumber输入0076845,单击cmdChange按钮,将在文本框txtChinese中显示“柒万陆仟捌佰肆拾伍”。程序中调用的函数CChinse(StrdigitAsString)的功能是先删除字符串的所有前导0,再调用函数Change(StrdigitAsString),将数字字符串Strdigit转换成中文大写格式。为了防止用户在文本框txtNumber输入非数字形式的字符,程序将以空字符代替所输入的非数字有形字符。【程序】PrivateFunctionTransfer(StrdigitAsString)AsString′完成小写向大写的转换,这段代码省略EndFunctionPrivateFunctionCChinese(StrdigitAsString)AsStringDimintLen,intCounterAsIntegerintLen=Len(Strdigit)IfintLen>0Thenintcounter=0Do'过滤数字串Strdigit的前导0intcounter=intcounter+1LoopUntilMid(Strdigit,(1))intLen=(2)Strdigit=(3)(Strdigit,intLen)CChinese=Transfer(Strdigit)EndIfEndFunctionPrivateSubcmdChange_Click()txtChinese.Text=CChinese((4))'调用Cchinese函数EndSubPrivateSubtxtNumber_KeyPress(KeyAsciiAsInteger)SelectCaseKeyAscii'根据输入字符的ASCII码的值进行判断CaseKeyAscii<32'保留输入的控制字符Case48To57'保留输入的数字字符CaseElse'其他情况以空字符代替(5)=0EndSelectEndSub
填空题[说明]字符组合:程序界面如图示,在文本框Text1中输入若干个任意字符,单击“颠倒”按钮,将这些字符按相反的顺序显示在标签Lable1中,并在标签Labs12处显示字符的个数。例如:输入abcabc2,显示为2cbabca,组成字符数为4。[VisualBasic代码]PrivateSubcmdshow_Click(DimnAsIntegerDimIAsIntegerDimstr1AsStringN=Len(Text1.Text)ForI=nToStep-1Str1={{U}}(1){{/U}}NextILabe11.Caption=Str1EndSubPrivateSubcmdcount_Click()DimnAsIntegerDiminAsIntegerDimiAsIntegerDimjAsIntegerDimflagAsIntegerDimstr1AsIntegerDimstr2AsIntegerN={{U}}(2){{/U}}Str1=Mid$(Text1.Text,i.1ForI=2TonStr2=mid$(Text1,Text,I,1M=Len(str1){{U}}(3){{/U}}Forj=1TomIf{{U}}(4){{/U}}Thenflag=1;ExitForNextjIfflag<>1thenstr1=str1&str2NextILable2.Caption={{U}}(5){{/U}}EndSub
填空题[说明] 已知一棵二叉树用二叉链表存储,t指向根结点,p指向树中任一结点。下列算法为输出从t到p之间路径上的结点。 [函数] #define MaxSize 1000 typedef struct node TelemType data; struct node *lchild,*rchild; BiNode, *BiTree; void Path(BiTree t, BiNode *p) BiTree *stack EMaxsize], *stack1 [maxsize], *q; int tag[Maxsizel, top=0, top1; q=t; /*通过前序遍历发现P*/ do while (q!=NULL&&q! =p) /*扫描左孩子,且相应的结点不为p*/ (1) ; stack [top] =q; tag [top] =0; (2) ; if (top>0) if (stack [top]==P) break; /*找到p,栈底到栈顶为t到p*/ if(tag[top]==1) top--; else q=stack[top]; q=q->rchild; tag [top] =1; (3) ; top--; top1=0; while(top>0) q=stack [top]; /*反向打印准备*/ top1++; (4) ; top--; while( (5) ) /*打印栈的内容*/ q=stack1[top1]; printf (q->data); top1--;
填空题1说明】
【函数2.1说明】
函数stremp()是比较两个字符串s和t的大小。若s<t函数返回负数;若s=t函数返回0;若s>t,函数返回正数。
【函数2.1】
int strcmp(char * s,char * t)
{ while(* st++;
}
return{{U}} (2) {{/U}};
}
【程序2.2说明】
在n行n列的矩阵中,每行都有最大的数,本程序求这n个最大数中的最小一个。
【程序2.2】
#include<stdio.h>
#define N 100
int a[N] [N];
void main()
{ int row ,col,max,min,n;
/* 输入合法n(n<100),和输入n×n个整数到数组a的代码略*/
for (row =0;row<n;row++) {
for(max=a[row] [0],col=1;col<n;col++)
if({{U}} (3) {{/U}})max=a[row][col];
if({{U}} (4) {{/U}})min=max;
else if({{U}} (5) {{/U}})min=max;
}
printf("The min of max numbers is %d/n",min);
}
填空题【说明】 下面是一个Applet程序,程序的功能是在显示面板上输出字符串。当html页面被其他窗口遮挡后再次显示时,请给出输出结果。 import java.awt.*; import java. (1) . *; public class MyApplet (2) Applet public void (3) (Graphics g) g.drawString(tip,20,40); tip ="I am Java Applet"; public void init() tip ="welcome"; private (4) tip; <html> <head> <title> A Simple Applet </title> </head> <body> <applet code="MyApplet.class" width=800 height=400> </applet> </body> </html> 网页输出 (5)
填空题[说明]
用输入/输出写一个程序,让用户输入一些姓名和电话号码。每一个姓名和号码将加在文件里。用户通过点“Done”按钮来告诉系统整个列表已输入完毕。 如果用户输入完整的列表,程序将创建一个输出文件并显示或打印出来。格式如:555,1212,Tom 123-456-7890,Peggy L.234-5678,Marc 234-5678,Ron 876-4321,Beth
byte [] name=new byte [lineLength];
int i;
try
{
fos= new FileOutputStream ("phone. numbers");
}
catch (FileNotFoundException e)
while (true)
{
{{U}}(1) {{/U}}("Enter a name (enter 'done' to quit)");
{{U}}(2) {{/U}} (name);
if ("done". equalsIgnoteCase (new String (name,0,0,4)))
{
break;
}
System.err.println ("Enter the phone number");
readLine (phone);
for (int i=0;phone [i]!=0;i++)
{
{{U}}(3) {{/U}};
}
fos.write (',');
for (int i=0{{U}} (4) {{/U}};i++)
{
fos. write (name [i]);
}
fos. write ('/n');
}
fos. close();
}
private static void readLine (byte line []) throws IOException
{
int i=0,b=0;
while ((i<{{U}} (5) {{/U}})
}
line[i]= ( byte ) 0;
}
}
填空题[说明]本流程图采用“双向冒泡法”实现对数组a[n]的排序。双向冒泡法就是在逐步缩小的数组内,分别从数组的两端开始向内搜索,同时将大数往上浮,小数往下沉,每次交换一组数。flag是一个标志,发生过交换就置为1,当这个循环过程都不再发生交换时,则数组排序完成。注:流程中循环开始的说明按照“循环变量:循环初值,循环终值,增量”格式描述;定义SWAP[a,b]为将a和b两数交换。[流程图][问题]将流程图的(1)~(5)处补充完整。
填空题阅读以下说明和C++代码,将解答写入对应栏内。
[说明]
类Stock的定义中有三处错误,分别在代码的第04、06、10行。请补齐下述代码中的空缺(1)~(3),修改错误并给出修改后该行的完整代码,最后完善程序运行后的输出结果。
[C++程序]
01 #include<iostream.h>
02 using namespace std;
03 class Stock{
04 protected:
05 Stock(){shares=0; share_val=0.0;Output();}
06 Stock(int n=0,double pr=3.5): {{U}}(1) {{/U}} {//初始化shares值为n
07 share_val=pr;
08 Output();
09 };
10 void~Stock(){};
11 void Output()(cout<<shares<<';'<<share val<<endl;}
12 public:
13 //成员函数
14 private:
15 //成员变量
16 int shares;
17 double share_val;
18 };
19
20 void main(){ //构造三个Stock对象a,b,C
21 Stock a(1);
22 Stock b;
23 Stock C;Stock();
24 //其他代码省略,且代码无输出
25 }
程序运行后的输出结果为:
1:3.5
{{U}} (2) {{/U}}
{{U}} (3) {{/U}}
填空题[说明]
设计一程序,输入10个整数到一个数组中,调整这10个数在数组中的位置,使得其中最小的一个数成为数组的首元素,最大的一个数成为数组的末元素。
[C++程序]
#include <iostream.h>
#define SIZE 10
void main ( )
{
int data [SIZE];
int m;
cout<<"请输入"<<SIZE<<"个整数:";
for ( m=0;m<SIZE; m++ ){{U}} (1) {{/U}};
int j=0,k=0;
for ( int i=1;i<SIZE; i++ )
if ({{U}} (2) {{/U}}) j=i;
else if ( data[i]<data[k] ){{U}} (3) {{/U}};
if (j>0 ) {
int d=data[0];
{{U}} (4) {{/U}};
data[k]=d;
}
if ( k<SIZE-1 )
{
int d=data [SIZE- 1 ];
data[SIZE- 1 ]=data[j];
{{U}} (5) {{/U}};
}
cout<<end1<<" 排序后: ";
for ( m=0;m<SIZE; m++ ) cout<<data[m]<<" " ;
}
填空题阅读以下说明和Java代码,回答问题。 [说明] 已知类SubClass的getSum方法返回其父类成员i与类SubClass成员j的和;类SuperClass中的getSum为抽象函数,程序中的第14行有错误,请修改该错误并给出修改后的完整结果,然后完善程序中的空缺,当程序运行到第22行且尚未执行第22行语句时成员变量i的值,最后给出程序运行后的输出结果。 [Java代码] 行号 代码 01 public class UainJava 02 public static void main(String[]args) 03 SuperClass s=new SubClass(); 04 System.out.printin(s.getValue()); 05 System.out.printIn(s.getSum()): 06 07 08 abstract class SuperClass 09 private int i; 10 public SuperClass()i=5; 11 public int getValue() 12 return i: 13 14 public final abstract int getSum(): 15 16 class SubClass extends SuperClass 17 int j; 18 public SubClass() 19 this(-3); 20 21 public SubClass(int j) 22 (1) .j=j; 23 24 publiC int getValue()(return j; 25 public int getSum() 26 return (2) .getValue()+j; 27 28
填空题【说明】
下面的程序功能的功能是以行为单位对字符串按下面的条件进行排序。排序条件为:从字符串中间一分为二,右边部分按字符的ASCII值降序排序,排序后左边部分与右边部分进行交换。如果原字符串长度为奇数,则最中间的字符不参加排序,字符仍放在原位置上
例如:位置:0 1 2 3 4 5 6 7
源字符串:h g f e a b c d
则处理后字符串:d c b a h g f e
函数ReadDat()实现从文件in.dat中读取数据(每行字符串长度均小于80),函数WriteDat()把结果dat输出到文件out.dat中。
#include<stdio.h>
#include<siring.h>
#include<conio.h>
char dat[20] [80];
void jsSort()
{
int i,j,k,strl;
char ch;
for(i=0;i<20;i++)
strl=strlen(date[i]);
for(j={{U}} (1) {{/U}};j<strl;j++) /*对后半部分的字符串进行排序*/
for(k=j+1;k<strl;k++)
if({{U}} (2) {{/U}})
{
ch=dat[i] [j];
dot[i] [j]=dat[i] [k];
dat[i] [k]=ch;
}
for(j=0;{{U}} (3) {{/U}}j++) /*前后两部分进行交换*/
{
ch = date [i] [j];
dote [i] [j] = date [i] [(strl+1)/2+j];
dat [i] [(strl+1)/2+j]=ch;
}
}
}
void main()
{
reodDat();
isSort();
writeDot();
}
readDat ()
{
FILE * in;
int i=0;
char * p;
{{U}} (4) {{/U}};
while(i<20
if(p) *p=0;
i++;
}
fclose (in);
}
writeDat()
{
FILE * out;
int i;
clrscr ();
out = fopen("out. dat" ,"W");
for(i=0;i<20;i++)
{
printf("%s/n",dar[i]);
printf ({{U}} (5) {{/U}});
}
fclose (out);
}
填空题[函数2.1说明] 编制一个字符替换函数rep(char *s,char *s1,char *s2),实现将已知字符串s中所有属于字符串s1中的字符都用字符串s2中的对应字符代替。 [函数2,1] #include <stdio.h> #define MAX 50 rep(char *s,char *s1,char *s2) char *p; for(; *s; s++) for (p=s1; *p&& (1) ; p++); /*检查当前字符是否在字符串s1中出现*/ if(*p) (2) ; /*当前字符在字符串s1中出现,用字符串s2中的对应字符代替s中的字符*/ [函数2.2说明] 函数Insert_Sort(int n)是一个直接插入排序的程序。其基本思想是,假设待排序的记录存放在数组R[1…n]中。初始时,R[1]自成一个有序区,无序区为R[2…n]。从i=2起直至i=n为止,依次将R[i]插入当前的有序区R[1…i-1]中,生成含n个记录的有序区。 [函数2.2] #define MAX 255 int R[MAX]; void Insert_Sort(int n) int i,j ; for(i=2; i<=n; i++) if( (3) ) R[0]=R[i]; j=i-1; /*R[0]是哨兵,且是R[i]的副本*/ do /*从右向左在有序区R[1…i-1]中查找R[i]的插入位置*/ (4) ; /*将关键字大于R[i]的记录后移*/ j--; while( (5) ); /*当R[i]≥R[j]时终止*/ R[j+1]=R[0]j /*R[i]插入到正确的位置上*/
填空题阅读以下说明和流程图,回答问题将解答填入对应栏。[说明]下面的流程图,用来完成计算一组数组中的中值,其方法是:将数组中的一个值与其他值比较,并计算大于等于被比较数的数值的个数,以及小于等于被比较数的数值的个数,如果两数都大于n/2,则已经找到了中值,否则继续之前的步骤。注:流程中循环开始的说明按照“循环变量:循环初值,循环终值,增量”格式描述;[问题]将流程图的(1)~(5)处补充完整。
填空题【流程图说明】下面的流程(如图1所示)用N-S盒图形式描述了在一棵二叉树排序中查找元素的过程,节点有3个成员:data,left和right。其查找的方法是:首先与树的根节点的元素值进行比较:若相等则找到,返回此结点的地址;若要查找的元素小于根节点的元素值,则指针指向此结点的左子树,继续查找;若要查找的元素大于根节点的元素值,则指针指向此结点的右子树,继续查找。直到指针为空,表示此树中不存在所要查找的元素。【算法说明】【流程图】将上题的排序二叉树中查找元素的过程用递归的方法实现。其中NODE是自定义类型:typedefstructnode{intdata;structnode*left;structnode*right;}NODE;【算法】NODE*SearchSortTree(NODE*tree,inte){if(tree!=NULL){if(tree->data<e){{U}}(4) {{/U}};//小于查找左子树elseif(tree->data<e){{U}}(5){{/U}}; //大于查找左子树elsereturntree;}returntree;}
填空题【说明】
下面的程序构造一棵以二叉链表为存储结构的二叉树算法。
【函数】
BTCHINALR *createbt ( BTCHINALR *bt )
{
BTCHINALR *q;
struct node1 *s [30];
int j,i;
char x;
printf ( "i,x =" ); scanf ( "%d,%c",
while (i!=0 //生成一个结点
{{U}} (1) {{/U}};
q->1child = NULL;
q->rchild = NULL;
{{U}}(2) {{/U}};
if({{U}} (3) {{/U}};)
{j=i/2 //j为i的双亲结点
if(i%2==0
{{U}}(4) {{/U}} //i为j的左孩子
else
{{U}} (5) {{/U}} //i为j的右孩子
}
printf ( "i,x =" ); scanf ( "%d,%c", }
return s[1]
}
