[简答题]
阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。
【说明】以下程序实现了二叉树的结点删除算法,若树中存在要删除的结点,则删除它,否则返回。 FindNode
( )函数能够在二叉树中找到给定值的结点,并返回其地址和父结点。
【C++程序】
template < class T >
void
BinSTree < T >: :Delete( const T& item)
{
TreeNode < T >
* DelNodePtr, * ParNodePtr, * RepNodePtr;
if(( DelNodePtr
= FindNode (item,ParNodePtr)) = = NULL)
(1)
if(DelNodePtr→right = = NULL)
//被删除结点只有一个子结点的情况
RepNodePtr = DelNodePtr→left;
else if( DelNodePtr→left = = NULL)
(2) ;
else
//
被删除结点有两个子结点的情况
{
TreeNode < T >*
PofRNodePtr = DelNodePtr;
RepNodePtr =
DelNodePtr→left;
while(RepNodePtr→right ! =
NULL)
{
//定位左子树的最右结点
PofRNodePtr =RepNodePtr;
RepNodePtr = RepNodePtr→right;
}
if(PofRNodePtr = = DelNodePtr) //左子树没有右子结点
(3) ;
else
//用左子顷的最右结点替换删除的结点
{
(4)
[简答题][说明]
以下程序实现了在applet里移动图形文件,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
import j ava. awt. *;
import j ava.awt.event.*;
import java.applet. Applet;
public class AppCIU extends Applet implements MouseMotionListener, MouseListener
{
Image IMG onClick=over(this) title=放大; // 声明 Image 类类型的变量 IMG onClick=over(this) title=放大
int x=70,y=60,posX=70,posY=60,dx,dy;
public void init ( )
{
IMG onClick=over(this) title=放大=getImage ( getCodeBase ( ) ,"mouse.gif" ); //载入影像
addMouseListener ( this );
addMouseMotionListener ( this );
}
public void mousePressed ( MouseEvent e )
{
dx=e.getX( )-posX; //取得按下之点与基准点X方向的距离
dy=e.getY( )-posY; //取得按下之点与基准点Y方向的距离
}
public void mouseDragged ( MouseEvent e )
{
(1)
(2)
if ( dx>0 && dx<120 && dy>0 && dy<60 ) //如果指针落在图形上方
{
Graphics g=getGraphics ( );
(3)
}
}
public void paint ( Graphics g )
{
(4)
(5
[多项选择][说明]
以下程序实现了在applet里移动图形文件,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
import j ava. awt. *;
import j ava.awt.event.*;
import java.applet. Applet;
public class AppCIU extends Applet implements MouseMotionListener, MouseListener
Image img; // 声明 Image 类类型的变量 img
int x=70,y=60,posX=70,posY=60,dx,dy;
public void init ( )
img=getImage ( getCodeBase ( ) ,"mouse.gif" ); //载入影像
addMouseListener ( this );
addMouseMotionListener ( this );
public void mousePressed ( MouseEvent e )
dx=e.getX( )-posX; //取得按下之点与基准点X方向的距离
dy=e.getY( )-posY; //取得按下之点与基准点Y方向的距离
public void mouseDragged ( MouseEvent e )
(1)
(2)
if ( dx>0 && dx<120 && dy>0 && dy<60 ) //如果指针落在图形上方
Graphics g=getGraphics ( );
(3)
public void paint ( Graphics g )
(4)
[简答题][说明]
以下程序实现了利用鼠标任意移动圆形的位置,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
import java.awt.*;
import java.awt.event.*;
public class CIUSAMPLE extends Frame implements MouseMotionListener, MouseListener
static CIUSAMPLE frm=new CIUSAMPLE ( );
int x=70,y=60,posX=70,posY=60,dx,dy;
public static void main (String args[])
frm.setTitle ("Dragging a circle");
frm.setSize (200,150);
(1)
ffm.addMouseMotionListener (frm);
frm.setVisible (true);
public void mousePressed (MouseEvent e)
(2)
dy=e.getY ( ) -posY;
public void mouseDragged (MouseEvent e)
(3)
y=e.getY ( ) -dy;
if(dx>0&&dx<50&&dy>0&&dy<50) //如果指针落在正方形区域内
Graphicsg=getGraphics ( );
(4)
public void paint (Graphics g)
g.setColor (Color.pink); //设置绘图颜色为粉红
g.fillOval(x,y,50
[填空题]【说明】
以下程序实现了利用鼠标任意移动该圆形的位置,仔细阅读代码和相关注释,将程序补充完整。
【代码6】
import java.awt.*;
import java.awt.event.*;
public class CIUSAMPLE extends Frame implements MouseMotionListener, MouseListener
{
static CIUSAMPLE frm=new CIUSAMPLE ( );
int x=70,y=60,posX=70,posY=60,dx,dy;
public static void main ( String args[])
{
frm.setTitle ("Dragging a circle");
frm.setSize ( 200,150 );
(1)
frm.addMouseMotionListener ( frm );
frm. setVisible ( true );
}
public void mousePressed ( MouseEvent e )
{
(2)
dy=e.getY ( ) -posY;
}
public void mouseDragged ( MouseEvent e )
{
(3)
y=e.getY ( ) -dy;
if ( dx>0 && dx<50 && dy>0 && dy<50 ) //如果指我落在正方形区域内
{
Graphics g=getGraphics ( );
(4)
}
}
public void paint ( Graphics g )
{
g.setColor ( Color. pink ); // 设置绘图颜色为粉红
g.fillOval ( x,y,50,50 ); //以基准点为图形的左上角绘出圆形
(5)
pos
[多项选择]【说明】
以下程序实现数据的排序,将n个整数分别按照升序和降序进行排序,类SortInt_1实现升序排序,类SortInt_2实现降序排序。
【Java代码】
class SortInt_1
int i,i,k,temp;
void SortInt(int a1,int a2[])//升序排序
for(i=0;i<a1-1;i++)
k=i;
for(j=i+1;j<a1;j++)
if( (1) ) k=j;
if(k !=i)
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
class SortInt_2 (2)
int i,j,k,temp;
void SortInt(int a1, int a2[])//降序排序
for(i=0; i<a1-1;i++)
k=i;
for(j=i+1;j<a1;j++)
if( (3) )k=j;
if(k !=i)
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
public class test
public static void main(String args[])
int a[]=10,55,100,35,87,90,100,16;
SortInt_1 NewInt= (4) ;
NewInt.SortInt(a.lenvh,a);//调用SortInt_1类的方法
System.out.prin
[简答题][说明]
以下JAVA程序实现了在接口interface iShape2D的定义和应用,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
interface iShape2D //定义接口
{
(1)
(2)
}
(3) //实现CRectangle类
{
int width, height;
(4) CRectangle (int w,int h) {
width=w;
height=h;
}
public void area ( ){ //定义area( )的处理方式
System. out.println ("area="+width*height);
}
}
(5) //实现CCircle类
{
double radius;
(6) CCircle (double r) {
radius=r;
}
public void area ( ) { //定义area( )的处理方式
System.out.println ("area="+pi*radius*radius);
}
}
[代码6-2]
public class app10_4
{
public static void main(String args[])
{
CRectangle rect=new CRectangle (5,10);
rect.area ( ); //调用CRectangle类里的area ( ) method
CCircle cir=new CCircle (2.0);
cir.area ( ); //调用CCircl类里的area ( ) method
}
}
[简答题][说明]
以下JAVA程序实现了在接口interface iShape2D的定义和应用,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
interface iShape2D //定义接口
(1)
(2)
(3) //实现CRectangle类
int width, height;
(4) CRectangle (int w,int h)
width=w;
height=h;
public void area ( ) //定义area( )的处理方式
System. out.println ("area="+width*height);
(5) //实现CCircle类
double radius;
(6) CCircle (double r)
radius=r;
public void area ( ) //定义area( )的处理方式
System.out.println ("area="+pi*radius*radius);
[代码6-2]
public class app10_4
public static void main(String args[])
CRectangle rect=new CRectangle (5,10);
rect.area ( ); //调用CRectangle类里的area ( ) method
CCircle cir=new CCircle (2.0);
cir.area ( ); //调用CCircl类里的area ( ) method
[简答题]【说明】
Stack类是java. ntil包中专门用来实现栈的工具类。以下Java程序是一个不使用库函数而实现字符串反转的程序。例如,输入:123456,则输出:654321:输入:asdfeg,则输出:gefdsa。
【Java程序】
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class UseStack extends Applet implements ActionListener
{ Stack MyStack;
Label prompt=new Label("输入要反转字符:");
Button pushBtn=new Button("压栈");
Button popBtn=new Button("弹栈"); //反转
TextField input=new TextField(5);
int[] DrawStack =new int[10]; //记录堆栈中数据
int[] PoppedOut=new int[20]; //记录被弹出的数据
int StackCnt=0; //记录模拟堆栈的数组中的数据个数
String msg=" ";
public void init( )
{ MyStack=new Stack( );
add(prompt);
add(input);
add(pushBtn);
add(popBtn);
pushBtn.addActionListener(this);
(1)
}
public void paint(Graphics g)
{ for (int i=10; i<StackCnt; i++) //模拟显示堆栈内部的数据排列情况
{ g.drawRect(50,200-i*20,80,20);
g.drawString(Integer.toString(DrawStack[i]),80,215-i*20);
}
for
[简答题]【程序说明】
定义一个多边形结构:struct polygon实现以下内容:(1)建立该结构的链表:create函数是创建链表,每输入一个结点的数据,就把该结点加入到链表当中,它返回创建的链表的头指针。(2)显示链表的各个结点数据:结点数据包括:多边形顶点数、各顶点的纵横坐标、当多边形顶点数为0时,链表创建结束。(3)编写一个函数disp,删除链表中的所有结点。需要注意的是:要先释放结点数据内存,再删除结点,如果在释放结点数据内存单元之前删除结点,则无法找到结点数据内存单元的地址,也就无法释放数据的内存单元。
【程序】
#include "iomanip.h"
struct polygon
{
int n;
int* x;
int *y;
polygon * next;
};
void Push(polygon * & head,int n)
{
polygon * newNOde=newpolygon;
newNOde=newpo,Ygon;
newNOde->next= (1) ;
newNOde->x=new int [n];
newNOde->y=new int[n];
newNOde->n= (2) ;
for(int i=0;i<= (3) ;i++){
cout<<“请输入多边形各顶点x、y坐标,坐标值之间用空格分隔:”;
cin>>newNOde->x[i]>>newNOde->y[i];
}
(4) =head; //在head前不需要额外的。
head=newNOde;
}
polygon * create( )
{
polygon * head=NULL;
polygon * tail;
int n;
cout<<“请输入多边形顶点的个数(顶点个数为0时结束):”;
cin>>n;
if(n==0)return (5) ;
Push(head, (6) ;
tail=head;
cout<<“请输入多边形顶点的个数(顶点个数为0时结束):”;
cin>>n;
[简答题]【程序说明】
定义一个多边形结构:struct polygon实现以下内容:(1)建立该结构的链表:create函数是创建链表,每输入一个结点的数据,就把该结点加入到链表当中,它返回创建的链表的头指针。(2)显示链表的各个结点数据:结点数据包括:多边形顶点数、各顶点的纵横坐标、当多边形顶点数为0时,链表创建结束。(3)编写一个函数disp,删除链表中的所有结点。需要注意的是:要先释放结点数据内存,再删除结点,如果在释放结点数据内存单元之前删除结点,则无法找到结点数据内存单元的地址,也就无法释放数据的内存单元。
【程序】
#include "iostxeam. h"
#include "iomanip. h"
stmct polygon
int n;
int *x;
int *y;
polygon *next;
;
void Push(polygon*& head, int n)
polygon* newNode = new polygon;
newNode = new polygon;
newNode->next= (1) ;
newNode->x = new int[n];
newNode->y = new int[n];
newNode->n= (2) ;
for(int i=0; i<= (3) ; i++)
cout<<"请输入多边形各顶点x、y坐标, 坐标值之间用空格分隔: ";
cin>>newNode->x[i]>>newNode->y[i];
(4) = head; //在head前不需要额外的*
head = newNode;
polygon *create( )
polygon* head = NULL;
polygon* tail;
int n;