Вы находитесь на странице: 1из 528

>

Paulo Franca

No experience required

SYBEX

-
--
-
2003

.
C++:
.



.
.
.
.
.
.

32.973.2-018.1
681.3.04
.
83 C++: . .: , 2003. 521 .: .
ISBN 5-314-00136-5
C++ ,
. C++: ,
C++.
,
, , -
, . ,
26- , .
.
, C++.
Sybex, 1997
, , 2003
, , , 2003
Published by arrangement with the original publisher, Sybex Inc., U.S.A.
Sybex Inc., .
ISBN 5-314-00136-5
ISBN 0252-112111- (.)

.
, , , .
, ,
, '

. 196105, -, . , . 67.
05784 07.09.01.
04.04.03. 70'/ |6 . . . . 42,9. . 4500 . 2728.
- 005-93, 2; 953005 - .
. . .
, .
197110, -, ., 15.


0.
0.

I.
1.
2.

22
23
82
83
94

3.

107

II.

117

4.
5.
6.
III.
7.
8.
9.

118
136
iso
170
171
195
212

IV.
10.

221
222

11.

248

12.

258

V.

266

13.

267

14.
15.

281

VI.
16.
17.
18.

VII.
19.
20.

328
329
350
366

381
382
407

21.

424

VIII.
22. franca.h

436
437

23.
24.
IX.
25. -

26.

458
484
491
492
509
516


0.
0.

22
23

C++

25


?
C++?
Turbo C++ Visual C++
Windows 3.1 Windows 95?
-?
?
?

25
25
26
26
27
27
27
27

28
28

33

34
34


Borland Turbo C++ 4.5
Borland C++ 4
Borland C++ 5
Microsoft Visual C++ 1,5
Microsoft Visual C++ 4
Microsoft Visual C++5

35
36

37
37
44
50
56
64
73

80

81

I.
1.

82
83

84

86

athlete

87
89

89

C++

90

91
92
92

93

93

2.

94

Clock
Clock
Clock
?

Box

95
95
96
97
,....98

99
100

Robot


100
101
102
103
103
104
104

106

3.



....

107
108
108
109
109

..

111

111
112
112
112
113
113

114

114
114
114
115
115
116

116

II.
4.

117
118

119

121

123

124
127
129

130

131

131

132

134

135

5.

136
-

137
139

141
142

143
143

10


ask{)
On


Cout

6.

144
145
145

146
146

149

150
152

153
157

157

159

162
165
166
167

168

169

III.
7.

while

170
171
173
173

177

do...while
for

177
178

181



yesnof)

181
182
184

186

Tracer

188

189

193

194

1_1

8.



195
196
197
199
200

201

201

201

203

206
206
206
207
207

210

211

9.


212
213
214
217

219

219
220

220

220

IV.

221

10.

222

if

223

226

227

228

if..else

229

230

230

12

231

231
233







break
continue

234
234
235
235
236
238
239
240

242
243

245

11.


12.



V.
13.


mafh.h

247

248
249
250
253

255
257

258
259
259
260
262

263
264
265

266
267
268
268
274
278
279

280

j_3

14.

281

282

282
283

290

290

293
293
293
294
294

296

296

297

297

299

15.

301
302
305

306

307

307

312

313

314
315


Stage

318
318
319
321

322
323
325

327

327

14

VI.
16.



,



Stage

328
329
331
332
335
338
339
340

341
342
343

345
346
348
348

348

349

17.






350
351
352
353
355
356
358
361

364

365

18.





satellite

366
367
367
368
369
372

374
375
377
378

380

VII.
19.


jj

381
382
383
385
390

392

392
395

396
397

398
401

403

404
405

406

20.
C++
char

407
408
408

410
410
413
416

417
418
419

421
423
423

21.

424

425

425

431
431

433
435

.
16

VIII.
22. franca.h
C++

/ C++
iostream.h


/



/


askfl, askwordsf) yesnof)

436
437
439
440

440
441
442
443
444

445
446
446
447
451
454
454

457

457

23.



textfile

-
-

458
459
460
468

471
471
473
473
473
474
475

476
476
477

479

479



payfile
,

479
480
481

]_7

483

483

24.

485

485
489

490

IX.
25. -

491
492

493

484

493

496

496
498

508

26.

509

C++

510

510
511
511
512
513
514
514
514

514
515

515

516


? , !
.
.
! ,
80 .
- (Ramamoorthy-Ram)
. (Daniel Lewis)
. , , .
(Brazilian Research Council, CNPq) - (Federal University of Rio de Janeiro, UFRJ). ,
(NCE-UFRJ)
.
(Adriano Cruz), . -
,
.
, Sybex,
(Kristine Plachy) : (Peter Kuhns),
(Ronn Jost), (Kim Wimpsett) (Dale Wright),
: (Amy Eoff), (Robin Kibby) (Andrew Benzie).
(Andrea Franca)
(Frederico Arentz). ,
, ,
.
(Eunhee Kim) . , .
,
. ,
.
(Santa Clara) ,
.
, , (Coffee Society in Cupertino),
. , .
.
, 24 1997 .


- ...
, C++ -
(Object Oriented Programming, OOP)
. C++ , .
- ,
, , ,
OOP. , , C++
C++ .
, , .
C++ - ?
, ,

. , , .
, .



. / , , . ,
.

Windows ,
. , , , , . . ,
,
. , , , . ,
.

20

, ,
, . ,
. IBM- , ( ), Microsoft Windows, , PC.
, . Borland Turbo C++, Borland C++
Microsoft Visual C++.
, . -
Windows. ,
Windows!
, , , .

?

. 10 , 0 IX. 0? , C++ . , 0 . - , , (Turbo C++, Borland C++,
Visual C++) .
, ,
0.

IIII ,
. .
.
,
.
. , , ,

._

._

._

__

._,..- , -- '

. .
,
. III. ( )
,
, ,
. .
IV . , .
V.
, ,
- . V
. , ,
, .
. VI.
VII . ( , . .)
.
, , /
. (), ,
. / Windows , /, , VIII.
IX .
, .
,
. ,
.
.
,
Web- Sybex http://www.sybex.com.
- (Paulo Bianchi Franca, Ph.D.),
: http://www.franca.com.

0 C++
. , C++
, , C++.
, . .
Web-,
, .
, .
, C++,
, .




Q
Q

,
, . , , , . , , ,
. , , .
. ,
, !
.
.
, , , , , . ., ,
, - !
! !
( ) , , .
: ! , , . , , ! , - , ,
.
. , !
0 , ,
. . , .


, . , , , , , , .
, , .

1/ , ,
.

__++

25

C++
. , .
, !
,
, .
, , !
. , ,
,
.


, ,
( ) . , , .
:
1. ,
C++.
2. ( .OB J).
3. .
2.
4. ,
.
( .). .
5. . - . , 1-5,


?
, , . , -

(X ^

26

, ,
. , .
, , . ,
, , .

C++?
. ( ) , . C++ ,
.
C++ ! , , .
, C++ ( ), ( ). - ,
C++! , , 1.

Turbo C++ Visual C++


Turbo C++ , Borland
International, C++. Borland Borland C++, ( ) .
Visual C++ , Microsoft2.
C++.
--_

^^^^^^^^^^1^^.^

C++ .

.

C++, . -*+: ( ).
.

Visual C++ . Visual C++ 6:


( ).

__

C++?

27

___
___
Web- . , .

:
Turbo C++ Windows 4,5 (Borland).
Borland C++ 4 5 Windows.
Microsoft Visual C++ 1.56, 4 5 Windows.

Windows 3.1 Windows 95?


.
Windows 3.1, Windows 95.
, ,
, .
, .

-?
! . .
- .
, -.

?
, ? ,
. ,
. .

?

. ,
. .

28

0.


. . , .
.
, , , ( ), .
. , ,
C++, ..
,
. ,
, . ,
, .

,
,

,
.

, ,
. ,
.
. , ,
.
, , !
,
.


, ,
- .
. .

29

, FRANCA
( ).
:
, : C:\FRANCA,
) . ,
C:\FRANCA.

-
(*) . . -. , *. ., *.* .


, MYDIR, . , , , .
, , . , MYDIR COMPUT, PHISICS,
PERSONAL.
. ,
, ;
, . 0.1 . , :. : .
, FRANCA TCWIN. FRANCA
OBJ FILES. ,
.
, . , OHBOY.CPP FRANCA,
TCWIN. ? .
- ( ). , , . ,
.
, . .

30

0.

. 0,1.
:
C:\FRANCA\OHBOY.CPP
C:\TCWIN\OHBOY.CPP


, PERSONAL. ,
MYDIR. ;
Turbo ++4.5,
. 0.2, Windows 95, . 0.3.

()

( :)

:

,

,

' ,

:
. 0.2. : (Turbo C++ 4.5)


:
( :)

,
. 0.3> : (Borland C++ 5)

mre
mmoniloi
L3 mpf
Cj mtdevstd
miotfice
mvc
3

,

. 0.4. MYDIR (Turbo C++ 4.5)

, MYDIR,
(, , ). . ? , , .
, . 0.4 . 0.5.

32

0.
,

Puc. 0.5. MYDIR (Borland C++ 5)


, , MYDIR.
, . 0.6, . 0.7.

. , MYDIR PERSONAL.


MYDIR ()

(MYDIR)
I (:)


( )



. 0.6. MYDIR (Turbo C++ 4.5)

(MYDIR)

33

( :\),

. 0.7. MYDIR (Borland C++ 5)


,
, , .
.
,
( );
. - , -
, .
.
, .




. ,
. ,
.

34

0.


(Borland Microsoft) . , , .


:
.
.
.



. FRANCA
:.
Windows (Windows Explorer) (File Manager) Windows 3.1.
Windows :,
(File) (New) (Folder),
FRANCA .


CPPNER.EXE FRANCA.


,
. CPPNER.EXE
, .
Windows, CPPNER.EXE
, C:\FRANCA sounds ().
. CCPNER.EXE ,
, .

35

Ha , FRANCA :. , ,
.
|^

,
,
, , .
, , .
,



. .
.

. , .

, :
.
, .
, .
, .
C:\FRANCA.
, , . , , . .
, : PAULO.RC,
FRAN * * * .OB J (, , ) ( C_SAL.CPP).

, .
, .

36

0.

, .

. , .
.
.

.
, . . .
C_SAL,CPP, , . 0.8.
I Paulo France's C+

Hi!

. 0.8. C^SAL.CPP

1 , , .


. ,
. - . -

37

, .
. , .
. PAULO.RC
. Windows . .OBJ . ,
(
.), *.
, , :
, ,
.


, .
, , , , , .
:
Turbo C++ 4.5 Windows.
Borland C++4 Windows.
Borland C++ 5 Windows.
Microsoft Visual C++ 1.56 Windows.
Microsoft Visual C++4 Windows.
Microsoft Visual C++ 5 Windows.

, Web- http://www.sybex.com
.

Borland Turbo C++ 4.5


Borland Turbo C++ 4.5,
.


Turbo C++ 4.5.
, . 0.9.

38

0,

. 0.9- Turbo C++ 4.5


.
Project Open Project ( Project > Open Project). Open
Project File, . 0.10.
FRANCA ( )


,
,





I
, :


.IDE
. 0.10. Turbo C++ 4.5

39

,
(C:\FRANCA). , File Name
, .
FRANCA45 . , (
, ).
s^-^^^^^^^_*^-^^^_^^^^-^-^^^^_

.
.

, .
:

PAULO,RC

FRANCA45.0BJ

(LSALCPP

(PAULO.RC FRANCA45.0BJ)
. (C_SAL.CPP) ,
. , ,
.

!
, , .
1. .
2. , .
3. .
4. , . , ?
5. .


..
C++, .
.
. , . ,
.

40

0.


. , Delete node.
)

,
, FRANCA.EXE,
.


,
(FRANCA45.IDE) Add node.
Add to Project List, . 0.11.
, .

,
,
. 0.11.


, . ,
( ) .. Debug >- Run.
C_SALCPP. , . 0.8.
, . Windows 3.1,

41

. Windows 95
.

!
C_SAL.CPP.
1. , .
2. .
, . 0.8?
3. .
4. .
C_SALCPP.
C_SOUND.CPP.
5. . , - .

6. .
7. C_SOUND.CPP C_SALCPP.
8. .


.

, File > New. ,
(. 0.12).

. 0.12.

42

0.

NONAME.CPP . File > Save As,


Save File As, . 0.13. ,
NONAME.CPP .
File Name , ,
, .

. 0.13.


, , File > Open. File
Open. , .
, . . (
). ,
.
,
, (, C_SALCPP).
, . ,
, ,
. 0.14. .
, , . ,
.
J

, .
, File > Save As
.

43


''-, Turbo C++ - tianca45
.-. jj

: _

or.'.-1

':'".'. -

:,';

!
C_SALCPP.
1. , .
2. , CJsALCPP
.. , . C_SALCPP.
3. C_SAL.CPP.
4. : Sal say (Hi!) ( (!*)); ! Hello! (
. Hello Delete , Hi).
5. C_SALNEW.CPP. ,
,
( C:\FRANCA). ,
, ? Hi Hello? -
Hi, C_SALNEW,CPP .
, .
6. C_SAL.CPP C^SALNEW.CPP.
7. . Hello?
8. C_SALNEW.CPP C_SALCPP .

44

Borland C++ 4
Borland C++ 4,
.


Borland C++ 4.
, . 0.15.

. 0,15. Borland C++ 4


.
Project > Open Project. Open Project
File, . 0.16.
,
(C:\FRANCA). , File Name
, .

45


Borland C++ 4 FRANCA40
,







.IDE ( )
. 0.16. Borland C++ 4

FRANCA40 . , (
, ).
,
, . 0.17.
:

PAULO.RC

FRANCA40.QBJ

C_SALCPP

(PAULO.RC FRANCA40.0BO)
. (C^SALCPP) ,
. , ,
.
"

!
, , .
1. .
2. , .
3.

46

0.

Q fianca40 [.objj
c_l I.cpp]
D paulnl.u:]

. 0.17. FRANCA40 Borland C++4

4. , . , ?
5. .


-.
C++, .
.
. , . ,
.


. , Delete node.
)

,
, FRANCA40.EXE,
.

47


,
(FRANCA40.EXE) Add node.
, .


, . ,
( ) .. Debug v Run.
C_SAL.CP , ,
. 0.8.
, . Windows 3.1,
. Windows 95
.

!

C_SALCPP:
1. , .
2. .
, . 0.8?
3.

4. .
C_SALCPP.
C_SOUND.CPP.
5. . , - .
6. .
7. C_SOUND.CPP C_SAL.CPP.
8. .

48

0.


, File > New. ,
(. 0.18).
, File > Save As, .

. 0.18. (NONAMEOO.CPP)


, , File > Open. File
Open. , .
, . . ( ). , .
,
, . , . , ,
, . 0.19.
.
, , . ,
.

49

. . 9.
J

, .
, File > Save As
.

!
C_SAL.CPP.
1. , .
2. , C_SAL.CPP
,. , . CJsAL.CPP.
3. C_SALCPP.
4. : Sal say (Hi!) ( (!)); Hi! HeLLo! (
. Hello Delete , Hi).
5. C_SALNEW.GPP. ,
,
( C:\FRANCA). ,
, ? Hi Hello? -
Hi, C_SALNEW.CPP .
, .

50

0.

6. C_SAL.CPP C_SALNEW.CPP.
7. . Hello?
8. C_SALNEW.CPP C_SAL.CPP .

Borland C++ 5
Borland C++ 5,
.


Borland C++ 4.
, . 0.20.

. 0.20. Borland C++ 5


.
Project >- Open Project. Open Project
File, . 0.21.
.
, (C:\FRANCA).
, File Name
, .
FRANCA50
Borland C++ 5. , ( , ), ,
. 0.22.

51


Borland C++ 5 FRANCA50

,

,

.IDE PRJ

. 0,21. Borland C++ 5

. 0.22, FRANCA50 Borland C++ 5

PAULO.RC

FRANCA50.0BJ

CJ5AL.CPP

(PAULO.RC FRANCA50.0B3)
. (C^SALCPP) ,

52

0.

. , ,
.
!
, , .
1.
2.
3.
4.

.
, .
.
, . , ?
5. .


-.
C++, .
.
. , . ,
.

. , Delete node.

,
, FRANCA50.EXE,
.


,
(FRANCA50.EXE) Add node.
, .
Add to Project List, . 0.23.
.


, . ,
( ) .. Debug > R u n .

53


Add to !'i,i|.-i 1 I isl

clock

C2ckcpj!
|fC2input
i?C2jmpbd1

ffC2squa[2
JSf CSasklor
|f C3doli

. 0,23.

C_SAL.CPP, ,
. 0.8.
, . Windows ,
. Windows 95
.

!
C_SAL.CPP.
1. , .
2. .
, . 0.8?
3. .
4. .
C^SAL.CPP.
C_SOUND.CPP.
5. . , - .
6. .
7. C_SOUND.CPP C_SAL.CPP.
8. .

54

0.


, Fi le > N ew. ,
(. 0.24).
, File >- Save As, .
(,
: \ FRANCA) , File name, . 0,25.

. 0.24.

_i___rrv,r-^=r

jga&jjnf / .j iii Franca

?C2change

P'c.somd
|F Cl clock
|ff C1 boll
^C1iobot2
4\

|f C2jmpbdy

IN . > .

|
! C2jmpick

Id'CSfll

?;

^fC25t:ope

|fC3saIe

,|

^j1 C3dotiffe

^C3stwe2 ;|

M|'ff!f;fffiJ
-^-3

|r narncOO

Sch-iS isj.^.i ]'- ++ source (",cpp;".c]

*..

IfCZnput

f:>e n- ;,

'^-^-^'
"*
"

...

|
,

. Save

, -"


,
. 0.25.


, , File > Open. File
Open. , .
, . - ( ). , .

55

,
, . , . , ,
, . 0.26.
.
, , . ,
.

, .
, File > Save As
.

Borland C++ - Iranca50

PIP! IT
t ? - -^

Puc. 0.26. C_SAL.CPP Borland C++ 5

!
C_SALCPP.
1. , .
2. , C_SALCPP
.. , . C_SAL.CPP.
3. C_SAL.CPP.
4. : Sal say (Hi!) ( (!)); Hi! Hello! (
. Hello Delete , Hi).

56

0.

5. C_SALNEW.CPP. ,
,
( C:\FRANCA). ,
, ? Hi Hello? -
Hi, C_SALNEW.CPP .
, .
6. C_SALCPP C_SALNEW.CPP.
7. . Helios-?
8. C_SALNEW.CPP C_SALCPP .

Microsoft Visual C++ 1.5


Microsoft: Visual C++ 1.5, .


Microsoft Visual C++ 1.5.
, . 0.27.

. 0,27. Microsoft, Visual


.
Projects Open Project, Open Project,
. 0.28.

57

,
(C:\FRANCA). , File name
.
FRANCAM1
Visual C++ 1.5. , ( , ).
,
Projects Edit. Edit, . 0.29).

,

,




File Name

,


, :
,

. 0.28. Open Project Visual C++ 1.5


, .

Edit-FRANCAM1.

XliSI
;

| ]cj al.cpp
c_i pund.cpp
cl i lock cpp
clmbotl.cpp
c1iobot2 cpp
2 change, cpp
c2clnout.cpp
c2clkcpy.cpp

',;j fianca
ffij debug

) JrJ

: *haip_v1 1

;!
c:\franca\c_sal.cpp
; cAFiancaUrancamt .def
' c:\francaMiancam1 .obj
ranca\paul .

:
,
. 0.29.

58

0.

PAULO.RC

FRANCAM1.0BJ

FRANCA.DEF

C_SAL.CPP

(PAULO.RC, FRANCAM1.0BJ FRANCAM1.DEF)


. (C_SAL.CPP) , . , , .

!
, , .
1. .
2. , .
3. .
4. , . , ?
5. .


..
C++, .
.
. , . ,
. ,
, .
Project^ Edit.


Project >- Edit.
, . . ,
. 0.30 C_SAL.CPP.

,
^
^

^
^
^
^
^
^
^

^
^
^
^
^
^
^
^
^
.
^
^

^
^
_
^
^
^
_
^

.
FRANCAM1.0BJ PAULO.RC .

59

c_*al.cpp
c_ound cpp
cl lock, cpp
cliobotl.cpp
cl iobot2.cpp
c2change.cpp
c2cnoul.cpp
c2cfccpf,cpp

Source (*.;*.;*.}

c:\franca\fiancaml. del
c:\franca\fiancam1 .obj
c:\lranca

Puc. 0.30. C_SALCPP


C_SALCPP Edit ,
. 0.31.

_ sound, cpp
1 clock.cpp
cliobotl.cpp
cl robot2.cpp
c2change.cpp
cZcinout.cpp
cpp

c:\tranca\ri ancaml .ob|


c: \f ranc a\pau lo. ic

Puc. 0.31. C_SALCPP



, .
Projects Edit. Edit,
. 0.31. . , , . , C_SOUND.CPP, , (
. 0.32), Add. , . 0.33.

60

_ 0.

c1 clock,
clioboH.cpp
diobot2cpp
c2change.cpp
c2cinoul.cpp
c2dkcpu.cpp

: \fianca\fiancam1. obj
: \franca\paulo ic

. 0.32. C_SOUND.CPP
Edit -FRANCAMI MAC

cl clock,
diobatl.cpp
c1iobot2.cpp
c2change.cpp
c2cinout.cpp
c2cbcpji.cpp

Source (-.c;".cppj'.ci)

cAfrancaVe round, cpp


: MrancaViancaml .del
c: VfrancaUiancaml .obj
: \ I anca\ paolo ic

. 0.33. C_SOUND,CPP


, . , ( ) ..
Visual C++ ( ), . ,
Project > Build, - Debug > Go.
C_SALCPP, , , . 0.8. . !

61

, . Windows 3.1,
. Windows 95
.
)


Debug > Go. Visual C++ ,
( , , ),
, Debug > Go.

!
C_SAL.CPP.
1. , .
2. .
, . 0.8?
3. .
4. .
C_SALCPP.
C_SOUND.CPP.
5. . , - .
6. .
7. C_SOUND.CPP C_SALCPP.
8. .



.

, File> New. ,
(. 0.34).
, File > Save As, .
, File Name, . 0.35.

62

0.

. 0.34.
,

-4.-..-.

:1!^

. 0.55.


, , File > Open.
Open File. , .
, . . ( ). , .

63

Open File

c_*al.cpp
c_tound.cpp
cl clock cpp
dtobotl.cpp
c1robol2.cpp
c2charige.cpp
c2cinout.cpp

. 0.36.

,
, . , . , - ,
, . 0.37.

trenca.h
athlete Sal;
v o i d nainprogt )
Sal.ready() ;
Sal.say("Hi! "

PMC. 0.37. C_SAL.CPP Visual C++ 1.5

.
, , . ,
.
, . , File > Save As.
Save As, . 0.38. File Name

64

0.

(C_SALCPP) , (, NEWSAL.CPP),
.

. 0.38.

!
C_SAL.CPP.
1. , .
2. , C_SALCPP
.. , . C_SALCPP.
3. C_SAL.CPP.
4. : Sal say (Hi!) ( (!)); Hi! Hello! (
. Hello Delete , Hi).
5. C_SALNEW.CPP. ,
,
( C:\FRANCA). ,
, ? Hi Hello? -
Hi, CJJALNEW.CPP .
, .
6. C__SALCPP C_SALNEW.CPP.
7. . Hello?
8. C_SALNEW.CPP C_SAL.CPP .

Microsoft Visual C++ 4


Microsoft Visual C++ 4,
.

65


Microsoft Visual C++ 4.
, . 0.39.


. 0.39. Microsoft Developer Studio
Microsoft Developer Studio :
.
.
.
, . , Hide. ,

Docking View. - ,
View > Project Workspace View > Output, ,
.

66

0.


.
File > Open Workspace. Open Project
Workspace, . 0.40.

,.._
-, -^
Debug
^^^,

Visual C++ 4

FRANMS4

| Project Wofk*pace*(".mdp]

,.,

.MDP

, 0.40, Open Project Workspace Visual C++4

, Look in (C:\FRANCA).
, .
FRANMS4
Visual C++ 4. , ( , ).
, , . 0.41.
. .
, . , . 0.42.
, .
, . , . 0.43.
:

PAULO.RC

FRANCMS4.0BJ

SALCPP

67


. 0.41. FRANMS4

. 0.42.

68

0.

pi r;f5s - /

Puc. 0.43.

(PAULO.RC FRANCMS4.0BJ)
. (C_SAL.CPP) ,
. ,
.
.
!
, , .
1. .
2. , .
3. .
4. , . , ?
5. .


..
C++, .

69

.
. , . ,
. ,
, .


, ( )
Edit > Delete.

.
.

.
FRANMS4.0BJ PAULO.RC .


Inserts Files into Project, Insert Files into Project,
. 0.44. .

Iribetl Files into Picm

jj l] j3J

C2change
cinout
^ C2npul
,f C2irnpbd1

C2|mpjck
jj$ C2scope
Jlf3 C2squ2
avgrd

|JC3sale2
Ja C3store1
Jf QstoreZ

1C MUM

*^.::{:|Source Files [*.;".;*.)

,
. 0.44.

70

0.


, . , ( ) ..
, Build > Debug > Go.
C__SAL.CPP, , , . 0.8.
, . Windows 3.1,
. Windows 95
.
!

C_SAL.CPP.
1. , .
2. .
, . 0.8?
3. .
4. .
C_SALCPP.
C_SOUND.CPP.
5. . , - .
6. .
7. C_SOUND.CPP C_SALCPP.
8. .



.

, File v New. New, . 0.45.

71

. 0.45.

, , 0.46.
File > Save As, .
, , , , File
Name.

. 0.46.


, , File>0pen. Open
File. , .
, . . ( ). , .
,
, . , -

72

0.

. , ,
, . 0.47.

Elude trance
athlete Sal:
d nainprog()

Sal.reedyO;
Sal.sayfHi I " ) ;

. 0.47. C_SAL. CPP Visual C++ 4

.
, , . ,
.
, . , File >- Save As.
Save As, . 0.48. File Name
(C_SALCPP) , (, NEWSALCPP),
.

|f C1robo(2
f?C2change
if C2cinout

;T:F|e |*

Jovial cpp)

?..

.-;

:,..i...

,-v., !

Puc. 0.48.

73

!
C_SALCPP.
1. , .
2. , C_SAL.CPP
.. , . C_SAL.CPP.
3. C_SALCPP.
4. : Sal say (Hi!} ( (!)); Hi! Hello! (
. Hello Delete , Hi),
5. C_SALNEW.CPP. ,
,
( C:\FRANCA). ,
, ? Hi Hello? -
Hi, C_SALNEW.CPP .
, .
6. C_SAL.CPP C_SALN EW.CPP.
7. . Hello?
8. C_SALNEW.CPP C_SAL.CPP .

Microsoft Visual C++ 5


Microsoft Visual C++ 5,
1.


Microsoft Visual C++ 4.
, . 0.49.
, . , Hide. ,

Docking View. - ,
View > Project Workspace View> Output, ,
.
Visual C++ 5
. Visual C++ 5 , , Visual C++ 6: (
),

74

0.
,



. 0.49. Microsoft Developer Studio Visual C++ 5


.
File> Open Workspace. Open Workspace,
. 0.50.
Open Woikspace
'

i . ."

'

3 franmsS.dsw

. 0.50. Open Workspace Visual C++ 5


, Look In (C:\FRANCA).
, .

75

FRANMS5
Visual C++ 4. , ( , ).
, , . 0.51.

. 0.51. FRANMS5

.
, , .
:

PAULO.RC

FRANMS5.0BJ
C_SAL.CPP
(PAUU). RC FRANCMS4.0BJ) . (C_SAL.CPP) ,
. , , .
!
, , .
1. .
2. , .
3. .
4. , . , ?
5. .

76

0.


..
C++, .
.
. , . ,
. ,
, .


, ( ) Edit >- Delete.
.
.

J ^

.
FRANMS5.0BJ PAULO.RC .


Project > Add To Project > Files, Insert Files into
Project, . 0.52. . ,
(FRANMS5) Add Files To Project.
,

,
,


. 0.52.

77


, . , ( ) ..
, Build > Execute Build v Start Debug >
Go. C_SAL.CPP, , , . 0.8.

(.). ,
. Yes,
Build> Execute Build Start DebugvGo.
, . Windows 3.1,
. Windows 95
.

!
C_SALCPP.
1. , .
2. .
, . 0.8?
3. .
4. .
C_SALCPP.
C_SOUND.CPP.
5. . , - .
6. .
7. C_SOUND.CPP C_SALCPP.
8. .



.

, File > New.
New, . 0.53.
, , 0.54.
File > Save As, .

78

0.

, , ,
, File Name.

Server Page
Binary File

_J

Bitmap File
C/C++ Headei File
C++ Source File
Cursor Fie
HTML Page
Icon File
Macro File
Resource Script
Resource Tempi ale

Puc. 0.53-

CLsal.obj - 0 eccar(s). U vaxni&gfs)

. 0,54.


, , File > Open. File
Open. , .
, . -

79

. ( ). , .
,
, . , . , ,
, . 0.55.
, , . , File> Save As.
Save As, . 0.56. File
Name (C_SAL.CPP) , (, NEW5AL.CPP), .

. 0.55. C_SAL.CPP, Microsoft Visual C++ 5

I Debug
I C_saLcpp

j^franrntS.ncb
franmsS.ctii

IC_sound.cpp
Franca
plrarrnsS.dsp

. 0.56.

80

0.

!
C_SALCPP.
1. , .
2. , C_SALCPP
.. , . C_SALCPP.
3.

(LSAL.CPP.

4. : SaL say (Hi!) ( (!)); Hi! Hello! (


. Hello Delete , Hi).
5. C_SALNEW.CPP. ,
,
( C:\FRANCA). ,
, ? Hi Hello? -
Hi, C_SALNEW.CPP .
, .
6. C_SALCPP C_SALNEW.CPP.
7. . Hello?
8. C_SALN EW.CPP C_SALCPP .
, ,
, , FRANCA. H .

,
( C:\FRANCA).


.
. , !
.
.WAV.
.
,
:

_ ?

81

(/include "franca.h"
athIete SaI; //c_sound.cpp
void mainprogO
{

Sal.readyf);
sound("hello.way");

?

0 .
0 .
0

0 .
0 .
0 .

I . C++, 0.
,
, . . , (). I.
, , , .


U


G

, .
,
0. , - .


,
. , , . , .

,
, 0.

C_SAL.CPP . :
include "franca.h"

ath I ete Sa I
voidmainprogO

{
Sa1. ready();

Sal.sayC'Hir);

}
.
tfincLude ,
, , , . ,
.
franca.h. C++. ( , ) .

85

, , - . ! , , (,
, , ). tfinclude.
athlete Sal . franca.h
, athlete ().
(Sal). , - , Sal athlete.
athlete , .
void mainprogO , . , ({}), .

C++ .

C++ mainQ winmain(). mainprogQ


. .

mainprogQ
, . winmain()

mainprog(). winmainQ , Windows.
, VIII, main prog ().

Sal.ready () .
ready Sal.
(Sal) (ready),
, ((}).
Sal.say("Hi!") , . , Hi (). ? , , , ! ,

86

1.

, .
.
, . .
! . 1.1.

Hi!

. 1.1. C_SAL.CPP

0, . , . 0 C++.


, , , .
(objects).
, . Sal, ,
. :

87

ready()
up ( )
left ( )
right ( )
Sal . , ,
ready, up, left right , .
1

,
, ,
- . C++
, - (member
function) . .
(message)
, , , ,
, -.

Hi. , ,
.

(statements) . .
. ( ) . , , .
, .
ready, up, Left right. , ,
( to go down), , , !
J athlete ( , }.

1,
. 1.2 .

ready

up

left

right

. i.2. , Sal ()
, .
. ,
: . up, ,
, ? ? ? ?
, ,
:
Sal. readyO;
,
.
C++ . Sal
, sal, ,
)

, . SaL,
sal .


( , include void main()).
, ,
, ( - ). :
S a l . readyO;
.

, ( , . .) ( ,
, ). , ,
.

89

- , ,
! , , , . (bugs).

athlete
athlete . ,
1
. i.i.
1

.
,
.
:
ready
up
left
right
. ,
say ( ).

(arguments) . , , , , , . say , , .
.
,
. , 5 , 8 :
Sal.left(5);
Sal.up(8);

, ready, up, left right, . ,


Sal.left(0.5);
, . , . (, ')

90

1.

ready, up, Left right , , , ,


say , , ,
,
.
J

,
! ,
File > Save As .

, - , -
? , hello:
Sai.say(hello);
, hello
- , :
hello* hello.
, , , .

C++,
. C++ hello "hello".

C++
, . , . - . ,
, - !
, ,
.
, , . , . (
, .)
, ,
, , ,
! .
!

C++

91

C++ (//), .
, :
//**-

//
C_Sal.CPP

//
// Paulo Franca, 21.08.94
// 21.08.94
#i nclude "franca, h" //
ath lete Sal;
//
vo i d ma i nprog()
//
{
Sal. readyO;
//
//
Sal.sayC'Hi!");
// " H i ! "
}
//

. , , SaLreadyQ. ,
. , !

,
,
, .

, . , .
( ,
).
1


, .
, .
,
. , .

92

1.

,
, .
.


,
, , . :
, . , , . , ,
, , , .
.
. ,
.


, . ,
:
ready
up
ready

, ?
, , .
. ! :
.
.
, , ,
. , SaLrightQ, Sal.leftQ . .
, ,
, .
? :
ready

up

left

93

up
ready

up
right

up
ready

, , .
, .
, , .


,
. , 5, ready, up, left, right.
, .
0.
C_SAL.CPP , Sab Hils>.
Sal , Sally (),
.
, : ready, up,
left, up, right, up, ready.
,
(ready, up, left, up, right, up, ready).

?

0 C++.
0 , (,
Hello!) .
0 .
0 athlete.
0 .

Clock ()
Box ()
Q Robot ()

, ,
. Clock.
,
. , . Box, - (label), .
, , Robot.
. ( ,
.)

Clock
Clock ( Clock, ), .
Clock, ,
:
Clock mywatch;

// mywatch - Clock

, :
, .
.
.

Clock
Clock :
wait(4noio );

tirneO;
reset();
wait ( ) , . .

96

2.

timeQ Clock ( ), ( ,
).
resetQ Clock . 0, Clock
.

Clock
, :
Hello!, 3 : How are you?.
:
1. "Hello!".
2. Clock 3 .
3. "How are you?".
athlete ( Sal)
Clock. , , (, ready), !
C1CLOCK.CPP mywatch.
//
// 19.10.94.
//
C1CLOCK.CPP

^include "franca.h"
v o i d mainprogO
{

Clock mywatch;
athlete Sal;
Sa I. ready();
Sal say("Hel l o ! " ) ;
mywatch. wait(3);
Sal .say("How are you?");
mywatch. wa i t{ 1);

//
// "Hel l o ! "
// ,
// 3
// "How are you?"
// 1

Clock , .
, time(), . ,
, , :
mywatch.waitfO.5);

Clock

97

mywatch .
, ,
. :
Sal.say(mywatch.time());

, , mywatch.timeQ Clock
, .

?

. , , . .
. .
.
, , : Sally!
Where are you? (! ?), 5
: Here I am! ( !).
, , ( Box) . , . ,
: Sally! Where are you?, . ,
:
// ( athlete)
//
// "Sal ly! Where are you?",
//

// 5
//
// " I !",
//

98

2,

.
, , , . , ,
.
, ,
. , .
:
// "Sal ly!"
// 2
// "Where"
// 2
// "are you?"

, :
// ( athlete)
//

//

"Sal ly!"

//

//

"Where"

//

//

"are you?"

// 5
//
// "Here I am!",

//

2
, ( ).
.


, , C++. ,
:
// ( athlete)

Box

99

:
athlete Sal, S a l l y ;
.
,
. , , ,
.

Box
, -
, . Clock, ?
, , . - , .
Box athlete.
Box, , ,
, athlete. Box
athlete:
.
Box .
( Box)
.
, , :
Box d i s p l a y ( " T i m e : " ) ;


, (
my watch):

d i spI ay.say(mywatch.t i me());


, Time:
, .
Box,
. 14
( Box).

100

2.


Robot ,
, , .

Robot
Robot ,
, .
Robot .
, . . 2.1.
! Paulo France's C-

. 2.1. Robot

Robot,
( ) .
Robot Tracer.
, . 2.1, clrobotl.cpp.

101

include "franca. h"


//1 robot! .
Robot Tracer;
void rnainprogO

{
Tracer. face(3);
Tracer. step();


Tracer, Robot,
:
Step() . .
.
, timescale (. ),
.
left() 90 .
right() 90 .
seewallQ ( ).
,
. ( 8.)
seenowalL() ( ). , . ( 8.)
() .
(0, 1, 2 3), , ,
. 4.
mark() . , , ( ). . 2.1.
5(" ) . ( ),
( ). 12.

1 02

2.

2.1. markQ

51() .
, , , ,
0,1 . , ,
0,5 , :
Trace .t i mescaIe(0.5);

C++ : ,
. , , .
, cl robotl. cpp, Tracer
, :
Tracer. stepO;
f*w

Tracer , . Tracer ,
, . , .


clrobotl.cpp .
.
:
Tracer, stepO;
Tracer.stepO;

1 03

Tracer.step();
Tracer.step();
:
Tracer.step(4);
.
.


, ,
. ,
.
, :
Tracer, ma rk();
Tracer.step();
Tracer.mark();
Tracer.step();
Tracer. mark();
Tracer.step();
Tracer.rnark();
Tracer.stepC);
Tracer .
.

, . ,
. 2.2.
, , .
? . . ?
( leftQ right()), ( face()). ,
, :
Tracer.face(1);

104

2.

2.2. face()

( )

( )

( )

( )


, . , . :
Tracer. sayC'Done!");

, , ? ? ...
, , , going () . , coming back ()
.

, , . . 3 3. , ,
, . .
:


:
#IIude "f .h" //drobot2.
Robot Tracer;
voidmainprogO
Tracer.face(3);
Tracer.mark();
Tracer.step();
Tracer.mark();
Tracer.step();
Tracer.mark();
Tracer.step();

//

Tracer, rightQ;
Tracer. mark();
Tracer.step();
Tracer. markO;
Tracer.step();
Tracer.mark();
Tracer. stepO;
Tracer.right();

//

Tracer.mark();
Tracer.stepC);
Tracer.mark();
Tracer.stepO;
Tracer. markO;
Tracer.stepO;
Tracer,right();

//

Tracer. markO;
Tracer.stepO;

06

2.

Tracer. ma rk();

Tracer. stepO;
Tracer. markO;
Tracer. stepO;

//

Tracer. rlght();
, , , . ! . 2.2.

. 2.2.

? 4,
3 ? , ?! ,
3 , ,
4 . 3 ,
.

?

0 , Clock.
0 , Box.
0 Robot.

. , , , . ,
. ,
, , .
. ,
: ,
. . ,
.


. .
, . , , . ,
, .


( ). .
, . ? ?
, :
3
.
, 3!

)9


,
, .
, , .
. , , . ,
:








, , .
. :




.


, . ,
. ? , , ,
.
, . , ,
?
. ,
. , , ( , ) , - .

110

3.


? . ,
. ,
.
c_sat.cpp :
Sal.readyQ,
SaL.ready);.
.
tfindude.
5("1!");.
, , , , . , . , ,
, , , ,
, ,
:
Sal.readyO Sal , s a y ( " H i ! " ) ;

, , , , .

, . , ;
. .
.
, , Hi!
. .
. ,
, .
, , . -
, , , . , ,
,
. .

111


.
, , . , .
, ,
, .

() , , , , ,
. , :
( _ ) .
, ( 0 )
.
32 (, ,
, 32).
.
( , ).
, .
*
^
^
^
^
^
^
V
^

:
2waystop ( ).
my number ( ).
this-number ( ).
"Sal" ( ).
:
twowaystop
mynumber

thisnumber

Sal

112

3.


. , void, , , .
.
, .
( ).


( ),
. .
.
,
. int () ,
. int . ,
( athlete).
, .
. , , athlete .
,
.
. , athlete ,
. ,
(, athlete),
.

(declaration) ,
. :
_ _;

_ , (, athlete clock
franca,h), _ . ,
, , , :
athlete Sal, Sal I;

113

, .
. ;
Clock mywatch;

// mywatch Clock

. int number_of_tirnes; // nu(nber_of_tinies

.
.
, .

(), , .
. :

_.();
SaL ():
Sal.upO;
Sal () 2:
Sal.up(2);
,
2 .
C++ -. - 16.

, -
. . C++
:
(//).
(/*) (*/)
.
:
/* $

*/

:
int/* */ iapcount;

114

3.


c_salcpp:

include "franca.h"
athlete Sal;
voidmainprogO

sal. readyf);
Saf.say(Hi);
}

. . .


, . ( ) , .
, ,
. 3.1 ( ). .

tf
ready

up

left

right

. 3.1.


c_sal.cpp:
^include "franca.h"
voidmainprogO

115

ready;
Sal.sayC'Hi!");
I

. , ,
.


:
2nd! ime

apol 1-13
apol 13
i gT i me
my friend
my. friend
my_friend
void

, ,
. 3.2.

. 3.2. -

:
, .
.
. , ? , , , , ?

116

3.


. ?
?
voidmainprogC)
{
athlete bi 1 1 , John doe, ann3;
Clocks days, my-clock, O'Hara, other;
Box show;
show.say(thetime);
bi 1 1 .say(other.timeO);
Marie. readyO;
Robert.upO;

?

0 .
0 .

II

. II
. ,
,
. .
II
.

Q
U

. ,
. , ,
. , ,
, , .
, . , .
.
, , ,
. ,
. , , .


. , .
:
Sal.upC);
Sal.leftO;
Sal.upQ;
Sal.rightC);
Sal.upf);
Sal. readyO;

, .
, , , -,
. ,
,
', . .
C++ (function), - (procedure).

1 20

4.

, .
.

. , . , ! , .

, .
/ .

, , ,
. , ,
:


. , , .
, , -, :

, .
, .
. ,
, ,
.

. ,
.

121

. ,
( ), , ,
.
:


.
C++ ;
v o i d JumpJackO

{
Sal.up();
Sal.readyC);


, -, , -, .
- : . , .


?
. ,
{}.
. . ,
.
"
^

^
^

^
"

^
^
^
^
^
^
^
^
^
^
^
^
^

^
^
^
^
^
^
^
^
^
^
^
^
^


{ , ).

, , ( ).

1 22

4.

void,
, {).
.
c2jmpjck.cpp:
//

c2jmpjck.cpp

// Paulo Franca
// 05.01.96
//
// JumpJack

^include "franca.h"
athIete S a I ;
void JumpJack()

//
//
// SaI athIete
// JumpJack

Sal .up();
Sa I. ready();

//
//

// JumpJack

void mainprogO
{

//

SaI.ready();
JumpJackO;
}

//
// !
//

JumpJackQ mainprog(). ,
mainprog , ,
. ,
, . mainprogf) . JumpJackO.

, ,
.
mainprogO :
void mainprogO
{

SaI.ready();
JumpJackO;

//

//
// !


JumpJack();

// !

JumpJackO;

// !

1 23

//

, , , , . ,
. ,
.


, , .
, :
athlete S a l , Sal I;
,
athlete , , ;
S a l . readyO;
Sal I. readyO;

, . , ,
?
JumpJackQ,
. ? :
JumpJack().
, .
. , ,
, . ( , , , ?)


, . , , .
- . ,
. , , ,
. ,
,
.

1 24

4.


, ,
. ,
, (parameters). . , , (arguments). .
, , JumpJackQ c2jmpjck.cpp , ( ,
). , , athlete.
1. , . - , ( somebody). . ,
. , - .
2. , ( ) . , ( , ),
.
, c2jmpbdy.cpp:
//

c2jmpbdy.cpp

//
^include "franca.h"

athlete S a l , S a l l y ;
v o i d JnpJack(athlete somebody)

somebody.up();
somebody. readyC);

>

void mainprogO
{

S a l . readyO;
JmpJack(Sal);
S a l l y readyC);
JmpJack(Sally);
}

3mpJack() sombody, , ,
. ,

1 25

, . , , JumpJackQ :
JmpJack(Sal);
JmpJackfSally);

Sal. ?
JumpJackQ, somebody
SaL , - .
Sally. , . 4.1.
; Paulo France's C

. 4.1. c2jmpbdy.cpp

,
:
. JmpJack() .
.
, JmpJackQ ,
.
JmpJackQ : , . .
, JmpJack(SalLy);,
JmpJack(Sal);.


. , ( ).
, , ,
.
, . , -

1 26

4.

JmpJackf) ,
,
( ):
void JmpJack(athlete sombody=Sal)
,
JmpJack();
,
JmpJack(Sal);

. . , ,
, ;
JmpJack(Sally);

, (. ).


. , ,
. C++ , () .
JmpJackQ, . ,
JmpJackQ :
void JmpJack(athlete first, athlete second)
JfnpJack(fiKst);
JmpJack(second);
}

. , ,
, ? ,
. ,
.
, 0 , .

1 27

. ,
. ,
( ), .
.
JmpJackQ
:
.
.
.
.
.
.
?
:
void JmpJackfathlete first, athlete second)

, . , , Sally
JmpJack():

Sally , , . C++ .
, .
.
, . , , .
, ,
Clock (). Clock, timer (), .
,
.
,
, .
Before:, D u r i n g : After:. c2clkcpy.cpp:

128

^4.

#include "franca.h"
// c2clkcpy.cpp
//
Box before ("Before:"), during ( " D u r i n g : " ) , after ("After:");
void zerofClock clone)
t
clone.reset();
//
during.sayfclone.timeO); //
}
void mainprogO

I
Clock timer;
timer.wait(1);
// 1
before.say(timer.timeO); //
zero(timer);
//
after. say(tImer. timeO); //
, .
, . - wait(l) 1.
, , zeroQ. 0.
, ,
. , , ,
, , , timer
. ! , , 1
. , timer, . . 4.2.

3efore:

1.00
During:
.
After:
1.01

. 4.2. c2clkcpy.cpp
, , .

1 29


C++ ,
, . ,
(value), (reference).
. , ( ). C++ &.
:
void zero(Clock &cione)
{
clone.reset();
//
during.say(clone.timeO); //
! .
(&) ,
. .

, &
, .


, ,
. , .
. . , . , athlete . , , ,
.


, . . , ,
. -. - 16 17.
athletes,
ready, up, Left right. -

130

4.

. ,
.

- --
. , athlets :
ready()
up()
left()
rightf)
say()

. , -
athlets. - . . , JmpJackf) ,
. ,
, --.


,
. , ,
. ,
.
( ) . , ,
tfindude. ,
, .
franca.h. , franca.h
:
include "franca.h"

.
JumpJackQ , jumpjack.cpp,
:
^include "jumpjack.cpp"

131

JumpJackQ (
franca.h). ,
, , ^include.
, ,
, (,
C:\franca). , :
include "c:\franca\jumpjack.cpp"
, ,
. , ( , ). . :
#inc!ude<math.h>;
jfinclude <iostream. h>;

(directive) . , , .
C++, .
(#)
. ,
tfinclude.


, ,
, (header file) .h ( .).
., , .h. File >- Store As File > Save As
.
.h (. ).


JmpJackQ.
, jump] ack.h.

4.

132


: , , ,
. , , , , .
- ,
(scope). , , , , .
C++ ,
.
, ,
. ,
, .
.
cZjmpjck.cpp Sal. JmpJackQ .
c2jmpbdy.cpp Sal
Sally. JmpJackQ somebody . Sal Sally - , mainprogO
Sal Sally.
, .
c2jmbdl.cpp,
c2jmpbdy.cpp.
//
//

c2jmbd1.cpp

//
// JumpJackO athlete
^include "franca.h"
void JrnpJackfathlete somebody)
{
somebody.up( );
somebody. ready( );
}
void mainprogO
{
athlete Sal, Saily;
S a l . readyO;
JmpJack(Sal);

133

Sally.ready();
JmpJack(Sally);
, Sal Sally mainprogQ. , .


. Jmp3ack(). somebody Sal.
Sal:
void JmpJack(athlete Sal)
{
Sal,up();
S a l . readyO;

Sal Sally:
void mainprogO

* athlete S a l , S a l l y ;
S a l . readyO;
JmpJack(Sal);
Sally.ready();
JmpJack(Sally);
1

-
. , .
JmpJackQ Sal , . Sal, mainprogO, a Sally.
. . .
( ).
SaL . JmpJackQ ,
,
. , , , , .

134

4.

Sal ():
athlete S a l ;

Sal :
void JmpJack(athlete S a l }
{

Sal.upC);
Sal.readyO;

}
Sal Sally:
void mainprogO
athlete Sal I;
S a l . readyO;
JmpJack(Sal);
Sal ly. readyO;
JmpJack{Sally);

, , , .
.

,
. , somebody

. :
v o i d JmpJack(athlete sombody)

athlete sombody;

// ! somebody !


. Sal
mainprogQ.
. Sal .
void mainprogO
athlete Sal;

jmpjack(Sal);
athlete Sal;

1 35

// ! Sal
//

, C++
.
.
. Sal . -,
. SaL . Sal
.
void mainprogO
1
athlete Sal;
Sa I. ready()
{
athlete Sal;
athlete S a l l y ;
Sal. upO;
Sally.left( );
;
Sa I. i ght();

// SaI
//
// Sal
//
// I


Sally. Salty
(,
). Sally .

?

0 .
0 .
0 .
0 .
0 .

,
-

Q
U

. , , ,
.
I, .
.

, .


. , . ( ) , . , ,
, , . C++
.
. .
int long.
int , 32768 +32767.
Long , -2147483648 +2147483647.
. . float double.
float
, 3.4x10"38 38. .
double
,
l.TxlO"308 UxlO308, .

138

5.

. char , - .
char .
)


int Long .

,
. . , , .
:
athlete S a l ;
Clock my time;
, , . :
int count, howmanytimes;
float elapsedtime;
, unsigned int
. . . , lapcount,
0 65 535:
unsined int lapcount;
J

, ,
. .

? . .
, . ,
( ), , , .

1 39


, ;
().
, .
.
, .
)

, - .
, .
, int,
-
, , .

:
.
.
0 .

:
Sinclude "franca.h"
void mainprogO
1
II ", , "
Athlete Sal;
int one, two, three;
Sal ready(5);
Sal .sayC'one");
Sal.say(one);
Sal,say("two");
Sal.say(two);
Sal,say("three");
Sal.say(three);

140

5.

one, two three? , , , - ,


. ,
.
S
^

^
^
^

^^^^^!-*.-^^^^^^^^^^^^^^^^^^^^^^^^^^-^^*-1^^^^

- , (, , }. , , ,
( ) .


=.
,
. - ,
, . , howmany 21,
:
howmany=21;

, howmany :
int howmany;

, C++ =
, ,
,
. ! , C++ , howmany 21:
21=howmany;

// !

, 21 21!
. , new old.
, new 15, old 13,
, 13, , :
new=old;

:
old=new;

141

, - 13, , new
13.
new old,
, ,
new old, new :
new=old;
old=new;

, , . :
temp=new;
new=old;
old=temp;

, ,
. , maybe, 21:
int maybe=21;


, , , :
int =1, two=2, three=3;

, (+) (-), . , ( *) (/),


.
,
= . :
intrnaybe, hisage, difference; //

142
maybe=21;

5.
// 21
// maybe

difference=5;

// 5
// difference

hisage=maybe+difference;

// maybe
// d ifference
//
// hi sage

hisage 26.


( ). :

maybe=maybe-i-1;
maybe,
. (, ?)
:

maybe=maybe-1;

++
C++ .
(--) (-). :
maybe++;

: :
maybe=25;
hisage=maybe++;

maybe,
. :
maybe 26, .
hisage 25, maybe.

143

,
maybe,
hisage:
rnaybe=25;
hisage=++maybe;
, (maybe hisage) 26.
:

maybe=25;
hisage=++maybe++;
, hisage
maybe? maybe ,
.

~ .

++ ~ , .
(), ,


, :
int 1=1, j=2, k=3;

, k . , . , .
k=++k+j;

k= i - j ;
i+j=k;
k++=j ;


. ,
, , :

144

5._

== .
!= .
< .
<= .
> .
>= .
, ( ).


.
- .
. .
.
, , . ,
, .
, , .


.
(input) , - (, ).
.
, , . , .
.
23.
, .
j

C++ VIII.

, , . , , -

145

,
. .
:
ask .
Ci n , C++.

ask()
ask() ,
. -
, . ask() :

ask{3anpoc);
,
. ,
(. 5.1),
:
Price=ask("Please enter the p r i c e : " ) ;

(
Enter), Price.
Paulo France's

Please enter the price:

. 5.1. ask()

Cancel .

Cin
Cin,
C++. ask();
C i n _;

146

5.

_ , . , .
(. ).
Gin ,
ask(). , , (. 5.2).
Paulo Franca's *+
Input a number

. 5-2. Cin


, , . , , (output).
,
, , , , . . . .
, . ,
. C++ (
) , , , .
Cout,
Cin. Cout C++.

Cout
Cout .
,

147

. ,
(. 5.3).
Paulo France's '

Output:

12.00

. 5.3. Cout
Cout Cin,
. ,
Cout Cin e .
Cout ^;
Price:
Cout Price;

ask(), say():
# include "franca. h"
athlete Jul ia;
void mainprogO

// c2input.cpp

float Price;
Jul ia. ready();
Price=ask("Please enter the price: ");
JuJ ia.say(Price);

148

5.

C++
cin cout ( ),
VIII.
Cin Cout
ft include "franca, h"
void mainprogO

// c2inout.cpp

float Price;
Cout"Enter the p r i c e : " ;
CinPrice;
CoutPrice;

}
Cout , (
), Cin .
.
, .
:
^include "franca. h"
void rnainprogf)
{
float Price, tax=8.25;
Cout"Enter the price: ";
CinPrice;
Price=Price+(Price*tax/10Q);
CoutPrice;

//

//

tax, 8.25,
. Price Price.

?

0 .
0 .
0 .

149

CJ

, , ,
. ,
. , .
- .
, , ,
. , , , !
, ( ). , .
. :
.
, .
, , .
, , .
, ,
. . , , .
, .
:
.
.
.
, .
.
.

152

6.

, , . ( ).


.
( JmpJackQ), (
Clock) .
. :
( JmpJackQ).
(
).

. ,
. :
, theprice ().
, theamount ().
, thechange ( ).
. , . , .
,
.
.
theprice theamount , thechange .


mainprog().

Ha C++ :
void dif(float theprice, float theamount, float &thechange)

{
thechange=theamout-theprice;

1 53

amount price, , :
dif(price, amount, &change);
Coutchange;
:
Coutdif(price, amount, change);
, , . , , , . , , , .
, ? , ,
:
Number=JumpJack(Sal);

:
Price=ask("Please enter the price");
?


C++ , . , :
v o i d dlff(float theprice, float theamount, float &thechange)
.
void ? , .
, :
. void, .
, C++ int.
return, , .
, , dif() ,

154

6.

. , :
float dif(float valuel, float value2)
. ,
dif valuel valueZ ( ) , . .
, . return, - :
return ;


. :
float dif(float valuel, float value2)

{
return value1-value2;

return
! ,
return, .


c2change.cpp.
, .
, .
.
:
price, amount change.
.
.
( dif()).
.
c2change.cpp (
. 6.1):

155

tfinclude "franca. h"


//c2change.cpp
float d iff (float valuel, float value2)

{
return value1-value2;
void mainprogf)
{

float price, amount, change;


Box given("Amount: "), thechange("Change: ")
price=ask("What is the price to pay?"};
amount=ask("How much are you g i v i n g me?");
change=d if (amount, price);
given. say(amount);
thechange. say(change) ;

, 6.1. c2change.cpp
, price amount
:
change=dif(amount, price);

156

6.

dif().
, .
change.
price amount.
dif () valuel value2 .
dif(), valuel amount, value2
price. , .
,
. :
change=dif (price, amount);



( ), .
dif(),
:
float d i f (float valuel, float value2)
value1=value1-value2;
Coutvalue1;
return valuel;
valuel,
. Cout.
, valuel
.
valuel ( amount), , . amount
- .

. dif() mainprogQ,
- .

, .

157


, , , ,
. , , .

. , ,
,
.
C++ , , . (inline functions). , , , .
, ,
.
. , ,
, ( 7.)

inline. ,
C++ .
,
,
2. .


. , , , !

:





158

6.



, ,
.
( Tracer).
, .
.
3 :




,
, , , .
. ,
.
3 , :



//

//

//



:
Robot Tracer;
v o i d I ine3()
{
Tracer mark();
Tracer step();
Tracer mark();
Tracer stepC);

1J9

, , . .
, . , .
,
:
void I ine3(int color)
Tracer mark(color);
Tracer step();
Tracer mark(color);
Tracer step();

}
, , ? ! :
void I ine3(int color=2)
. . - .
:
void square3(int color=2)
I ineS(color);
Tracer. rlghtC);
I ine3(color);
Tracer. rightC);
I ine3(color);
Tracer.rightC);
I ineS(color);
Tracer.right();

Tj50

6.

() square3(). squareBQ
Iine3(),
.
pointQ, Line3() squareSQ. pointQ
,
Iine3 ().
Line3(). square3()
.
. mainprogQ.


, , .
. ,
.
,
. , ,
, . :
.

.
, ,
. , . . , , .
, ,
, , .
. , ,
. , .
, , . .
? ! ,

. ?
, ,
, . -

16\

, . : .
,
C++. ?
, ,
.
c2squar2.cpp .
mainprog() ,
. .
include "franca.h"
//c2square2.cpp
Robot Tracer;
inl ine void paint(int color=2)

Tracer.mark(color);
Tracer. step();

void I ine3(int color=2);


paint(color)
paint(color)
void square3(int color=2);
i
I ine3(color);
Tracer. right();
Iine3(color);
Tracer.right();
I ine3(color);
Tracer. rightO;
Iine3(color);
Tracer.right;
void mainprogO
(

Tracer.face(3);
Tracer.step(5);
Tracer.right();
Tracer,step(7);
Tracer. leftO;
square3();
Tracer.step(5);
square3(3);

]62.

^^


(function definition)
, , . . :

{


(function header) :
( void).
( ) , , ( , ).
'

void
. , float time (void) ,
, float time().

,
( ). .
,
void Jrnp Jack (athlete somebody)
, :
(void).
JmpJack.
somebody athlete (, somebody athlete).

1jS3


(function body) , . , , .
, 3mpJack() :
somebody. up();
somebody. readyO;
:
void JmpJack(athlete somebody)
{

somebody.up();
somebody. readyO;

:
, .
, !


(type matching)
. , , Clock,
athlete:
void jumps(Clock timekeeper, athlete somebody)
,
.


, C++

. (function overloading).
, JmpJackQ, ,
. 3mpJack()
athlete .

:
JmpJack(Sal);
JmpJack(Sal, Sal ly);

]64

6.


. ,
( ), . ,
.
&,
, .
, . ,
. ( ). , :
float d i f ( f l o a t &value1, float Svalue)
;
change=dif(100.00, price);
valuel,
100.00 . ( ) .


, , (function call).
:
_ (_);

.
,
.


. , ,
. , , . , (function prototype).
)

, ,
.
.

, :
:
float change (f I oat price, float amount);
, :
float change(f loat, float);
, , . .


^include, . ,
tfincLude ,
, ^include, , , .
, gymnasth, ,
, QmpJack.h. , ,
gymnast.h tfinclude
3mp3ack.h.
:

ftinclude "gymnast. h"


tt include "JmpJack.h"
gymnast. h:

include "JmpJack.h"

, OmpJack.h.
. :
ffifndef

JUMPJACK_H

define JUMPJACK H

:
ttendif

OUMPJACK^H .
, #, C++.
, .
.

6 .


#include,
JmpJack.h. JumpJack.h .
, , -. athlete. fitness. h
.
, fitness.h . .


-32 768 +32 767.
int . float, , ,
float.
. :
int number, alpha, count;
float x, , z;

.
;
intone=1, two=2, three=3, other;
float x=3.45, y, z;

: one, two, three


other, 1, 2 3,
. ,
3.45, z , .
,
, (=) . . :
=54;
two=one+three;


. :
one+two=three;

1 67

float int, .
,
( , ). ,
, .

(expressions)
, ,
, (). , .
:
+
-

*
/

%
, .

+, -,*,/,% =.
(),
.

.
, .
. *, / %, + -. , , ( ).
-.

. (
, , -)

68

6.

.
.
, ,
.
(++) (--) .

( ) (=).

,
.

, , .
. ,- howmany=times=times-fl . times+1,
times,
howmany. howmany=times+l=5,
times+1,
, .


semmetry (athlete he, athlete she),
.
* he :

169

she :






, .

?

0 .
0 .
0 .

III

/
,
,
.
,
,
, ,
.
.
, .

.

Q while
do...while
Q for
Q
a

. , , .
. , 15 .
, . , , .
, , . , ,
15 .
- , ,
, , .
, :
! ! !... . 7,1.
Count

JumpJack
Count =
Count-

!
. 7.1.

, ,
. ( count).
, .

173


(loops),
. :
while
do...while
for

while
. 7.1 :
( count)
15, :
( Jump3ack())

!
,
. ,
.
, .., , ( )
,
15 .
?
.


. 7.1 .
- (flowchart). (
count=0) , 15 ( count<15). . , , (
JumpOack) ( count=count+l).

15. 1, -
15, . , -,

7. 1
, .

.
15 (
0 14 ). 14, JumpJackQ
15. , 15 ( count<15),
.


, :

Count=Count+1;
? ! 15. 15 ( ). , (infinite
loop). , , , .
.
, , (, ).

, , ,
Ctrl+Alt+Delete.
Windows, ,
. Enter ,
C++.


.
:
( JumpJackQ)

, . , , ,
.

V75

(compound statement)
( ), . ( ) , , .
, C++, , .

, mainprog(), ( ).

while
! .

,
, Count 15, ... :
whi le(Count<15);

// !

JumpJack();
Count=Count+1;
? , , , ! , Count .

. , 1,2,3,..., 14. 14, (14 15).
(), 15.
. ( ) 15, (15 15) ! ,
( ).
1 (count=l) count<=15 count<16,
.

1176

7. ]11

C++ :
count=0;
while(count<15)

//
//
// 15

JumpJack(Sal);
count++;
;

//
//
//

, . 7.1 - while.
:
count=0;

whi le(count<15)
(JurnpJack(Sal);
count+-i-;}
, .

while
while , , .
. , 15, < (). :
==
>
>=
<
<=
!=

, . .
,
while .
:
count<15

17/

, ,
15. < ()
(relational operator) (conditional operator),
. , .
. 10 .


,
-.
, ,
-.
, , .
,
.
3mpJack() :
v o i d JmpJack(athlete somebody, int howmany)
somebody athlete
, howmany.

do...while
do...while while. while
. , , !
do.. .while , . 7.2.
, ,
. .
do.. .while do ,
while, , . , !
, :
count=0
do

-&

178

7.

JmpJack(Sal);
count++;
I
whi le(count<1 5);
, , while .

!
. 7.2. - do...wkile

for
white for, while
,
, .
while:
count=0;
whi le(count<15)
{
JmpJack(Sal);
count++;
:
forCcount=0; count<15; count++)
(

JmpJack(Sal);


, , for ,
, , :
(count=0) , . ,
.
(count<15) .
while, .
(count--) , . , .
:
for(count=1; count<=15; count--)
, , , , .

, for
!

for . :
for(int counter=1; counter<=15; counter++);
for cSaskfor.cpp. ask().
//
// 07.09.1994
include "franca. h"
vo i d JmpJack(ath I ete she)

// JumpJackO she
she.upO;
she. readyO;

//cSaskfor.cpp

\^7 ^

1 80
void mainprogO

{
athlete Kim;
int howmany, done;
// : " ?"
howmany=Kim.ask("How many jumping Jacks?");
//
for(done=1; done<=howmany; done++)

{
JurnpJack(Kim);

// JumpJackf)

Kim.say(done);

}
Kim.sayC'dorie");

// ?

, JmpJack(). howmany , (
done) howmany.

for while
for
while. for while, for while.
, :
(<1>; <2>; <>)

<>

}
, :

<1>;
whi 1(<2>)
I
<>
<>;

181


,
.
,
. ,
- ,
.
.
> . ,
.


. , ,
, .
:
float value;
//
value=ask{"Enter a positive value;");
while(value<0)

// .
value=ask("Wrong, enter a POSITIVE value:");


. , . ,
, , !


, .
, cSstorel.cpp.
Sinclude "franca.h"
void mainprogO

//cSstorel.cpp
&

182

_. _. _.

_7._^

float price, change, amount, tax=8.25;


//
price=ask("Input the price:");
price=price+price*tax/lOO;
Coutprice;
//
amount=ask{"Enter the amount:");
whi le(arnount<price)
{

//
amount=ask("Not enough, re-enter the amount:");
}
change=amount-price;
Coutchange;
>

( amount) while
, . ,
( ) . , ,
. , , , !
do.. .while. , . while
do...while:
do

//
amount=ask("Enter the amount:");
}
whi le(amount<pnce);

, do...while
. ,
, .


. , -

183

. , .
,
while, do.. .while for.


, , , 12 . , , 12
(, , ).
.
, cSdotime.cpp:
// . . . c3dotime.cpp
// 08.08.1994
((include "franca. h"
void JumpJackC athlete she) // JumpJackO
she ready();
she up();
she readyO;
void mainprogO

//

athlete Kim;
// Kim athlete
int howmany=1;
//
Clock timex;
//
whi le(timex.time(}<12) // ,
// 12

JumpJack(Kim);
// JumpJackO
Kim.say(howmany++); //

. , while
( timex Clock) ,
12.

184

VpjjK^.


, Kim .
, ready up,
. , , 12 . . , , . , .

yesnoQ
,
.
c3st.orel.cpp. .
, ,
, . . ?
: Do you want to continue?
Yes or No? ( ? ?). Yes,
.
?
franca.h, , Yes No.
( Do you want to
continue ?), . . 7.3 ,
yesnoQ.

. 7.3. yesno()

yesno():
yesnof'Do you want to continue?"};

yesnoQ ,
. Yes,

85

, No .
, c3store2.cpp.
,
do.. .while. yesnoQ
.
include "franca. h"
void malnprogC)

//c3store2,cpp

float price, change, amount, tax=8.25;

do

{
//
price=ask(" Input the price:");
price=price+price*tax/1GO;

Countprice;
//
amount=ask("Enter the amount: ");
w h i le(amount<price)

{
//
amount=ask("Not enough, re-enter the amount: ");
}
change=amount-pr i ce;

Countchange;

}
//
whi le(yesno("Do you want to continue?"));
\

yesno() do. . .while,


.

Yes , ,
Yes. Enter ,
No.

yesno()
C++ .

1 86

7.


, . ,
10 . 10
, . , :
10 :
2 :


20 .
, .
, . . 7.4
(inner loop), , (nested loop), , . 7.5 ,
(outer loop), .

. 7.4. ,

. , :
5 :
10 :

5 :
-
3 :

187

.I

I
; I

I
]

. 7.5.
10 , -,
5 ( 50 25 ) ,
3 .
, , . C++ :
^include "franca, h"
void mainprogO
athlete Sal;
int bigcount, smal Icount;
for(bigcount=1; bigcount<=5; bigcount--)

// -
//

for(smal lcount=1; sma! lcount<=10; smal lcount++)


JmpJack(Sal);

for(smal lcount=1; smal lcount<=5; sma!lcount++)

7.

leftright(Sal);
}
I/
for(smallcount=1; smallcount<=3; smallcount++)
{

JmpJackfSa!);
\

Tracer
, , -
. ,
. ,
.
.
:
v o i d I ine( i n t size, i n t c o l o r = 2 )
:
mark(color);
step();
, ( ).
for:
for( int howmany=1; howrnany<size; howmany++)

howmany<size howmany=l
size-1 .
:
void I ine(int size, int color=2)
I
forfint howmany=1; howrnany<size; howmany++)
f

Tracer.mark(color);
Tracer,step();

Tracer _

89

, ,
.
. cBsquare.cpp, .
I no I ude "franca, h"
//cSsquare. cpp
Robot Tracer;
// 1 ine
void I ine( int size, int color=2)
(
for (int howmany=1; howmany<size; howmany++)

Tracer. mark(color);
Tracer. stepO;
}
' squareO
//
void square(int size, int color=2)

{
f o r f i n t turn=1; turn<=4; turn++)

\I

I ine(size, color);
Tracer, rlghtO;

II mainprogO
void mainprogO
{

Tracer, face(3);
4
square(3);
Tracer. step(3);
square(4,5);


?
,
:

1_90

7.

Tracer. seewal l();


, :

Tracer.seenowal l();
, , , . Tracer.seenowallQ , . :
for(; Tracer.seenowal l(); )
{

//
Tracer. mark();
Tracer. stepC);
}

for? !
. , -
.
,
, :
for(lnt howrnany=0; Tracer.seenowal I; howmany++)
{
//
Tracer, markf);
Tracer, step();


, ( . 7.6),
, , ( Tracer).
, ? , , :
, .
, ,
. !
?
? !
.

Tracer

191

. 7.6. ,

1.
. , , ,
. .

:
,

,

,

,

, .
. ,
.

7.
j

2.
.
:
:
,

. , , 3.
3.
, :
void f i n d w a l l ( ) ;
, ,
. ,
:
:
findwallQ
4.
,
. , , .
:
int f i n d w a l l ( )
{
int steps=Q;
whi le(Tracer.seenowal!())
{
Tracer.step();
step++;

}
tracer. right();
return steps;

193

. , . , :
int howfar;
howfar=findwalI ();

, , , :
for(int=1; i<4; H+)
findwall();


int distance{).

, ,
. .

, - ,
, .
.
, - ,
, .
.
, . 7.7.

, 7.7.
, . 7.8.

194

7.

. 7.8.
, . 7.9.

. 73.
, (. . 7.77.9)?

?

0 .
0 while, do...while for.
0 .
0 .

Q

Q

, , ,
- .
, ,
.
! , , . . :
.
.
, , .


. 8.1 8.2 .


. 8.1.

197


. 8.2.

, , , ,
, ,
.
,
. , ,
12 , 8 4.
:
. 8.2.
, 12,8 4! , 12 ,
4 .
, .


;
12
8
4
, .
! ,
.

198

8.


:
^include "franca. h"
Robot Tracer;
void mairtprogO
{
square(12);
square(8);
square(4);

}
, .
I

, ?
! , : 3, 15 100, . .

()
:
# include "franca. h"
Robot Tracer;
void malnprogO
int many, size;
size=12;
for(many=1; many<=3; many++)
SQuare(size);

, .
. , ,
.

199


:
include "franca. h"
Robot Tracer;

//cSsqS.cpp

void mainprogC)

{
int many, size;
size=12;
for(many=1; many<=3; many++)

square(size);
size=size-4;

, ? 12 , . , 4 . , 8 . .
. 8.2, , . ,
. ! , ,
,
. .
cSsqS.cpp.


! ,
. , . , , , - , . ?
, , , . , , . ,
, . ,
. , ,
10 , .
? ! ,
, .

200

8.

, .
.
.
total, :
for(many=1; many<=total; many++)


. ,
. ,
,
.
.
. . ,
12 , :
12
3 :
4 :



C++ :
include "franca.h"
i n t s i z e , squarecount, I inecount;
size=12;
for(squarecount=1; squarecount<=3; squarecount++)
{

for(l inecount=1; I inecount<=4; I inecount++)


{
I ine(size);
Tracer. rlghtO;

}
size=size-4;

201

.
squareQ. , , .
, .


. 8.2.
. 8.2.


, .
.
, , .
10.

,
, . , ,
, .


, :
?
?
?
?


, . , -

202

8,

, . ,
. ,
.

,
, (loop invariant).

, c3sq3.cpp:
square(12);

, .
. ,
, . , :


, .
.
4 .
, for. , ,


, (, ). 10 ,
. ,
, , .
. ,
, .


, . . .
. 12, 1.

203


,
. ?
, . :



?
, :
,
:

+ ()
- ()
, :
,
:


, , .
, :


C++ :
//
grade=ask("Please enter next grade:");
tota!=total+grade;

,
.
. .

204

8.


.
. , 26, :
for(int which=0; which<26; which++)
? . .
, , 24 .
:
.

.
,
, .
. for:
int class_size;
//
class_size=ask("Enter the number of grades:");
for (int which=0; wh ich<classesize; which++)
, , -
. , .
yesno() . ,
Yes No
.
yesnoQ ,
:
for(int which=0; yesno("Input another grade?"); which++)
Yes,
.

, which .
- , . ,

205

, which :
for(intwhich=1; which<class_size; which++)
, ,
!
J

,
Yes No.

, , . - , ,
. , , () , .
, , 10.


. , total .
:
include "franca. h"
void mainprogO

//c3avgrd.cpp

float grade, total=0;


float average;
for(int which=1; yesno("lnput another grade?"); which--)
{
w

//
grade=ask("Enter next grade: ");
total=total+grade;
}
average=tota I /wh i ch ;
Countaverage;

206

8.


, .


, { }, . :
for(int times=1; times<=lO; times++)
//
{

Kim.JumpJack();
Kim.say(times);
}

, . :
void mainprogC)

//
//

athlete Kim;
Klm.upO;
Kim. readyO;
}


. :
void mainprogC)
{
athlete Kirn;
for(int times=1; times<=10; times++)
{
Kim. JumpJackQ;
Kim.sayftimes);

207

. .

, .


( ), , (
). (
), , . :
int i , j;

for(i=0; i<3; i++)


/i
int m;
m=i+3;


for. ( , ).
m .
.

m ,
, .
.


(conditional expression)
(relational expression). ,
. ,
.

208

8.

, , C++ . , .


, . :
if(a=b) . . . ;
C++ , . b (
). == ( =):
tf(a==b) , . . ;

if(a=b) ...
. ,
. , .
.


, . ,
:
k=1--a<b;

,
1, , k 2.
( ) k 1.
, .

while
while :
whi 1( )

.
, , , .
,
, .
,
while.

209

. , , , . ,
.

do...while
do.. .while :
do whi le ( );

, .
, .

for
for , .
for :
^- ; ; )

:
, .
,
. , .
. .
( ), .


. for
, , () .
, ! :
for(;;)
Sal,say("l am stuck!");


C++ . , done to_do 0 10:
for(int done=0, to_do=10; done<=howmany; done++, to_do-)

210

j-_ ^

done , to_do
.
, , , , . , howmany
3:


. ,
.


for . ,
. , :
f o r ( i n t done=1; done<=howmany; done++)
;
int done;
for(done=1; done<=howmany; done++)

, for,
, . ,
,
, for,
. .


? ?
tfinclude "franca.h"
void mainprogO
{.

athlete Sal;

^?

21

int done=5;
{

int done;
for( int done=1; done<=10; done++)
{

Sal . ready();
Sal.upO;

Sal .say(done);
j.
for(int m=0, int j=0; m<5;) ++m++;
for(int m=0, int j=0; m<5; m++) j++;
for(intj=0,intm=0; -5; m-H-;)j++;
for(int j=0, int m=0; m<5; m=j++;) m++;
:

N
1 N. 1 . ,
. , . .

.
, int
float long?

?

0 .
0 .

Q
Q

( , ). , ,
, .
.
, , .


, . ,
.
,
.
( ) , . ,
. ( )
.

- ,
. , , !
()
.

, ,
( ) .
, .
7, c3storel.cpp.

. , >
, , . . ,
, .

214

9._

, .
, :
, :




, , . .
, . , ,
,
. ,
.
( )
. :
float getitemsf);

.
Cout. ,
, , Cout,
. .
, : , , . . .
.
J


:
Current total ( ) () .
Sales total ( ) , ( ).

215

Amount tendered ( ) ,
,
Change () , .
:
Box Cur_total ("Current total:");
Box total("Total S a l e : " } ;
Box Amount("Tendered:");
Box Change("Change due:");
.
,
:
//
// :
saIetotaI=get i tems()*sa1estax;
// :
total.say(saletotaI);
// :
amount=ask("Enter amount tenderd:");
Amount.say(amount);

// :
change=amount-saletotal;
Change.say(change);
. , ?
, . , , . . :
whi le(yesno ( " I s there another customer?"))
,
for customer:
for(;yesno ("is there another customer?"); customer++)


. . -

216

j.

getitemsQ
. , . , / , , . , .

saletotal=0;
amount=0;
change=0;
Cur_total=0;
SaIetotaI.say(saIetotaI);
Amount,say(amount);
Change.say(change);
Cur_total.say(0);
,
. , . :
Amount.sayC"");

getitemsQ
getitemsQ , ,
.
:
float getitemsO

{
float total=0;

for(int item=1; yesno("another item?"); item++)


{
total= total+ask("Enter item price:");
return total;

, , , () .
. , .
, .

217


, . mainprogQ
getitemsQ ,
. , ( ) ,
( ).
.
,
.
:
float get items{Box &display)
float total=0;

for(int item=1; yesno("anothe item?"); item++)


I
total=tota1+ask("Enter item price:");
display,say(total);

}
return total;

;
mainprogQ :
saletotal=getitems(Cur_total}*(1+tax);
. , , . , , . ( ).
:
tax=ask("Enter the sales tax %"}/1QQ;


c3sale2.cpp:
include "franca.h"

//c3sale2.cpp

// :
//
//
//
//

218

9.

float getitems(Box &di splay)


{

float total=0;
for(int itein=1; yesno("another item?"); item--)
{
total=total+ask("Enter item price:");
display.say(total);
}
return total;
}
void mainprogO
{

Box Curjiotalf'Current total: ")l


Box total("IotaI S a l e : " ) ;
Box Amount("Tendered:");
Box Change("Change due:");
float tax, amount, saletotal, change;
dai lytotal=0;
tax=ask("Enter the sales tax %")/100.;
for( int customer=1; yesno("ls there another customer?"); customer++)
{

//
saletotal=0;
amount=0;
change=0;
Saletotal.say(saletotal);
Amount.say(amount);
Change.say(change);
saIetotaI=get i tems(Cu r_totaI)*(1+tax);
SaIetotaI.say(saIetotaI);
amount=ask("Enter amount tendered:");
Amount.say(amount);

change=-amount-saletotal;
Change.say(change);

. 9.1 ,
S 12.24 ( 8.25%).

219

. 9.1.

, .
, , . , - (, getitemsQ), , . ,
, . . .
.
, .


. (user interface) , -

220

9. ]

, . . .
, , () .
, , . .


,
. , , ,
. .
, - .
, . , . , . .


, :
, .

.

?

0 .
0 .
1-

IV

,
. IV ,
. , , .
, , .

Q if
Q if... else
Q
Q

.
. , if,
.
,
. Robot.

if
, , . , , . ,
. .
. , ,
, .
.
, . if .
: . , ,
:
:

, ( ). , , ,
,
.
(. 10.1, ):
:

224

10,

.10. 1,

, .
, . ? . :
:


- ,
, , , ,
(. 10.1, ). , , , .
C++ .

. ,
,
. :
whi le(Tracer.seenowal f ())

Tracer, step();
while , . ,
, , ? ,
:
;

225

if

, , . , ( )? . , .
C++ if (. 10.2).

. 10.2, if
if :
if()

//

:
if (Tracer. seenowal !())
Tracer. step();
if:
if.
, , .
.

, . , . .

226

10.

, , ,
. .

, , , ( ), :
if (Tracer. seewal !())

{
Tracer. markO;


. , (), ,
. , .
crossingf), , , . , crossingQ
(N, S, E, W) .
() :
:

, , . ():
void go()
{
if(Tracer.seenowai !{))
{
\\ ,
\\ :
Tracer. step();
}
I
, :
.
$>, .

227

- ( 4),
:
void go()
{
if (Tracer. seenowal !(})
{
\\ ,
\\ :
Tracer. mark(4);
Tracer. step();
Tracer. sayC'OK");

crossingQ .
(howmany)
, , . , howmany . , howmany
.


Robot (, Robot Tracer(l);), .
, .
, .
Robot
(, Robot Tracer;), , .
Robot , ,
, . .


int crossingQ .
:
include "franca.h"
Robot Tracer(1);
void mainprogO

Tracer.say(crossing());

228

10.


.
.
include "franca. h"

void mainprogO
//c4change.cpp
{
float price, amount, change;
float tax=0.08;
arnount=ask( "Enter the amount: ");
price=ask( "Enter the price")*(1+tax);
change=amount-price;
Countchange;
, , . ,
.
,
, . :
include "franca. h"
void rnainprogC)
float price, amount, change;
float tax=0.08;
amount=ask("Enter the amount: ");
price=ask("Enter the price")*(1+tax);
lf(amount<price)

Clock time;
Count"Tnis is not enough! ";
t/me.wait(5);
change=amount-pr i ce;
Countchange;
}

,
( CountThis is not enough!;). ( time. wait(5};) ,
.

if...else

229

if...else

:

:


:


, , :
( ).
.
!
, , , . ,
(. 10.3).

. 10.3.

if, else, , .
if ()
<
//

230

10.

Q&
1\ ot;

//

, ,
, :
int black=7, green=2;
if (Tracer.seewal!())

// ? , :

Tracer.mark(black);

//

}
else
{

Tracer..mark(green);

// :

, . :
if (Tracer, seewal!(}) Tracer. mark(b lack);
e I se Trace r.ma rk(g reen};


. , ,
, , !
.


c4search.cpp, .
, , .
().
if...else, , () .
:
if ()
{

//
!

23_]

else
{

//

}
? , !


() c4search.cpp , , :

QUCHl.


, , .
:
,
:

:


:
, :



, ,
\ , , :

:

232

10.

:


, , *?
if.. .else ?
, ,
. , . , , if, , else, . , , , if, , else.
, , :
Tracer.rightC);
if(Tracer.seenowal!())
{
Tracer.mark(green);

else
{
Tracer, mark(biack);
}
Tracer.left();

, check:
v o i d check()
{
if(Tracer.seenowal!()) Tracer.rnark(green);
else Tracer,mark(black);
}
:
tfinclude "franca.h"
Robot Tracer(1);
void mainprogC)
{
whi le(Tracer. seenowal I) //,
Tracer.left();
check();

//

233

Tracer.right();

Tracer.right();
checkC);
Tracer.left();

//

Tracer.stepC);

//


c4change.cpp.
, ,
,
if...else.
, , .
:
include "franca.h"

void mainprogO

//c4change.cpp

!
float price, amount, change;
float tax=0.08;
amount=ask("Enter the amount:");
price=ask("Enter the price")*(1+tax);
if(amount<price)
Count"This is not enough!";
else
change=amount-price;
Countchange;

, , , .

234

10.


. , . ,
, count 15:
s

count< =15


,
. :
. , ,
count howmany:
count == howrnany
( , , , ). ,
, howmany :
howmany ! =

, . , , , myclock
time(), 20:
m y c l o c k . t i m e O <= 20
. :
howmany - steps > 10
JP

,
,
.

, . , count<=15 :
15>count


, 5, :
*=
!=

235

<
<=
>
>=

.
. , count 15, :
count<-15
^^*..^^-.,^^^..^^^-^1-^^^^^-+^~~,~~-^*~-^^^,^1^*-^-^^*~^-^~^^-^*~~^~~^^~

: if(a=b).
, ! (=).


. , 5 ,
50 . :

count>=50
time>=300
( ) ,
. , ,
. ,
( ).


.
:
&& ( ).
|| ( , ,
).
! (
).
. ,
. ,

236

10.

.
.
:
(count>=15);I(mycIock.t i me()>300)
count 15, time() 300.
(count>=15)&&(!(myclock.tiirie(}<120)
count 15, timeQ 120.
{count>15)&&(!(myclock.time()<240))
count 15, timef)
240.

, :
!.
&&.
||.
. :
(myclock.tlme()>300)! !(myclock.time()>120)&&(count>=50)
&&, 11. , ,
, :
300

120 50


, , , . , if if. :

237

// :
Tracer. right{);
if (Tracer. seewal I ())
(

// ;
Tracer. left();
Tracer. left();
if (Tracer. seewal !())
{
Tracer. say( "corridor");

.
if ( ), if .
, Tracer.
,
. , ,
. ,
( ) .
, , , .
.
, . , ,
. -
, .
C++ , , if else.
, , :
k=0;
if(a<0)
if(b<0) k=1;
else k=2;

k=0;

if(a<0)
if(b<0) k=1;
else k=2;

238

10.

,
. , .
, , else
k 2. else if, , ,
, if k .
, , , , .
?
, else if.
else if.

, else if,
else:
if(a<0)
if(b<0) k=1;
else;
else k=2;
, else if:

k=0;
if(a<0)

I
if(b<0) k=1;
else k=2;


, , - .
, 50 .
1,2,3,4, . :
? , ! .

239

break
C++ break. , break . 10.4 .

. 10.4: break

, . :
?, .
, .
. ,
, , :
50 :
50 :


:
athlete Sai;
Clock watch;
for( int times=1; times<=50; times++)
{
if(watch.time()>50.) break;
JumpJack(Sal);
He else? : , , , , if. . .else, if. ,
:

10.

240

athlete Sal;
Clock watch;
forfint times=1; times<=50; times++)

{
if(watch.time(}>50.) break;
else JumpJack(Sal);
}

, . break
, ( ), - , JmpJackQ , break.
10 :
10 ;

, , :
10 :
.
:


!
? ,
, !. break:
int stepcount;
for(stepcount=1, stepcoijnt<=1t), stepcount++)
(

if(Tracer.seewal !())
break;
Tracer. step();

continue
. continue.

241

break, continue
, .

, , break
, , continue , .

, 50 :

-
, :
JumpJack(athlete)
leftright(athlete)
:
athlete Kim;
Clock watch;
for(int times=1; times<=50; times--)

>

JumpJack(Kim);
leftright(Klm);
Kim.sayC'Done!");
, 15 ,
(-).
if. :
athlete Kim;
lock watch;
for(int times=1; times<=50; times++)
JumpJack(Kim);
if(wath.time()>15.) continue;
leftrlght(Klm);
Kim.sayC'Done!");

242

10.

.
15 ,
-. 15 , continue.
, times 1 .
, , 15 ,
-.
continue break, .


? 8 conti n us brea k. , .
cSavgrd.cpp,
. :
forfint which=0; yesno(" Input another grade?"); which++)
{
grade=ask{ "Enter next grade: ");
totaKotal+grade;
, yesno() .
, 40 , 40 !
. , , .
:
for(int which=1; ; which++)
{
grade=ask("Enter next grade: ");
if(grade<0) break;
total=totaf+grade;

243


. , ( break ). break which
, 0 1. ,
total which.
, for .
. , break !
for. ,
, , ,
:
for(whlch=1; grade>=0; which++)
{
grade=ask("Enter next grade:");
total=total+grade;
. , .
, , which . , , ! , (
).


. , , code
, :
if(code==1)
if(code==2)
if(code==3)
if(code==4)

Kim. ready();
Kim.upO;
Klm.ieftO;
Kim.rlghtQ;

() switch (. 10.5).

244

10,

. 10.5' switch

switch :
:
1:
ready()

2:
()

3:
LeftQ

4:
right()


switch ( . 10.5). switch:
switch ()

245

, (int,
long char), ( ) , :
case :

break;
switch :
, ( ) ( case).

, , case, break.


switch, , .
, .
if ,
switch. country_code, switch:
switch(country_code):
case 1:
message.sayC"Thank you");
break;
case 34:
message.say("G i as");
break;
,
, . !
.
.
ttincIude "franca.h"
void mainprogO

//c4switch.cpp

Box display;
int country, area;
whi le(yesno("Shai I we continue?"))

&

246

10.

case 55:
//
351:
//
display,say("Obr i hado"};
break;
case 81:
//
display.sayC'Arigato");
break;
case 82:
//
d i spI ay.say("Gamzahamn i dah"};
break;
case 52:
//
case 34:
//
case 54:
//
display.sayC'Gracias");
break;
case 41:
//

switcn(area)
case 21;
//
case 41:
//
case 22:
//
display.say("Merci");
break;
default:
display.say("Danke");
break;
case 49:
//
case 43:
//
display.say("Danke");
break;
default:

display.say("Thank you!");

, :
switch, ( , ), . , .
{}.

247

(),
break. ( ) break, , break.
. , , break.
case, .
, . , :
case 51: message.say("obrigado");

, .
, , . default.
:
default:
message.say("Thank you");
)

,
break. ,

, switch
break. , ,

?

0 if.
if.. .else.
0 .

0 .
0 .
0 switch.

(recursion) ,
, ,
( ).
? , , . , ?
, .
, , , . ?
.
.


. , ,
. :
:
,
:

1+
\

- , ?
.

1+ , .
. , .
, , . . ,
, .
. ? ,
, , . .

250

jl.

. , ,
(, , ). (), ,
, . .


C++ , . :
int distanceO
if(Tracer.seewal|()) return 0;
Tracer.stepf);
return l+distance();
.
l+distance(). distance() ( ). ,
.

,
. , . :
int distanceO
{
iffTracer.seewal !()) return 0;
Tracer. step();
Tracer. say(1+di stance ( ));
return 1+distance();
, :
int f indwal I ()
!

251

int steps;
if(Tracer.seewal!()} return 0;
Tracer.step();
steps=1+findwalI();
Tracer.say(steps);
return steps;
, :
steps 1,2,3...
, . ?

__

^*-^^^^^^^^

^_^^^_^-*^_^*^_

_^-*.

__

_-*^^_^^

, , . , distanceQ . , . ,
, , ?
distanceQ ,
, distance() . !
c4distl.cpp.

?
,
, .
, , (, , ). , , .
findwaU(). ,
. , .


( ), steps 1. , . 2.
steps
l+findwalL(). . -

252

. , .
, steps . ( ).

. steps,
2. - ,
.
3.
steps
l+findwall(). .
. (, ,
.)
, .
, steps .
( ).


. steps
( ), 3.
,
0.
, . steps - ( ).


. steps 1+0, , ( 1).
, (,
).

. steps
1+1. , ( 2).
, , .

253


. . ?
:
steps findwaU(),
:
, .
, .
.
, , .
.
, , :
int distanceO

<

int steps;
if(Tracer.seewa!!()) return 0;
Tracer.step();
steps=l+distance();

//
//

race , say (steps);


Tracer.step();
return steps;

//
// !

, . . , . ?
-

, , , ,
. ! ,
.
, . (!
!)

^
.
i
^
^
^

^
^

^
^
^

^
^

^
^

^
^

^
^
^
^
^

^
^
^
^
^

^
^
^
^

^
^
^

^
^
^

^
^

^
^
*
!

, .
.
:

254

11.

int distance

{
int steps;
if(Tracer.seewal!());
{
Tracer,rlght();

Tracer.right();
return 0;
}
Tracer.step();

steps=1+distance();
Tracer, say(steps);
Tracer.step();
return steps;

//
//
//
// !

c4dist2.cpp .


.
, .
, , , , .
, , .


,
. ,
,
:
. , findwallQ steps
.
.

2j)5

, , .
. 1000 , 1000 1000 .
, , , , .


,
. , - . , .
: ,
. , ?
, ,
. ,
, ,
.
,
, . (
), ,
, !


,
c4seach.cpp, , .
, . ,

. , ? !
, , .
c4seach.cpp.
, ,

256

. ,
c4backl.cpp:
#include "franca. h"

//
Robot ( 1 ) ;
// c4back1 .
void explore1()
{
int derection;
//
if(yesno("Should I go on?"))
//
{
// yes,
do
// :
I
di rection=ask( "which way should I go?");
Tracer. face(di rection);
Tracer. mark(7);
}
// ,
// :
whi le(Tracer.seewa! !()); // , :
Tracer. mark( ) ;
//
Tracer. step();
//

explorelQ;

//
//
//
// !

void mainprogO
explore1();

}
.
, , c4seach.cpp
, yesno().
. ? ,
!
yesno() yes,
expLorel(). explorel(). ? 15 yes, ,
15 expLorelf).
distance, . -

HOBoroj/bi ?

257

.
?
,
, direction. direction . , , explorelQ (
). 15
distance , 14 . ,
.
?
,
?
, . , (
findwalLQ). He , .
, direction. To
,
. ? !

?

0 , .
0 .
0 .

12 . ,
.
Robot,

,
. :
( ) ( ).
( ) ( ).

.
, , , , ,
.
, (
). :
BoxX("Horizontal:"), Y { " V e r t i c a l : " ) ;


.
, ,
(1,1).
.
( ), .
, ,
.

260

12.___

:
:



- ? . .

. ,
(1,1) (4,9).
?
1 4 3 .
1 9 ~ 8 .
, {, },
(newx, newy):
newx-x .
newy-y .
? , ( ) ().


. , , , . , , :
v o i d move(int xsteps, int ysteps)

, , :
void moveto(int Scurrentx, int Scurrenty, int newx, int newy)

, , .

261

, - :
// ;
horiz=ask("horizontal:");
// :
vert=ask("vertical:");
// :
rnoveto(currentx, currenty, horiz, vert)
// :
X.say(currentx);
Y.say(currenty);
yesno(), :
whi le (yesno("Want to move?"})
mainprogQ .
.

movetoQ
movetoQ move() . , moveto()

:
void moveto(int &currentx, int Scurrenty, intnewx, int newy)
, , ,
move().
, newx-currentx newy-currenty .
move() :
move(newx-currentx, newy-currenty);
newx=currentx;
newy=currenty;
He ? !

move()
moveQ:
void move(int xsteps, int ysteps)
, xsteps , . ysteps.

262

12.

, , ( ) ( ) . .
void move( int x, int )
{
if(x>0)
{
Tracer, face(3);
Tracer, step(x);
}
else
{
Tracer. face(1);
x=-x;
if(x!=0) Tracer. step(x);
}
if(y>0)
{
Tracer. face(2);
Tracer. step(y);
}
else

//

// ,
//

//

Tracer. face(O);
y=-y;
if(y!=0) Tracer, step(y);

}
Tracer, face(3);
>
, , -, ,
-, .


, .
1 13.
, , :

263

do
horiz=ask("horizonta!(between 1 and 13):");
!
whi le((horiz<1):i(horiz>13);


c4move2,cpp:
#include "franca, h"
^include "robot.h"
Robot Tracer;
void rnovefint x, int y)

if(x>0)
{
Tracer. face(3);
Tracer. step(x);
}
else
{
Tracer. face(1);
x=-x;
if(x!=0) Tracer. step(x);
}
if(y>0)
{
Tracer. face(2);
Tracer. step(y);

// c4move2.cpp

//

// ,
//

//

else
Tracer. face(O);

--;

if(y!=0) Tracer. step(y);

I
Tracer. face(3);

264

__

12.

void moveto(int &currentx, int &currenty, i n t x , i n t y )


{
int stepsx, stepy;
//
stepx=x-currentx;
//
stepy=y-currenty;
movefstepsx, stepy);
//
current/ =currentx+stepx;
currenty= currenty+stepy;
}
void mainprogO
{
int currentx=1, currenty=1;
Box X("Horizontal:"), Y ( " V e r t i c a l : " ) ;
int horiz, vert;
for(; yesnoC'Want to move?");)
{
do
{

horiz=ask("horizontal(between 1 and 13):");

}
while((horiz<l) !; (horiz>13);
noveto(currentx, currenty, horiz, vert);
X.say(currentx);
Y.say(currenty);


, :
, $10.00.
, 10.00
$50.00.

265

, 50.00
S 100,00.

, $100.00.

, 50 .
^ ,
.

:
, 1 .
, -1.
, . .

. .

, . ,
-.

( 0 100).
,
25, 26 50, 51 75 , , 76 100. .

?

0 .
0 .
0 , .
0 .

V . .
, .
.

C++
Q
U

13 . , , .
. 14 15 .


. , .
, , . , , ,
, , , .

, . , 14 15
.



5. .
, , , . (type)
. . 8 ,
.


, . int long.
:

269

int . ( int) .

, , 16 . , -32768
+32767. , ,
.

Long ,
.
, , , .

,
.
32 . ,
-2147483648 +2147483647. long
,
int
^

,
.


, (, ),
int ( long),
unsigned. :
unsigned int ;

, , . int -32768
+32767, 0 +65535.
. 32767 .

270

13.


, , ,
short. :
short int c o l o r ;
. 8 , -128 +127.
unsigned short.


, . ,
/ .

float. ,
int, , ,
, . 3,4 10~38 3,4 1038
, ,
.
J

, . .

1.0000001,
.
(1.000000). . ,
234.56789 234,5678.

double.
float, .
.

271

,
double
Long. .

,
.
, . :
(literal constants) .
(labeled constants) , . , .
(enumerated constants) , .


,
. :
+ -.
.
.
,

.
j
j
)

, . C++
.
. , C++ 010
8.


C++ . :
+ -.

272

13. ^^

( ) .
.
:

35.
73.12
0.001
+54.3
-0.2

, . : ,
( ),
, , , .
, :
6.02x1023
C++ , 6.0223 6.02+23. .


. 3.1416, :
float pi=3.1416;
3.1416, , pi. , pi.
, const.
:
const float pi=3.1416;
pi. ,
, .
,
. , 3.1416 3.14159.
, , 3.1416! const

273

, .


, . , , , , , , 1, 2 3.
, :
int N=0, =1, S=2, W=3;

, :
const int N=0, E=1, S=2, W=3;

N, , S W. :
if (direction==N) ...
,
:
enum {_}
:
enum di rections {N, E, S, W};

, , int, , 1, 2 3.
, ,
.
, directions ().
, . , , :
directions comingfrom, goingto;

, :
comingf = N;
goingto ~ S;

,
( ), .
, :

274

13. ___

comingf = 8;
goingto = 2;

,
, . :
enum colors (red=1, green, pink=5, y e l l o w ) ;

red 1, green 2 ( ),
pink 5 yellow 6 (
).

,
int float. ,

i, 1. :
float x=0.5, ;
int 1=5;
= - i + 1;
, . , C++ (
) .
- , :
: , .
C++ . , , , . , , * ().
, , .
,
.
/. . ,
/ , .

275


-. ,
! , 3./2. (, ) 1.5, 3/2
( ) 1, . . .

, ,
, ,
:
int i , j;
1=30000;

j = i + i;

60 000 .
, , .


:
+

/
%

,
(=). :
=3*+1; // 3, 1
=+1*3; // 1 3,

, , C++, , . , + ,
3, .
:
*, / %.
+ -.

, , .

276

13.

,
.
:
3*(+1) // 1, 3
3*+1 // 3, 1
. . , 1 , , 4 3:
3*</(+1)+4);
, +1, +1+4.

, (=), ,
. ,
, ! ,
C++ , :
.
s=s+3;
s+3=s; //!

, , . , , , s 3,
3 :
:
if(s=3).. s 3, 3
s .
3 , if .


, : ==1;

=1 1, .
.


,
(convert) , . :

Int =1, j=2, k;


float x=0.5, ;
m=x;
0.5,
int, . - . :
==;

(
, ).
:
==;
, ?


.
, (promote) .
, float int,
int float .



, ( ,
int float). , float
int, (demote)
float, , .
. .

278

13^ ^

, x=(float)m/(float)j; , m j float1.


() , .
, , , .
, :
. , int, float, Long
. . void .
return,

return
return return,
. . return : ( ) .
, , , .
:
h=0.5gt2
h , g t .
g (9.81
), t h,
. ? float, , ,
.
height, :
float height(float t)
{
return 0.5*g*t*t;
i

0.5 1/2? ! 1/2, (1) i nt (2)


int. , int,
, . ,
float. , 1./2. , , 1
2. - 0.5.
1

,
(cast). . .

279

C++ .
, t t t
sqr(t), .

math.h

math.h. .
double
. , float, float .
. 13.1.
13. i. math.h

s i n ()

()

t a n ()

as i n )

acos(x)

at an {)

{)

I ()

( )

I 10 ()

sq rt ()

sq ()

f abs )

i I ()

f loor(x)
rand ()

,
(
)

280

1_3.


, .
ttinclude "franca, h"
^include <math,h>
void raainprogO
{

float value, root;


// :
value=ask(" Enter a positive value: "};
if(value>=0)
{

root=sqrt(value);
Coutroot;

I
else
//
Cout"Sorry, negative value! ";

?

0 .
0 .
0 math.h.

Q
D
Q

14 ,
,
-
, . , , . ,
.

Cirl.ce, Square, Stage


C++.
.

.
:
( ) ,
.
( ) .


, , ,
. , .
, ,
12, .

(0,0). ( ), ( ,
).

283


.
, , , .
. , 1024x768, 1024 768 .
. . 14.1 .
640

800

1024

480

VGA 640X480

SVGA 800x600
SVGA 1024x768
. 14.1.

, ,
.
(pixel).
, .

, ,
. ,
.

( )
.
,
.


.
franca.h. :
( 2).
.
.

284

14.


, .
:
. place(x, )
(, ). , balL.place(20,20);.
( , ). show() . ,
baU.show();.
. erase() ,
. , baLl.erase();. , , .
.
20 . .
, ball resize (40); . . , baU.resize(40, 30); 40
30 . resizeQ
absizeQ. , resize() ( absize()),
.
. color(). ,
. ,
, , baU.cotor(2);. ,
, balL.color(2,4);.
Box
:
. ,
message Box, message.say(Here!); Hereh. .
, . . message.labeL(Your change:); message,
say(change); . , change 12.45,
:
Your change:

12.45

, , -

285

say(). , . , , .
. 14.1 14.2 .
14.1.

place(float float)

show()

eraseQ

resize (float)

absize(float)

color(int int)

scale (float float)

origin(float float)

14.2. Box

label(Label)

say(Sentence)

,

,

resize(float)

,
place(), showQ erase().
Circle ,
20 . . ,
10 :
Circle myci rcle;
myci rcle. resize(10);

286

14.

, Square() ,
20 ( ).

. . ,
. :
0
1
2

4 -
5
6

7
,
:
enum(white, red, green, blue, lightblue, pink, yellow, black);
7, 7.

:

; (, ).
.


, .
. float,
.

(50,50) :

287

tfinclude "franca,h"
void mainprogC)
int x, y;
x=50;
=50;
i rcle myci rcle;
myci rcle.place(x, y);
mycircle.showO

He , ( ) .
cScircl.cpp, , . , (50, 100),
, . 14.2.
include "franca,h"
c5ci rcl.cpp
// ,
// :
void mainprogO
\ /
Box coordx("X:"), coordy("Y:");//
//
int x, ;
//
Ci rcle myci rcle;
//
do
>

// :
x=ask("Enter x coordinate;");
y=ask{"Enter coordinate:");
myci rcle. place(x, );
//
myci rcle.show();
//
// :
coordx.say(x);
coordy.say(y);

// :
while(yesno("Wanna try again?"});
}

288

14.
. ....

^ Pdulo Fiancd's l>+

100

. 14,2. c5circ1.cpp
coordx coordy Box.
coordx coordy .
, , .
:
x=ask("Enter x coordinate:");
y=ask("Enter coordinate:");
,
(50, 50), . 14.2. do.. .while:
do

whi le(yesno("Wanna try again?"));


? , .
, yesnoQ yes, .
.

289


, . ? erase().
, :
mycircle.eraseO;
?
, . , .
Clock ( Clock 2).

. eraseQ ,
, , , eraseQ , .
Clock, :
.

, . ( , ) .
)

(0, 0),
Object out of range
( ).

, . ? , .
, yesnoQ (1 yes,
).
J
:
int answer;
do

answer = yes("Wanna try again?");


myci rcle.eraseO;

I
while(answer);

290

14.


Object out of range, , 14.3. ( ) .

. 143. ,


cScircl.cpp , .
cScircl.cpp , ,
( ).
.
( cScircl.cpp) ,
.
(, ) ,
. (: .)
Clock, .


, ,
. , . 1.
, 30 . 30 ? ,
30 . , 1/30 ! , . , , ,
.
c5circ2.cpp, .
, .

291

, 300
=100 =100. .
include "franca, h"
// c5circ2.cpp
// :
void drawing(Cricle &mycircle, i n t x , i n t y , Box Scoordx, Box &coordy)
{

// :
myci rcle.place(x,y);
//
I I . show( ) ;
//
// :
coordx.say(x);
coordy.say(y);
}
void mainprogO
{
// :
Box coordx("X:"), coordy("Y:"): //
//
Int x, ;
//
Ci rcle myci rcle;
//
Ci rcle.mytimer;
//
int howmany;
do
{
// :
x~ask(" Enter x coordinate: ");
y=ask("Enter coordinate: ");
// :
howmany=ask("How many circles?");
for(intk=1; k<=howmany;
drawing(myci rcle, x, y, coordx, coordy);
mytimer.wait(0.033);
myci rcle. eraseQ;

// :
while(yesno("Wanna try again?"));

292

14.

c5circ2.cpp
c5circ2.cpp :
coordx
coordy
mycircle
coordx coordy ,
. mydrde , .
drawing() . :
, .
(, ).
.
.
:
.
.
, yesno() yes. , ( ). drawing() , .
,
Clock , drawing().
:
void drawing(Ci rcle &myci rcle, i n t x , i n t y , Box &coordx, Box &coordy)
{
Clock mytimer;
//
myci rcle.place(x, );
//
myci rcle.show();
//
// :
coordx.say(x);
coordy.say(y);
mytimer.watch(.033);
myci rcle.erase();

293

drawingO 0.033 , .
, waitQ eraseQ .
, .

,
.


c5circ2.cpp , , .
c5circ2.cpp ,
( 2 ).
c5drc2.cpp ,
.


.
.


. , .
, 100 150 .
, (100,150). (, )
, , , (, ) ,
.
(1, 1)
:
1 = -;
. 1 - +150;

, , (, /0~),
:

294

14^

1 = +;
1 = +;
, (, ), (+,+).


, , - . , .
. , 100 1000 (,
1000 100 ). ? 1000 100 10 , ,
, 10,
. ,
scaLex, scaley, :
2 = x/scalex;
2 = y/scaley;
) ,
. 1
10 . , 10, 12 18
.
, :
- (x+xO)/scalex;
= (y+yO)/scaley;
)

, ,

.


, ,
, ? ,

.

295

.
.
-1.
, , .
:
. ,
(50,400).
. . , , 60 . : scalex=l/60.
60
scaley=-l/ ,
.
cScircl.cpp .
,
:
mycircle.place(x, );
//
:
I I.!((+50)/60., (+400)/(-60.));
V

^^-W^

*_**^. i.^-**i __^^^^*.^**^^*_^_*-^^^.^^^4^_m.wi>*_^^^_-*-^_iii^^^^<

!
, , ,
!

, , , .
, .
, , , , . . , :
f I oat sea Iex=1./60., sea Iey=-1/60/;
float xO=50., yO=150.;
:
float newx(f I oat oldx)

296

J_4.

return(o!dx+xO)/scalex;
float newy(float oldy)
{
return(oIdy+y0)/scaIey;

}
:
mycircle.place(new(x),new(y));


cScircl.cpp , (50,400), 0,01 .
cSdrcl.cpp ,
.


, ,
. ..
scale(),
, .
. ,
, , ! , , , , , ,
.
, -
origin(). , :
Circle balI;
bal l.scale(1., -1.);
b a l l . o r i g i n ( 2 0 , 400);

, (20, 400), .
-
.
, .

297

, ! .
, ,
,
.

Grid
Grid
. Grid , . , (0, ff).


cScircl.cpp , , .



, ( )
( ).
. 14.4 . (, /), 0 .

'. .

. 14.4. ()

298

14.

. ,
.
, :
: -
: w -
:
= r*cos(w);
= r*stn{w);
0 ( )
(, ) (, ),
:
= r*cos(w)+xO;
= r*sin(w)+yO;
. theta, :
float coordx(float , float theta, float xO)
return r*cos(theta)+xO;
float coordy(float r, float theta, f l o a t y O )

{
return r*sin(theta)+yO;
:

void polarxyff loat r, float theta, f loat xO, floatyO,

float &x, float &y)


{
x= r*cos(theta)+xO;
y= r * s i n ( t h e t a ) + y O ;

}
.

( &). ,
.

299

?

0 .
0 : , .
0 , , .
0 .


Q

Q

, , , , , , .
, ,
. . . .
, .
.
:
1. .
2. .
3. .
,
.
,
Stage, ,
.

, ,
, .
y=f(x), ( ) (, f(x)).

302

15.

, =2-+1 =-5 =20.


C++:
float f(float x)

<
return x*x-x+1;

}
,
f(x), f().
, ,
.
, .


:
dot .
, (,
0.01).

dot .
dot .

C++ :
Ci rcle dot;
//
dot.color(7, 7);
//
dot. resize(2);
//
// :
for(x=xstart; x<=xend; x=x+0.01)
{

dot.placefx, f(x));
dot.show();

//
//

, . , :
0 640 ().
0 480 ().

303

, ,
. . ? . ,
.

drawQ
, . , :
void draw(f I oat xstart, f loat xend, int dotcolor=7)
{

C i r c l e dot;

dot.color(dotcolor);

//

//

dot.absize(4);
//
for(x=xstart; x<=xend; x=x+0.01) //
{
dot.place(x, f(x));
//
dot.showO;
//

:
void mainprogO
{
drawf-5., 20.);

}
sin(x).
. 15.1.
^include "franca. h"
// c5sin.cpp
include <math.h>
// sin(x)
float f (float x)
{

return sin(x);
}
void draw(f loat firstx, float lastx, intcolor=7)
(
float x;
I rcle dot;

//

304

15.

dot.color(color, color):
dot.absize(4);
for(x=firstx; x<=lastx; x-x+0.1)

//
//
//

{
dot.place(x, f(x)J;
dot,show();

//
//

void mainprogO
// ,
Grid mygrid;
mygrid.scale(20., -20);
mygrid.origin(50., 300.);
mygrid.resize(10.);
mygrid,showf);
const float pi=3.14159;
float xstart=0.;
float xlimit=8.*pi;
draw(xstart, xlimit);
.'! Paula Fianca i C++

Puc. 15.1. c5sin,cpp

305

draw() .
, , .

drawQ, :
. draw() ?
.
?
. draw() , ,
Grid. , . - ,
.
. ,
. , ,
. . , . ?
, , draw().


C++ . , , fl(x) f2(x) , draw(), '
. .
drawf() fl(x) f2(x) :
drawf(f1, xstart, xend);
drawf(f2, xstart, xend);
.
: (fl() f2()), (xstart) (xend). drawf().
draw() :
void'draw(float xstart, float send);
drawf() , . ? , 1 nt float.
? , .

306

15.


:
(, float, void . .).
(, func).
(, (float)).
drawfQ :
void drawf(f I oat funcff loat), float firstx, float lastx);
:
, float float. func.
. firstx firsty.
:
void drawf(f loat func (float), float firstx, float lastx)

I
float x;

i re I e dot;
dot.co!or(7, 7);
dot. resize(2);
for(x=fistx; x<=lastx; =+0.01)

//
//
//
//

{
dot.placefx, func(x)};
dot. show();

//
//

, func(x),
funcQ .
.


sin(x) cos(x).
sin(x) , cos(x)

307

. : sin(x) cos(x) math, h.


, . 15.2.
: Haulo Franco's IN

, 15.2.


. cScirc.cpp. ,
,


, , . .
,
. , ,
.

308

15.


, ,
x=dist(y) y=height(t). , .
, (t).


:
= speedx*t + xO;
= ;

speedx , , .
.


:
= speex*t + ;
= speedy*t + ;

speedx speedy
.


, :
= accx*t*t/2 + speedx*t + xO;
= accy*t*t/2 + speedy*t + ;
, ,
dist(t) height(t) , .
, ,
:
, (, ).
Clock, .
, (
1/30 ). :

309

1. .
2. .
3. .
4.
(1/30 ),
5. .
.


cSbody.cpp dist(t) height(t).
.
include "franca.h"
ffinclude "math.h"

// .

Grid migrid;
,,,
//
const float vOx=10., vOy=20.;
float dist (f loat t)
//
'
return vOx*t+10;
float height (float t)
,

//

return vOy+50;
void rnainprogO
//
// ,
Box boxxC'x:"), boxyC'y:"), boxt("Time:"),
mygrid.show();
//
float t;
//
Circle body;
//
body. resize(12);
//
body.color(3,3);
//
Clock timer;
//
Clock mywatch;
//
// , 15 ;

15.
// t
whi le((t=mywatch.time(})<15. )
(

//
// :
boxx.say(dist(t));
boxy.say(height(t)};
boxt.say(t);
// :
body.place(dist(t), height(t));
body . show( ) ;
//
timer. watch (. 033);
// 0.033
timer. reset();
//
body.erasef);
//

, , . , , .

Clock - watch ()
Clock mywatch timer.
(mywatch), (timer). timer 0.033 .
- watch() , waitQ, . wait()
, .
watch () .

while
while . , -, , time 15, -,
time t.
:
whi lefmywatch.
\
t = mywatch. timeO;

31

, , , , t, , while (
-, , ).
, , ;
whi le(t=mywatch.tiirie(}<15)
, ,
. time 15. 1, .
t , time 15, t 1.


, .
, .

. , , ,
!
, . -
.
:
0

oy

C++
= hO+vOy*t+0.5*g*t*t;
,,

, :
t=0.
vOy t=0.
(9,8 /2).
, vOy=0.
height(t) :
float height(f loat t)
I
const float hO=200., g=32.18;
return hO+Q.5*g*t*t;

312

.15.

, ,
(, hO, vOy g). , .
vOx vOy .

,
.

cSbody.cpp height(t), . ,
. , , .
, . :
while,
, 15 . :
whi le (((t=mywatch.time(t))<15.) && (height(t)>=0))
)

. ,
:
if (height(t}<=0) break;


cSbody.cpp , . ,
.


, .
, . cSbody.cpp, , dist(t) height(t) . ,
t !

3JJ3

? . ,
, . :
f l o a t x, ;
:
whi le (t=mywatch.time(})<15.)
x=dist(t);
y=helght(t);
//
// :
boxx.say(x);
boxy.say(y);
boxt.say(t);
body,place{x, );
//
}

,
:
boxx.say(x=dist(t));
boxy.say(y=height(t));
)


, .

(, 1./2).
, ,
. , 0.5.
. :
const float pi2=3.14159/2
, pi 2,
12.


, . .

314

15. _

theta. , :

velocx = veloc*cos(theta);
velocy = veloc*sin(theta);
, ,
dist(t) height(t). ( ),
.
:
const float g=32.18;
//
//
float dist(f!oat velocx, float t)

{
return velocx*t;

}
float heightffloat velocy, float t)

;
return velocy*t - 0.5 * g * t * t;

}
g .
theta
veloc. , velocx velocy., , .

, , .


cScannon.cpp . :
.
.
.
.
, , , ,

315

, .
.
, , , . height() dist()
. ,
, . , , , , !
, , . 1000 , ,
30 000 45 ( ). 90 ( ), 15 000 .
,
. ,
30 000 .

VGA
, 640x480 ,
, , , 400x400 . , 30 000 400 .
400/30 000 .
f >')

, . 30 000 400 .
, : 400/30 000
30 000/400? ,
,
, , (
)
.


. , 50 .
. . 0.033 ,
. .
cScannon.cpp.

316

15.

void mainprogO

// 1

Grid mygrid;
mygrid.origin(50.,420);
mygrid.seale(400/30000, -400/30000);
//
// ,
Box (":"), ':"), boxt("Time:"), Box vx("Vx:"), vy("Vy:");

mygrid. resize(30000.);
mygrid. show();
//
float t;
//
Ci rcle body;
//
body.absize(IO);
//
body.col or(3,3);
//
Clock timer;
//
Clock mywatch;
//
float theta, veloc, velx, vely;
//
theta=ask("Enter angle of f i ring:");
theta= theta*3.14159/180,;
//
//
veloc=ask("Enter i n i t i a l velocity;");
velx=veloc*cos(theta);
//
veIy=veIoc*s i n(theta);
vx.say(velx);
vy.say(vely);
float , ;
// 2
// , 15 ;
// t
timer. reset();
watch.reset();
whi le((t=watch.time())<50.)
y=height(vely, t);
if(y<0) break;
x=dist(ve!x, t);
//
// ;
boxx.say(x);

boxy.say(y);
boxt.say(t);

body.eraseC);
body.place(x, );
body,show();
timer,watch(.033);
timer, reset ;

//
//
// 0.033
//

317

. 15.3 15.5 ,
.
; Paulo Fianc.Vs C+

:
3993.21

/:
5312.89

:
9.9

692.82

. 15.3. 5.

. 15.4. 5,

318

15.

. 15-5. 5.


dist(t) height(t) ,
. , ,
. .
cScannon.cpp , ,
.



, . ? ?
, , :

3 19

().
().
().
().
.
L W, . 15.6. W. W L. L.

. 15-6.


? : .
, ( !).
Stage ( !).
C i r c l e head;
Square trunk;
Square left leg, rightleg;
Square leftarm, rightarrn;
, .
, , . , W L , armwidth armsize.
const int armsize = 20;
const Int armwidth = 6;

320

15.


:
heacf.color(5, 5);
trunk.color(5, 5);
!eftarm.color(3, 3);
rightarm.color(3, 3);
leftleg.color(3, 3);
right.color(3, 3);


:
head, resize(2*arrnwidth);
trunk, resize(armsize);
leftarm.resizefarmsize, armwidth);
rightarn. resize(armsize, armwidth);
I eft I eg. resize(armsize, armwidth);
rightleg,resize(armsize, armwidth);


:
head. pIace(x, y-armsize/2.-armwidth);
trunk.place(x ,y);
leftarm.place((x-(arrnsize+armwidth}/2.), y);
rightarrn.place((x+Cannsize+armwidth)/2,), y);
leftleg.place((x-(armsize/2.-armwidth), y+armsize);
rightieg.p!ace((x+(armsize/2.-armwidth), y+armsize);


:
head.show();
trunk.show();
leftarm.showf);

321

rightarm,show();

leftleg.show();
rightlet,show();
. Stage.

Stage
Stage .
Stage ScreenObj ( ). , , ,
.
Stage .
Stage, show()
, Stage.
,
Stage insert (), Stage.
j ^

Stage
Stage.

, . , :
.
.
.

Stage
Stage
, Stage,
Stage! ,
, , . ., :
Stage body;
body.place(x, );
Stage,
Stage !
, , Stage
( ):

322

15.

body.insert(trunk);
body, insert(leftarrn);
body, insert(rightarm);
body, insert(leftleg);
body, insert(rightleg);
Stage xl, yl,
:
body.eraseO;
body.place(x, );
body.showC);
, , .


15 . , ,
, .
,
, !
(),
.
, .
. ,
.
, , ,
365 , 365 . ,
, !
, .
)

, ,
365.25 ,
28 .

( Sun), ( Earth)
( Moon) ( Circle)
.
.
, ,

323

. ,
.


, Circle . :
Circle Sun, Earth, Moon;
, .
. 15.7. . , , , .

. 15.7. ,
:
const float earthradius=160., moonradius=4Q.;
const float earthsize= 20., moons i ze=8,, sunsize=40.;

He Stage
, Stage.

324

J5.

. Stage,
.
:
Stage universe;
universe. insert(Sun);
universe. insert(Earth);
universe. insert(Moori);
Clock, . .
, ,
, . Clock 0.033 .
Clock universal, clock stopwatch;

, .
, .

,
. , . .

Stage. :
universe.scale(1, -1);
universe.scale(25Q., 250.};


earthradius
, moonradius ,
:
: =0; 0;
: x=earth radius; y=0;
: = earthradius+moonradius; y=o;

:
const float earthradius=160., moonradius=40;
const float earthsize=20., moonsize=8., sunsize=40.;
Stage universe;
universe.origin(250., 250.);

325


universe.scale(1., -1.);
// ;
Ci rcle sun;
float xsun=0, ysun=0;
sun,resize(sunsize);
sun.color(6, 6);
sun.place(xsun, ysun);
universe, insert(sun);
//
Ci rcle earth;
float xearth= earthradius, yearth=0;
float wearth=(2*pi)/365.25;

//
//

//
//
//
//

earth, resize(earthsize);
earth.color(5, 5);

earth.pIace(xsun+earthradius, ysun)
universe, insert(earth);
//
i rcle moon;
float xmoon= moonradius, ymoon=0;
f I oat wmoon=(2*pi)/27;
//
moon.resize(moonsize);
moon.color(1, 1);
moon.place(xsun+ earthradtus+moonradius, ysun);
universe, insert(moon);


,
.
.
:
1.
2.
3.
4.
5.

( universe Stage).
.
.
,
.

6. .
7. .
8. .

326

15.


? ,
365.25 , .
, :
w = 365.25/360 ;
w = 365.25/(2*pi) .
( wealth)
( thetaearth) ( time):
thetaearth = wearth*time;
, ( earthradius)
.
xearth yearth, polarxy:
polarxy(earthradius, wearth*time, xsun, ysun, xearth, yearth);


. ,
- . polarxy, :
polarxy(moonradius, wmoon*time, xearth, yearth, xmoon, ymoon);
:
for(;time<365.25;)
{
universe.erasef);
polarxy(earthradius, wearth*time, xsun, ysun, xearth, yearth);
eath. placefxearth, yearth);
polarxy(moonradius, wmoon*time, xearth, yearth, xrnoon, ymoon);
moon.place(xmoon, ymoon);
day.say(time);
universe.show();

stopwatch,watch(.033);
stopwatch, reset0;
t i me=s i de I.t i me{)*timescaIe;

327

timescale,
. 10 timescale , . ,
365.25 ( ).

cSstars.cpp.


cSstars.cpp ,
. .
cSstars.cpp , .

?

0 , .
0 ,
.
0 Stage, .
0 .

VI

, ,
. VI , ,
.
. .

Q
U ,

LJ
Q

, 1.
.
, . , athlete up, ready
. . clock reset, time, wait . . , ,
. . . 16.1 , -. (messages), -
(member functions) . -
.

. 16.1. , -

-, ,
. - , - -. , , -
.
.
(class) , . ,
, , , . (object) . , , , .
, .
16 ,
. , .

331


( athlete),
, -:

ready()
()

IfiftQ

right()

athlete. ,
:
Sal. readyQ;

, ready(), -, :
readyO;

, Clock - readyQ, :
myclock. readyO;

, -,
, , . . 16.2 athlete -.
athlete

ready();
up();
leftO;
rightO;
, 16.2. - athlete

, , . , , . ,
. ,
, .

332

16.

, . . , . , . Square (, ),
Circle. , , -
Circle Square,
.
, ,
-. Clock , ,
, .
timestarted,
( ),
. - timeQ timestarted , - reset()
timestarted.
- . , int, float, long . . , , athlete, Square, Circle, .
- -, . , ,
Circle (, ) , (,
) . - (, ,
). ,
. - - ,
. , - .
, clockl cLock2 Clock, ,

timestarted,
. , Clock,
timestarted ,
reset(), time() wait().


, . , Clock.

333

.
Clock :
, .
( , ).
reset(), time(), wait() watchQ.
.
( ).

Clock, , (
) .

Clock
Windows , :
timeGetTimeO;
,
mmsystem.h.
, ,
.
timestarted, - Clock :
void reset()
t i mesta rted=t i meGetT i me();
}
float tirtieO
<

// , ,
// 1000 ( 0.001)
retu rn(t imeGetT i me{)-t i mestarted)*.001;
}

waitQ .
( tsec),
, tsec.

334

16. ^

willbe.
, wilLbe.
void walt(f I oat tsec)
{
float wi 1 1 be;
w i I lbe=tirne()+tsec;
whi le(time()<wi I ibe;

// tsec ,
// ,
//
// tsec

, - timeQ waitQ. ,
. , - . - time() , .


- time() , waitQ Clock,
timeQ.

, - ,
.

, BigBen Clock:
Clock SigBen;
, wait() BigBen:
BigBen.waitdO);
, waitQ time(), , time() BigBen.
. (, waitQ timeQ), ,
. mywatch? timer? BigBen?
, .
Clock,
timestarted long - waitQ, timeQ resetQ.
-, ,
, -, .

335

- waitQ
60 :
void wait(float tsec)
float w i I Ibe;
i f (tsec>60) tsec=60;

//
//

wi Ilbe=time()+tsec;
whi le(time()<wi IIbe);

g5 )

, while willbe.
, timeQ wilLbe. .
.


, ,
reset(), ti me() wait(). ,
, .
:
(class declaration) , (-) (-). - -

.
(class definition) -.
:
class _
{

-
-
};
// !
) ,
class struct.
20, .

336

16.

- - ,
! .
.
:
.
- , .
- (prototypes) . , ,
. .
- -
.

Clock

Clock.
class Clock //******* ***.*********** lock
{

long timestarted;
pub! ic:
float time();
void wait(f loat tsec);
void reset ();

//,
// tsec
//

class ()
, . ,
( ), .
- Clock timestarted Long.
,
.
-: time(), waitQ reset(). , .
, ()
.
( tsec
waftQ).

J37

, , public,
.


,
-. , -,
, ( ).
, . C++ wait() wait() Clock.
.
, , , , - .
, . . (qualification)
, . :
_ :: _ (_)


:
Clock :: walt(f I oat tsec)
Clock:
void Clock :: reset(}
{
t i me rsta rted=t i tneGetT i me();
}
float Clock ;: time()
{

II , ,
// 1000 ( 0.001)
retu rn(t i meGetT i me()-tImesta rted)*.001;

>

void Clock :: walt(f loat tsec)


{
float wi IIbe;
if(tsec>60) tsec=6Q;
wi Ilbe=time()+tsec;
while(time()<wiIIbe);

//
//

16.

338

Cj)BET


. Clock, . , Clock clock. , mmsystem.h.

,
, ( ) , . .


(public) -
, , .
Clock reset(), time() waft().
, Clock .


(private) -
. . . , , . timestarted ,
resetQ, timef) waitQ, Clock.

-
. ,
( ), . ,
, , ,
.


(protected) , - ,
. public, private protected -

339

,
. , private. public protected,
. ,
. Clock -
, public,
private protected .

, Clock .
, Clock timestarted
. ( ) ?
, . C++ ,
(constructor).
-, :
. , Clock ClockQ.
. , mine yours Clock, ClockQ
mine yours.
. , mine,ClockQ;.
.
( void). . ,
Box .
, box x, y(Y:);
. Y:.

. , ,
.


.
.

340

1_6.

, . ,
. .
CLock :
I 3SS U I Q C K / / * * * * * * * * * * * * * * * * * * * * * * * * * * *(j [

protected;
long timestarted;
t

publ ic:
ClockC);

float t irne( );

// ,

void waitff loat tsec); // tsec


void watchffloat tsec); // ,
// tsec
void reset();
//

};
, . :
C l o c k : : Clock()
t i rcesta rted=t i meGetT i me( ) ;


. .
,
. Clock
, Clock.
, .
, . ,
() .
, ;
_ __1, __2, . . . ;

:
Clock timer, mywatch;
athlete Sal ;

( ) , -

341

.
, , , , .
, :
__1, __2, , . . ;

:
float x, ;

,
. ,
,
.


( ) . .
- athlete. ,
ready(), sal:
sal.ready();
-. , timestaeted - Clock,
:
bigben.t imGstarted = mirie.t imestarted;
timestarted ,
, - Clock.

-
- ,
. -
, ,
- - - , . - - -, .


,
. wagon (). -

342

1j6. ^

. , . 16.3.

. 16.3.

wagon - move(),
. , , ,
, .
wagon:
class wagon
I
Circle frontwheel, rearwheell;
Square body;
float x, ;
//
publ ic:
wagon();
void move();
};
// He !
-: frontwheel, rearwheell ( ), body () .
, , .
, - move() .


,
. , ,
, .
wagon ( -):
const int red=1, green=2;
wagon : : wagon()
{

// :
x=60;
y=200;

// :
frontwheel. resize(20);

343

rearwheel.resize(2Q);

// :
body.resize(2G, 80);
// :
frontwheel.color(red);
rearwheel.color(red);
body.color(green);
// :
body.place(x, );
rearwheel.place(x-25,y+10);
frontwheel.place(x+25,y+10);

// :
body.showO;
rearwhee!.show();
frontwheel.show();
}
void wagon :: move()
{
// :
body.erase();
rearwheel.erase();
frontwheel.erase();
x++;
//
// ;
body.place(x.y);
rearwheel.place(x-25,y+10);
frontwheel.place(x+25,y+10);
// :
body.showO;
rearwheel.show();
frontwheel.show();
}

wagon ,
. eraseQ, place()
show(), .

Stage
Stage
, .
c6train.cpp:

344

16.

ffinclude "franca.h"
// wagon
cetrain.cpp
/,/ 2.0, Stage

int const red=1, green=2;


class wagon

{
Stage rai I road;
Ci rcle f rontwheel , rearwhee
Square body;
float x r y;
publ ic:
wagon();
void move();

// Stage
//
//

}; // He !
wagon : : wagon()
=60;

//

y=2QQ;
rai i road.placefx, );

frontwheel . resize(20);
rearwheel . resize(20);

//

body.resize(20, 80);
frontwheel .color(red);
//
rearwheel ,co[or(red);
body.color(green);
body.place(x, y);
//
rearwheel . place(x-25, y+10);
frontwheel . place(x+25, y+10);
// Stage
rai I road, insert (rearwhee I );
rai I road, insert (frontwheel );
rai [road, insert(body);
}
void wagon : : move()
{
//
rai Iroad.eraseO;
++;

//

rai I road.piace(x, y);

//


raiIroad.show();

345

//

.
Stage. ,
,
Stage, .


moveQ , Stage, .
:
void mainprogO
int i ;
wagon front;

Clock station;

f ront.moveO;
station.wait(.033);
wagon caboose;

for(i=1; i<=200;
f ront.moveO;
caboose. move( };
station. wait(. 06);

// ,
// wagon
// ,
// Clock
//
//

// .
// wagon.
//
//
//
//


- athlete.
- , . :

346

16.

Sal. readyO;
- ready() SaL, athlete. -. , c6train.cpp, wagon, - :
front.-0;
front, rearwheel. resize(15);
cabooze.y=10;

//
//
//

,
- . ? , -
wagon . - public ( ), .
(60, 200). , - .
:
.
.
- .


, . , .
. ? , !

wagon :
publ ic:
,
. :
front.=120;
front.y=100;
caboose.=60;
caboose.=100;
, . :
front.=180;
front. move I

347

, ,
( ), . :
front.move();
front.x=2QO;
front.move();

, , . ? , ?
, - :
caboose.xz1;
caboose.y=z2;

, ? ,
zl z2 (,
- -432
221.345).
. - moveQ ,
. , move() ( ) , , erase()
, !
.
- -,
, .
, , . ,
, -. ( ).

...

, , -, , .
, ,
, . . , . ,
, - .
.

348

16.


, .
:
wagon : : wagon ( int xO, int yO)

{
=;

(
),
.
wagon front {120, 220);
wagon caboose (60, 220);


(default constructor).
, .

-
.
, . - .
~-~
>f)

__

_-

--

--

_..

_-

wagon
. !
17, , wagon Stage, , .


wagon , .
.
Clock (, Clockl) , markQ -

^?

349

. ,
, :
Clock mytimer;
for(tnt 1=1, i<=20, i++);

{
mytimer.mark(5.);

}
pair .
- readyQ, up(), LeftQ right(). ,
( pair) .

- CLock::wait(fLoat), willbe. Clock::tirne().


wago
. .

?
**
0 .
0 , .
0 .
0 .
0 - - ,


. .
, . ,
.
, , , . , athlete
ready, up, left right. ^ .
.

wagon.
, :
, , ,
.
. .
.
.
. .
,
. . !
, , ( ) .
( ) , ,
. , , ,
( - ) , . .

- . , , ,
. C++ (base class), , , (derived class).
, , ,
, -

352

17. __

. : locomotive, wagon, timer, Clock.


. , .
: , , (inherited) .
, locomotive, , . , ,
, , wagon.
timer - markQ,
.


,
:
1. , ( -, -, ). , .
2. -.


, -, ,
, -, . :
I ass __: pub I i ,
{
};


// !

public , .
timer:
class t i m e r i p u b l ic Clock
void mark(float time);

joiacco

353


- :
void timer: : rk( float time)
{
watch (t ime);
reset();
timer - -, . resetQ, timeQ, watch{) waitQ
. .
, , , !
, .


-
, . ,
. ? : , , ,
! ( ,
).
, , . . , , . ,
, , .
, , , . :
.
.
,
.
.
, . , , ,
, .
, , .
, , .
:

354

17.

, , , . .
.
.


. 17.1 , :
. .

. 17.1.

,
. , , . ,
.
: . , , .
, , , . , , , .
, ( )
( ). , , .

^ ^

355

, .
( )
. Circle
ScreenObj.


, , .
. ,
(instance) .
, . . , , , ,
, , , . !
, , .
, . , ,
.
.
. , : , , , , , .



. ScreenObj , , , . Box
ScreenObj. , Box ScreenObj. ,
ScreenObj, Box. , , Box
. :
,
:
. , Box, , ScreenObj. athlete, , Stage.
, ,
. , Box , .
ScreenObj. athlete : , , , .
Stage,

356

17.

,
. , Box Label say, athlete
ready, up, left right.

, , .
, .
.
. 17.2 ScreenObj Circle Box. ScreenObj , .
Box Circle .

ScreenObj
-

resize

COtbr

"... place
./:;.: sfjow
I
erase

label
say

show
" , erase

. i7.2. ScreenObj Circle Box

, ScreenObj
Box. , Box,
-, , ScreenObj. , Circle -, , ScreenObj.


, ,
, . 17.3. .
. , ScreenObj
Circle, Square, Box Stage, Stage
athlete. , , athlete
Robot (, ).

357

^__1

Circle

Square

Box

Stage

PMC. 17.3.

C++ (parent class) (child class)


(ancestor class) (descendant class).
.
(inheritance). ,
/ .
, .
, Circle ScreenObj, , ScreenObj,
.
J

,
.


ScreenObj, Circle . , , (. . 17.2), - showQ eraseQ ( ). -
ScreenObj?
,
? showQ ScreenObj? , , .
, , ,
. showQ ScreenObj !
, , ,
, . ,
show().

358

17.

show()? ,
? . ,
show() Circle, , .
(polymorphism), .

show() ScreenObj
, . , -.

athlete
athlete Stage, , , ScreenObj. ScreenObj
, .
, z
.
- ScreenOfaj:
protected:
f loat , , z;
int colorbrush, colorpen;

//
//

float size, lenght;

- , , , , -
Stage athlete.


athlete :
runner , ,
. , .

skater , .
,
.


. , , ,
, stepleftf) steprightQ.

359

. 17.4 , . , , , ,
. 17.5, ,
.

, 17.4, ,

. 17.5.
steprightQ ( ). stepLeft() .
, , .
, ..
armsize armwidth franca.h,
, :
//const int arnisize=2Q, armwidth=6;
void runner::step ight(float time)

//,h

Clock any;
eraseO;
I eftarm.resize(armsize/2, armwidth);
leftarm.place((x-(armsize+armwidth}/2.), y-armsize/4);
rightleg.resize(armsize/2, armwidth);
rightleg.place(x+(arrnsize/2.+armwidth), y+3*armsize/4);
show();
any.wait(time);
>

void runner; :stepleft(fI oat time)


{
Clock any;
eraseO;
rightarm.resize(armsize/2, armwidth);
rightarm.place((x+(armsize+arrriwidth)/2.,), y-armsize/4);

d>

360

17.

left leg. resize(armsize/2, armwidth);

leftleg.place(x-(armsize/2.+armwidth), y+3*armsize/4);
show();
any.wait(time);

, , , ():

,
, , erase() , .
, , , .

. (, , Stage), - placeQ,
. , , .
.
, .
- run():
vo id runner: : run()

stepleft(.l);
erasef);
ready(.l);
stepright(.l);
erase();
ready(. 1);

JOIOCCOB

361

eraseC);
place(x+1, );
ready(0.);

, . - place() , Stage, (
place (}) (
).

++
, .

runner :
class runner: pub i ic athlete
{
protected:
v o i d stepleft(f loat);
v o i d stepright(f loat);
publ ic:
voi d run( );
};
// He !
- stepleftQ steprightQ (protected),
,
- (, skater).
skater :
class skatenpubl ic runner


, stepLeftQ steprightQ . runner. run () , ,

362

1_7.

, ! ?
, - run(),
skater, , . (polymorphism).
( runQ), , .
run() . :
void skater;:run()

(
for(int i=1; i<=5; i++) // 5
{ //
stepleft{.1);
erase();
place(x+1, );
}
ready(0.);

for(i=1; i<=5; i++)


// 5
{ //
steprightf.1);
erasef);
place(x+1, );
}
ready(0.);

I
:
void mainprogO
{
runner jane;
jane.place(2Q, 80);
skater J u l i a ;
int k;

for(k=1; k<=1G; k++)


jane.run();

}
, ?
, ( skater
runner). - run(),
runner, skater. ,
, skater , runner. -

363

run () runner. , juLia skater


runner, :
for(k=1; k<=10; k++)
Julia, runner: : run():


- . ,
, , . ,
. ,
. .
, , , .
.
, . , ,
. . , ScreenObj !

Stage
Stage ,
,
Stage. ,
(, Stage). Stage
, ,
.
.
, ,
runner:
void march( runner volunteer)
volunteer. run();

, ,
smith , yamaguchi .
runner smith;
skater yamaguchi ;
march(smith);
march (yamaguchi );

. march()
runner, . -

364

17.

( ), .

smith yamaguchi. ! march() , run() runner.
run(), , (virtual function).
() runner virtual:
v i rtual v o i d run();
, run() runner,
, , .


- skater::run() , , ,
( ).
, ,
. ,
runner skater, ,
.
, . , ,
( athlete), (
runQ)?
JumpJack() , athlete.
runner skater.
, . C++ (
). JumpJackQ
run skater, athlete?
goaway(runner someone).
runner, , 10 .
runner, skater. .
skater , .
.

365

skater Stage,
( insertQ) skater
.

wagon , ,
, ,
wagon ScreenObj. Stage wagon. Stage wagon .

?

0 .
0 .
0 .

. . 18
:
.


, ,
9.
, 9 (
c3sale2.cpp).
. ,
, , . .
9 c3sale2.cpp.


. . , , . ,
. , , .
, .



, .

franca.h. ,
. , , .

368

18.

, , .
.
.
, . , .
!


.
, , . , , , .
:
void mainprogO

terminal cashregister;
cash register.operate();

I
,
.
, , .
,
. , -,
.


,
c3sale2.cpp. ,
.
,
. . ,
.
!


terminal . , :
class terminal
protected:
float tax, amount, price, saletotal, change;
Box Price, Cur_Total, Saletotal, Amount, Change;
void itemsQ;
public:
terminal();
void operateO;
, c3sale2.cpp . .
.
J

.
, .
terminal::terminal()
{
tax=ask("Enter the sale tax %")/100.;
Prise.place(450, 100);
Prise.label("Prise:");
Cur_Total.place(450, 140);
Cur_Total,IabeI("Subtotal:");
Saletotal.place(450, 180);
SaIetotaI.IabeI("TotaI SaIe:");
Amount.place(450, 220);
Amount. I abe I ("Amount Tendered:");
Change.place(45Q, 260);
Change.IabeI("Change:");
. 18.1 .

370

18.

Price:
12.00
Subtotal:
12.00
Total Sale:
12.99
Amount Tendered
20.00
Change:

. 18.1,

- operateQ
- operateQ ,
. , , . .
.
, , , , itemsQ. - c3sale2.cpp. , - items().
.
.
operate () ;
void terminal; :operate()
{
float tendered, change;
for(;;)
saletotal=0;
Price. say(" ");

jepMHHgng

37^1

Cur_Total.say(" -);
Saletotal .say(" ");
Amount. say(" ");
Change. say(" ");

items();
Saletotal .say(saletotal);
a[fiount=ask("Enter amount tendered:");
Amount. say(amount);

change=amount-saletotal ;
Change. sayCchange);
if(yesno("Another customer?")) break;

,
. - items(),
.

operateQ
: Change ( )
Box change ( ).

- itemsQ
itemsQ, terminal, , getitemsQ
c3sale2,cpp. ,
.
itemsQ :
void terminal : : items()
{
for(;;)

{.

price=ask("enter the price:");


sa 1 etota I =sa I etota 1 +pr I ce ;
Price. say(price);
Cu r_Tota I . say( sa I etota I ) ;
if(yesno("Another item?")) break;
saIetotaI=sa1etotaI *(1+tax);
SaIetotaI.say(saIetotaI);

372

18,

-.
. , , .



c6term.cpp c6term.h,

c6term.cpp
c6term.cpp:
tfinclude "franca. h"
include "cGterm.h"
void mainprogO

\i

terminal cashregister;
cash reg i ster . operatef ) ;

c6term.h
c6term.h:
ffifndef C6TERM_H
ffdef i ne C6TERM_H
^include "franca.h"
class terminal
{
protected:
float tax, amount, price, saletotal, change;
Box Price, Curjbtai, Saletotal, Amount, Change;
v o i d itemsQ;
pub I ic:
terminal();
void operateO;
};
terminal::terminal()
.{
tax=ask("Enter the sale tax %")/100.;
Prise.place(450, 100);

373

Prise.label("Prise:");
Cur_Total,place(450, 140);
Cur_Total.label("Subtotal:");
Saletotal.place(450, 180);
Saletotal.label("Total Sale:");
Amount.piace{450, 220);
Amount.label("Amount Tendered:");
Change.place(450, 260);
Change.label("Change:");
void terminal:; itemsO
for(;;)

price=ask("enter the price:");


sa I etotaI=saIetotaI+p r i ce;
Price,say(price);
Cu r_TotaI.say(saIetotaI);
if(!yesno("Another item?")) break;
saIetotaI=saIetotaI *(1+tax);
SaIetotaI.say(saIetotaI);
void terminal : ;operate()
float tendered, change;
for(;;)

saletotal=0;
Price. say(" ");
Cur_Total . say(" ");
Saletotal .say(" ");
Amount. say(" ");
Change, say(" ");

itemsO;
Sa 1 etota I . say ( sa I etota I ) ;
amount=ask("Enter amount tendered: ");
Amount. say(amount);

*&

374

18.

change=amount-sa I etota I ;

Change. say(change);
if(y6sno("Anoth6r customer?")) break;

Send if


cSstars.cpp, 15,
. ,
(), () () .
, . , , .
, , , , .
, . .
cSstars.cpp, .
,
, , , (, ).
, :
, .
.
.

.
,
. move(). ( )
.

375

satellite
satellite:
class sate! I ite:publ ic Ci rcle

protected:
float xplanet, yplanet;
float radiius, omega, wspeed;
publ ic:
satel I ite();
moveO;
i.
- : (xplanet yplanet), (radius), (omega) (wspeed).
.
, ( ). , ( )
, .

.
, , , .
,
move().
, .
, .
-?
,
.
, , ,
. , :
-.
- -.

376

18:

- (, ,
) .
, .
- :
.
.
.


, cSstars.cpp . polarxyQ,
. :
class sate! I i t e : p u b l i c Ci role

<
protected:
float xplanet, yplanet;
float radius, omega, wspeed;
void polarxy(f loat r, float theta,
float xO, floatyO, float&x, f loat &y);

pub I ic:

sate 1 1 ite();
void center(f loat x, floaty);
void dist(f loat rad);
void angle(f ioat ang);
void speedff loat ws);
vo id move();

void centerfsatel I ite Sanother);


centerf). (, ) .
polarxyQ,
. , , , .

377


, , .

:
satelI ite::sate I Iite()
{

color(2);
xplanet=yplanet=0;
radius=0;
omega=wspeed=0;
}

. ,
, colorQ Circle .


:
v o i d satei I i t e : : center ( f l o a t xc, f l o a t yc);
{

xplanet=xc;
yplanet=yc;
}
void satel I ite;: center (sate! 1 ite &another);
{

center(another.x, another.y);
}

- ?
? ?
, ! . - satellite, Circle, . , ,
-.
satellite
center(), ,
, :
Hoon.center(Earth);

378

18.

-
, :
void sat el I ite: :dist(f loat rad)

{
radius=rad;

}
void sate 1 1 ite: :angle(f loat ang)

(
omega=ang;

f
void sate 1 1 ite; :speed(f ioat ws)
I
wspeed=ws;


, :
void satel I ite: :move()
{

omega=omega+wspeed ;
polarxy(radius, omega, xplanet, yplanet, x, y);
\

, ? . placefx, ). move()
.


satellite ( c6satell.h),
cSstars.cpp c6sat.cpp,
:
const float pi2=2*3, 14159;
Stage universe;
Clock timer, sidereal ;
satel I ite Sun, Earth, Moon;
Sun. resize(40);

379

Sun.origin{320, 240);
Sun.scale(1. -1.);
Earth. dlst(120);
Earth.speed(pi2/36.5/30.);
Earth. center(Sun);
Moon. resize(12);
Moon.dist(80);
Moon.cotor(S);
Earth. color(4);
Moon. speed( pi 2/2. 8/30.);
universe. insert(Earth);
universe. insert(Moon);
Sun.showO;
:
for(; sidereal .time()<36. 5; )
Earth.moveO;
Moon.center(Earth):
Moon.move();
universe. show();
timer. watchf. 033);
timer. resetO;
universe. erase();
?
, ?
, !
, ,
.
. , ?


?
.
,
. , 10 , 10 , ! , . .
!

380

18.

?

0 .
0 .

VII

. , . , .
VII , . , , . , .

Q
Q
Q

, , . , .
, , ,
.


(array) , . ,
, ,
: , , ...
, . ,
, , , . 19.1, .
; Pauto Fiance's f>+

t t f t t
Julia

Andrea Rlcardo

Andy

Michael

Puc. 19.1.

384

19.


:
athlete Jul ia, Andrea, Ricardo, Andy, Michael;

:
JumpJack(Julia);
JurfipJack(Andrea);
JumpJack(Ricardo);
JumpJack(Andy);

JumpJack(Michael);
, , , , ? ?
,
, , , ...
:
Guy
(index) 1 5:
Guy
, , ,
, , Guy !
. 19.2 .

Guy[OJ Guy[1] Guy[2] Guy[3] Guy[4]


. 19.2. Guy,

:
void mainprogO
{
athlete Jul ia, Andrea, Ricardo, Andy, Michael;
JumpJack(Jul ia);
JumpJack(Andrea);
JumpJack(Ricardo);
JumpJack(Andy);
JumpJack(Michael);
I

385

void mainprogO
{
athlete Guy[5];
//
for (int which=0; which<=4; which++);

JumpJack(Guy[which]);

//


( ).
, ?
, :
void mainprogO
;
runner Guy[EJ];
//
for (int whicn=0; which<=4; which--);

{
Guy[which]. run();

//


: , , , ,
int float C++
(int) . , 0, 1 . .
Guy :
Guy[0]
Guy[l]
Guy[2]
Guy[3]
Guy[4]
Guy[5] , !
S
^

^ ^ ^ ^ ^ ^ ^ ^ ^^^

: N
N,
, , . 0 N-1.
.

386

J 9. jy\a


, . , :
int points[10];
points 10
( 0 9).
measurements 12 ( 0 11):
float measurements[12];
. , , . ,
10 :
points
()
:
points[1]
, :
Guy[which] Guy.
which, .
6[/1-1] .
. Guy[which] which 0 4 . . C++ 1.


. . , . ,
List 10 ( 0 9). , :
1

,
. ..

387

iist[(k-3)*j>0;

k j (
0 9 ). ,
, k j.
,
, , -13.
, , List[-13], , , . !


, , int float.
, .
:
void mainprog()(

int value[10];
//
for (int index=0; index<=9; index++)
{
value[index]=9-index;
//


? ?
1?
, 9-index value [index]. ,
,
.
, , :
void mainprog(}(

{
int value[10];

//

for (int index=0; index<=9; index++)

4>

388

19.

value[index]=9- index; //

}
Box Sa I ;
for (which=0; which<=9; which++)
{
Sal.say(value[which]);
f

, ? , .


. ,
, . , :
athlete Guy[10];
:

JumpJack(Guy);
JumpJack(Guy[1j);
! , ?
JumpJack() athlete, Guy (
athlete), Guy[l]l
JumpJackQ , ,
.
JumpJackQ .
:
JumpJack(Guy);


, . :

_ _ _ . _ _ . _ . _ _ _ _ _

389

void jumpjack( athlete eachone[10], int howmany_athletes)


{

for(int i=0; i<howmany_athletes; i++);


{

eachone[i]. readyO;
eachone[l].up();
eachone[i]. ready(0.);

Jump3ack()
. , 10, howtnany_athletes.
, eachone . , . , . (- ),
( ) ,
C++ .
, eachone , , .
, :
vo i d j umpj ack(ath I ete eachoneC ] , i nt howmany_ath I etes)

(
),
.

c7jack.cpp
, , c7jack.cpp.
//

c?jack.cpp

//

//

31.07.1994

include "franca.h";

390

9.

athlete Guy[7];
void JmpJackfathlete somebody)
{
somebody. up();
somebody. readyO;
}
void mainprogO
<
for (int i=0; i<7; i++)
JumpJack(Guy[j]};
Guy[i].say("Done! ");

, : . :
.
.


, ,
. , . ,
, , .
, . , . 28 ,
, , , . .
, :
int student_age[28];
28 , 32,
. ,
. ,
, :
sum=0;
for(int i=0; K28; i++);

sum=sum+student_age[ i ];
}
average=surn/28;

39J

, 28 32?
, ,
!
, ,
. !

, , ,
.

.
int number_of_students;
. ,
, . :
int student_age[100];

:
for (int i=0; i<nurnber_of_students; i++);
{

sum=suiri+student_age[i];
>
average=sum/numberj)f_students;


. , . ,
, :
athlete Guy[n];
. , ,
, . ,
( ). , .
,
. . , :

392

19.

const int arraysize;


float prices[arraysize];
, arraysize , .


cTjack.cpp , . :

Hi.

(0,1, 2...).


, . int, Long, float double.


. :
, ?
, .
, , 1, 2...
, . ,
.
, :
0 ().
1 ().
2 ().
: , , . ,
, . . 19.3.
:
athlete Guy[3][4];
, Guy ,
.

393


( )


( )


{ )

19.3.


. 19.3, ,
. , ,
. , ,
.
( . 19.3), , . , Guy[2][3] . . 19.4
.

-.,
-. 1
-.

1- -

. 19.4.

394

19.


, ,
, .
, . ,
.


. .
. ( ), ( 1) . .
int values[10]={9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

9 [0], 8
[1], 7 [2] . .
:
.
.
.
:
int matrix[2][3]={{0, 0, 1}, {2, 3, 4}};
,
. :
matrix[0][0] = 0;
matrix[0][1] = 0;
matrix[0][2] = 1;
matrix[1][0] = 2;
matrix[1][1] = 3;
matrix[1][2] = 4;

,
, 0 2. 1,
0 2.

395


, . :
athlete guy[5];
int age[5];
, .

7.
7..
//
.
//
//
31.07.1994
tfinclude "franca. h"
athlete Guy[5];
void malnprogO
(
int age[5];
for (int i=0; i<=4; i++)
{
Guy[i]. readyO;
Guy[i].say(i);
age[i]=Guy[i].ask("What is my age?");
}
for (i=0; i<=4; i++)
{
Guy[i].say(age[i]);


, athletes. age. ,

396

19.

. . ,
age.


age. .
ask(), .


, , .
, . ,
, . , :
Box result ("Average;");

int n;
//
float value, total ;
n=ask(" Input number of elements"};
total = 0;
for( int i=1; i<=n; i++);
total=total+ask("lnput a number");
total = total/n;

result. say(total);
. .

.


, :
float matrix[5][3], prices[5][3], cost[5][3];

397

typedef float theusual[5][3];


theusual matrix, prices, cost;
.
theusual , . typedef
.
:
typedef _ ;


7. , .
. ! , , Guy [Q], Guy[l] . ., . , .

, oldest ( ).
,
oldest,
. , , ,
oldest . , oldest
, , ,
.

. .
float average(int array[], int from, int to).
, from to.
int less(int array [], int from, int to, int value). , value, from
to. , 21, 32, 15, 80, 75 43,
: x=less(array, 1, 4, 50), 2.

398

VpQKj ^


.
, . . 10 ,


,
. :
.
.
.
,
.
:
void mainprogO

int values[10];
getarrayCvalues, 0, 9);
showarrayfvalues, 0, 9);

//

}
getarrayQ (
) showarrayf)
( ).
.
, .

getarray()
getarray() ,
. , , :
getarrayO;
. n u mber
, mainprogQ, . , -

399

10 0 9. . , , , . :
.
number. !
, . showarray(). , , . ,
, .


getarray() .
, .
:
void getarray(int number[], intfrom, intto)

{
for(lnt i=from; i<=to; i++);
<

number[i ]=ask(" Input a number");

C++ , , &
, , .


showarray() .
:
void showarray(lnt numberf], intfrom, intto)

{
Box list[10];

400

19.

for(int i=from; i<=to; i++)


{
list[i%10].place(400, 50+(1%10)*40)
list[i%10].label(i);
I ist[i*10].$ay(number[i]);

,
10 .
, 31 40,
31 1, 32 2 . . 40 ,
10.
. 19.5 , 15 24 .

i Paufo Fianca's > *-

21
/
22
8
23
9

10
15
1
16

17
3
18
4
19
5

. 19-5. 10

, . , .
,
.
.

401

, , c7intfun.h.


.
,
. ,
, :
findlargest(values, from, to);
, , .
\ ?
:
23,12,45,11,89,21,32,55,81,32
, , 89.
, 4 ( ,
C++ ).
, . ,
,
.
, .


?
,
.
. ,
.
, ,
, . .
( ).
, .
, . , ,
().

402 _

19.

:
int f indlargestfint number[], int from, int to)
int indox;

int guess;
guess=from;
//
for( i ndex=f rom; i ndexoto; i ndex++)
{
if(nuinber[fO!jnd]<number[ index])
{
guess=index;
//
}
}
return guess;
}

? guess . , ,
(from). , ,
number[guess].
. index . from,
, to, .

from+1. ,
numberfindex] number[found],
index found . , from to ,
.

, (numberfguess]), (numberfindex]), .
, guess (index). , , guess
. , guess
.
findLargestQ . , ,
. -

403


.
void mainprogO

Box largest(50, 350, "Lagest:");


Box index(50, 400, "Pozition:");
int values[10];
int k;
getarray(values, 0, 9);
showarray(vaiues, 0, 9);
k=findiagest(values, 0, 9);
lagest.say(values[k]};
index.say(k);


,
.
. , , , . ,
. .
, , , sort() . :
sort(values, from, to);
sort() !
sortQ .
, ? , Larger, last, ,
, number [Larger] number [last]. , .
? 0 8 .
f1ndLargest(),
0 8 . .

.
, , !

404

19.

;
void sort( int array[], int framwhere, int towhere)
{

int lagest, temp;


// last
for (int !ast=towhere; last>fromwhere; last--)
{
//
// last
largest=f indlargest(array, fromwhere, last);
// last
//
temp=array[ largest];
array[ largest]=array[ last];
array[last]=temp;

:
void iriainprogO
{
int values[10];
getarray(values, 0, 9);
showarray(values, 0, 9);
sort(values, 0, 9);
showarray(values, 0, 9);


. ,
:
=;
=;

, .
, . , , , . , , temp:

^_ .405

temp=a;
a=b;
b=temp;


, . .
, - .
- .


sort():
. , . ,
,
. .
? ,
sort() . , ,
sort() .
:
void recsort(int array[], int fromwhere, int towhere)

//
int largest, temp;
int last;
last=towhere;
//
if(fromwhere<=towhere)
{

//
// last
largest=find!agest(array, fromwhere, last);
// last
//
temp=array[largest];
array[!argest]= array[last];
array[last]=ternp;
//
recsort(array, fromwhere, last-1);

406

?

0 .
0 .
0 .
0 .

_] 9.__

Q
Q
string.h
,
G

C++ ,
.
. ,
, , .
, .
, . , .

C++
.
,
.
, , .
, .

char
char , . ,
initial:
char initial;
char . . , , . , initial ,
( ):
initial

C++

409

. :
char choi ~ ;
, . .


, , , :
char choice;
if (choice==N) break;
choice N ( ). choice N, break.

. :
char bIank= ;
blank char,
.


C++ (int, Long,
float . .),
0 255. , , . , 'N' , 78, '' 65, '1 97 . . , ,
. .
0 255, ,
.
. ( ) .
, ,
0 255. , 256 . , -

20.

, , . , , .
ASCII (American Standard Code for Information Interchange, ).


, :

int i , j , k ;
f l o a t x, ;
char a, b;
:

i=j;
a=j;
=;
f o r ( a = a ; a< = z ; a++)

b-b+3;


,
.
( )
(strings). , C++
. !
, . :
char first_name[20];

. :
f i rst_name[0]=a;
f i rst_name[1] = n ;
fi rst_name[2]=first_name[0];

first_name ana.
:
char I ast_name[2Q]=; { f ,

r, a, n, , };

411



, , , , , ? ,
, ,
-. ,
, .
C++ , ,
. C++ , . null.
, C++. ,
, ,
( 0).
Lastjiame , :
char last^narne[20] = { f , , , , , a, n u l l } ;
char I a s t _ n a m e [ 2 0 ] = { f , , , , , , 0};

,
, 0.

,
C++ :
char last_name[20]="franca";
, . , , . null , .
, null,
(null-terminated string).
,
. ? , , . ,
, , .
, :
S a l . say( last__name);
,
.

412


C++ ,
, . ,
20 :
char name [5][20];
, , . , name[0], name[l], name[2],
name[3] name[4], . . ,
20 :
for (int i=0; i<=4; i++)
askwords(naine[ i ], 20, "Enter a name");
, .
.
name[0], name[l] . .
:
typecfef char onename[20];
onenarne name! ist[5];

// onename
// namel 1st onename
// 5 20

for (int i=0; i<=4; it+)


askwords(namelist[i], 20, "Enter a name");
. ,
char f i r s t _ n a m e [ ] = " M i t i ko";
[] , ,
first_jiame 7
( null). , . , , .


askwords{), franca.h. , :
askwords(char inputstring[], i n t m a x s i z e , char message[])
, .

4j_3

, .
include "franca. h"
void mainprogO

// cTnames.cpp

athlete player[5];
char name [5][20];
for (int 1=0; i<=4; i++)
askwords(name[i], 20, "Enter a name");
for (i=0; 1<=4; I++)
{
player[i]. ready(0.);
player[ i ].say(name[ i ]);


, , . , :
char agent[]="Bond", cl ient[]="FI int";
:
if (agent=="Bond") . . .
cl ient=agent;

. string. h. , C++. ^include:
include <string.h>

string, h, .
. :
strcmpQ ( ).
stricmQ ( )
.

414

20.

strcpy() .
strcatQ .
strlenf) .


si s2:
int strcmp (char s[1], char s2[]);
si s2,
.
, 0. si
s2, .
:
char clty[20];
Cincity;
if (strcmp(city, "Cupertino")==0)
Cout"Ygu l i v e in good town!";
city
Cupertino. ,
You Live in good town!. , , , Cupertino, , .
stricmp().


si s2:
int stricmpfchar s1[], chars2[]);
strcmpQ, .


( source)
( dest):
strcpy {char dest[], char source[J);
. , , .
:
char name1[>"Brandon", name2[]="Daisy";
strcpy(name1, name2);

415

namel Daisy.
( namel name2) , name2 , namel.


( source) ( dest):
strcat (char dest[], char source[]);
:
char message[20]="The exit "
strcpy(message, "is near!");
message The exit is near!.


:
int strien(char string[]);
(null).
:
char sentence[80]="He that shal I persevere to the end . . . " ;
Cout strlen(sentence);
38,
sentence. , !
( n u LI). . , .
.

sizeof ()
, C++ ,
. sizeof().
sizeof() ,
, , . , . ?

416

20.

, . ,
.
, ?
char I isten[]="He that sha! I persevere to the end, he shall be saved.";
.
, , , , ,
. ,
.
, , , , 40 , 50. 0 39
, ,
. :
char array[40];
for (int i=0; i<=39; i++)

, :
char array[40];
for (int i=0; Ksizeoffarray); i++)

,
0,1,2,3,4,5,6,7,8 9 ,
, ,
(), . C++ ;

atoiQ - () .

atof (} - () .
itoa () - () .
stdlib. h, :

#include<stdl ib.h>


, () number:
int atoi(char number[])

417

:
char number[2u];
int k;
askwords(number, 20, "Enter the value");
k=atoi(number);
Coutk;
,
.



, () number:
float atof(char number[])
,
.


value number:
itoa(int value, char number[], int radix)
radix .
, 10.
:
int k=25;
char number[20];
itoa(k, number, 10);


, , . , .
, , i
], , k. 1, , 0 .

418

20. ^

, N,
N , .
,
m .

(structure) / , . , :



, (field), .
, , . .
,
, (record). , , . .
,
, . , , , , . .
- . , . .

, ,
-.

, class struct:
struct

<

};


// !

419

:
struct student

{
char lastname[20];
char f i rstname[20];
long enrol lment_number;
,
. ,
.

. ,
. , customer

student:
IV
JV
student customer;
. :
customer. enrol lment_number=924Q54;

, .


. , student firstname
lastname .
. :
struct adress
char street[40];
char city [20];
char zipcode[10];
,
, . , . :
struct employee
{
char lastname[20];
char firstname[20];

&

420

20.

long salary;
address home;
}

, , :
employee person;
strcpy(person.home,street, "221 Baker St.");
strcpy(person.home.city, "London");


employee
address. , , , .
struct employee;address
char lastnarne[20];
char fi rstnane[20];
long salary;
};


, . :
employee staff[lOO];
student group[30];
100 , ,
employee. 30 .
city , , . :
staff[10].home.city


, ,
. , :
employe worker"!, worker2, staff[100];

_ _

421

, , :
worker~l=worker2;
staff[5]=worker1;
workerl.home=staff[1].home;



. , ,
.

. , null, ? , strlenQ ?
, , ,
null. , , , , .
, , . .
:
char name[50];

strlen(). :
forfint position=0; position<50; position**)
{
if (name[position]==0) break;
position .
, 50.
, null, ,
, t.


. , :

422

20.

struct student
int idnumber;

char lastname[30];
)
student myclass[50];

myclass 50 . , .

00

5002

Rogers

01

6754

Smith

02

6003

Adams

03

6532

Rodriguez

19

6021

Arentz

, . 6021?
- , .
idnumber.
, :
for (int position=0; position<50; position++)

if(myclass[position], idnumber6021) break; // !

}
, , 19 , , lastname 19 .
:

myc!ass[position].lastname


?
, break .
- position . .
, position, 0 49. position 50.

423

.
/ , .


inputstring thischaracter.
inputstring , athischaracter . , thischaracter
inputstring. , .
, 10
.
. .

?

EI .
0 .
0 .

JT

0 .
0 .


(
9) ( 18). :
.
.


, , ,
. , ,
, - . ?
. , .
.
? , !

( ) - .
.
, ,
.

426

21.

, :
struct product

{
int code;
char description[20];
float price;

I;
product list[20];

. ,
. ()
, .
catalog. .
. ?
!

catalog
catalog , . ,
, ,
. :
struct product

{
int code;
char description[20];

float price;
};
class catalog

{
protected:
product list[20];
int Iistsize;
publ ic:
catalogO;
vi rtual product find(int part_number);


- , ( ).
, .
-, find(). ,
. findQ .

find(), ,
. .

:
catalog::catalog )
I istsize=0;
for(l istsize=0; yesno("Another item?")&&(I istsize<20); listsize++);
I ist[l istsize].code=ask("Enter product code:");
strcpy( 1 ist[l istsize].description, "Product deeription");
askwords(I ist[I istsize].description, 20, "Description;");
Iist[listsize].price=ask("Enter the price:");

findQ , :
product catalog: :f i n d ( i n t partjiumber)
for(int item=0; item<l istsize; item++);
if(part_number==l ist[item.code) return list[item];
product nonexistent;
nonexistent.code=0;
return nonexistent;
,
, ,
. ,
item, item.

428

2]

catalog c7catalo.h
.

saleterm
saleterm terminal. , . :
class saleterm:publ ic terminal
protected;
catalog parts;
product sale;
Box Part_Number, PartJ)escription;
void items();
publ ic:
saletermf);
terminal , , . .
, (, ), (Part_Number) (Part_Description).
itemsQ . , , .
, .
- operateQ , terminal, , .

,
. ( !) , . ,
, . , .
, .

429

:
saleterm;:saleterm()
{

Part_Number.place(450, 300);
Part_Description.place(45G, 340);
PartJJumber. label ("Part_Nu(Tiber:");
Part_Description.label("Description;");
items():
v o i d saleterm;: iterns()
{

Int somecode;
for(;;)
{
do

somecode=ask("Enter part number:");


sale=part.find(somecode);
if(sale.code==0) yesno("Wrong part number, please check");

}
whi le (sale.codes=0);
PartNumber.say(saIe.code);
Part J)escr i ptI on.say(saIe.descri pt ion);
saIetotaI=saIetota1+sa1e.pr i ce;
Price.say(sale.price);
Cur_Total.say(saletotal);
if(!yesno("Another item?");
}
saIetotaI=saIetota1 *(1+tax);
Saletotal.say(saletotal);

}
, c7term.h c7term.cpp
, :
8i fndef C7TERM_H
ftdefineC7TERM_H
ttinclude "franca.h"
include "c6term,h"

//c7te rn.h

^>

_ 2 .

430

Sinciude "c7catalo,h"

^include <string.h>
class saleterm;publ ic terminal
{
protected:
catalog parts;
product sale;
Box PartJJumber, Part_Discription;
void itemsf);
publ ic:
saleterrnO;
};
saleterm: ;saleterm()
{
Part_Number.place(45Q, 300);
Part_Desc r i pt i on . p I ace (450 , 340) ;
Part_Number. label ("Part^Nurnber: ");
Pa rt_Desc r i pt i on . I abe I ( "Desc r i pt i on : " ) ;
i

void saleterm: : items()


I

int somecode;

for(;;)

do
somecode=ask( "Enter part number: ");
sa I e=pa rt . f i nd ( somecode) ;

if(sale.code==Q) yesno("Wrong part number, please check");


}
whi le (sale,code==0);
Part_Number.say(sale.code);
Part_Descr i pt i on. sayfsale. description);
sa I etota I =sa 1 etota I +sa I e . p r i ce ;
Price, sayfsale. price);
Cu r_Tota I . say ( sa I etota I ) ;
if(!yesno( "Another item?");
I
sa I etota I =sa I etota I ( 1+tax) ;
Sa I etota I . say( sa I etota I ) ;

#endif

43_1

//
include "franca. h"
// cTterm.cpp
# include "cTterm.h"
ffinclude <string.h>
void mainprogC)
{
saleterm cashregister;
cash register. ope rate ();

j ,
?
, ? ,
.


. ,
18, ,
. .
(, electron [n]), .
, .
satellite electron, 10
satellite. ,
( satellite).

,
. ( c7atom.cpp) ether.
:

432

21.

const float pi2=2*3.14159;


Box clock("Tlme:");
Clock timer, sidereal;
satelI ite electron[10];
sate 11ite nucleus, ether;
nucleus.resize(4Q);
ether.place(320, 200);
nucleus.center(ether);
nucleus.dist(80);
nucIeus.speed(p i 2/800.);
nucleus.move();


.
:

for (int i=0; i<2; i++)


{
electron[i].centernucIeus);
electron[i].dist(80);
electron[i].speed(pi2/300.);
electron[i].resize(12);
electron[i].color(5, 5);
electron[i].angle(i*pi2/2.+p12/4.);
elect ron[i J.moveO;

}
;
for (int i=0; K10; i++)
{
6lectron[i].center(nucIeus);
electron[i].dist(120);
eIectron[i],speed(p i 2/600.);
electron[i]. resize(12);
electron^i].color(5, 5);
eIect ron[i].ang i e(i *p i2/8.);

electron[i].move();

I
, :
nucleus.showC);
for(; sidereal.time(}<20.;)

433

nucleus.erase();
nucleus. moveO;
nucleus. showO;
for(int i=0;

electron[i].erase();
eIectron[I].center(nucIeus);
electron[i].move();
electron[i].show();
}
clock.say(sidereal ,tirne()*10);
timer.watch .033);
timer, reset();
, . , , electron [10].


c7pool.cpp pool,
,
.
* ,
( , . 21.1).
( ), . , (, ) .
, , . 15, 10 .

. 21.1,

434

2j. ^^^
c7pooLcpp:

^include "franca.h"
tfinclude "math.h"
#includ6 <stdl ib.h>
const int bai lradius=10. poolradius=15;
const int poolx=400, pooly=200;
class pool:publ ic Stage
{
protected:
Square table;
Ci rcle b a l I ;
float xb, yb;
//
float incx, incy;
//
float speed;
pub I ic:
poo I);
void shootffloat angle);

};
poo I::poo IC}
{
float angle;
sp6ed=1,5;
table. resize(poolx, pooly);
table.place(320, 240);
table.color(2);
ball.colorfO, 0);
balI.resize(balIradius*2);
table.showf);
insert(table);
insertfbalI);
xb=rand()%(poolx-bal !radiusH320-poolx/2.-*-bal I radius;
yb= rand )%(poo I y-ba 11 rad i us)-*-240-poo I y/2. +ba 1 1 rad i us;
bal l.place(xb.yb);
ball.show();
}

void pool:; shoot (f loat angle)


{
angle=3.14159*angle/180.;

435

incx=cos(angle)*speed;
incy=sin(angle)*speed;
Clock cuckoo, timer;
for(;cuckoo, time()<30; )
{
1

if((xb<=(320- poolx/2.+bal I radius)) . \


(xb>=(320+poolx/2. -bal 1 radius))) incxincx;
if((yb<=(240- poo iy/2,+bal I radius))!!
(yb>=(240.+pooly/2.-bal I radius))) incy=-incy;
xb=xb+i ncx;
yb=yb+incy;
bal I .place(xb, yb);
table. show();
bal I .show();
timer, watch(. 033);
timer. reset{);

void mainprogO

{
pool b i l l iard;
float angle;
for(;yesno("Take a shot?"); )

{
angle=ask("input the angle: ")
b i l l iard.shoot(angle);

?

0
.
0 .

VIII

. , .
/, . VIII / / C++. /.
,
. . VIII .
, . : .

franca.n

franca. h, ScreenObj,
athlete, Clock, , C++ . ,
, , , , .
, - Windows1. , , .
, , , , .
,
. , C++ .
, ,
.

, -
, franca. h.
C++ , , Windows. .

, C++
/. ,
, ,
.

Windows
,
. Windows,
. . .

C++

439

C++
. , ( ) C++ .
.
, ,
, , , !
.
,
, , ,
.
, !
^include <iostream.h>
Sinclude <iomanip.h>
void main()

//cScngrat.cpp

{
char yourname[30];
cout"Hel lo! "<<end"What is your name?";
cin>yourname;
coutendl;
cout"Congratu I at i ons, "yourname;
cout", you are now a programmer!";

}
, :
franca.h , , , ,
, . . , , ask(), yesno() . .
iostream.h
iomanip.h. , .
)

franca.h iostream.h !

440

22.

_.

void mainprogQ, main ().


. ,
.
J I , .
- , .

. (, .)
, . . , ?

, :
, . , .
, File | Open
.
, .
Microsoft . ,
.

/ C++
/ C++ :
. , / C++
, C++.
/ C++,
.
. C++. , , (cout), .
number,
:
coutnumber;

/ C++

44J

(cin), ,
. . number
:
cinnumber;

cin cout input ( ) output


( ),

, C++
=. , (coutnumber), (cinnumber).
, Cin Cout
franca.h.

. , .
, , , ,
, , , .

iostream.h
, franca.h
iostream.h. :
^ i n c l u d e <iostream.h>

.

.
iostream.h
cin cout.
/ C++. :

int
char
float
double

442

22. frgncg.h

, / .
. .
, , :
, /
. :
char name[] = " A l f r e d E. Newman";
coutname;
iostream.h ,
cin cout.


cout, ( ):
cout _;

:
coutniimber;

,
():
cout"The result is: "<<number" and that is f i n a l " ;

. :
int number=32;
char name[]="Sonny Bonds";
coutnamenuinber;

:
Sonny Bonds32

.
, ,
:
coutname;
coutnu riber;

C++

443

,
:
coutname" "number;
:
cout"Customer Name: "<<name" code is: "number;


.
: \ ( ) endl.
:
cout"Customer Name: "<<name\n" code is: "number;
:
cout"Customer Name: "<<nameendl" code is: "number;
:
Customer Name: Sonny Bonds

code is: 32


cin, ,
:
1_;

i:
1 n i ;


, :
,
, :
#include<iostream.h>
#include<iQirianip.h>
vo i d ma i n ( )

&

444

22. franca. h

int i, j, k;
cout"Enter three values:\n
coutendl"Your values are: "<<:\n;
couti" "<<j" "k;

,
.
,
/ endl, \.
, .
,
(, Enter three values: \), (
cout ).
, . . ( ), .
:

Enter.
( ) .
(, ).

/
, . , , . showf).
void showfint array[], int from, int to)

coutend I "Contents of array: "<<endl "lndex"" V a l u e " ;


for int k=from; k<=to; k++)
coutendlk " "<<array[k];

445

: , . , .
readarray():
void readarray{ Int array[], int from, int to)
{
for (int i=from; i<=to; i++)
{
coutendl"Please input element insdex "<<i: ;
cinarray[ i];

, ,
. ,
, , .
, ,
. ,
. , .
cSshow.cpp:
void tnaint)

//cSshow.cpp

{
int number[10];
readarray(number, 0, 9);
show(number, 0, 9);

. . ,
.
(formatting). ,
, .
, .
, .

446

22. fronco.h


, ,
. :

#include<iomanip.h>
void main()
!
float price, tax=6.75, total;
cout "Enter the price:";
cinprice;
totaI=pr ice+tax*pri ce/100;
coutendl "Please pay: $"totai;

, .
10 , :
Please pay: $10,675

. . ?
.


/ .
. , :
Contents of the array:
Index Value

2
.

5
6
7
:

;.
;
.

23456
23
-4567
3
23
-21005
32
21
6789

447

, .
, . , ,
,

/
iomanip.h , / .
endl, (. 22.1).
22,1. iomanip.h

endl

ends

null

flush

setiosftags(long flag)
resetiosflags(Long
setfilL(charfiLlchar)

flag)

/
-

setprecision(int places)

setw(int width)

setiosflags, resetiosflags, setfUln setprecision


, . , - , ,
-. setw .
, .
, endl? ,
, showQ:
void showC'mt array[], int from, intto)
{
coutendl "Contents of array: "endl "lndex"" Value";
for (int k=from; k<=to; k--)
coutendlsetw(3)k "
"<<setw(8)array[k];

448

22. franca.h


Microsoft
1$::1 ios::right , , :
setiosflags{ios::left)
, , :
resetiosflags(ios::left)
Borland
.

setw
show() setw. k , , .
.
.
, ,
( ). setw(O), .

setprecision
. , , . setprecision.
setprecision. ,
, :
float price;
coutsetprec i s i on(2);
coutprice ;
, , cout, . ,
.

449

,
. , . , :
float x[3]={12.35, 10., 5.};
coutsteprec i s i on(2);
for(int i=0, i<3, i++);
coutendsetw( 8)x[ i ];

:
12.35
10
5

, , .
setiosftags.
, /
, , () . , setiosftags.

setfill
( setw) , , ,
. - .
.
setfill. , setfill. -, setfill(' ').
,
- *:
cout"US$"<<setf i I l ( * ) v a l u e ;
, , , - .

setiosflags resetiosflags
setiosftags resetiosflags /
. . 22.2.

450

22. franco.h
22.2. iomanip.h

skfpws

left

right

showpoint

scientific
fixed

()

setiosflags, ios:: . ,
resetiosflags . , skipws ( ) :
i nreset i osf Iags( i os:: skI pws);

skipws
skipws . ,
. , ,
.
:
#include <iomanip.h>
vo i d ma i n()
{

char mark[]="

+";

char name[30];

cinname;
coutname;
}

, , Sonny Bonds,
Sonny, .
skipws:
cinresetiosf lags(ios:: ski pws);

451

skipws ! (, . .)?
, C++
. , skipws, . :
ftinclude <iomanip.h>
void main()

//cSskipws.cpp
const char enter=10;
char mark[]="
+";
char name[30];
i nreset i osf I ags( i os:: sk i pws);
coutmarkmarkmarkendl;
for(int i=0; i<29; i++)
<
cinname[ i ];
if(name[i]==enter) break;
}
name[i]=0;]

coutendIsetw(5)("characters read";
coutendlname;

skipws . 30 , ,
Enter.
Enter ( , 10).
resetiosflags, ,
. , Enter
, 30 .
mark ,
, .


, ,
, . -

452

ypoKJ2.

franca.

( , , ), , .
:
#include<iomanip.h>
vo i d ma i )
{

charname[5][20];
int code[5];
forfint i=0; i<=4; i++);
coutend!"Enter a name:";
cinname[i ];
coutendl"Enter a code:";
cincode[ i];
for(i=0; i<=4; i++)

<

coutend lsetw(20)name[ i ]setw(6)code[ i ];


. :
Clarice
Liz
Lucia
Marcos
i audio

12
4532
435
21
43


.
, . :
include <iomanip.h>
void main()
{

char name[5][20];
int code[5];

453

for(int 1=0; i<=4; i++);


coutendl"Enter a name:";
cinname[i];
coutendl"Enter a code:";
cincode[l];
for(i=0; i<=4; i++)
coutend i set i osf I ags( i os:: I ef t)setw(2Q)name[ i ]
reset i osf lags(ios:: left)
set i osf lags(ios:: right)setw(6)code[i ];

, , :
Clarice
Liz

12
4532

Lucia

435

Marcos

21

I audio

43

,
Microsoft.

showpoint
showpoint . , setprecision. showpoint
(
, setprecision).

scientific
scientific () .

fixed
fixed scientific .

454

22. jargnoBciHHbig franca.h


20 char , ASCII 0 255.
, , ,
:
^include <iostream.h>
void rnainf)

//cSascii.cpp

{
//
char code, choice='y';
int number;

while(choice=='y')
{
coutendl"Input the code you want to know:";
cinnumber;
code=number;
coutendl<x"This code corresponds to the charcter: "code;
coutendIX<"Do you want to continue(y/n)?";
cinchoice;

, ,
. , code
, number .
, , , . code n u m b e r
65, number 65,
code .
, (code) .

, : code=(char)number;. , .

ask(), askwordsQ yesnoQ


ask(), askwordsQ yesno(), a Box / . -

455

Windows /
, ( ) .

nofranca.h
nofranca.h . ,
, :
include "nofranca.h"
franca.h.
C++,
. .
, FRANCA_H
_CANVAS_H. franca.h,
nofranca.h.

nofranca.h
nofranca.h, ;
ftifndef NOFRANCA.H
ffdefineNOFRANCA_H
Sdefine FRANCA_H
#define_CANVAS_H
include < lost ream, h>
include <string.h>
include <stdl ib.h>
include <iomanip.h>
float ask(char question[])
{
float answer;
coutendl"Asking:"question;
cinanswer;
return answer;
}

void askwords(char sentence!], intsize, char question[])


{
coutend I "Ask i ng(sentence): "quest i on;
cinsentence;

&

456

22. franco.h

int yesno(char sentencet])


I
char answer;
coutendl"yes or no: "question;
coutendl"Please enter or n:";
cinanswer;
if(answer=='y) return 1;
return 0;
class Box
char title[40];
char message[40];
publ ic:
Box();
Box(char alabei[]);
void say(fI oat);
void say(char msg[]);
void label(int);
void labelfchar msg[]);
void placet int x, int y);

void Box::Box()
strcpy(title, " ");
strcpy(message, "");
void Box:: Box(char msg[])
strcpy(title,msg);
void Box:;place(int x, int y)

void Box: :say(f loat value)


I\
coutendletitle;
coutendlesetprecision(2)setiosf lags( ios: :showpoint);
coutsetiosf lagsfios: :f ixed)value;

void Box; :say(char msg[])


{
coutendletitle;
coutendlemsg;
}
void Box: : label (char msg[])
i
strcpy(title,msg);
;.

void Box: : l a b e l ( i n t value)

itoa(value, title, 10);

}
ttend i f


cSascii.cpp ,
(),
, . 0 255, , .
, .
10 2 .

?

0 franca.h.
0 /,
0 /.

23 . C++.
. ,
.
.



, . , . , ,
.
. , ,
, :
.
(
), ( ).
,
. / cin cout,
. .
( cin) (
cout). ,
fstream .
- fstream
. , /
, cin cout.
!

460

23.

ffinclude<fstream.h>
void main()

{
fstream myf i le;
myfi le.open("l Ist.txt", ios: :out);
myf i le"Anything goes";
rnyf i le.closeO;
tist.txt,
Anything goes.
fstream.h.

Hst.txt .
, .


:
.
.
,
(endl). .
, .
, .
, , , , .
.
.
, , , .


fstream,
:
fstream ;

46J

,
, !
- ().
- fstream. - :
()
close ()
eof()

- openQ
- open () fstream
. :
, , . . ():
open(char _[], int ^);

: (_),
, (_), .

. , , . . \

\\.

:
ios::out ( ).
ios::in ( ).
ios::app ( ).
ios::nocreate , .
:
// I ist.txt,
// \franca, :
myf!le.open("a:\\franca\\!ist.txt", ios:: in);
// ,
// "_", :
myfi 1.(_, ios::out);

462

23.

,
(|). :
_.open("roster.txt", ios::nocreate!ios:: in);
( ios::in) roster.txt.
, .

Microsoft . ,
ios::nocreate. Borland ( } .


, . ,
, .
Microsoft Borland -.
Microsoft - is_open(). , , . is_open() :
_.("81", ios:; in! ios: :nocreate);
^.. is_open()==0) cout"Oiun6Ka ";
Borland
fstream NULL. , , . :
_.open("roster", ios::in! ios::nocreate);
if(Bau)_(|>alta==NULL) 1"0 ";

- closeQ
- close() . , . .

- eof()
- eof() . 1, ,
( end-of-file eof).
eof() ,
. ? eof.

463


c8persnl.txt . , .
, , , .
:

1
2
2

1
,0

Clark Kent
Alfred Newman
Oliver Twist
Huck Finn
James Bond
Saint Nick

.,,-,

20.00
6.00
15.00
17.00
32.00
22.00

, ( , 5,6,8 9, ),
. ,
. .
.
, . :
1. .
2. , .
3. , , .
, , :
include <fstream.h>
include <iomanip.h>
void main()

//cBwage.cpp

// :
char filename[]="c8persnl.txt";

int id;
char fname[20], lname[20];
float wage;
fstream employees;
employees.open(filname, ios::in 1 ios::nocreate);

&

464

23.

// , :
for(;;)

<

employees id;
if(employees.eof()) break;
emp I oyeesf name I namewage;
// , ,
// :
coutendl;
coutsetw(5)set i osf I ags( i os:: i ght)i d
setw(20)setiosf !ags(ios:: left)fname
setw(20)lname
setw(6)setprecision(2)setiosf lagsCios: .'fixed)
resetiosf lags(ios:: left)
set i osf lagsfios: :showpoint)wage;

}
char choice;
coutendle"Enter any character to finish";
cinchoice;

}
:
filename . , , . .
, .
.
. ,
. (,
whiLe(!employees.eof()..,)), .
, , , , () () . Microsoft ,
, .
, ,
Microsoft .
. . , . , .

465

, :
.
.
, if, .
Borland :
if(employee==NULL) cout"Error: f i le does not open";
eIse ...
Microsoft :
if(emptoyee. is_open()==0) cout"Error: f i le does not open";
eIse ...
, if.
,
?
,
C++ ,
. .
, , .
: . ,
, . :
char ful i_name[40];
strcpy(full_name, fname);
strcpy(full_name, " ");
strcpy(full_name, Iname);
,
full_name , .
.

466

23.


.
, - displayQ.
, .
include <fstream.h>
include <iomanip.h>
include <string.h>
struct hi red_person
{
int id;
char fname[20], lname[20J;
float wage;
void displayO;

};
void hired_person: :display()
{
char full_name[40];
strcpy(ful Ijiame, fname);
strcatfful I name, " ");
strcat(fu) Lnarne, Iname);
coutsetiosf lags(ios: : right)setw(6)id" "<<setw[40]
set iosf lags ( ios: : left)ful l_namesetw(6)setprecision(2)
reset iosf lags ( ios: : left)
setiosf lags( ios: : right)
set iosf lags ( ios: : fixed)
setiosf lags ( ios; :showpo int} wage;
cSwagel.cpp
:
vo I d ma i )

//cSwagel .

{
// :
char f i lename[]="c:\\f ranca\\c8persnl . txt";
hi red_person worker;
fstrearn employees;

467

// :
employees,open(fi lename, ios:: in);
// ;
<

for(;;)
{

// :
employeesworker. id;
// :
if(employees.eofO) break;
employeesworker.fnaraeworker. lnameworker, wage;
coutendl;
// :
worker.displayO;

}
coutendlendl"Enter any character to finish:";
char enter;
ci nenter;

display() -, , , , . ,
,
. ,
, .



. :
if {employee. 1(}==0) cout" Error: f i l e does not open";
:
if(empioyee. is_open()) cout"Error: f i le does not open";
.

468

23.


:
tfdefine
tfifdef
tffndef
tfendid

, .
, .
.

ftdefine
tfdefine . :
define pi 3.1416

pi ( ) 3.1416. , :
const float pi=3.1416;
,
,
(, . .). , :
#def ine "! employee. is_open()
tfdef ine emp!oyee==NULL

:
if (condition"!) cout"Error";
,
!, . , .

tfifdef
ftifdef ,
ftdefine. , , .
(Borland Microsoft)
:

469

ttdefine Borland
define Microsoft
, , , . :
ttifdef Borland
tfdef ine condition employeeNULL
flendif
ffifdef Microsoft
define condition employee. is_open(}==0
Send if

ttendif
C++ if {}. tfifdef tfendif.
#endif.
Borland, condition
if employee=N U LL, ,
Borland. Borland
, , ttendif. , Microsoft . .
, , condition
:
employee==NULL
employee, is_open()==0

, :
if (condition) cout"Error";

?
condition, . , , . ,
Borland Microsoft,
. :
ttdefine condition 01=0
ttlfdef Borland
#define condition employeeNULL
ftendif

470

23.

ftifdef Microsoft
#def ine condition employee, is_open()0
tfend i f

#ifndef
#ifdef, , . ,
#endif.



: (employee). ?
,
.
, fiLeopenQ, ,
, , , 1, 0.
:
^include <fstream,h>
int f i leopen(fstream Sdataf i le)

int yesopen=1;
#ifdef Microsoft
yesopen^datafi le. is_open();
Send if
tflfdef Borland
yesopen=!(datafile==NULL);
Send if
return yesopen;

}
, , .
, , Borland Microsoft ( ). , Borland, Microsoft
:
int yesopen=1;
yesopen=!(datafi le==NULL);
return yesopen;

textfile _

471

, Microsoft,
Borland.
, , fHeopen() :
if(fi leopen(employee)==0) cout"Error";

c8tfiLe.h
textfile, .


cSwagel.cpp,
. - read(). 1,
, .

textfile
, , , , . ,
, .
. textfile ( )
.

workers ( textfile),
:

workers staff; //
for(;;)
<

if(staff.read()==0) break;
staff, d i s p l a y O ;

, ?

.

472

23.

fstream .
().
, . - read(), .
- readQ ,
. , .
, ,
. ? , , - ,
.

( ). ,
,
7.
textfile. ,
. , , . , fHeopen(), cStfile.h.
nofranca.h, . :
askwordsQ, yesnoQ . .
C++,
fra . h. ,
. franca.h, ^include "franca.h"
, cStfile.h. franca.h nofranca.h . , , .


textfile:
class textf i le
I
protected;
fstream data;
char datafi le[40];
int fi lemode;

textfile _

473

pub! ic:

char id[40];
textfi le();
textfi le(char fi lename[], int mode=ios:: i n !
ios::app! ios::nocreate);
"textfi le();
void displayC);
vi rtual int read();
int find(char
int input();
void write();

-
- ,
textfile :
data fstream .
datafile ,
.
filemode .
id, , () . , .

-
- :
textfile .
. . .
(destructor) -textfUeQ -. . .
.
.
display() .
inputQ .

474

23.

findf) (), . , . (39 )


( ).
write{) .
readQ .

:
^include "nof . h"
textf i le: :textf i le()
I
askwords(dataf i le, 40, "enter the fi lename;");
f i lemode=ios: : in ! ios::app! ios: ;nocreate;
data,open(dataf i le, filemode);
if(f i leopen(data}==0)
if(yesno("Fi le not open, create?"))

f i lemode=ios: :out:
data.openfdataf i le, filemode);
if(f i leopen(data)0) //

>

>

yesno{"Sorry, f i le does not open! ");


exit(O);

else

else exit(O);

textf i le: : textf i lefchar f i lename[], int mode)


{
if(sizeof(dataf I le)strlen(f i lename))
strcpy(dataf i le, filename);

textfile j)6

475

data.open(datafi le, mode);

fi lemode=mode;
if(fi leopen(data)==0)
yesno("Error in opening, w i l l you check?"};
exit(O);

>f)

, , nofranca.h,
askwordsQ yesno(). ,
cStfiLe.h, ^include "franca.h". He ,
franca.h main(),
mainprogQ.

franca.h
cin cout, .

exit(O) , .
.

ios::in|ios::app .

.
, .

:
textf i le::4extf i le()

data.closeO;

476

23.


display() inputQ :
void textf i le: :display()

int textf i le: : input()


{
askwords(id, 40, "Enter id code");
lf(ld[0]==0) return 0;
return 1;
? display(), , ! ? ?
, . ,
, ! (, , , . .) , textfile , display() .
- .
in put() .
,

-.
inputQ,
inputQ
(, Enter).
, . 1 0. , .
, - .


- readQ writeQ:
int textf i le: : readf)

{
// :
data id;

__

477

if(data.eofO) return 0;
return 1;

void textf i le; :write()


{
data.closeO;
data.open(dataf i le, ios::app);
if (f i leopen(data)==0)
{
yesno("Fai led reopen for append, w i I I you check?");
retutn;
dataendlid ;
data.closeO;
data.openfdataf i le, filemode};
i

read () .
,
.
write() , , , . ,
. . -
.


- find():
int textf i le: :f ind(char idnumber[])
{
data.closeO;
data.open(dataf i le, filemode);
If (f i !eopen(data)==0)
{

yesno("Fai led reopen for find, wi I I you check?");


exit{0);
}
for(;;)

23.

478

if (data.eof())break;
if (strcmp( id, idnumber)!=0) read(); // !
else return 1;

}
data.closeC);
data.open(dataf i le, filemode);
return 0;
!
, . , 1. .
. , , . , .
findQ . - readf).
, read()
textfile read(),
. read() ?
read(),
textfiLe, ,
. , , .
, , , - readQ .
' readQ , . , , ,
, .
readQ , , . read() textfiLe .
textfiLe cStfiLe.h .
) textfile a . , - .
.

479


c8persnl.txt workers,
textfile. , .


,
, .

. , . , .
,
. , , ,
. , ,
.
textfile :
.
.
.
.
.
, , . .
.
, ? . .
, ? , . .


,
, . .

480

23.

, .
, . ,
,
. , .
. . ,
. , , .
_

, .


, ,
(matching).
, .
, , workhours, . workhours
worker, . ,
.
textfile , - find()
.
.
:
c8persnl.txt ( payroll) , .
cSpayrlLtxt ( worker) .
,
.
, .

481

payroll
worker, , , worker
payroll. ,
( payroll) , (
worker).
, payroll, :

.
worker .
.
, , , . , worker rnred_person,
-
flnd().
payroll , textfile. ,

- .
, , !

payfile
payroll ;
^include "nofranca.h"

1 ass payf i 1 : publ i textf i Ie


{
public:
float hours;
virtual int read();
};
int payfile::read()
{
if (textfi le::read()==0) return 0;
datahours;
return 1;

482

23.

read() read() textfile.


, -
, ( ) . , ,
:
_::_();
;
if (textfi le:;read()==0) return 0;
,
:
void mainf)

//cSpayrl i.cpp

// :
char fi lename[40];
cout "Open ing the employees f i le:";
hi red_person worker;
cout "Open ing the payrol I file:";
payfile workhours;
float paycheck, payrol1=0;
char idnumber[40];
// :
for(;;)
// workhours,
// :
iffworkhours. read()==0) break;
// worker:
iffworker.findfworkhours. id))
paycheck=wo rke r.wage*wo rkhou rs.hou rs;
payroll=payroll+paycheck;
worker. displayO;
cout" $"setw(12)
set iosf lags(ios:: fixed) paycheck;
else

coutendl"No record found for worker:"


worker, id;
coutendl"Payrol I total: $"setw(12)payrol I ;

483


payroll - input(), write() display().
(
).

?

0 .
0 tfdefine, tfifdef, ttifndef tfendif.
0 .
0 .
0 .


"

, . ,
.

21 , . , ,
.
, .


, , catalog .
, catalog , , , .
, ,
catalog!
, , catalog . .
catalog . , , ?
catalog , .

486

24.___


catalog, : catalog,
( - display textfile),
, arraycatalog fHecatalog. , -.
, (abstract class).

catalog
catalog textfile.
:
struct product

char code[40];
char descript ion[40];
float price;

I ass cata 1 og : pub ! i textf i I e

{
product saleitem;
vi rtual int read();
word writef);
publ ic:
virtual product find(char part_number[]);
vi rtual product f ind( int somecode);

};

find ()
, , find().
textfile () . . !

17 textfile, .

487

,
-. , :
runner j u l ia;
skater mike;
jul ia.run(};
mike.run()

. , julia run(), , mike (), . , ,


, .
, , !
17 ,
runner:
void march (runner volunteer)
{
volunteer.run();
I
, , ( ). ,
, . (). ,
, ( , ).
, ,

. (late binding).
,
. ?
.
, , , , . , C++ , .
, ( ),
textfile. findQ read(). He ,

488

24.

read{) ,
.
, ,
find() .
, readQ ,
.


textfile . ?
!
. .
c8parts.txt . .

. textfile. . , .

read() write ()
readf) writeQ saleitem
. :
int catalog::read()
{
int testeof=1;
testeof=textfi l e ; : read();
if(testeof==0) strcpy(id, " ");
strcpyCsaleitem.code, id);
datasa I e i tern, descr i pt i on;
datasa lei tern, price;
return testeof;
void catalog::write()
{
textfi le;:write();

data saleitem, descript ion;


data saleitem.price;

489

, (textfHe::read() textfiLe::write()). ,
.
, catalog id textfiLe, . , , id.



:
product catalog: :f ind(char part_number[])
{
int kfind;
kfind=textfi le::find(part_number);
if (kfind==0)
{
saleitem.code[0]=0;
id[0]=0;
}
return saleitem;
}

product catalog::find(int somecode)


<
char thecode[40];
!toa(somecode, thecode,10);
return find(thecode);
}
cScatlg. h cBterm. h .
cSterm.cpp franca.h Windows.
//
//cSterm.cpp
// (Borland Microsoft)
// :
// ttdefine Microsoft
//
define Borland
ffinclude "franca.h"
^include "cBcatlg.h"

490

24.

ttinclude "c8term,h"
void mainprogf)
{
saleterm cashregister;
cashregister.opetatef);
}

tfdefine. ( ) .
tfindude "franca.h" mainprog() mainQ.

?

0 .
0 .

IX

IX , ,
-
.
, ,
C++ . ,
, .

, . C++: (
). Borland C++ Builder, . Borland C++ Builder:
( ).

-
(Object Oriented Programming, OOP),
, .
, , . , . .

-
- .
, . , .
, !
,
( ) .
. ? ,
, ?
.
;
.
.
,
.

,

,
.
, , ,
.

494

J5.

:
. ! , , ,
. .
, .
, . , . .
, , , .
. .
. , .
(, ), - .


, . , , ,
. :
1.

, .
, -
. ( , ), .

2. , , .
, .
,

. - , ,
- .


. ,

495

,
. , .
? ,
. , .

(, , ).
,
,
. , ,
,
.
.
, , , . ( ),
. ,
. .
.

. . , , , , , - .
.
, .
. ,
. , , !
, (
) .

,

496

25. -

. . , , !
,
.
, , .
! - , - .
, ,
.
, . ,
. , ?
, ? ,
?


.
. ,
.
, . . ,
, . , .
. , ,
, .
, .


. , .

.
.
.
.

497

:
1. wordnumber ( ).
2. testnumber ( 6 20).
3. .
4. :
.
.
5. ,
testnumber:
1 wordnumber.
, wordnumber-1.

.
.
6. .

,
:

.
, , : , .


, .
, . , !
. 25.1 .
( ) ,

498

25. -

( ) . ,
, : All right!

Right:
17
Wrong:
5

. 25.1-

-
- .
:
1. . , . . , , .
2. , . , ,
.
, , , .
, , , ,
.

499

,
. . , .

3. , , , .


. .

4. ,
.
.
5.

-.

, , . .
, . !


.
, . , ,
franca.h , , .
. ?
( 2 ),
, .
,
, , .

c9spell.cpp
. sounds.



teacher (), .
:

500

__25.

.
.
.
, . ,
. , , ,
. jukebox ( ) ( , ), :
.
.
.
, ,
.
jukebox, , , , ,
.
9
, :
typedef char words[9j;
:
class jukebox
protected:
char di rectory[30], f I lename[40];
int word/lumber;
words I ist[40];
void wharf i le(words name);
publ ic;
int howrnanywords();
void pronounce(words thisword);
void pronounce( int whichword);
void spel I C int which, words she I led);
jukeboxfchar path[j);
jukeboxO;
int f ind(words which chone);

501

, - pronounce() .
( )
. ,
. howmanywordsQ .
spellQ ,
, , .
, , k, , ,
- jukebox.

, jukebox . ,
, . , c:\\franca\\sounds\\
(, ). jukebox , .
teacher jukebox , .
jukebox .

sounds
, sounds\\.

whatfile() , , . filename. , -.
jukebox,
, .


teacher , . ,
. blackboard (), teacher . Box.
- teacher : yourword ( ), plus ( )

502

25. -

minus ( ).
] ukebox, teacher .

teacher
teacher:
class teacher

bank FistNational;
Box yourword;
Clock myclock;
Box plus, minus;
jukebox speaker;
int right, wrong;
publ ic;
void playlist();
void testO;
teacherfjulebox &neon);
teacher
jukebox. , !
? .
bank ()?
.
.
, ,
bank . bank
( ) ( ) .
right wrong.

bank
bank:
class bank
protected:
Circle greencoin, redcoin;
float xgreen, xred;
float nextygreen, nextyred;

503

publ ic:
void plus();
void minus();
bank();
plus() , a minusQ . .
bank , , . .
bank :
bank(float x=50, float =400);
- :
void mainprogO
{
jukebox speaker("sounds\\");
teacher MrRoberts(speaker);
MrRoberts.playl ist();
MrRoberts.test();
speaker.pronounce("al I right");
speaker. pronounce("byebye");
}

jukebox teacher.
jukebox , .
teacher
jukebox. ,
.


, , allright bye-bye.
, , speaker jukebox. ,
? ,
, .
alLright bye-bye?

504

25. -

! , c:\franca\testl\,
c:\franca\soundsl\. ?
( jukebox):
, :
jukebox speaker(":\\franca\\test1\\");
jukebox greet ing(":\\franca\\sound1\\");
speaker , ,
teacher, speaker greeting,
allright bye-bye,

affright . , , .


, , ,
,
. -
.
jukebox
jukebox:
jukebox;:jukebox()

{
wordnurnber=0;
di rectory[0]=0;

}
jukebox::jukebox(char path[])
{
fstream wordl ist;
if(strlen(directory)>30)directory[0]=0;
strcpy(di rectory, path); //
strcpy(fi lename, directory);
strcat(fi lename, "I ist");
strcat(fi lenarne, ".txt");
word! ist.openffi lename, i o s : : i n ! ios:inocreate);//

505

if(f i leopen(wordl ist)==0)


{ // :
Box errormsgC'Error:"};
errormsg.sayC'No List! ");
exit(O);
for(int k=0; k<100;
{ // :
if (word I ist.eoff}) break;
wordl istl ist[k];
}
wordnumber=k;
}
i nt j ukebox : : howmanywo rds( )
{
return wordnumber;
}
void jukebox; :spel i( int which, words, spelled)
{
if (which>=wordnumber)
!
strcpy(spel led, "");
return;
}

strcpy(spel led, I ist[which]);


spel led
}

void jukebox; :whatf i le(words I ist)


{
strcpy(fi lename, directory);
strcat(f i lename, I ist);
strcatff i lename, ".wav");
}

void jukebox: ; pronounce( int whichword)


{
whatf i le(l ist[whicnword]);
sound(f i lename);
}
void jukebox: :pronounce( words thi sword)
i

whatf i le(thisword);
sound(f i lename);

//

// !
//

//
//
//

506

25. -


tfdefine Microsoft,
fldefine Borland ( ),
.

bank
bank:
bank: :bank(f loat , f l o a t y )

{
xgreen=x;
xred=x+50;
nextygreen=y;
nextyred=y;
greencoin.resize(20, 40);
greencoin.color(2, 7);
redcoin.resize(20, 40);
redcoin.color(1,7);

}
void bank::plus()

{
greencoin.place(xgreen, nextygreen);
greencoin,show();
nextygreen=nextygreen-1Q;
}
void bank::minus()
{

redcoin.place(xred, nextyred);
redcoin.showO;
nextyred=nextyred-10;
}
teacher
, teacher
teacher: :teacher(jukebox uneon)
(
speaker=neon;
right=wrong=0;
plus.label("Rlght:");

//
// teacher

_ 507

plus.say(right);
minus. label("Wrong: ");
minus. say(wrong);

void teacher: ;playi ist()


f
words oneword;
int n=speaker.howmanywords();
for(lnt k=0; k<n;
speaker. spel t ( k , oneword);
yourword.say(oneword);
speaker. pronounce(k);
myctock.wait(2);
yourword.eraseO;

void teacher: :test()


{
char spei I ing[20];
char correct[20];
int j ;

//
//

int count=2*speaker.howmanywords(); //
forflnt k=1; k<=cout; k++)
{
j = rand()%speaker.howrnanywords();
strcpy(spel I ing, "");
//
speaker. pronounce(j);
aslwords(spel I ing, 9, "Please spel I :");
speaker. spel I ( j , correct);
if(stricmp(spel I ing, correct)==0)
{
speaker.pronounce("al I right"};
plus.say(++right);
FistNational .p!us();

else
yesno("Wrong! Wi 1 1 you study harder?");
minus.say(-H-wrong);
FistNational . (minus);

508

25. __ -

?

0 - .
0 .
0 .
0 - .

C++

,
C++. , , , , .
, , ,
, .
, C++
. , , , 1.
C++,
. , ,
,
, .

C++
C++. . .

(pointer) , . C++ . ( ) .
, .

.
1

. -C++:
( ).

C++

511


Stage franca.h.
Stage (
ScreenObj), .
,
Stage, .
- Stage -. ,
- show() ,
- show(). , . -.


, . :

*=
'"

, :
+=5;
:
=+5;
. , , . ,
, .


C++,
, (operator overloading). C++ . , C++ + . .

5_12

26.

, C++ : ,
, , .
- operator+(), +
, ( ) . , .
, = =
:
if (stringA == stringB) stringA=a copy;

-
franca.h, - insertQ,
Stage. :
Stage sold ier;
soldier, insert(rightarm);
a
i
soldier, insert(leftarm);
Stage - operater(ScreenObj
&), :
soldierrightarm;
soldierleftarm;

-, , (operator function). - ,
, Stage ScreenObj, -.
- - insertQ.

C++ (templates). ,
, .


textfile . , 40 . :
class textfi le

C++
publ ic:
char id[40];

, :
template<int size>
class textf i le

1
char id[size];
textfile :
textf ilemyfile<65>;
,
65.
. , , . , , char:
anytype id;
, , .
strcmp{), , - .
, (generic classes), ,
.


(friend functions) (friend classes),
, C++. . ( friend),
. , , - - . ,
.

514

26.


.
(multiple inheritance),
- -
.



. , , .
. ,
.
. , ( ), .
, , . , , , . , ,
.


C++ . &&, || !.


, . , . , , ,
.
! , !

jAbi ?

515


, . , ! , , .
, ,
.
,
!
,
.
. , , . ?
, ,
, . , , ,
, , ...
, . ?
. , ,
. , , .

?

0 , C++ .
0 ,

E-F

abstract class, 486


American Standart Code for Information
Interchange (ASCII), 410
ancestor class, 357
argument, 89, 124
array, 383
ASCII, 410

enumerated constant, 271


expression, 167
field, 418
File Manager, 34
flowchart, 173
formatting, 445
friend class, 513
friend function, 513
function, 119
body, 163
call, 164
definition, 162
header, 162
overloading, 163
prototype, 164

base class, 351


binding, 487
bug, 89

cast, 278
child class, 357
class, 330
class declaration, 335
class definition, 335
compound statement, 175
conditional expression, 207
conditional operator, 177
constructor, 339

declaration, 112
default constructor, 348
derived class, 351
descendent class, 357
destructor, 474

G-I
generic class, 513
header file, 131
index, 384
infinite loop, 174
inheritance, 352, 357
inline function, 157
inner loop, 186
input, 144
instance, 355
labeled constant, 271
late binding, 487
literal constant, 271
loop, 173
loop invariant, 202

517

matching, 480
member function, 87, 330
message, 87, 330
multiple inheritance, 514

N-0
nested loop, 186
null-terminated string, 411
object, 86, 330
Object Oriented Programming (OOP),
19, 493
OOP, 19, 493
operator function, 512
operator overloading, 511
outer loop, 186
output, 146

parameter, 124
parent class, 357
pixel, 283
pointer, 510
polymorphism, 358, 362
private, 338
procedure, 119
protected, 338
prototype, 336
public, 338
Q-R

qualification, 337
record, 418
recursion, 249
reference, 129
relational expression, 207
relational operator, 177
return, 153

scope, 132
statement, 87

string, 410
structure, 418

template, 512
type matching, 163

u-w
user interface, 219
value, 129
virtual function, 364
void, 153
Windows Explorer, 34

, 486
, 91
, 255

, 410
, 89, 124
, 125

, 351
, 269
, 174

math.h, 279
-, 173

, 144
, 443
, 364
, 157
, 146
, 442
, 514

518
, 164
, 167
, 207

-
, 132
-, 332
, 392
, 474, 475
, 131
#defme, 468
#endif, 469
tfifdef, 469
, 84, 468
, 357
, 513
, 513

-
, 89
, 162
, 130, 131
iomanip.h, 447
iostream.h, 441
string.h, 413
, 338
, 418
, 338
, 129

, 111
, 29
, 202
, 384
, 141
, 394
, 340
, 412
, 339
, 87
break, 239
continue, 240
if, 223, 236

(')
if...else, 229
return, 278
switch, 243, 245
typedef, 397
, 25

, 28
, 85, 330
bank, 502, 506
catalog, 426, 486
jukebox, 500, 504
payfile, 481
saleterm, 428
satellite, 375
ScreenObj,321
Stage, 319, 321
teacher, 502, 506
terminal, 369, 428
textfile,471
, 351
, 357
, 513
, 335
, 335, 337
, 357
, 357
, 351
, 357
, 513
, 355

class, 336
const, 272
return, 153
void, 153
, 511
, 90, 113
, 25
, 26
, 415

, 271
, 271

519

()
, 271
, 271
, 271
, 339
, 348
, 414
, 270

-
, 132

endl,443, 447
resetiosflags, 449
setffll, 449
setiosflags, 449
setprecision, 448
setw, 448
/, 447
, 383
, 392
, 394
, 392
, 403
, 412
, 392
, 25
, 514

, 352, 357
, 514
, 334

, 132
, 86, 330
cm, 443
cout, 440, 442
Grid, 297
, 132
, 132
cm, 441

-
, 19, 493
, 25
, 112
, 335
, 460
, 411

sizeofQ, 415
, 511
, 235
, 234
, 511
, 177
-, 512
, 335, 337, 340
, 162
, 35
, 338
, 110

, 124
, 511
, 163

, 129,164
, 129,164
, 137
, 514
, 283
, 277
, 29
, 487
, 418
, 358, 362
, 29
, 29
, 219
, 277
, 443
, 442
, 277
, 84
, 239

520
, 278
, 24
, 28
, 35
, 351
, 164
-, 336
, 119

, 25
, 28
, 461

, 405
, 250
, 249
, 357
, 513

, 487
, 110
, 34
, 87, 113, 330

, 480
, 163

, 403
,405
, 175
, 409
, 414
, 129
, 334
, 410
, 413
, 412
, 418

, 29
, 163

double, 270
float, 270
int, 268
long, 268
, 268

, 510
, 235
, 207
, 177
, 337

, 28
, 25
, 25
, 460

fixed, 453
scientific, 453
showpoint, 453
skipws, 450
, 445
, 119
absize(), 284
acos(), 279
angle(), 378
asin(), 279
ask(), 454
askwords()>412, 454
atan(), 279
atof(),417
atoi(),416
ceil(), 279
centerQ, 376, 377
close(),462
colorQ, 284
cos(), 279
displayQ, 466, 467, 476
dist(),314, 378
draw(),303, 305
drawfQ, 305
eof(), 462

521

()
eraseQ, 284
(), 279
fabs(), 279
fileopenQ, 470
floor(), 279
height(),311, 314
howmany\vords(), 501
inputQ, 476
items(),371
itoa(),417
label(),284
log(), 279
loglOQ, 279
main(), 85
mainprogO, 85
minus(), 503
open(),461
operate(), 370
originO, 296
place(), 284, 360
plusQ, 503
rand(), 279
read(), 476
readarray(), 445
recsort(), 405
resetQ, 336
resizeQ, 284
run(),360, 364
say(), 284
scale(), 296
show(),284, 321, 444, 447
sin(), 279
sort(), 403
speed(), 378
spell(),501
sqr(), 279
sqrt(),279 .
stepleft(),359
stepright(),359
strcat(),415
strcmp(), 414
strcpyO, 414
stricmp(),414
strlen(), 415
tan(), 279

()
time(), 336
timeGetTimeO, 333
wait(), 336
whatfile(),501
winmain(), 85
writeQ, 476
yesno(), 454
, 364, 486
, 513
, 250
, 413
-, 87, 113, 130, 330

, 173
do...while, 177
for, 178
while, 173
, 186
, 186
, 186

, 392

, 338
, 338
, 338

-
, 512
, 355
, 25

1997
-
. ,
. ,
,
,
.
:
;

(, );

( ).
?

:

www.piter.com;

postbook@piter.com . (81 2) 1 03-73-74;
500
.

, . , , .
- , :

1 5% , ;
- 3%;
;
- .

w w w. p i .

.

?

www.piter.com
:


- - , ,
- - CD
, ,

-

:

: (812) 103-73-74;
: postbook@piter.com;
: www.piter.com;
: 197198, -, / 619
.


:

. (
). -
.
(
-).
.
:

, , , , , e-mail;
, , , ,
, , , ;
, , ,
.

.


WWW.PITER.COM

-
.
.
5-

.
,
Windows 98, Microsoft Office XP (Word
2002, Excel 2002 PowerPoint 2002).
, ,
.
, .

560., 17x24, .

376


197.


.
. 7-
,
- .
-
.
, ,


.
,
. : Norton Commander,
Norton Utilities, DOS, , ,
, , MS Windows 95/98/,
, Windows,
Word, Excel,
.
, .

.,.

,

,
.
, ,
,
.
.

656 17x24, .
2294

178 .

272., 17x24, .
2368

137.

-

.
\(5

,
Windows, - Microsoft Windows
688 17x24, . . ,

263
, .
Windows ,
410 .
.
,
, ,
Windows.

., .

480 17x24, .
523

150 .

:

IBM PC.
,
, ,
, ,
. ,

,
.

., .
. .
3-

320 ., 12,5x20, .
3066

110.

-
,
,
2003 .
,
,
. ,
.

.

>

D II T
E
R
! . C
'O

WWW.P
M

^^^^ \ / \ / \ /


, ,
,

. , , , . 176, 207, 240; ./ (095) 777-54-67;

e-mail: sales@piter.msk.ru

- . , . ., . 29;
. (812) 103-73-73, (812) 103-73-82; e-mail: sales@piter.com
. , . 138; . (0732)51-95-00;
e-mail: piter-vrn@vmail.ru; piterv@comch.ru
. 8 , . 2676; ./ (3432) 25-39-94; e-mail: piter-ural@r66.ru
. , . 31; . (8312) 58-50-15, 58-50-25;
e-mail: piter@infonet.nnov.ru
--

. , . 17; . (8632) 95-36-31 , (8632) 95-36-32;

e-mail: jupiter@rost.ru

. , . 4; . (8462)37-06-07; e-mail: piter-volga@sama.ru

. , . 29, 610; . (0572) 23-75-63, (0572) 28-20-04, (0572) 28-20-05,


(0572) 14-96-09; e-mail: piter@tender.kharkov.ua
. , . 6, . 1 ; ./ (044) 490-35-68, 490-35-69;
e-mail: office@piter-press.kiev.ua

. ., 21, 3; ./ (37517) 226-19-53; e-mail: piter@mail.by

-; . , 65, 345; . (3732) 22-69-52,


(3732) 27-24-82; e-mail: lili@auratip.mldnet.com

, .
: (812) 103-73-73.
E-mail: grigorjan@piter.com
.
: - (812) 103-73-72,
- (095) 777-54-67.
: (812)103-73-73.
- e-mail: kozin@piter.com


WWW.PITER.COM

, , . , . 70 ( ),
. , . , . 88,
./ (3472) 50-39-00.
E-mail: asiaufa@ufanet.ru


, ,
./ (4232) 23-82-12.
E-mail: bookbase@mail.primorye.ru
, ,
. (4212) 30-54-47, 22-73-30.
E-mail: sale_book@bookmirs. khv.ru
, ,
. (4212) 32-85-51, 32-82-50.
E-mail: postmaster@worldbooks.kht.ru


, ,
. (8182) 65-41 -34, 65-41 -34.
E-mail: book@atnet.ru

!



.

, -,
./ (3952) 33-42-47.
E-mail: antey@irk.ru
, ,
./ (3912) 27-39-71.
E-mail: book-world@public.krasnet.ru
, ,
. (3466) 23-27-14, 23-59-50.
E-mail: book@nvartovsk.wsnet.ru
, -,
. (3832) 36-10-26, 36-10-27.
E-mail: office@top-kniga.ru
htlp://www.top-kniga. ru
, ,
./ (3452) 21-34-82.
E-mail: drug@tyumen.ru
, ,
. (3452) 27-36-06, 27-36-11.
E-mail: fo!iant@tyu men.ru

, ,
./ (0112) 21-56-28, 21 -62-07.
E-mail: nshibkova@vester.ru
http://www.vester.ru

, , . , . 61,
./ (3512) 52-49-23.
E-mail:evrika@chel .surnet.ru

--, ,
. , . 73,
./ (8632) 32-18-20.
E-mail: ostrom@don.sitek.net

, ,
. (8432) 72-34-55, 72-27-82.
E-mail: tais@bancorp.ru


, , . , 424,
./ (87934) 6-93-09.
E-mail: rossy@kmw.ru

, ,
. (3952) 59-13-70, 51-30-70.
E-mail: prodalit@irk.ru
http://www.prodalit.irk.ru

, 14,
. , . 23,
./ (3432) 53-24-90.
E-mail: gvardia@mait.ur.ru
, -,
. , . 5,
./ (3432) 42-56-00.
E-mail: valeo@etel.ru

Вам также может понравиться