数据结构与程序设计C++描述(Kruse著)高等教育出版社_课后答案.

  • 格式:doc
  • 大小:7.53 MB
  • 文档页数:800

下载文档原格式

  / 255
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Programming Principles 1

1.2 THE GAME OF LIFE

Exercises 1.2

Determine by hand calculation what will happen to each of the configurations shown in Figure 1.1 over

the course of five generations. [Suggestion: Set up the Life configuration on a checkerboard. Use one color of checkers for living cells in the current generation and a second color to mark those that will be born or die in the next generation.]

Answer

(a)

Figure remains stable.

(b)

(c)

(d)

Figure is stable.

1

2 Chapter 1 _ Programming Principles

(e)

(f)

Figure repeats itself.

(g)

(h)

(i)

Figure repeats itself.

(j)

(k)

(l)

Figure repeats itself.

Section 1.3 _ Programming Style 3

1.3 PROGRAMMING STYLE

Exercises 1.3

E1. What classes would you define in implementing the following projects? What methods would your classes

possess?

(a) A program to store telephone numbers.

Answer The program could use classes called Phone_book and Person. The methods for a

Phone_book

object would include look_up_name, add_person, remove_person. The methods for a Person object would include Look_up_number. Additional methods to initialize and print objects of

both classes would also be useful.

(b) A program to play Monopoly.

Answer The program could use classes called Game_board, Property, Bank, Player, and Dice. In addition

to initialization and printing methods for all classes, the following methods would be useful. The class Game_board needs methods next_card and operate_jail. The class Property needs methods change_owner, look_up_owner, rent, build, mortgage, and unmortgage. The class Bank needs methods pay and collect. The class Player needs methods roll_dice, move_location, buy_property and pay_rent. The class Dice needs a method roll.

(c) A program to play tic-tac-toe.

Answer The program could use classes called Game_board and Square. The classes need initialization and printing methods. The class Game_board would also need methods make_move and

is_game_over. The class Square would need methods is_occupied, occupied_by, and occupy. (d) A program to model the build up of queues of cars waiting at a busy intersection with a traffic light. Answer The program could use classes Car, Traffic_light, and Queue. The classes would all need initialization

and printing methods. The class Traffic_light would need additional methods change_status

and status. The class Queue would need additional methods add_car and remove_car.

E2. Rewrite the following class definition, which is supposed to model a deck of playing cards, so that it

conforms to our principles of style.

class a { // a deck of cards

int X; thing Y1[52]; /* X is the location of the top card in the deck. Y1 lists the cards. */ public:

a( );

void Shuffle( ); // Shuffle randomly arranges the cards.

thing d( ); // deals the top card off the deck

}

;

Answer

class Card_deck {

Card deck[52];

int top_card;

public:

Card_deck( );

void Shuffle( );

Card deal( );