首页 > 题库 > 程序员

阅读下列说明和C++代码,填写代码中的空缺,将解答写入答题纸的对应栏内。【说明】对某简单文档编辑器中的文档(Document)打开、存储和关闭操作功能进行设计,得到图6-1所示的类图,其中,DocumentOperation定义对文档的操作,通过菜单项(MenuOptions)选择执行相应的打开、存储和关闭操作。下面的C++代码实现上述设计,请完善代码中的空(1)~(6)。【C++代码】#include <iostream>using namespace std;class Document {    public:      void open () {        cout<<"Document Opened"<<endl;      }void save () {        cout<<"Document saved"<<endl;      }      void close () {        save ( ) ;        cout<<"DOcument Closed"<<endl;    }} ;class ActionListener {  public :     (1)execute () = 0;};class ActionSave(2){  private:      Document * doc;  public:      ActionSave ((3)){        this->doc = doc;      }      void execute () {          (4);    }};//类ActionOpen、ActionClose 与Actionsave类似,代码略class MenuOptions {  private :    ActionListener* action ;  public:    MenuOptions (ActionListener* action){      this->action = action ;    }    void execute( ) {     (5);    }};class DocumentOperation {  public :    void test ( ) {      Document* doc = new Document () ;      ActionListener* action = new ActionSave (doc) ;      MenuOptions* menu =(6);      menu->execute ( ) ;    }};int main () {    DocumentOperation* operation = new DocumentOperation( );    operation->test ( ) ;}

查看试题

阅读以下说明和Java代码,填写代码中的空缺,将解答写入答题纸的对应栏内。【说明】对某简单文档编辑器中的文档(Document)打开、存储和关闭操作功能进行设计,得到图5-1所示的类图,其中,DocumentOperation定义对文档的操作,通过菜单项(MenuOptions )选择执行相应的打开、存储和关闭操作。下面的Java代码实现上述设计,请完善代码中的空(1)~ (6)。【Java代码】class Document {      public void open ( ) {          system. out.println ( "Document Opened" ) ;      }      public void save(){          System. out.println ( "Document Saved" ) ;      }      public void close (){          this.save () ;          System.out.println ( " Document Closed" ) ;      }}interface ActionListener {      (1)execute ( ) ;}class Actionsave(2){      private Document doc;      public ActionSave((3)){          this.doc = doc;      }      public void execute () {          (4);      }}//类ActionOpen、ActionClose 与 ActionSave类似,代码略class MenuOptions {      private ActionListener action;      public MenuOptions (ActionListener action) {           this.action = action;      }      void execute () {         (5);      }}public class DocumentOperation {      public void test () {          Document doc = new Document();          ActionListener action = new ActionSave (doc) ;          Menuoptions menu =(6);          menu.execute ( ) ;      }      public static void main (string[] args) {          DocumentOperation operation = new DocumentOperation ( ) ;operation.test ( ) ;}

查看试题

阅读以下说明和C代码,填补C代码中的空缺,将解答写在答题纸的对应栏内。【说明】栈是一种常用的数据结构,在栈中插入和删除元素都在称为“栈顶”的位置进行操作,因此满足“后进先出”的特点。在下面的C代码中,先实现了一个可以适时扩充空间的栈,然后在main 函数中利用栈来判断输入的字符序列是否是“以&为中心的对称字符串或空串”。处理思路是先将“&”之前的字符存入栈中,再将“&”之后输入的字符逐一与栈中的字符比较,直到所有字符对都匹配且输入结束,或者发现不匹配后终止,最后输出判断结果。【C代码】#include <stdio.h>#include <stdlib.h>#define INIT_SIZE 128typedef enum { false, true} _bool;t ypedef struct {           char *base;                    //栈空间的首地址           int top;                           //指示栈顶位置           int capacity;                  //栈的容量} STACK;_bool createstack (STACK *s ) {      s->base = (char * ) malloc (INIT_SIZE *sizeof(char) );      if ( !s->base) return false;      s->capacity =INIT_SIZE;      s->top = 0 ;      return true;}_bool empty (STACK *s){ return (0 == s->top) ; }_bool push (STACK *s,char elem){    if(s->top == s->capacity) {      int size = s->capacity + s->capacity / 2;      char *p = (char * ) realloc (s->base, size*sizeof(char) ) ;if ( !p) return false;      s->base = p;s->capacity = size;    }    s->base [s->top] = elem;    ++s->top ;    return true;}void pop (STACK *s){    if(s->top){      --s->top;      return;    }    printf( "Error : The stack is empty ! \n" ) ;char top (sTACK *s){    if (s->top) {      return s->base [ s->top - 1];    }    printf ( "Error: The stack is empty ! \n" ) ;    return 0 ;}int main( ) {    char ch;STACK st;    if ((1)!= true)    //创建一个空栈        exit(0) ;    while (true){        ch = getchar ( );        if (ch==' &'ll ch==' \n' ll ch==' \r' ll ch==EOF) break;        (2);      //字符入栈    }    if (ch==' &'){      do {         ch = getchar ( ) ;         if (ch==' \n' ll ch== ' \r'll ch==EOF ) break;         if ( !empty ( &st) ) {         if ((3)!= ch) break;    //与栈顶元素比较          (4);     //字符出栈        }        else break;     }while (1) ;  }  if ( (ch=='\n' ll ch=='\r' ll ch==EOF ) &&_(5)_)      printf ( "The input is a symmetric string. \n" ) ;  else      printf ( "The input is not a symmetric string. \n") ;  return 0 ;}

查看试题

阅读以下说明和C代码,填补C代码中的空缺,将解答写在答题纸的对应栏内。【说明】实现二路归并排序的一种方法是先递归地划分出子序列(当子序列仅包含0个或1个元素时得到初始的有序子序列),然后两两合并后得到更长的有序子序列,最后完成排序处理。函数mergesort(int a[],int n)对数组a的前n个元素进行排序。函数m _sort(int a[], int left, int right, int tp[])对序列进行划分,对以(left+right)/2为界的两个子序列排序后,调用merge()对两个有序子序列进行合并。函数merge(int a[],int left,int m,int right,int *tp)对数组下标范围[left, m-1]和[m,right-1]所表示的两个有序子序列进行合并。将非递减有序子序列a[left]~a[m-1]和 a[m]~a[right-1]合并为一个非递减有序序列的过程是:分别用i、j表示两个有序子序列的元素下标,比较a[i]和 a[j],若a[i]较小,则输出 a[i]且i递增,否则输出 a[j]且j递增,再次比较a[i]和 a[j]并重复以上过程,直到其中任一子序列到达结尾,然后输出另一序列的剩余元素。合并过程中所输出的元素暂存在空间足够大的辅助数组 tp[]中,最后再复制到数组a[]。【C代码】#include <stdio.h>#include <stdlib.h>//将非递减有序子序列a [left] ~a[m-1]和 a[m]~a[right-1]合并为有序序列a [ left] ~a [right-1]void merge(int a[ ] ,int left,int m,int right, int *tp){       int i,j,k =0;       for(i = left,j = m; i < m &&j < right; )           if (a[i] < a[j])  tp [k++] = a[i++] ;           else tp [ k++] = a[j++];      while(  (1)    )          //复制未结尾序列的剩余元素到辅助数组           tp [k++]= a [ i++];      while(    (2)    )       //复制未结尾序列的剩余元素到辅助数组           tp [k++] = a[j++];      for (k = 0,i = left; i < right; ++i,++k)                                   //合并所得序列的元素复制回数组a[ ]           a [ i] =        (3)          ;}void m_sort (int a[], int left, int right,int tp[]){      if( right-left<2 )   return ;      int mid = (left + right) / 2;          //设置待排序序列的划分点      m_sort(a, left, mid,tp) ;             //对划分出的第1个子序列进行归并排序      m_sort ( a,    (4)    , tp);            //对划分出的第2个子序列进行归并排序      merge (a,    (5)      , tp);           //合并已有序的第1、2个子序列void mergesort (int a[], int n) {      int *tp = (int * ) malloc (n*sizeof (int) );                                                     //申请归并过程中需要使用的辅助空间      if ( ! tp) return;      m_sort(a,0 , n,tp) ;                 //调用m_sort对a [ 0] ~a [n-1]进行归并排序      free (tp) ;}int main (){      int a [10]= { 15,3,26,8,4,1,9,7,3,11};      int i, n = 10;      for (i=0 ; i<n;i++)         printf ( "%d \t" , a [i]);      printf ( "\n") ;      mergesort (a,n) ;      printf ( "after sorting\n" ) ;      for (i=0 ; i<n ;i++)         printf ( "%d \t" , a [ i] );      printf ( "\n" ) ;      return 0;}

查看试题

暂未登录

成为学员

学员用户尊享特权

老师批改作业做题助教答疑 学员专用题库高频考点梳理

本模块为学员专用
学员专享优势
老师批改作业 做题助教答疑
学员专用题库 高频考点梳理
成为学员