软件水平考试(中级)软件设计师下午(应用技术)试题模拟试卷51

0
收藏   分享
  • 卷面总分:110分
  • 试卷类型:模拟考试
  • 测试费用:免费
  • 答案解析:是
  • 练习次数:266次
  • 作答时间:150分钟
试卷简介
试卷预览
1

阅读以下说明和Java代码,回答问题

[说明]

 对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图6-1显示了各个类间的关系。以下是JAVA语言实现,能够正确编译通过。

 [图6-1]

 [Java代码]

 //Iterator. java文件

 public interface Iterator {

 public abstract boolean hasNext();

 public abstract Object next();

 }

 //Aggregate. java文件

 public interface Aggregate {

 public abstract Iterator iterator();

 }

 //Book. java

 public class Book {

 //省略具体方法和属性

 }

 //BookshelfIterator. java文件

 public class Bookshelf工terator (1) Iterator{

 private BookShelf bookShelf;

 private int index;

 public BookshelfIterator(BookShelf bookShelf) {

 this. bookShelf = bookShelf;

 this. index = 0;

 }

 public boolean hasNext(){//判断是否还有下一个元素

 if(index< bookShelf. getLength()){

 return true;

 }else{

 return false;

 }

 }

 public Object next()f//取得下一个元素

 Book book = bookShelf. getBookAt(index);

 index++;

 return book;

 }

 }

 //BookShelf. java

 import java. util. Vector;

 public class BookShelf {

 private Vector books;

 public BookShelf(int initialsize){

 this. books = new Vector(initialsize);

 }

 public Book getBookAt(int index){

 return(Book)books.get(index);

 }

 public int getLength(){

 return books.size();

 }

 public Iterator iterator(){

 return new BookShelfIterator( (2) );

 }

 }

 //Main. java文件

 public class Main {

 public static void main(String args){

 BookShelf bookShelf = new BookShelf(4);

 //将书籍上架,省略代码

 Iterator it = bookShelf. (3) ;

 while( (4) ){//遍历书架,输出书名

 Book book = (Book)it. (5) ;

 System.out.printin(" "+book.getName());

 }

 }

 }

1

阅读下列函数说明和C++代码,回答问题

[说明]

 对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图5-1显示了各个类间的关系。以下是C++语言实现,能够正确编译通过。

 [图5-1]

[C++代码]

 template (1) >

 class Iterator{

 public:

 virtual bool hasNext() = 0;

  (2) Object* next() = 0;

 };

 class Book{

 //省略具体方法和属性

 };

 class BookShelf{

 private:

 vector books;

 public:

 BookShelf(){

 }

 Book* getBookAt(int index){

 return &booksindex;

 }

 int getLength(){

 return books. size();

 }

 };

 template

 class BookshelfIterator : public (3) {

 private:

 BookShelf * bookShelf;

 int index;

 public:

 BookshelfIterator(BookShelf *bookShelf){

 this->bookShelf = bookShelf;

 index = 0;

 }

 bool hasNext(){//判断是否还有下一个元素

 if(index< bookShelf->getLength()){

 return true;

 }else{

 return false;

 }

 }

 Objeot* next(){//取得下一个元素

 return bookShelf->getBookAt(index++);

 }

 };

 int main()

 {

 BookShelf bookShelf;

 //将书籍上架,省略代码

 Book *book;

 Iterator *it = new BookShelfIterator( (4) );

 while( (5) ){//遍历书架,输出书名

 book=(Book*)it->next();

 /*访问元素*/

 }

 return 0;

 }

1

阅读以下函数说明和C代码,回答问题

[说明]

 对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图7-1显示了各个类间的关系。以下是JAVA语言实现,能够正确编译通过。

 [图7-1]

[C代码]

 typedef bool(*fun1)();

 typedef (1) (*fun2)();

 const int BOOK_MAX = 10;//最大书本数

 struct Book{

 char name30;

 };

 struct BookShelf{//书架

 struct Book books[BOOK MAX];

 int index;//书架上最后一本书的下标加1,即下一本书的下标,如0表示有0本书

 };

 Struct Book* getBookAt(struct BookShelf *BS, int index)

 //从书架BS上取得下标为index的书

 //只有当下标大于等于0且不大于当前书架上的最后一本书对应的下标,才取书成功:

 //否则失败,返回NULL

 {

 if(index >= 0 && (2) ){

 return &BS->books[index];

 }

 return NULL;

 }

 bool appendBook(struct BookShelf *BS, struct Book book)

 {

 if(BS->index< BOOK_MAX){

 BS->books[BS->index++] = book;

 return true;

 }

 return false;

 }

 int getLength(struct BookShelf *bookShelf)

 {

 return bookShelf->index;

 }

 struct Iterator{//迭代器

 fun1 hasNext;//判断是否还有下一个元素

 fun2 next;//取得下一个元素

 };

 struct BookshelfIteratorf{//书架迭代器

 int index;

 struet BookShelf* bookShelf;

 }bookShelfIterator = {0, NULL};

 bool BShasNext()//判断是否还有下一本书

 {

 if(bookShelfIterator.index

   return true;

 }else{

 return false;

 }

 }

 struct Book* BSnext()//取得下一本书,并将index加1,以便下一次正确访问

 {

 return getBookAt(bookShelfIterator.bookShelf,

  (3) );

 }

 void main()

 {

 struct BookShelf bookShelf;

 bookShelf.index = 0;

 //将书籍上架,省略代码

 //将bookShelf与bookShelfIterator相关联

 bookShelfIterator.bookShelf = (4) ;

 struct Iterator iterator;

 iterator.hasNext = BShasNext;

 iterator.next = BSnext;

 struct Book* b;

 while( (5) ){//遍历书架,输出书名

 b=iterator.next();

 printf("%s\n", b->name);

 }

 }