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

Programming

Principles and Practice


Using ++

Second edition

Bja rne Stroustru

...
.... ....
Addison
Wesley

Upper Saddle River, NJ Boston lndianapolis San Francisco


New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City


++


-
2016
32.973.26-018.2. 75
83
681.3.07
""
. ..
. . ..
"" :
lnfo@wtlliamspu!shlng.com, http://www.wtlliamspu!shing.com
, .
83 : ++,
2- . : . . - . : " .. ", 20 1 6. - 1 328 . :
. - . . .
ISBN 978-5-8459- 1 949-6 (.)
32.973.26-018.2. 75

.

.
, .
Addlso-Wesley Pullshlg . !.
Authorlzed traslatlo from the Eglish laguage edltlo plshed Addlso-Wesley
Pllshig . !. Copyright 2014 Pearso Educatio. !.
All rlghts reserved. No part of thls pulicatlo may reproduced. stored l retleval system.
or trasmltted. l any form. or meas, electrolc, mechalcal. pttocopylng. recordlng, or
otherwlse. without the prlor wrltten permisslo of Plisher.
Russia lagage edltlon pullshed W llliams Pullshlng s accordlg to the rmt
with R&I Eterprlses Iteratloal, Copyrlght 2016

.-r

:
++
2-

.. .
..
.. m.
.. .

26.10.2015. 7 0 \ / 16.
lmes.
. . . 107.07. .-. . 66,7.
300 . No 6311




142300, . . . . . .1

". . ", 127055, . , . , . 43. . 1

ISBN 978-5-8459-1949-6 (.) "". 2016


JSBN 978-0-321-99278-9 (.) .
Pearso EdLtcatlo. / . 2014

25
. 33
1. , 53

1. 81
2. Hello, World! 83
3. , 99
4. 129
5. 173
6. 217
7. 267
8. : 301
9. : 351

11. 391
1 . 393
11. 429
12. 463
13. 495
14. 539
15. 575
16. 607

111. 639
17. 641
18. 687
19. , 729
20. 777
21. 827

IV. 873
22. 875
23. 925
24. 965
25. 1003
6

26. 1073
27. 1107

V. 1159

. 1161
. 1219
. Visual Studio 1283
. FLTK 1289
.
1295
1305
1313
1316
1327

25
28
29
ISO 30
31
31

. 33
0 . 1 . 34
0.1.1. 35
0.1.2. , .. 37
0.1.3. 38
0.2. 39
0.2.1. 42
0.2.2. 44
0.2.3. 45
0.3. 46
0.4. 46
0 . 5 . 47
0.6. 47
0.7. 48
48
"" 49
! 51

1. , 53
1 . 1 . 54
1 .2. 55
1 .3 . 58
1 .4. 62
1 . 5 . 62
1.5.1. 63
1.5.2. 64
1.5.3. 65
1.5.4. 68
1.5.5. 69
1.5.6. 71
1.5.7. ? 73
1 .6. 73

1. 81
2. Hello, World! 83
2.1. 84
2.2. 85
2.3. 88
2.4. 91
2 . 5 . 92
3. , 99
3. 1 . 100
3.2. 102
3.3. 104
3.4. 106
3.5. 109
3.5. l. : 111
3.6. 113
3.6. l. : 114
3.7. 115
3 . 8 . l17
3.9. 119
3. 9. l. 120
3.9.2. 121
4. 129
4. 1 . 130
4.2. 132
4.3. 135
4.3. l. 136
4.3.2. 138
4.3.3. 140
4.4. 141
4.4. l. 143
4.4.2. 149
4 . 5 . 154
4.5. l. 156
4.5.2. 157
4.6. 158
4.6. l. 160
4.6.2. 160
4.6.3. 161
4.6.4. 164
4. 7. 166
5. 173
5. 1 . 174
5 . 2 . 176
9

5 .3. 177
5.3.1. 1 78
5.3.2. . 179
5.3.3. 180
5.4. 1 81
5 . 5. 1 82
5.5. 1. 1 83
5.5.2. 185
5.5.3. 187
5.6. 188
5.6.1. 189
5.6.2. . 1 90
5.6.3. 1 92
5.6.4. 1 96
5. 7. 1 97
5.8. 200
5.9. 201
5.9.1. 203
5. 1 0 . - 207
5. 10.1. 209
5. 1 1 . 21 0

6. 217
6. 1 . 21 8
6.2. 21 9
6.2.1. 220
6.2.2. 220
6.3. 223
6.3.1. 224
6.3.2. 226
6.3.3. 228
6.3.4. 230
6.3.5. ! 232
6.4. 233
6.4.1. : 238
6.4.2. 239
6.5. 241
6.5.1. 241
6.5.2. 242
6.5.3. 246
6.5.4. 248
6.6. 249
6. 7. 254
6.8. 255
6.8.1. Token stream 257
10

6.8.2. 259
6.8.3. 260
6.9. 26 1

7. 267
7. 1 . 268
7.2. 268
7.3. 270
7.4. 275
7.5. : 0J 276
7.6. 278
7.6.1. 278
7.6.2. 280
7.6.3. 281
7.6.4. 283
7. 7. 285
7.8. 288
7 .8.1. 288
7.8.2. 293
7.8.3. 296
7.8.4. ? 296

8. : 301
8 . 1 . 302
8 . 2 . 303
8.2. 1. 308
8.2.2. 308
8.2.3. 3l
8.3. 310
8.4. 313
8 . 5 . 319
8.5.1. 319
8.5.2. 321
8.5.3. 322
8.5.4. 323
8.5.5. 325
8.5.6.
328
8.5. 7. 33 l
8.5.8. 332
8.5.9. nstr- 337
8.6. 338
8.6. l. 340
8.6.2. I 340
8 .7. 342
8. 7.1. using using 343
11

9. : 351
9.1. , 352
9.2. JUiacca 354
9.3. 354
9.4. JUiacca 356
9.4.1. 356
9.4.2. - 358
9.4.3. 360
9.4.4. - 362
9.4.5. 365
9.4.6. 365
9.5. 367
9.5. 1. "" 369
9.6. 370
9.7. JUiacco 371
9.7.1. 372
9.7.2. 375
9. 7.3. 376
9. 7.4. - 379
9. 7. 5. 381
9.8. Date 383

11. 391
1. 393
1.1. 394
10.2. - 396
10.3. 398
l 0.4. 399
10.5. 401
l 0.6. - 403
10.7. 407
10. 7.1. 409
1. 7.2. 412
l 0.8. , 413
10.9. . 414
10.10. JUI 415
10. l l. 417
10.11.1. 418
10.11.2. 420
10.11.3. 424
1 1. 429
11. l. 430
11.2. 431
1 1.2.1. 431
12

11.2.2. 434
11.2.3. 435
11.2.4. 436
11.2. 5. 437
11.3. 438
11.3. l. 439
11.3.2. 440
11.3.3. 443
11.4. 444
11.5. , 446
11.6. 447
11. 7. 449
11.8. 456
12. 463
12.1. ? 464
12.2. 465
12.3. 467
12.4.
471
12.5. 472
12.6. Shape 473
12.7. Shape 474
12. 7.1. main 474
12.7.2. 475
12.7.3. 477
12.7.4. 479
12.7.5. 480
12.7.6. 481
12.7.7. 484
12.7.8. 484
12.7.9. 486
12.7.10. 487
12.8. 488
12.8. 1. 490
13. 495
13.1. 496
13.2. Point Line 498
13.3. Lines 501
13.4. Color 504
13.5. Line _style 507
13.6. Open _polyline 509
13.7. Closed_polyline 510
13.8. Polygon 512
13.9. Rectangle 514
13

1 3 . 1 0. 519
13.11. t 521
13.12. Circle 523
1 3. 1 3 . Ellipse 525
13. 1 4. Marked_polyline 527
13. 1 5. Marks 529
1 3. 1 6. Mark 530
1 3. 1 7 . Image 532
14. 539
14. 1 . 540
14.1.1. 540
14.1.2. 542
14.1.3. 543
14.1.4. 545
14.2. Shape 546
14.2. 1. 54 7
14.2.2. 549
14.2.3. 552
14.2.4. 556
1 4.3. 557
14.3.1. 559
14.3.2. 56 1
14.3.3. 562
14.3.4. 564
14.3.5. 565
1 4.4. -
567
1 5. 575
1 5. 1 . 576
1 5.2. 576
1 5.3. Function 580
15.3.1. 582
15.3.2. 583
15.3.3. - 585
1 5.4. 586
1 5. 5 . 588
15.6. 594
15.6. 1. 596
15.6.2. 598
15.6.3. 598
15.6.4. 600
14

16. 607
16.1. 608
16.2. Next 609
16.3. 61 1
16.3. 1. 6 13
16.3.2. 6 16
16.3.3. - 617
1 6.4. Button Widget 618
16.4. 1. Widget 618
16.4.2. Button 620
16.4.3. In Out 620
16.4.4. Menu 62 1
1 6. 5. 622
1 6.6. 626
1 6.7. 627
16.8. GUI- 632

111. 639
17. 64 1
17. 1 . 642
1 7.2. 644
17.3. , 646
17.3. 1.0epaopsizeof 649
1 7.4. 650
17.4.1. 65 1
17.4.2. 653
17.4.3. 654
17.4.4. 656
17.4.5. 657
17.4.6. 658
1 7. 5 . 66 1
17.5.1. 663
17.5.2. 664
1 7.6. 665
1 7. 7. 666
17.8. : void* 668
17.9. 67 1
17.9. 1. 672
17.9.2. , 673
17.9.3. : 674
17.9.4. 676
17.9.5. 677
17. 1 0. this 679
17. 10. 1. 681
15

18. 687
18. 1. 688
18.2. 689
18.3. 691
18.3. l. 693
18.3.2. 694
18.3.3. , 696
18.3.4. 697
18.4. 700
18.4. l. 702
18.4.2. 703
18.5. 706
18.5.1. 707
18.6. 708
18.6. 1. 710
18.6.2. 712
18.6.3. 714
18.6.4. 716
18.7. : 719
18.7.1. string 719
18. 7.2. 720
18. 7.3. 721
19. , 729
19.1. 730
19.2. 734
19.2. 1. 734
19.2.2. reserve capaci ty 736
19.2.3. resize 736
19.2.4. push back
_ 737
19.2.5. 738
19.2.6. 740
19.3. 741
19.3.1. 741
19.3.2. 744
19.3.3. 747
19.3.4. 750
19.3.5. 751
19.3.6. 753
19.3.7. vector 753
19.4. 757
19.4. l . : 758
19.4.2. 760
19.5. 762
19.5. 1. 763
19.5.2. - 765
16

19 .5 .3 . 766
19 .5 .4 . unique_ptr 767
19 .5 .5 . 769
19 .5 .6 . RAII I vector 770

20. 777
20 .1 . 778
20 .1 .1 . 779
20 .1.2. 780
20 .2 . STL 783
20 . . 787
20.3 .1 . 790
20 .4 . 791
20.4 .1. 793
20.4.2. 794
20 .5 . vector 797
20.5.1. 799
20 .5 .2 auto 799
20 .6 . : 801
20 .6. 1. 803
20 .6 .2 . 804
20 . 7. vector , li s t s tring 808
20.7 .1. insert erase 810
20.8 . vector STL 813
20 .9 . STL 815
20 .10 . 817
20 .1 .1 . 820

21. 827
21 .1 . 828
21 .2 . : f ind () 829
21 .2.1. 832
21 .3 . : f i nd_i f () 833
21 .4 . 835
21.4 .1 . 836
21.4.2. 838
21.4 .3 . - 839
21 .5 . 840
21.5 .1. accumulate () 841
21 .5 .2. accumulate () 842
21 .5 .3 . inner_product () 844
21 .5 .4 . inner_product () 845
21 .6 . 846
21 .6 .1. map 84 7
21 .6.2. map 849
21 .6 .3. map 852
17

2 1.6.4. unordered_map 854


2 1.6.5. set 857
21.7. 859
2 1.7. 1. 859
2 1.7.2. 860
2 1. 7.3. set 863
21.7.4. copy _if 863
2 1 .8. 864
2 1 .9. 866

IV. 873
22. 875
22.1. , 876
22.1.1. 877
22. 1.2. 878
22. 1.3. 887
22.2. 89 1
22.2. 1. 892
22.2.2. 894
22.2.3. Algol 90 1
22.2.4. Simula 909
22.2.5. 91 1
22.2.6. ++ 9 15
22.2.7. 919
22.2.8. 920
23. 925
23.1. 926
23.2. 926
23.3. - 930
23.4. 931
23.4. 1. 937
23.5. 939
23.6. 94 1
23.6. 1. 944
23.7. 945
23.8. 947
23.8. 1. 948
23.8.2. 949
23.8.3. 950
23.8.4. 951
23.8.5. 951
23.8.6. 952
23.8.7. 954
18

23 .9 . 955
23 .10 . 960

24. 965
24 .1 . 966
24 .2 . , 967
24 .2 .1 . 970
24 .3 . 971
24 .4 . 972
24 .5 . Matrix 974
24 .5.1 . 975
24 .5 .2 . 978
24 .5 .3 . 981
24 .5 .4 . - 984
24 .5.5 . 984
24 .6 . : 985
24.6 .1 . 987
24.6 .2 . 988
24 .6 .3. 989
24 .7 . 991
24 .8 . 994
24 .9 . 995
24 .10 . 997

25. 1003
25 .1 . 1004
25 .2 . 1008
25 .2 .1 . 111
25 .2 .2 . 1012
25 .2 .3 . !013
25 .3 . !015
25 .3.1 . !017
25 .3.2 . 1020
25 .3 .3 . 1021
25.3.4 . 1023
25 .4 . , 1024
25.4 .1. 1024
25 .4 .2 . : 1025
25.4.3. : 1029
25 .4 .4 . 1032
25 .5 . , 1036
25 .5 .1 . 1036
25 .5 .2 . i tset 1041
25 .5 .3 . 1042
25 .5 .4 . !047
25.5.5. 1049
19

25 .5.6 . : 1051
25 .6 . 1056
25 .6 .1. ? 1058
25 .6 .2 . 1059
25 .6 .3 . 1065

26. 1073
26 . 1 . 1074
26 . 1 . 1 .
1076
26 .2 . 1076
26 .3 . 1076
26 .3 .1 . 1078
26 .3 .2 . 1078
26 .3 .3 . 1086
26 .3 .4 . 1094
26 .3 .5 . . 1095
26 .4 . 1097
26 .5 . 1098
26 .6 . 1099
26 .6 . 1 . 1101
26 .7 . 1103

27. 1 107
27 . 1 . ++: 1108
27 . 1. l.
++ 1 1 11
1112
27 . l.2. ++, 1112
27 . 1 .3. 1 114
27 .2 . 1115
27 .2. l. 1 116
27 .2 .2 . 1 116
27 .2 .3 . 1118
27 .2 .4 . - ++- 1 120
27 .2 .5 . 1 122
27 .3 . 1 123
27 .3 . l. struct 1 124
27 .3 .2 . 1 125
27 .3 .3 . 1 125
27 .3 .4 . 1 127
27 .3 .5. void* 1128
27 .3 .6 . 1129
27 .3 .7. 1130
27 .4 . 1130
27 .5 . 1132
27 .5 . 1 . const 1135
27 .5.2 . 1136
20

27 .5 .3 .: strcpy() 1136
27 .5 .4. 1137
27 .6 . -: stdio . h 1138
27.6 .1 . 1138
27 .6.2 . 1139
27.6.3 . 1141
27 .7 . 1142
27 .8 . 1 143
27 .8 .1 . . 1144
27 .8.2 . 1145
27 .8.3 . 1146
27 .9 . : 1 147

V. 1 159

. 1161
1 . 1 162
.1.1 . 1163
.1.2 . 1164
. 1 .3 . 1164
.2 . 1165
.2. 1 . 1165
.2 .2 . 1 167
.2 .3 . 1 168
.2 .4 . 1168
.2 .5 . 1 169
.2 .6 . 1 169
. 1169
. .1 . 1170
.4 . , 1170
.4 .1 . 1171
.4 .2 . 1172
.4 .3 . 1173
.5 . 1174
.5 .1. , 1178
.5 .2 . 1179
.5 .3. 1 18 1
.5 .4 . sizeof 1182
.5 .5 . 1182
.5 .6. new delete 1182
.5 .7 . 1183
.6 . 1184
7 . 1186
. 7.1 . 1187
. . 1187
.8 .1 . 1 188
21

.8 .2 . 1190
.8 .3 . 1191
.9 . 1191
.9 .1 . 1192
.9 .2. 1193
.9 .3 . 1194
.9 .4 . 1194
l . , 1195
. 1 . l. 1195
.11 . 1196
.12 . 1197
.12. l. 1197
.12 .2 . 1200
.12 .3 . , 1201
.12 .4 . 1204
.12.5 . 1208
.12 .6 . 1209
.13 . 1209
.13 . 1 . 1210
.13.2. 1211
.13 .3. - 1212
.14 . 1213
.15 . 1215
.16 . 1216
.17 . 1216
.17 .1 . #include 1217
.17 .2. #define 1217

. 1219
.1 . 1220
.1. 1 . 1221
.1.2. s t d 1224
.1.3. 1224
.2 . 1225
.2.1. 1225
.3 . 1227
.3.1. 1227
.3.2. 1229
.4 . 1231
.4 .1 . 1233
.4.2. - 1234
.4.3. . 1234
.4.4. 1235
.4.5. 1235
.4 .6 . 1236
.4.7 . 1237
22

.4.8. 1237
.4.9. 1238
.4.1. 1238
. 5. 1239
.5.1. 1240
.5.2. . 1241
.5.3. 1244
.5.4. 1244
.5.5. 1246
.5.6. 1247
.5.7. 1248
.5.8. min max 1249
.6. STL 1250
.6.1. 1250
.6.2. 1250
.6.3. pair tuple 1252
.6.4. 1253
.6.5. 1254
.7. - 1255
.7.1. - 1256
.7.2. 1258
.7.3. 1258
.7.4. 1259
.7.5. 1260
.7.6. 1260
.8. 1261
.8.1. 1262
.8.2. 1262
.8.3. 1264
.9. 1266
.9.1. 1266
.9.2. 1267
.9. 3. 1268
.9.4. valarray 1269
.9.5. 1269
.9.6. 1270
. l . 1270
. 1 1 . 1271
.11.1. 1271
.11.2. printf () 1272
. 11.3. - 1277
.11.4. 1278
.11.5. 1279
. 11. 6. 1280
. 12. 1281
23

. Visual Studio 1283


. 1 . 1284
.2 . Visual Studio 1284
. . 1285
.3.1. 1285
.3.2. std_ lib_ f acilities. h 1285
.3.3. ++ 1286
.3.4. 1286
.3.5. 1286
.3.6. 1287
.3.7. 1287
.4 . 1287
. FLTK 1289
. 1. 1290
.2. FLTK 1290
. . FLTK 1291
.4. FLTK Visual Studio 1292
.5. , 1292
.
1295
.1. 1296
.2. Widget 1297
. . Window 1298
.4 . Vector_ref 1300
.5. : Widget 1301
1305
13 13

1316
1327

- .
.
. .

.
. .
.

++. .


. .
( .
).
, .
(. . 15
. , 14 ) .
. .
.
. . :
.
.
- . . .
- .
u . .
".
?
. .

1 I (1801-1870) - .

. .
.
26

, " " ,
,
. ,
-
.

(, ,
) , .
, - -
,
.
,
. . .
.
, - .
++? .
, ++
.
. ++
.
. , + + ,
. ,
++,
. /
.
, ++ ,
. . # , Fortran
Java. ,
.

. , .
.
.
,
. ,
.
, -
, ,
. . ,
.
,
. .
.
27

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

. ,
.
-
. "
" .
. , ,
.

. ++
++.
, ,
.
.
: , ,
. ++
. . .
.
, " ++ " (.
, ) .
.
,
. ,
, . -
. ,
.
.
. ,
, ,
. ,
.
. .
28

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

.
, . ,
. .
.

. !



(1exas & University).
60% . 40%
.
. ,
.
.
, .
. ,
. .

. -
29

, .
!
,
. ,
,

.
,
. ,
( ).
: ( ) - ,
. (..
, ),
.
- -
, .
- ,
1 , 22. ,
,
.
, .
, .
, ,
,
- .
, "
",
,
,
.
, .
,
.


, ,
CllIA (Computer Science 1 0 1 ) . ,
.
, (
. .
, ..). ,
30

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

150
++ ISO . ISO
1998 . ++ ++98 .
++11,
(,
, for ,
, -)
. , , " ++11 .
.
++11 ,
IS-, ++14,
++. , . ++ 11
-

++14 . ,
vector<int> vl;
vector<int> v2 {vl}; // ++14


vector<int> vl;
vector<int> v2 = vl; // ++98

++1 1
.

. , ++
( . www. stroustrup . / cornpilers. htrnl).
,
.
31


- ; , www . stroustrup . com/

Pr ogr amming ,
.
, -, ,
.


,

,




.

"" (Law
rence "Pete" Petersen) ,
.
.
, ,
, . "",
, " ".
,
(
ENGR 112, ENGR 113 CSCE 121),
, (Walter Daugherity),
(Hyunyounp; Lee), (Thresa Leyk). (Ronnle
Ward) (Jennifer Welch), .
, (Damian Dechev),
(Tracy Hammond), (Arne Tolstru p Madsen),
(Gabriel Dos Reis) , (Nlcholas
Stroustrup), . . (J. . van Wlnkel), (Greg
Versoonder), (Ronnie Ward) (Leor Zolman)
. r
(Moens Hansen)
32

, . (Al Aho),
(Stephen Edwards), (Brian Kernighan)
(Daisy Nguyen) , ,
.
(Art Werschulz)
,

- , (Nick Maclaren)
,
.

.
, Addison-Wesley
. ,
++ . .
(Richard Enbody), r (David Gustafson), -
(Ron McCarty) . (. Narayanaswamy) .
I (Peter Gordon)
( ) .
Addison-Wesley.
. (Linda
Begley), (m Arney Keane),
(Rob Mauhar), (Julie Nahil)
(BarbaraWood).
,
. ,
(L Joly)
(Michel Michaud),
.
(Brian Kernighan)
- (Doug Mcllroy) ,
. (Dennis
Ritchie) (risten Nygaard)
.

.4, ,,
, ,.,-

1.,t.- -f-'"&4.-

;
- ,
. ,
,
.
.
, ,
;
" " "
" . ,
,
.
34

0 . 1 . 0.3.
0.1 . 1 .
0.1 .2. , .. 0.4.
0.1 .3.
0.2. 0.5.
0.2. 1 . 0.6.
0.2.2. 0.7.

0.2.3. ""

0. 1 .
.
I, "",
++ .
.
, , .
, ,
.
, " ",
,
. , ,
,
,
(GUI).
III, " ",
++ (standard template library -

STL) .
(, vector, list map) ,
, , .
,
(, sort, find inneryroduct).
N, " ",
.
, ,
.
,
. ,
++ ,
0.1. 35


(integrated development environment IDE)
-

(graphical
user interface GUI).
-

,
.
, (
). ,
-
++. -
, ,
.
, .
,
( . 0.2).
.
,
, ,
,
.
" " ( ).

"".

"".

0.1 . 1 .
. ,
.
"" , ,
"" - ,
, .
, .

- .
,
.
. ,
, ,
36

, .
,
.
, . .
(. ,
- ).
. ,
, ,
. ,
" "
, . .
, .
,
- , .
, , -.
.
" .
!" ,
.
. ,
. - ,
, ,
. ( )
, , .
,
.

. , ,
, .

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

.
.
,
, . -
.
" ", ..
, ,
0.1. 37

. ,
- ; ++
The ++ Programming Language. Fourth Edition (Addi
son-Wesley, 20 1 3) ISO ++.

0.1 .2. , ..
-
,
.
.
. ,
, .
.
.
, .
. ,
, .
. , ,
,
. ,
.
, , .
.
.
, .
, ,
.
, , .
,
( , ).
,
.
(, ),
, " ".
,
.
, ;
.
,
, ,
.
38

. . .
, .
- , ,
, - .
;
, .
.
, .
.
,
.
.
-"
++. ,
, ,
.
- .
.
.

0.1 ..

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

.
, .

- (, Bjame
Stroustrup. h ++ Programming Language, Special Edition (Addison-Wesley,
2000)), ,
(, Qt
GUI
, ,
++. . , .
0.2. 39

++. - . : " ", 2002 . -


384 . : . ++ . - . :
, 2002. - 400 . ; . , " " .
- .
. - . : , 2004. - 366 . ) .
0 . 6 " "
.

.
( -
),
.

0 . 2.

? ,
, .
.

( )

- (1 / 0)

//

++

(
). ,
.
40 .n

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

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

, .
"
++.
. , vector
.
,
. - ,

. ,
, ,
.
.
. (
, I?)
. , ,
.
.
. (. - ,
1-11) . -
.
, .
,
, .
. ,
. ,
, , ,
" . , ! ,
, .
,
0.2. 41

, . , ;
.
.
.
. - "
; ,
!" .
. ? .
. ,
. .
, , .
, !

- . ,
++
. ,
. .
,
, .
, . .
. ,
, , . .
, .
.
.
.
.
.
, . . .
.
, . . . .
,
.
" " . ,

.
. .

.
.
++
. . ,
.
42

.
. , -
. -,
. -
- . - .
- www . s trous trup . /
Programm i ng.
, "" .
- ,
, .
, ,
,
. ,
, ,
50 .

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

D.2. 1 .
.
.
", , -
" .
,
. ,

.
, .
,
, . ,
, ,
.
.
. ,
; ,
. , , .
0.2. 43

, , ,
.

" 113 ' .


++
,
, ,
. ++
, ,

.
" .
.
,
, r
.
" - .
.
"

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

,
, . .

. .
.
" 1" .
.

. ,
. .
"
", (
) ,
.
44

"w
". . "
", , " " :


.
" -
" . - -
,
. ,
,
.
(,
),
6 1 2.
" ".

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

0.2.2.
. ,

.
. -
, .
. ,
,
,
.
,
( 5 6), ( 9.4.3) -
0.2. 45

( 9.7 , 1 4. 1 1 4.2),
.

. ,
,
. -
. ,
- . "", .

, .
++.
. . ++,
,
, .

0.2. .
, ++
. ++
, !


.
ISO Standard ++,
. ,

++
.
.
++ .

.
,
Vlsual Studio Microsoft ++
Windows, .
.
.
; .
, ,
, , my_filel .
my_file2 . cpp. GNU ++
Unix Llnux, :
46 D

q++ - my_.Proqram my_filel . cpp my_file2 . cpp


my_.Proqram

, .

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

) . . .
, ,
, .
,
, .

0.4.
-
, . ,
, , ,
. ,
:
,
;
. ,
.
, ""
.
0.5. 47

0.5.
;
.
.
; .
, , , ,
. .
, ,
, , ..
.
- www . stroustrup . com/Prograrnrning.

0 . 6 .
, , ,
.
Becker, Pete, ed. The ++ Standard. ISO/IEC 1 4882:20 1 1 .
1. Blanchette, Jasmin, and Mark Summerfield. ++ GUI Programming with
Qt 4, Second Edition. Prentice Hall, 2008. ISBN 0 1 32354 1 60.
2. Koenlg, Andrew, and Barbara . . Acce/erated ++: Practical Pro
grwnming /. Addison-Wesley, 2000. ISBN 020 1 70353.
3. Meyers, Scott. Elfective ++: 55 Specific Ways to Improve Your Progrwns
and Desigrts, hird Edition. Addison-Wesley. 2005. ISBN 032 1 334876.
4. Schmidt, Douglas . , and Stephen . Huston . ++ Network Programming,
Volume 1 : Masterin.g Complexity with and Patterns. Addison-Wesley,
200 1 . ISBN 020 1 604647 .
5. Schmidt, Douglas . , and Stephen . Huston. ++ Network Progrwnming,
Volume 2: Systematic Reuse with and Frameworks. Addison-Wesley,
2002. ISBN 020 1 795256.
6. Stroustrup, Bjarne. h Design and Evolution qf ++. Addison-Wesley,
1 994. ISBN 020 1 543303.
7. Stroustrup. Bjarne. "Learning Standard ++ as New Language." /++
Users Joumal, 1 999.
8. Stroustrup, Bjarne. h ++ Programming Lan.guage, Fourth Edition. Addi
son-Wesley, 20 1 3 . ISBN 032 1 563840.
9. Stroustrup, Bjarne. Tour ojC++. Addison-Wesley, 20 1 3 .
ISBN 032 1 9583 1 4.
1 0. Sutter, Herb. Excepiional ++: 4 7 Engineerin.g Puzz/es, Programmin.g Prob
lerrts, and Solutions. Addison-Wesley. 1 999. ISBN 020 1 6 1 5622.
.
48

. 7.
w ,
?" ,
. . ,
w"
.



++.



.
,
,
,
, .
.
,
++
. ,
-
ISO ++,
.
, .
, w urm ++" " 11
++", .
, ,

.
, . &,

(Computer Science Research Center of Bell Labs;
Unix, ++. ),
- & Labs-Research.
(U .S.
National Academy of Engineering).
(Association for Computing Machinery - ).
(lnstitute of Electrical and Electronics
Engineers - IEEE). .
0. 7 . 49

2005
(Willlam Procter Prize for Scientific Achievement),
Sigma Xi .
20 1
-, , -
Rigmor og Carl Holst-Knudsens Videnskapspris. 20 1 3

, -. .
. . .
. .
( . .
) ( . , ) .
.
.
, .
-
www . s t rous t rup . . , .
2

" "
2006 : " -
.

(Thxas & ) .

(Thaching
Excellence Awards). 1 996
(Distinguished
Thaching Award)
(Alumni Association for the
College of Engineertng) .
(Wakonse Program for Thaching Excel
Ience).
(Academy for Educator Development).
. .
,
-

2 - http : / /www . s t roust rup . com/bs_fa q . html


, ,
, . .
, .
. . . -
50

. 1 97 1 1 973
-, (Field Artillery
Officer's Advanced Course at Fort Sill, Oklahoma). 1 979
- 1 978 1 98 1 1 985
1 989
ClllA.
1 99 1 ,
1 999 .
,
, .

(Georgia Thch),
.
.

.
. , .
, .
,
, , , .
".
, 2007 .
.

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

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

.
:
E-maH: i n f o @w i l l i amspu i shing .
VVVV:VV h t tp : / /www . wi l l i amspu i s h ing . com

: 1 27055, . . , . 43, 1
: 03 1 50, , / 1 52
,

, ,
, ,
.

,
.
,
,

, .
54 1 , .n

1 .1 . 1 .5.3.
1 .2. 1 .5.4.
1 .3. 1 .5.5.
1 .4. 1 .5.6.
1 .5.7.
1 .5 .
1 .6.
1 .5 . 1 .
1 .5.2.

1 . 1 .
,
. ,
,
. ,
, . .
. .
, ,
, .
,
.
, .
,
.
.
.
?
?
. ?
.
?
, ,
..? ?
?
- ,

, ! ,
,
- ,
,
, ,
1 .2. 55

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

.
-
. .

1 .2.
.
, . -
, .
,
, , , ,
, .
. ,

, .
? ;
. , .
, , , ,
.
( )
? ,
, - , - -
- . , (
. ),
. ,
, .
.
,
. - ,
, ,
, :
, ..
,
! ,
, ( , , . . ) ;
,
56 1 . r

. ,
,
( ).
, ,
r .
,
. , ,
, .
,
.
?
. , . -
, ,
. ,
,
, .
; ,
. ( ,
, ..).
, ,
, .
, , ; ,
,
( )
-
. ,
, ( ,
) . ?
;
, (
, )
. (, ,
, )
.
;

(. . ) .
. ,
,
.
.
, .
, ,
1 . 2. 57

:
.
.
.
, , .
" ";

.
. . , .
,
.
,

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

.
, .
, .
. ,
, . , !
,
. ,
.
, .
58 1 .

,
. .
( ),
, ,
. ,
.

1 ..
.
,
.
. , -
, - - ,
- . .
.
h " "
. , ,
- .
,
. ( - )
, . ,
, .
, ,
, , , ,
.
.
, ?
, ! ,

, ,
. . . ,
. . -.
, , ,
, .
, , , ,
.
, , ,
, . , ,
,
. .
, :
1 .3. 59

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

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

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

.
,
, .
.
- .
, : , ,
. . ,
, .
, ,
, .
,
, -
. . .
. , -
60 1 ,

,
- . JI
-
, .
, - -

. -
- ! -
.

. .
,
, .
. JI
, ,
.
,
, , ,
. , . ,
,
- .
(, ), .
, .
, ? -
,
. ,
- ,
- ,
, , .
, ,
. (Dilbert),
,
(
) . ,
.
JI ?
,
, . ,
, ,
,
, , . ,
,
.
1 . 3. 61

, , ,
.
,
, ,
. -
. .
,
, , ,
.
- ,
.
?

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

. ,
.
, -
.
,

(
) .
-
- ,
.
,
.
,
-
.
.
.
62 1 .

1 .4.

.
, ,
,
. ,
, , , , ,
, .
1 995 ClllA "
", : "
.
, ,
; , ;
; ;
". ,
: " ,
, ,
.
;
(, ) ,
(, )
, . .

,
" .
-
,
,
. - ,
;
,
. , ,
, -
. .
: , -
. .

1 .5.
.
. . ,
1 . 5. 63

. , ,
- , -
, .

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

1 .5 . 1 .

.
.

. ,
, ,
-.
( ,
). ,
.
- (
64 1 ,

) .
. ,
, .

1 .5.2.

.
.

,
.
. . ,
.

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

CAD/CAM
(computer-aided design and computer-aided manufacture).
.
.
1 00 (.
) . , . -
1 .5. 65

, ,
. ,
( ), .
. .
-
( ),
, ,
. -
,
. , , -
(397 56 ),
.
. ;
,
.
,
(
). ,
(global positioning system - GPS), ,
. ,
( , . . )
( ), .
,
.
,
? 25 .
-
. , .
. ,
- , .
, ;
.

1 .5..

( , -, F -
-).
,
. ,
, , , ,
66 1 ,

. ,
,
.
( ""
). , .
, ( )
, .
-.
: ,
. - .
, ,
- , ,
:
.
,
, . , ,
- - . ,
"!" .
,
. ? ?
?
,
,
. ,
( )
. " ".
20 . ;
( , ) (
,
1 .5. 67

).
:
99 ,9999 % (,
20 20 ).

.
,
,
,
, ,
, , , .

.
? ?
?
, ,
, .
.
, , -
,
. .
, ,
.
.

, ,
-
- , (
).
,
,
.
68 1 ,

1 .5.4.


( ).

,
. -
; ,
,

.

. ,

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

,
.
,
.
? ?
?
? ? .
1 .5. 69

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

1 .5.5.

.


: ,
.
. , .

. ,
, .
- ,
-. ,
Google, Arnazo Microsoft,
( , .
). ,
.
Google (
), " " (data
ceter) 25 50.
,
60 1 00 2 .
70 1 ,

20 1 l N York imes ,
Google - 260
( -).

3 24 .
l 2x l 0 1 5 24 l 0 1 5 8- .
4 .
4x l 01 8 .
. .
. .
,

. () -
. () - , . .
. Google
. .
-
. Amazon
( ). Amadeus (
) (-). .
.
,
, .

. , , .
, . .
, . . (
, ).
.
: ()
,
. .
1 2:30?
?
. ?
1 996
?
2 1 2
20 1 2 ?

?
1 .5. 71

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

(information processtng). -
, - ,
,
:
. ,
(,
" " " " )
, ,

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

1 .5.6.
r.
, ,
. , ,
,
. ,
NASA .
72 1 .

, ,
.
, .
. , . , ..
. .
-
. (Spirit) 6 .
( Opportunity)
20 1 4 . ,
.
25-
;
,

. ,
, -
.
, ,
, -
, .
. .
, .
.
, ,
. ,
. .
D/. ,
,
,

( ).
.

,
.

-.
,
( ,
). (
"" ,
"").
1 .6. 73

1 .5. 7 . ?


++? -
.
. ,
++
, . . .
++. -. .
, , .
. ++,
- www . s troustrup . com/ appl icat ions . html.

1 . 6.
?
?
( ) .
. , ,
, - .
. . ..
; ,
. , ,
. , .
. . .
; , -
. .
.
(.
) .
,
.
. .
:
r. .. ,
.
"" ( ), .
, .
.
- (, , ).
..
.
74 1 .

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

. ,
. , ,
, ,
- ,
. , , ,
,
. , " -
", .
" ". ,
,
(
,
).
, "
- ":
, , . , -
, .
,
. -
. ,
, , .
.

. ? ?
? ?
?
. ?
? ?
?
?
1 .6. 75

. ()
.
( , . , ..).
.
.
. .
.
.
,
.
, ,
. ,
.
,
; ,
, ,
; . ,
.
.
.
,
, . -
.
,
. ,
,
, ,
. ,
(
, ,
). !
. ,
- .
, ,
, .
, ,
. , -
.
, .
. ,
. -: ,
. ,
76 1 .

, - .
( ) .
-!

. ,
.
. ,
,
" ". ,
.
-
. ,
.
.
, int,
.
, string,
. ,
, , - "",
,
( ). ,

(), .
, complex
atrix . ,
Shape (), Circle (), Color () Dialog_box
( ) . , ,
. istream (
"i" "" (input)). ,
.
. ,
. .
, .
,
. ,

. ,
( ) .

,
. 22.
1 . 6. 77


,
.
. ,
.
1 . ?
2. ?
3. ?
4. ,
? .
5. ?
.
6.
? .
7. ?
8. ! ,
?
9. ?
1 0. ? .
1 1 .
? .
1 2. ?
.
1 3 .
? .
1 4. -
?
1 5. ?
1 6 . .
1 7.
? .
1 8. ?
1 9. ?
78 1 .


++. ,
, , .
CAD/CAM




1 . .
(. ,
). ,
.
2. ,
- . ,
.
3. , . 2,
,
. . ,
. : , ,
, .
4. , ,
. .
5. ( ),
. ,
(,
-), ,
(,
).
6. , ,
, .
, !
7 . ,
, . ,
. .
1 .6. 79

8. , (
1 00, 500 ). ,
, , .
.
9. , (
, ),
( 1 00, 500 ).
1 . -
, ?
( 1 00 ).
1 1 . .
. .
.
1 2. .
, ,
.
( 1 00 ).
1 3 . )
, ) ) ?
, ,
20/.
1 4. ?
? ?


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

, .
80 1 .

, (
. ). .
. .
, , .
.
. ..
- .
H e l l o, Worl d !

-
- (l )

++. - .
.


.
, -
- .
.
,
.
,
.
84 2 Hello. Worldl

2 . 1 .
2.2.
2.3.
2.4.
2.5.

2.1 .
-. ( - )
- , -
. , " ",
, - ,
.
. ,
,
, " " "
".
:
, ,
. , " , ,
" - , ,
.
, ,
.
. , . ,
.
, .
( ), ..
, , ,
, . ,
, , , .
.
. "
, , ".
? ? ""? ?
? ? ?
? ?
? .
.
( ),
2.2. 85

,
. .
++ - ,
.
,
1 . ,
,
. .

2.2.
.
Hello , World ! .
11 r "Hel l o , World ! "
iinclude " td_lib_facilities . h"
int ain() // r ++ ma i n
{
cout << " Hello , World ! \n"; // "Hel l o , Wo rl d ! "
return ;

,
;
. ,
. :
cout << " Hello , World! \n" ; // "Hel l o , Worl d ! "

.
Hello , World ! ,
; . Hello , World !
. -
, ,
.
++ ( " ) ;
.. "Hello , Word ! \ n " - . \n - "
", . cout
. , " cout"
<<. . cout
"see-out", "character output stream" ("
").
. ,
, , ,
,
.
86 2 Hello. Worldl


11 "He l l o , World ! "

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

, .
, ,
, ,
, , . ,
. 7.6.4
. .
. ,
, .
. -
,
.
.
: , ,
- .
, - ,
.
; .
- ,
, .
11 "Hel l o , World ! "

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

#include " std li facilities . h"

#include.
("") ,
s td lib facilities . h.
2.2. 87

, ++ (
++).

. ++. ,
,
. std_lib facili ties . h
_

,
- ++.
cout <<. ,
#include, . h
(heade), (heade file).
, cout.
.
. ?
main ,
. main :
int main ( ) // ++ ma i n
{
cout << "Hello , World! \n"; // "He l l o , World ! "
return ;

.
++ main. ,

, .
.
- int (.. integer,
), ,
( - ). int
++ ( ).
- ( .
.3. 1 ) .
; main.
, (. 8.2
8.6); ( ) .
1. { }
(). .
, ++
:
int main () { }
88 2 Hello. World!

. .
main "Hello, Worldl" :
cout < < " Hello, World! \n"; // "Hel l o , Worl d ! "
return ;

Hel lo , World ! ,
() . main ( )
, .
( , Unix/Linux)
. (),
main ( ) , , .
++,
#include ( :
. 4.4 . 1 7), (statement).

2 . .
++ - . ,

, . . .
, r.
, ,
, , ,
, .
++ (, hello_ .

world . cpp) . h (, s td_lib_facilities . h) ,


. obj ( Windows) . ( Unix) . ,

: ,
. ,
" " "
",
.

++ ++

, .
, ,
. ,
, .
. - ,
2.3. 89

#include, . . ,
.
,
. .
11
int main ( )
{
cout << "Hello , World! \n";
return ;

,
cout, .
. #include.
#include " std facilities . h"
int main ( )
{
cout << "Hello , World! \n";
return ;

, ,
std_lib_faci li ties . h. .
#include "std lib facilities . h "
int main ( )
{
cout << "Hello , World! \n;
return ;

( " ).
.
#include " std lib facilities . h"
inteqer main ( )
{
cout << "Hello, World ! \n";
return ;

int integer.
++ . .
#include " std lib facilities.h"
int main ( )
{
cout < "Hello , World! \n";
return ;
90 2 Hello. World!

<< ( ) < (
""). .
llinclude " std lib facilities.h"
int ain()
{
cout << ' Hello , World! \ n ' ;
return ;

, ,
. .
flinclude " std lib facilities . h"
int ain()
{
cout << " Hello, World ! \n"
return ;

,
, . , ++
( ; ).

. ,
, .
: ,
} .

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

, ,
: , , ,
++.
2.4. 91

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

2.4.
,
. . "Hello,
World!" . ,
++. ( ..
) ,
,
. , ,
( ) .

++
hello_world.cpp

++


++:

ostream.obj
hello_world.obj

hello_world.exe
92 2 Hello, World!

,
. .
Windows, Windows,
Linux.
- (
). ,
, #include.
-

, ,
: ( . .
, 4.5.2).
. ,
; , ,
. .
,
. ,
,
. .
, .
5.

2.5.
.
, ,
. ,

. ,
, .
,
, ,
. IDE
(interactive (integrated) development environment - (
) ),
,
.
.
,
, . ,
,
, . -

(
) .
2.5. 93

, ,
, ++.
, ,
++,
.
.


. (
, ). .

.
.
,
. "Hello, Worldl" ,
.
-
. ,
-
, ""
.

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

-
.
, ,
; .
,
.
94 2 Hello, World!

,
,
. ,
, .
, .
1 . , ,
. ++
hello world.
2. hello_world . cpp ,
,
hello world.
finclude " std li facilities . h"
int main ( ) // ++ ma i n
{
cout << " Hello , World! \n" ; // "He l l o , Worl d ! "
keep_window_open ( ) ; 11
return ;

keep_window_open ( )
Windows ,
, .
Windows, ++.
,
keep_window_open ( ) std_lib_faci lities . h.
s td_lib_facilities . h?
, . ,
www . s trous trup . com/ Programming.
, ? ( !)
linclude
linclude<iostrea>
linclude<strinq>
linclude<vector>
finclude<alqoritlun>
linclude<cath>
usinq naespace s td ;
inline void keep_window_open ( ) { char ch ; cin>>ch ; }

. 5
8. 7.
3 . "Hello, World!".
, .

2.5. 95

.
!
,
, , ,
.
4. ,
. ,
!
, ,
2.3, .

(, keep window open ( ) ,
_ _


) ,
.


-
, , .
;
, ,
.
, ,
? , .
,
? , ,
?
1 . , "Hello, Worldl"?
2. .
3. ,
++.
4. , return "Hello,Worldl"?
5. , ?
6. , #include?
7. . h ++?
8. ?
9. ?
10.
?
11. , ?
96 2 Hello. World!

,
. ,
, .
:
,
.

11

++. ,
, , .
tinclude

11
<<
++
cout
IDE
main ()
,
.

;
, .
.
1 . , :
Hello , proqrai nq !
Here we qo !

2. , ,
,
, 2 . 1 .
, .
- ? .
, . :
" - .
, (.
),
. ,
. .
2.5. 97

3. ,
( , : ,
).
. ,
.
4.
(
, - ).
, ,
,
, .

. , ,

. , ,
!
,
.
,
. ,
, ,
,
.
(
, ,
). , (
, ) .
. (
? ?
?)
.
5. ,
"". ,
( ), ,
. ,
.
- . ,
www . stroustrup . com/glossary. html . ,
.
, .
.
98 2 Hello. World!

,
.
, .

"Hello, Worldl" ? -
.
.
:
,
, ,
.
,
. .
,
.
, ,
.
,


.
i -
.
, ,

char, int , doule
string.
100 ,

3.1. 3.6. 1 . : -
3.2.
3.3. 3.7.
3.4. 3.8.
3.5. 3.9.
3.5 . 1 . : 3.9. 1 .
3.9.2.
3.6.

. 1 .
"Hello, Worldl" .
. , ..
. . , ,
- , ,
, .
, ,
; , -
, , . -
"" . - ,
, ,
. . ,
string, -
int. "",
, .
lnt :
age: j 42

, int age,
42. ,
,
.
11
#include " s td lib facilities . h"
int main ( )

cout << "'l' ( ' enter ' ) : \n " ;


strinq first_name ; // fi r s t_name s t ring
-

cin >> first_nae ; // fi rs t


cout << " , " << first name << " ! \n " ;
.1. 101

#include main ( ) 2.
#include (
1 2). , .
.
main ( )

cout << "'l' ( ' enter ' ) : \n " ;

,
.
main ( ) ,
.
.
- . string
first_name.
. .
strinq first_naJll8 ; // fi rs t_name - s t ring


first_name.
string :
first_name : '
----

.
, 11w .
()
:
cin >> first_na.s ; // fi rs t_ name

cin ( "-"
character input),
. >> (" ")
, . ,
. Nicolas, <Enter>
. "Nicolas " first_name.
strlng :
first_name : 1 Nlcholas 1
,
.
( <Enter>) . -
. "" ,
,
102 ,

<Enter>.
. .
first_name,
.
cout << " , " << first_nae << " ! \n" ;

,
Nicolas ( first_name) ( ! )
( ' \n ' ) .
, Nicolas !

,
.
cout < < " , " ;
cout << first_nae ;
cout << " ! \n " ;

. ,
(
), .
, " "
, first_name.
.
, , .

cout << "first n " << " - " << first_nae ;

" first_name" ,
first_name
first_name, - Nicolas. ,

first n - Nicolas

.2.
, ,
,
. "",
, .
.
(, int s tring), ,
(, int
1 2 3 , s tring -
.2. 103

"Hello , World ! \n"). (


, int
*, string <=).
, , . ,
, ( ) .
( )
. :
strinq name "Annemarie" ;
=

int numer_of_steps 39 ; =

:
int : string :
number_of_steps : 1 39 name : 1 Annemarle

.
strinq name2 39; = // : 3 9 -
int numer_of s teps = "Annemarie" ; 11 : "Annema ri e " -
_
//


. ,
.
++ ( .
.8). ,
.
int numer_of_steps = 3 9 ; 1 1 i n t -
doue flyinq_time = 3 . 5 ; 1 1 do ue -
1 1 .
char decimal_yoint ' 1 1 ch a r -
strinq name "Annemarie" ; 1 1 s t ring -
bool tap_on true ; = 1 1 bool -

doue :
"
" ("doue precision floating point. ) "


.
,
.
39 11 i n t :
3.5 // dou e :
1 1
// cha r :
"Annemarie" // s t ri n g : ,
11
true // bool : ,
104 ,

, (, 1234 , 2 976)
, (.
' 1 ' . ' @ ' ' ' ) ,
(. 1 . 234, . 12 . 98)
, ,
(, 11 1234 11 , 11Howdy ! 11 11Annemarie 11 ), .
. 2.

..
>> (" ") ,
..
, . .
11
int main ( )
{
cout << " , \n" ;
s trinq first_nae ; //
int aqe ; //
cin >> first_nae ; / /
cin >> aqe ; //
cout << " , " << first_name << " ( aqe " << aqe << " ) \n " ;

, Carlos 22. >>


Carlos first_name, 22 -
age :
, Carlos ( 2 2 )

first_name Carlos 22?


,
(whitespace), .. ,
.
>> . .
;
>> .
22 Carlos.
, ,
. 22 first_name, .
, 22 - .
. Carlos ,
. 22,
11 ( 11 -
, -96739 . ?
.. 105

age, .
- " ",
. 1 0. 6
, .
age .
.
// ( 2 - )
int main ( )

cout << "Ji , \n" ;


strinq first_name " ? ? ? " ; 11 ( " ? ? ? "
11 " ")
int aqe = - 1 ; // (-1
11 " ")
cin >> first name >> aqe ; // ,
11
cout << " , " << first name << " ( " << aqe << " ) \n" ;

22 Carlos :
, 22 ( - 1 )

,
,
. , <<.
>>. . age
int first_name
" , 11 1 1 ( 11 11 \n 11

s tring >>
( ), :
. >> .
.
. . , ,
.
int main ( )

cout << "Ji , \n" ;


strinq firs t ;
strinq second ;
cin >> first >> second ; //
cout << " , << first << ' ' << second << ' \n ' ;
"

>> .
. .
.
106 3 .

&
" ". .
: ,
. 1 2 ( * ) .
doule, ,
, .

.4.
, ,
, , .
.
int count ;
cin >> count ; // >> coun t
strinq name ;
cin >> name ; // >>
int 2 count+2 ;
= // +
strinq s2 name + " Jr . " ;
= // +
int count-2 ;
= // -
strinq s = n - " Jr . " ; 11 : "_ "

.
, .
,
, , ,
. ,
,
, , :
int aqe = -100 ;

, (
?), ,
.
.

bool char int doule s tring

=
+ +
+

* *
/ /
%
3 .4. 107

bool char int doule s t ring

1 ++ ++
1
n +=n +=n
+=
n - =n -=n
*= *=
/= /=
%=
s s>>x s>>x s>>x s>>x s>>x
s s<<x s<<x s<<x s<<x s<<x
--

!= != != != !=
> > > > >
>= >= >= >= >=
< < < < <
<= <= <= <= <=

.
(
; . 3.9. 1).
. I, .
.
. .
11 ,
int main ( )

cout << " , nJ:11 : " ;


doue n ;
cin >> n ;
cout << " n = << n
"

<< 11 \nn+l " << n+l


=

<< 11 \n n == " << *n


<< 11 \n n == " << n+n
<< 11 \nn " << n*n
=

<< 11 \n n = << n/2


"

<< 11 \n I n == " << sqrt (n)


<< ' \n ' ; //

,
. .
.
; ,
108 .

.
. n
sqrt ( ) .
.
4 . 5 8.5.


. ,
int, douhle.
, sqrt ( ) ,
n doue
sqrt ( ) . , .
, /
, %
-

, 5/2 2 ( 2 . 5 3), 5%2 1 .


* , / % .
int /* + % == .

, ,
23, .
,
:
11
int ain ( )
(
cout << " , \n" ;
string first ;
string second ;
cin >> first >> second; 11
string n = first + ' ' + second ; 11
cout << " , " << n << \n ' ;

+ : .
sl s2 string, sl+s2 ,
s l s2. .
sl "Hello " , s2 "World" ,
-

s l+s2 " HelloWorld" .


.
11
int ain ( )

cout << " , \n " ;


string first ;
string second ;
3.5. 109

cin >> first >> second ; //


if (first = second) cout << " :l'l'\n" ;
if (first < second)
cout << first << " n111 " << second
<< " " << ' \n ' ;
if ( first > second)
cout << first << " " << second
<< " " << 1 \n ' ;


if, 4.4. 1 . 1 .

.5.

, =.
. .
int = ; // 3

: 3

= 4; 11 4
// ( " ")

: 1 4

int = ; //
11 (. . 4)

: 4

: 4

= +5 ; 11 + 5 (. . 9)

: 4

: 9

= +7 ; 11 + 7 (. . 11)

: 11

: 9
110 .

. -.
. "" ,
, +7 .
. .. .
, =+7 .
1 . ; 4.
2 . . 1 1 .
3 . 1 1 .

strinq = " alpha" ; // "a lpha "

: 1 alpha

= "beta " ; 11 "be t a "


// ( "be ta ")

: 1 beta

strinq ; //
// (. . "be ta ")

: beta

: beta

= +" q. " ; / / + "gamma "


11 ( . . "be t a gamma ")

: beta

: betagamma

= a+ " delta " ; 11 a + "del ta "


11 ( . . "be t a del ta ")

: betadelta

: betagamma


" " ".
, :
( ).
( ).
3.5. 111

, ++
=.
int = 8 ; // 8
= 9; 11 9

strinq t = "howdy ! " ; // t


// "howdy ! "
s = "G day" ; // s
// "G ' da y "


. ,
(. int string). - .
, .
, ( )
.
. -
(, ). .
, ,
, , (..
) ("
"), . ,
, ,
.

.5. 1 . :
,
. , ,
,
. ,
. ,
.
.
int ma.in ( )
{
strinq previous " "; // previ o u s ;
// " "
strinq current ; 11
while ( cin>>current) //
if (previous current) //
==

//
cout << " : " << current << ' \n ' ;
previous = current ;
11 2 ',

,
. .
. .
string current ; 11

,
(.. )
while ( cin>>current)

, while, .
4.4.2. 1 . while
, , (cin>>current),
, cin>>current .
,
. , string
>> , .
( .
). Windows
<Ctrl+Z>. - <Enter>. Unix
Linux <Ctrl+D>.
,
curren t (
previous). ,
.
if (previous == current) //
cout << "su : " << current << ' \n ' ;

.
current previous:
previous = current ;

, .
, ,
?
previous :
string previous = " " ; // previ o us ;
11 " "

11 11 (, -
). >>
, .
, while
if (previous == current)

, ( ).
3.6. 11

, "
", .. .
, -
, .

6
,
.
The cat cat j wnped.

.

6

. She she laughed
because what he did did not look very very good good.
? ?
? w ? ,
She she ?
-

.6.
(..
) ++,
. ,

++counter

counter = counter + 1

. , 7, 9
2.
++, :
+= 7 ; 11 = +7
9 ; 11 = -9
*= 2 ; 11 = *2

oper oper=
= oper (. .5).
+=, -=, *=. /= %=.
114 .

. , *= /=
"".

3.6.1 . :
, .
,
. ,
.
int main ( )
{
int nWDer of words = ;
string previous = "" ; //
string current;
while ( cin>>current) {
++nWDer_of_words ; //
if (previous == current)
cout << " " << current
<< <<
" 11
nWDer of words
<< <<
11
.
" ' \n ' ;
previous = current ;

. ,
, :
++nWDer_of_words ;
, 1 , - 2 ..
:
nWDer of words += 1 ;
:
nWDer of words = nWDer_of_words+l ;
++numer_of_words
.
,
3.5. 1 . , 3.5. 1
.
: - ,
, -
. " ",
.
.
3 . 7 . 11 5

3 . 7 .
,
. ++?
++
, ,

numer of elements
Fourier transform
z2
Polyqon

:
2 //
tie$ to$arket // $ ,
-

Start menu // - ,

,
, . ++
.
.
, ,
, _foo.
;
. ,
.
; ,
,
-

. ,
.
#include " s td lib facilities . h "
int &in ( )
{
SRING s " , ! " ;
"'

<< <<
cOut S ' \n ' ;

, , ,
one One, ;
-

, .

&
", !"

. ? -
116 3 ,

?
? .
, , ( )
.

++ .
.. 1 .
, , ..
.
int if = 7 ; / / : "i f" -

,
(, string), .
,
.
int s trinq = 7 ; //

, .
, ; ,
, , -
. .
, xl, 2 . s
7 . ,
.
, ,
:
.tf
TLA
w
NV

, . ,
i, , ..
(.
4 . 5 8.4), i - (. 4.4.2.3).
; ,
. , .
:
partial_swn
element count
stale_yartition

:
.. 1 117

the nuer of eleents


reaininq free_slots_in_symol_tae
_

, element_count,
elementCount ElementCount.
, ,
ALL_CAPITAL_LETTERS,
(. 27.8 . 1 7.2),
.
, Square Graph. ++
,
int string, Int String. ,

.
, ,
, :
Name names nameS
foo fOO fl
fl fI fi

(). ( ), (
), 1 (), 1 ( L), I ( i).

..
++
.
. ,
.
.
u ,
.
,
-

.
,
-

.
- .
- ,
.
- , .
11 8 3 .

,
.
int , 7, 42 -399.
string ,
" Interoperaility" , " tokens : ! @ f$ % " &* " "Old acDonald had farm" .
:
int 7; : 1 7

int 9; : 1 9

char = '' ; :
[!]
doue = 1.2; :
1 1 .2

strinq sl " Hell o , World ! " ; s1 : 1 13 Hello, Worldl

strinq s 2 "1. 2" ; s2 : 1 3 1 .2


s tring ,
int, string .
. doue ,
string - . , 1 . 2,
s2 - : ' 1 ' , ' . ' ' 2 ' .
.
int : .
int
.
4 (32 ).
l , char doue.
l char, , (8 ),
doue - 8 . ,
. ,
char , int,
string doue. int
char , .
, , ,
.
: :
. , ,
, ,
.
. 12 . 5? .
12 . 5 . , 12 . 5 12 . 5 . ,
12 . 5 , .
,
120, int,
' ' , char.
. 9 . 119

string, ,
.
( 1
).

1 00000000 00000000 00000000 01 1 1 1 000


, (),
int (120) char ( ' ' ,
8 ). - ,
. 1 . ()
.2. 1 . 1 .

. 9 .
. -
-
(type-safe),
, . , .
.
,
.
int main ( )
{
doue ; // ,
//
doule ; //
doule z 2 . + ; // + z

11


.
!
- - (,
).
- .
.
, .
. , ++
,
,
.
, .
1 20 3 .

. ,

. , ,
, ,
++.
,
,
.
.
. ,
.

. 9 1
. .

3.4 ,
char doue int.
++ .
char int,
int - doue. .

char ' ' ;


int il ;
int i2 '' ;

i l i2 120, .. 8- ASCil-oy
' '
.

. char
int , :
. .
int, char .
char 2 = il ;
cout << << ' ' << il << ' ' << 2 << ' \n ' ;

:
120

-
( doue)
- .
l char
bool int
bool doue
char int
char doue
int doue
. 9 . 1 21

in t
doue,
.
doue dl = 2 . 3 ;
doue d2 = d1+2 ; // 2 2 . 0
if (dl < ) // .
cout << "dl " ;
-

int
doue ( )
. .

3. 9 .2.

. , ++
() .
,
, .
.
int ain ( )
{
int = 20000 ;
char = ; // i n t
// ch a r
int = ;
if ( ! = ) // ! = " "
cout << " ! : " << << " ! =" << << ' \n ; 1

else
cout << " ! char\n" ;

"",
, ("")
. ,
char
int. , int, ,
char, ( )
int, char.
.
int ain ( )
{
doule d = ;
while ( cin>>d) { // ,
11
int i = d ; // do u e i n t
1 22 .

char = i ; // i n t cha r
int i2 = ; // ch a r
cout < < 11 d=11 < < d // d
<< " i= 11 << i // i n t
<< 11
i2== 11 << i2 11 ch a r
<< 11 char ( 11 << << 11 ) \n" ; //

while,
, 4.4.2. 1 .

&
. .
(, 2 );
(, 127; , 1000); ;
56, 89, 128; (, 56 . 9 56 . 2).
doue int int char
, , (
) .

, ""
. , ,
-.
, .
doue int
doue char
doue l
int char
int bool
char bool
,
, ,
. ? -
, .
.
doule 2 . 7 ;
// -
int ; // 2

,
douhle, , doue
int ( ).
: .
. 9 . 1 23

int char -
int, char .
char
. char
1 . int - 4 .
char :
int :
11
, ,
1 . char :
"". .
int = 1000 ;
char = ; // -24

int char.
char .
char
[-128, 127J,
[0,127J, .
char,
[0,255J .
?
: ++
. .
++ .
.
,
,
. , "
. ". .

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

:
doue { 2 . 7 } ; 11 !:\.
int { } ; 11 : doue -> i n t
int ( 1000 } ; 11
char ( } ; 11 : i n t -> cha r
1 24 3 .

.

, :
char { 1000 } ; / / : (
// - cha r)
char 2 { 4 8 } ; //

, ,
? { }
, ,
,
.
5 .6.4 7 . 5 .
(universal and
unifonn initialization). .



, , .
, .
1 . ,
.
3. 1 ,
", first_name", first_name - ,
.
: " "
" first_name , " (
!).
2. . " ?
. " . ,
.
- .
3.
friend_name.
: " friend_name?" .
4. char friend_sex
. m,
- , f .
-

friend_sex . if
.
3 . 9 . 1 25

- , " friend_
name, , ".
- , " friend_
name, , ".
5.
age, int.
" ,
age ". age
1 1 0, simple_error ( " 'l'
! " ) , simple_error ( )
std lib faci l ities . h.
6. .
1 2 , "
age+ 1 ".
1 7 , "
".
70 , " ,
".
,
.
7. " ",
.


1 . ?
2. ?
3. ,
numer, ,

?
4. \n ?
5. ?
6. ?
7. ?
11 .
cout " , '

cout first_name ;
cout " ! \n " ;

8. ?
1 26 .

9 . ?
1 0. ?
1 1 . ?
1 2 . char, int doue?
1 3 . , -
, int string?

1 4. = =?
1 5. ?
1 6. ?
1 7 . ++?
1 8 .
++? , , .
hi_l ittley ig hi 1 i fine 2_For_l_pecial
latet thing the_$ 12_ethod thi i ok
iniineine nuer correct?
1 9. .
, .
20. .
2 1 . ?
22. doue int
?
23. , ,
.

cin

1 . .
.
2. ++ ,
.
. :
1 .609 .
3. 9 . 127

3. . .
(
, int doule = ; ). .
4. .
. int
val l val2 . .
, , ,
.
5. ,
doule.

. ?
? ?
6. .
, ,
. , 10 4 6.
4, 6, 10. .
. .
4 5 4, 4, 4, 5.
7. . 6 . ,
Steinbeck, Hemingway, Fitzgerald,
Fitzgerald, Hemingway. Steinbeck.
8. .
. . , .
,
"" "". , "
4 JJ " . : .
3.4.
9. , "", ""
. 2 .. ,
.
, 1 , 2, 3 4.
- , "r ! " ,
" r ! "
1 0 . ,
.
:
+ 100 3 . 14
* 4 5
1 28 3 .

operation ,
if, ,
, if (operation= " + " ) .
doue. +, -, * /, plus, minus. mul
div, .
1 1 . .
1 -, 5-, 1 0- , 25-, 50- .

, " ?"
.
23 w .
17 w .
14 w .
7 w .
3 w .
573 .
:
, . ,
"1 4 " "1 " ( " 1
") . ,
, .. 5 73 . 573 .


. -
.

(. 6 9 11-IV).

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

, vector,
.
1 30 4

4. 1 . 4.5 .
4.2. 4.5 . 1 .
4.3. 4.5.2.
4.3.1 . 4.6.
4.3.2. 4.6. 1 .
4.3.3. 4.6.2.
4.4. 4.6..
4.4. 1 . 4.6.4.
4.4.2. 4.7.

4. 1 .
- ; .
- - .
, , ,
( t "" .
- -

. .) ,
.


(
)

, ,
, ,
. " "
: ,
, ,
.. .

, (data stru
ctures) (states) . , ,
,
.
, ,
. ,
-
.
(, )
(, ).
4.1 . 1 31

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

- " " "
".
:
.
. .



lj jj !!

"1/0" "-".
.
, ,
(, )
.
, ,
r (,
). ,
(, ),
, (,
).
"" "", ,
,
, ,
, .
, ,
, , .
-

wn.
,
.
(), 4 9, (),
1 32 4

7 . () square
(. 4.5). , 1 950-
() ClllA .
. . .
(
). .

4 . 2.
- .

;
;
.
, : ,
, .

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

,
. ? ?
? ,
? ? ,
?
. (
)
.
,
,
-
; . ,
. ,
, ;
.
,
.
,
4.2. 1 33

: .
. ,
,
,
.
-
, , -

. .
. ,
(
) . .
(
)
sort ++. ,
, - , ,
sort () , -
: sort ( ) sort (
2 1 .8 .5.4), std library . h.
.
,
,
( 3.2), vector (
4.6, 1 7- 1 9), map ( 2 1 ) . .
" " .
. ,
, : ,
.
.
? ,
, , ,
. ,
. ,
, . ,
,
, .
, , ,
1 , 1
, 1 00 ,
,
1 00 . , , 1
, " "
1 34 4

. .

.
. ,
, .
, ,

. ,
,
.
.
; .
. ,
, ,
++,
.
. , -
-.
.
.
,
: .
.
? (. , )
, .
.
. .
.
-
. - ,
"",
.
(
).

. . ,
. . ,
( ).
. .
.
. .
4 .3. 1 35

4. .

.
.
. ' ' , . 14
"Norah " .
. -
. . .
// :
int lenqth 20 ; = // ( -
11 )
int width 40 ;=

int area = lenqth*width ; //

20 4 0
length width, .
; , length
width. " length"
", length".
.
lenqth = 99; // l ength 9 9

length,
, " length",
: " 99 length".
length,
( "lvalue length" "
length") (
"rvalue length", " length"
" length").
. .
int :
length : 1 99

, length - int,
99. ( lvalue) length ().
( rvalue) - ,
.
, + ,
, , .
.
int perimeter = ( lenqth+width) *2 ; //
1 36 4

:
int perimeter = lenqth*2+width*2 ;

:
int perimeter = lenqth+width*2 ; 11 wi dth *2 l ength

,
. perimeter,
.
, .
, - .
,
, length+width *2
length+ (width*2) . a*b+c/d (*) +
+ ( c/d) , * (+) /d.
.5.
: W ,
".
, a*b+c/d.
, (*) + (c/d) ,
.
?
, , , ,
.
, .
.
, , .

a*b+c/d* ( e-f/q) /h+7 //

,,. 1 .
, , .
,
", -
2 . 54. ,
(, pi, . 14 1 59).
. ++
, .. ,
.
.
4 .3. 1 37

constexpr doule pi = 3 . 14159 ;


pi = 7 ; // :
int v = 2 *pi/r ; // : , pi

.
. . , ,
3 . 14159 "",
299792458? , .
"" 1 2 , .
, . 1 4 , -
"" 22/7, ,
, .
pi, .
constexpr doule pi = 3 . 141592653 5 9 ;

,
( , 1).
-
. (
) .
, case ( .
4.4. 1 .3), ++
. .. ,
. .
constexpr int = 1 7 ; //
int val = 1 9 ; 11
max+2 //
11 ( )
va1+2 11 :
// .

, 2 99792458
-

, ,
. . , ,
.
"" !
constexpr ,
. :
constexpr int = 100 ;
void use ( int n)
{
constexpr int cl = +7 ; // : cl 1 0 7
constexpr int 2 n+7 ; // : 2
11 . . .
1 38 4

. ""
, ,
. ++
(cons t) :

constexpr i n t 100 ;
void use ( int n )
{
cons texpr int cl = max+7 ; // : cl 1 0 7
const int 2 = n+7 ; // ,
11 2
11 . . .
2 7 ; // : 2 con s t

" "
.
++98 cons texpr,
const.
"", (
),
,
.

4. .2.
.

.
, .
.


f () f
++ lval

- - lval

! bool
-
*
/
%
+
4. . 1 39


-
out<< out out - ostream

inb in in - istream

< bool

<= bool
> bool
>= bool
== =
! = bool
&& bool

al lb bool
lval=a =
lval*=a lval=lval *; / , %,
+-

, .
lval ( ",
"). .5.
& & (). 1 1 () ! ()
5 . 5 . 1 , 7 . 7 , 7.8.2 1 0.4.
, << ( <) <,
< l , ..
true, false. , << ,
true<c. fal se<c.
, << "
?", ( )
. , <<
.
, , - ,
.
:
++
a+=l
a=a+l
? ? ,
, ++,
. ,
(
).
, .
1 40 4

, .
a=a+l , ,
. ,
a=b+l, =+2 =-1;
++,
. , ,

, . ,
,
a=a+l ++, ,
.
*= scale, = a* scale.

4. . .
"" . . 2 . 5/2
doue int.
? : ?
; , 5/2 2.
,
; , 5 . 0/2 . 2 . 5. ,
" 2 . 5/2:
?" : " , ;
".
1 . 25, 1 , 1 . 25 . (
) : doule,

doule; ,
int. :
5/2 2 ( 2 . 5)
2 . 5/2 2 . 5/doule ( 2 ) , .. 1 . 25
' ' + 1 int{ ' ' } + 1
type (value) type { value } " value
type. type
value". ,
("") int doule,
char - int. .

, :
doule d = 2 . 5 ;
int i = 2 ;
doule d2 = d/i ; // d2 1 . 25
4. 4. 1 41

int i2 = d/i ; 11 i2 == 1
int i { d/i } ; 11 : do ue -> i n t
11 ( 3 . 9 . 2)
d2 = d/i ; 11 d2 1 . 25
i2 d/i ; 11 i2 == 1

:
, .

: f= 9/ 5 + 2. :
doule dc ;
cin >> dc ;
doule df = 9/5*d+2 ; // !

, ,
: 9/5 1 , 1 . 8,
. , 9,
5 ( ) doule.
doule dc ;
cin >> dc ;
doule df = 9 . 0/S*dc+32 ; //

4 . 4 .
,
4.3. ,
? -
? ,
?
? ++, ,
,
(statement).
:
.
, , :
= ;
++ ;

, .
, = , = - ,
-

= ; ;
. ?
. .
= ++ ; // :
1 42 4

, :
=++ ; ; = ; ++ ; .
. , "
!" ?
, . ,
, : " , !"
,
. .
int = 7 ;
cout << << ' \n ' ;

.
. - (
). , ,
. .
1+2 ; // ,
* ; // ,


,
. , , ,
, -
.
: .
:
if ( == 5) ;
( = ; }

, , .
, -, . ,
, ++ -
. , .. ,
. , ,
.
,
.
, ?
, 5.
, ().
,
. 5,
(
) (
, , 5). ,
4.4 . 1 43

if :
.
, ,
.
.
, ,
.

4.4. 1 .
, ,
. ++ if swi tch.

4.4. 1 . 1 . if
++
if, .
.
int main ( )

int = ;
int = ;
cout << " , wr \n " ;
cin >> >> ;
i f ( <) //
11 1 -
11 (, ) :
cout << "lllAX ( " << << " , " <<
<< " ) " << <<" \n" ;
else
// 2 -
11 (, ) :
cout << " ( " << << " , " <<
<< " ) " << << "\n" ;

if .
. ;
. .
.

,
. , , ,
, , ,
, : "
, , ,
" . ++ - :
1 44 4

if ( traffic_liqht==qreen) qo ( ) ;
if ( traffic_liqht==red) wait ( ) ;

, ,
. (
, #include).
11
11 ' i ' ' '
in t ain ( )

constexpr doule c_per_inch 2 . 54 ; //


doule lenqth = 1 ; 11 ( )
char unit = ;
cout << " , "
<< " ( i) : \n " ;
cin >> lenqth >> uni t ;
if (unit -- ' i ' )
cout << lenqth << "in == 11
<< c_per_inch*lenqth << " \n" ;
else
cout << lenqth << " == 1 1
<< lenqth/c_per_inch << " in\n" ;

,
: . lin = 2 . 54cm; 2 . 54,
2 . 54cm = = l in. -
.
,
. .
. uni t= ' i '
' i ' .
' ' .
. 15f () ",
. "? (uni t ' i ' ) ,
-

else ( ).
. . , ,
' f ' .
,
- - - -
. .
.
.
//
11 ' i ' ' ' ,
11
int ain ( )
4 .4 . 145

constexpr doule cm_yer_inch = 2 . 54 ; //


doule lenqth 1; = // ( )
char unit ' ' ;
= // -
cout << 11 , 11
<< 11 ( i) : \n 11 ;
cin >> lenqth >> unit ;
i f (unit 'i')
cout << lenqth << 11 in == 11
<< cm_yer_inch *lenqth << 11 \n 11 ;
else if (unit '' )
==

cout << lenqth << 11 cm == 11


<< lenqth/cm_yer_inch << 11 in\n 11 ;
else
cout << 11 , JD, 1 11
<< unit << 11 1 \n 11 ;

uni t= ' i ' , uni t== ' ' .


,
" , . " . ,
. .

"else-if" , ++ .
if. if
:
if ( ) else

, if ,
, else .
-

if else if:
if ( ) else
if ( ) else

:
if (unit 'i')
==

. . . 11 1 -
else if (unit '')
==

// 2 -
else
11 3 -


.
, , .
,
. ,
, .
1 46 4


,
, .
, .

4.4. 1 .2. swi tch


uni t ' i ' ' '
: ,
.
, ++
: swi tch.
int ma.in ( )

constexpr doue cm_yer_inch 2 . 54 ; //


doue lenqth = 1 ; // ( )
char unit = ' ' ;
cout << " . , "
<< " ( i) : \n" ;
cin >> lenqth >> unit ;
switch (unit) {
case ' i ' :
cout << lenqth << " in = "
<< cm_yer_inch*lenqth << " cm\n" ;
break ;
case ' ' :
cout << lenqth << " cm = 11
<< lenqth/cm_yer inch << " in\n " ;
_
break ;
default :
cout << " , , 1 ' "
<< unit << " ' \n" ;
break ;

swi tch ,
if,
. ,
swi tch, .
case. case,
case. case -
break.
case, ,
defaul t. , , -
. , ,
4.4 . 1 47

4.4.1 .3. swi tch



, swi tch.
1 . . .
, char ( 9 . 5).
, string .
2. case
( 4.3. 1 ) . . case
.
3. case .
4. case.
5. case break.
, ,
.
.
int ain ( ) //
{ // . .
cout << " JD pwy? \n" ;
strinq s ;
cin >> s ;
switch ( s ) 11 : ,
case " " : !/
11 . .
break ;
case "" :
11 . .
break ;


if map (
2 1 ).
swi tch
.
if.
, case
. .
1 48 4

int ain ( ) // ca se
11 :
int = ' ' ; / /
constexpr char n = ' n ' ;
constexpr char m = ' ? ' ;
cout << " ? \n" ;
char ;
cin >> ;
switch ()
case n :
11 . .
break ;
case : 11 : ca se
11 . .
break ;
case m :
11 . .
break ;
case ' n ' : 11 : ca se
11 ( ' ' )
11 .
break ;
default :
11 . .
break ;

switch
.
. .
int ain ( ) //

cout << " , \n" ;


char ;
cin >> ;

switch ( )
case ' ' : case ' 2 ' : case ' 4 ' : case ' ' : case ' ' :
cout << " \n" ;
break ;
case ' 1 1 : case ' ' : case ' 5 ' : case ' 7 ' : case ' 9 ' :
cout << " \n" ;
break ;
default :
cout << " \n" ;
break ;
4 .4 . 1 49

, swi tch,
case break.
.
int ain ( ) // ( brea k )
{
constexpr doule c_per_inch = 2 54 ; //
.

doule lenqth 1 ; //
char unit ' ' ;
cout << " , "
<< " ( i) : \n" ;
cin >> lenqth >> uni t ;

switch (unit)
case ' i ' :
cout << lenqth << "in == "
<< c_per_inch*lenqth << " \n" ;
case ' ' :
cout << lenqth << " == "
<< lenqth/c_per_inch << " in\n " ;

. ,
case ' i ' , "" case
' ' , 2i
:
2in == 5 .
2 == 0 . 787402in



. swi tch.
. ,
? ?

4.4.2.
- .

. (
,
) .
1 50 4

4.4.2.1 . while
,
(EDSAC).
(David Wheele)
(Cambridge Univesity, England) 6 1 949 .
.

1 1
2 4
9
4 16

98 9604
99 9801

,
( ' \ t ' ) .
++ :
// 0 - 9 9
int ain ( )
(
int i = ; 11
while ( i<lOO ) (
cout << i << 1 \t ' << square ( i ) << \n ' ;
++i ; // i (i i + l )

square ( i ) i . ,
4.5, , .
,
++, .
.
, 1 00, .
.
,
( ' \ t ' ) .
.

, .
().
,
( .
) . int
i .
4.4 . 1 51

( - ).
(
1 00 ).
, ( ).
while.
while , .
while ( i<lOO) // , i
{
cout << i << ' \t ' << square ( i ) << ' \n ' ;
++i ; // i (i i + l )

11 ( ).
-

i .
i<lO O .
,
. , . . i 1 0 0 ,
while , .
,
.
while
, while.
, .
. ,
( - "
i "),
. !
,
.
.
int i = ; //

.
, .
.
,
, .


' ' char ( ' a ' +l ) , ' ' char ( ' a ' +2 ) ..
,
.
152 4

97
98

z 122

4.4.2.2.
, ,
.
while ( i<lOO) {
cout << i << ' \t ' << square ( i ) << 1 \n ' ;
++i ; 11 i (i i + l )

,
(( }). . -
. { }
,
. .
if ( <=) 11
}
else { 11
int t = ;
= ;
= t;

4.4.2.. for

++. .
.
for while. .
,
. " :
11 0 - 9 9
int main ( )
{
for ( int i = ; i<lOO ; ++i )
cout << i << ' \t ' << square ( i ) << ' \n ' ;

, i ,
, .
i 100". for
while.
4 .4. 153

for ( int i = ; i<lOO ; ++i)


cout << i << 1 \ t ' << square ( i ) << 1 \n ' ;

int = ; // for
while ( i<lOO) 11 for
// for
cout << i << ' \ t ' << square ( i ) << ' \n ' ;
++i ; 11 for

while,
for. for
. for -
, .
while . .
for.

. .
int main ( )
{
for ( int i = ; i<lOO ; ++i) { // i { 0 , 1 0 0 )
cout << i << 1 \t ' << square ( i ) << ' \n ' ;
++i ; // ? !

. . ,
1 00 . . ++i
i ,
50 .
, . .
for while.
, 2, :
11
11 { 0 , 1 0 0)
int main ( )
{
for ( int i ; i<lOO ; i+=2)
cout << i << ' \t ' << square ( i ) << ' \n ' ;

. ,
. .
1 54 4


, "" ,

. for. .
.

"for ",
, vector; . 4.6.

4.5.

square ( i ) . . , ,
i. -
. (
).
,
,
sqrt ( ) , 3.4.
. square.
int square ( int ) //

return * ;

, (
), square,
int ( ) int (
); ,
:
int main ( )
{
cout << square ( 2 ) << ' \n ' ; 11 4
cout << square ( l O ) << 1 \n ' ; // 1 0 0

, ,
, .
.
square ( 2 ) ; //
11 :
int vl = square ( ) ; //
4 .5. 155

int v2 square ; //
int v = square ( l , 2 ) ; 11
int v4 = square ( " two" ) ; 11 - i n t


. . . -
. , , ,
" two" . 2.
++ .
,
++. .
. . -
- .
. .
.
(. 4.4.2.2),
.

return ; //

square :
.
++ . . .
,
.
:
wn ( _ ) _
( ) wn (
). - . -
(. ). ,
, .
- ( ).
. .

void ( ""). .
void write_sorry ( ) // ;
{ / /
cout << " \n" ;

.
, 8.
1 56 4

4.5.1 .
,
,
.
.
( -
).
.
.
,
. .
,
. , ,
(.. )
. , .
,
. , , ,
* 7 * 7 , (+7) * (+7) , square () , square ( 7 )
square (+7 ) , , square .
( ++
sqrt): sqrt ()
sqrt ( 7 ) , sqrt ( x+7 ) , (
) .
, . : ,
.
, sqrt () .
8.5 ,
, .
main ( ) .
:
void print_square ( int v)
{
cout << v << ' \t ' << v*v << ' \n ' ;

int main ( )

for ( int i = ; i<lOO ; ++i ) print_square ( i ) ;


print_square ( ) ? , ,
, square ( ) , , ,
4.5. 157

print square ( )
,
square ( ) , ,
;
square ( ) ,
print_square ( ) , , .
print_square ( ) :
;
.
,
. square ( )
.
,
square ( i ) , i * i .
.
,
1 949 .
"".
i * i
.
. .
( (subroutine))
.
!


square ( ) ;

. *
( . ,
). "".
square ( ) .

.5.2.
, . .
? .
int square ( int )

.
int = square ( 4 4 ) ;
1 58 4

.

. ,
sqrt ( ) ? ,
. , square ( ) ?
, .
, -
. , ++
, ,
. r.
int square ( int) ; 11 square
doule sqrt (doule) ; 11 sqr t

,

.
int square ( int ) 11 square

return ;

. ,
. -
#include.
. " " 8.3 8.7.

.
(
4 . 2).

4.6.
- ,
, . .
,
. . . ,
.
, - ..
,
.
( ,
20 2 1 ) . , .
: vector.
4.6. 159

- ,
. ,
vector v.
size( )

v: 6
v[O] v[1 ] v[2] v[] v[4] v[5]
v 5 7 9 4 6 8

, ; -
1 .. ,
, v [ O ] 5,
v [ l ] 7 ..
. :
++ -
. , ,
, " ", . .
.
, , :
vector<int> v = ( 5 , 7 , 9 , 4 , 6 , 8 } ; // 6

, ,
.
vector (<>). int.
.
vector<strinq> philosopher //
= { "ant" , " Plato" , " Hume " , "ierkeqaard" } ;

, ,
.
philosopher [ 2 ] 9 9 ; // :
v [2 ] = "Hume " ; 11 :

vector ,
. ( n ) , n -
,
. :
vector<int> vi ( ) ; // ve c t o r 6 in t ,
vector<strinq> vs ( 4 ) ; // ve c t o r 4 ,

(11 11) .
,
:
vd [20000] = 4.7; //
1 60 4

4.6. 1 .
vector "" ,
:
vector<int> v = ( 5 , 7 , 9 , 4 , 6 , 8 } ;
for ( int i=O ; i < v . size ( ) ; ++i )
cout << v [ i ] << 1 \n ' ;

v . size ( ) v .
v . size ( ) vector
.
v [ O , v . size ( ) ).
.
v v [ O ] , - v [v . size ( ) -1 ] . v . size ( ) =,
v , .. .
++
++ ( 1 7.3, 20.3) .


, , , :
vector<int> v = ( 5 , 7 , 9, 4 , 6 , 8 } ;
for ( int : v) // v
cout << << ' \n ' ;

for ,
(range)
. for ( int v) " int v " ,

[O,v . size ( ) ) .
.



for ( 4.4.2.3).

4.6.2.

.
push_back ( ) ,
4.6. 1 61

, .
.
vector<doue> v; // ,
11 v

v: 11
v . push_back ( 2 . 7 ) ; /! v 2 . 7
// v v [ 0 ] ==2 . 7

v: [!)3---@
v . push_back ( S . ) ; // v 5 . 6
11 v v [ l ] == 5 . 6

v: 1 2 1 ---+--1 2.1 l I
s.e

v . push_back ( 7 . 9 ) ; 11 v 7 . 9
11 v v [2 } == 7 . 9

push back ( ) .
_

. r- push back ( ) -
_

vector, .
. -:
_. u_u_ ( u_ )
, -
vector - size ( ) , v . size ( ) ,
.

, push k ( ) 3.
_

, ,
vector .
() ,
.
, vector ++
.

4.6..
.
, . -
. -" ,
, , ,
, ,
. .
, .
1 62 4

- , -
.
, .
//
int main ( )
{
vector<doule> temps ; 11
for ( doule temp ; cin>>temp ; 11 temp
temps . push_back ( temp) ; 11 t emp vector
11 . . . - .

, ?
:
vector<doule> temps ; /!

.
du.
.
for (doule temp ; cin>>temp ; 11 t emp
temps . push_back ( temp) ; 11 temp vector

temp doue
. cin>>temp doule,
( ).
. ,
for
cin>>temp. cin>>temp ,
, ,
for doule,
. .
1.2 3.4 5. 7.8 9.0 1

temps : 1 . 2. 3 . 4 . 5 . 6, 7 . 8, 9 .
( , temps [ ] =1 . 2).
' 1 ' , .. . doule.
1 0.6
.
temp
, for, while:
doule temp ;
while ( cin>>temp) //
temps . push_back ( temp) ; //
11 t emp
.6. r 1 63

, for , ,
, ,
.
, .
.
11
int ain ( )
{
vector<doule> teps ; 11
for (doule t ; cin>>temp ; 11 t emp
teps . push back ( temp) ; 11 t emp
_

// :
doule sum = ;
for ( int : temps ) sum += ;
cout << " : "
<< sum/ temps . size ( ) << 1 \n ' ;

11 :
sort ( teps ) ; //
cout << " : "
<< temps [ teps . size ( ) /2 ] << ' \n ' ;

,
(.. temps . size ( ) ) .

// :
doule sum = ;
for ( int : temp s ) sum += ;
cout << " : "
<< sum/temps . size ( ) << ' \n ' ;

, +=.
(,
, - )
.
sort ( ) .
11 :
sort ( temps) ; 11
cout << " : "
<< temps [ teps . size ( ) /2 ] << ' \n ' ;

( 20).
:
, .. temps . size ( ) /2.
(
), ,
1 64 4

.
. 2 .

4.6.4.
.
,
- , , - , ,
- ,
.
, ,
( ;
. 2 1 ). .
11 :
int ain ( )
{
vector<strinq> words ;
for ( strinq temp ; cin>>temp ; 11 ,
11
words . push_back ( temp ) ; 11
cout << " : " << words . size ( ) << ' \n ' ;

sort (words ) ; //
for ( int i = ; i<words . size ( ) ; ++i )
if ( i==O 1 1 words [i-l ] ! =words [ i ] ) // ?
cout << words [ i ] << "\n" ;

,
. , ,

n plan canal n

canal
n
n
plan

? , ?
for ( strinq temp ; cin>>temp ; 11
words . push_back ( temp) ; 11

( 4 . 6. 2) .
- , .
,
4.6. 1 65

() . , ,
"". 3.5. l ,
Windows <Ctrl+Z>.
Unix - <Ctrl+D>.

. "
" ( r
) " ".

if ( i==O 1 1 words [i-l ] ! =words [ i ] ) // ?

. :

canal
man
pana.ma
plan

,
. ? ,
(words [ i -1 ] ! =words [ i ] ), ,
, , . ,
(i==O),
1 1 ().
if ( i==O 1 1 words [i-l ] ! =words [ i ] ) // ?

, .
! = ( ), == (), < (), <= (
), > () >= ( ),
. <. >
, " " "Apple"
"Chimpanzee" .

&
. "" ;
, cin
cout, BLEEP.
,
string disl iked = "Broccol i " ;
, .
1 66 4

4. 7 .

, :
( for while), ( if),
( ++ +=), (=, ! =
1 1 ) . (. main ( ) , sort ( ) size ( ) ) .
, .
vector ( ). cout ( ) sort ( ) ().
, ,
.
,
,
. : -
. ,
,
,
.


.
. ,
- , .
1 . . while,
int.
' 1 '

2. . "
: " ,
- " : "
.
3. . " " ,
.
4. , doule,
int.
5. . "
" , 1 . 0/100.
6. .
doule .
,
.
4. 7. 1 67

.
, " " .
,
" " .
7. doue
; , , l Ocm, 2 . Sin, Sft
3 . m. : cm, m, in,
ft. : lm =lOOcm,
lin =2 . 54cm, lft == 12in.
. 12 m (
) 12m ( ).
8. , yard,
meter, km gallons, .
9. (
) . ,
,
, .
, . .
( ).
1 . ( )
, .
1 1 . ,
.


1 . ?
2.
? .
3.
?
4. ?
5. ,
?
6. lvalue? , lvalue.
, lvalue?
7. ?
8. ?
9. ?
1 68 4

1 0 . "" ? .
1 1 . . .
.
1 2. ,
?
1 3 . string?
1 4. swi tch if?
1 5. swi tch?
1 6. , for
?
1 7. , for. - while?
1 8 . ( char)?
1 9. char foo ( int ) .
20. , ?
.
2 1 . int,
s tring?
22. string, -
int?
23. ?
24. for, ?
25. vector<char>alphaet ( 2 6 ) ; ?
26. - push back ( ) .
27. - begin ( ) , end ( ) size ( ) .
28. vector?
29. ?

11
begin ( )
else
end ( )
lvalue
push back ( )
_

rvalue -
size ( )
sort ( ) i f for
4. 7 . 1 69

vector swi tch for


while

1 . . .
2. , ",
,
- ". 4.6.2 ,
. :
.
3. doule .
,
, .
( ).

.
.
4. , r .
1 1 00, ,
, (, "
50").
. :
< <=. if-else.
5 . , r
.
- , ,
.
: doue .
35 . 6, 24 . 1 ' + ' ,
" 35 . 6 24 . 1 5 9 . 7 " . 6
.
6. , " zero " , "one " ,
" . , "nine " . ,
: ,
7 seven.
, ,
: , seven
7 .
1 70 4

7. -, . 5, .
. ,
.
8. ,
.
. -
. - ,
64 . .
.
! . .
. 1 ,
1 1 . ,
. . ,
int. .

.
,
.
9. ,
. 8. .
. int,
doue. .
(
int). .

( doue)?
1 0. ". . '' .
.
Google ( -
) .
swi tch. , (..
,
) .
.
.
. .

.
.
1 1 . . 1 100.
. .
(.. ,
4. 7 . 1 71

). ,
(, primes, primes [ 0 ] =2 ,
primes [ 1 ] =3, primes [2 ] =5 ..). 1
1 00.
. .
.
primes. 2.
1 2. . ,
max, 1 max.
1 3 . . . l 1 00.
"
". ,
. . .
1 4. . . , ,
max,
1 max.
1 5. , n
n .
1 6. .

. ,
, . .
. .
1 7 . ,
, . s tring.
1 8 . .
2 + + = 0 .
, .
, ,
. ,
doue.
, , xl 2 .
1 9. , ,
, Joe 17 Barbara 22.
names , - scores
( . names [ 7 ] == " Joe" .
scores [ 7 ] =17 ) . . NoName .
. .
. , .
( . ) .
1 72 4

20. . 1 9 ,
"
" .
2 1 . . 1 9 ,
,
, " " .


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

51 1 &- .#U-

;;;'/'

.. fu. "'k.
- ( IJ', f'I-'1)

,
,
. ,

, - .
? !
,
,
. ,
" , "

.
1 74 5

. 1 . 5.6.
5.2. 5.6.1 .
5.3. 5.6.2. ,
5.3 . 1 .
5.3.2. , 5.6.3.
5.3.3. 5.6.4.
5.4. 5.7.
5.8.
5 .5. 5.9.
5.9. 1 .
5.5 . 1 .
5.10. -
5.5.2. 5.10.1 .
5.11 .
5.5.3.

5. 1 .
, ,
, , .
.

.
.
.
. ,
.
, :
+ ;
+ , .
. ,

.
11. ,
.
:
+ , (
/ );
+ , (,
++);
5.1. 1 75

, .
. ,
.
, -
. , ,
.
, " ". ,

.
? .
,
, , .
, ,
,
,
. -
: ?
, ,
.
1 .
.
2.
.
3.
.
4.
.
5. .
. 3-5 ,
. 1 2
,
- , .
1 00%, .
.
, . ,
,
90% . ,
,
. ,
, . .
1 76 5


.
.
.


.
, .

; .
, .. , ,
,
. , ,
. ,
,
.

5.2.
.
. ,
,
" " ,
(. .
,
) .
.
, . - .
.
. .
, ,
, , ,
- 1 . 2: sqrt ( -1 . 2) .
sqrt ( )
doue doue.
.
5.5.3.
. . ..
( r, ,
, ..).
. ,
5.3. 1 77

, .
,
"!"? 5.6.3 1 0.6.
.
(""),
.
, vector
.
?
. 26.3.5.
. .
, ; .
6.6 6.9.
.
.
,
.
. ,
,
. .

5.3.
.
. .

. ,
.
. , ,
" ".
.
ru .
.
- -
.
.
:
int area ( int lenqth , int width) ; 11
1 78 5

5.3. 1 .
, area ( ) :
int sl area ( 7 ; 11 : )
int s2 area ( 7 ) 11 :
Int s area ( 7 ) ; 11 : In t -
1
int s4 area ( ' 7 ) ; // :

: .
++,
. ,
. , . ,

, , .
( ,
, !)
, ,
, .
, ,
,
.
, ,
,
, , . .
s,
- :
" int:
i . "
, :
: ' ; ' 's'"
" 's' "
'Int"'
,
. ,
. ,
- . ,
:
s , -
Int, s."
.
5.3. 1 79

5.3.2. .
,
, ; ,
, ( ,
) ,
, ,
..
int = arena ( 7 ) ; // :
int xl area ( 7 ) ; //
:
//

int 2 area ( " seven" , 2 ) ; /!
:
11

.
1 . arena ( 7 ) : area
arena, ,
arena. ( ""? ,
.) arena ( ) ,
,
. arena
7 ,
: ,
( ;
. 5. 7).
2. area ( 7 ) ,
. ++
,
.
,
( 1 4. 1 ) .
3. area ( 11 seven11 , 2 ) , ,
11 seven11 ,
7? .
, . ++
(. 3.9),
strinq int. ,
1 80 5

. ,
area ( "Hovel lane" , 2 ) , area ( " 7 , 2 " ) area ( " sieen" , " zwei " ) ,
?

.
, .



.
.

5. . .
, ,
; . ,
. . ,
,
,
, . .
int 4 = area ( l0 , -7 ) ; 11 , ,
11 11 ?
int 5 = area ( l0 . 7 , 9 . 3 ) ; 11 , a rea (1 0, 9)
char area ( lO O , 9999) ; 11 ,
=

4
. area ( l 0 , -7 ) :
area ( ) , :
, .
5
, doue, 1 0 . 7 9 . 3,
int, 10 9 (. 3. 9.2). ()
,
doue int,
area ( 1 0 . 7 , 9 . 3 ) .

, area ( 1 0 . 7 , 9 . 3 ) . int,
area ( 100 , 9999) , , 999900,
char. , ,
" -36.
,
.
5.4. 1 81

,
.
: " " ,
. , ,
, .
.

5 . 4.

, -1. (translation
units). -
,
. (
8.3). ,
.
, .
( )
8.3. ,
.
int area ( int lenqth , int width) ; 11

int a.in ( )
{
int = area (2 , 3 ) ;

area ( )
, ,
, area ( ) .
area ( )
( ),
.
int area ( int , int ) { /* " . */ } // " " a rea ()

,
"" .
doule area (doule , doule }
{ / * . . . */ } // " " a rea ()
int area ( int , int , char unit)
{ / * . . . */ } // " " a rea ()

,
.
182 5


. :
, .
.
,
, .
:
, ,
.
8 .2 8.3.

5.5.

, . -
.
, .
, . .
int area ( int length , int width) //

return length*width ;

int fraJDed_area ( int , int ) //

return area ( x-2 , y-2 ) ;

int ain ( )
(
int -1 ;
int 2;
int z = 4 ;
11 . . .
int areal = area (x ,y) ;
int area2 = framed_area ( l , z ) ;
int r framed_area (y , z ) ;
doue ratio = doule ( area l ) /area ; // do u e ,
1 1


.
. z, .
,
areal area2. .
5.5. 1 83

? ,
: , area ( ) ,
? ?
,
ratio .
. , - ?
, : r .
doue (areal ) /r .
, ;

.
,
. .
,
. .
"- !" ,
- .
. .
,
area ( ) . .
1 . .
area ( ) .
2.
area ( ) .

5.5.1 .
("
!"). , .
area ( ) ,
. ,
.
area ( , ) main ( )
:
if (<=) rrr ( "n " ) ;
if (<=) rrr ( "n " ) ;
int areal = area (x , y) ;

, . .
error ( ) . -
. std_lib_facil i ties . h
error ( ) .
.
1 84 5

, error ( ) .

. runtime_error
( 5 . 6.2, 7.3, 7.8. .2. 1 ) .
.
.
,
.
if (<= 1 1 <= ) error ( 11 n'l' area ( ) 11 ) ;
11 1 1 ""
int areal = area ( x , y) ;

area ( )
, framed_area ( ) .
:
if ( z<=2)
rrr ( 11 n'l' area ( ) "
11 framed_area ( ) " ) ;
int area2 = framed_area ( l , z ) ;
if (<=2 1 1 z<=2)
rrr ( 11 nn'l' area ( ) "
11 framed_area ( ) " ) ;
int r = framed_area (y , z ) ;

, .
, , framed area ( )
area ( ) . , framed_area ( ) 2
. !
- framed area ( ) , 2
1?
framed_area ( )
. "",
. "
" ( 4. 3 . 1 ). ,
, framed area ( ) , .
constexpr int frame_width = 2 ;
int framed_area ( int , int ) //

return area ( x-frae_width , y-frame_width) ;

. framed_
area ( ) .
if ( 1-frame_width<=O 1 1 z-frame_width<=O )
rrr ( 11n1 area ( ) 11
11 framed_area ( ) 11 ) ;
5.5. 1 85

int area2 = framed_area ( l , z ) ;


if (y-frame_width<=O 1 1 z-frame_width<=O)
rrr ( 11 1 r J area ( ) 11
1 1 framed_area ( ) 11 ) ;
int r = framed_area (y , z ) ;

! , ? ?
? ( ,
).
, framed_area ( ) m .
!
.
int area2 framed_area ( l , z ) ;
int r = framed_area (y, z ) ;

. ,
. ,
framed_area ( ) .

5.5.2.
framed_area ( )
, -
error ( ) .

int framed area ( int , int ) //


_

constexpr int frame width = 2 ;


if (x- frame_width<=O 1 1 y-frame_width<=O )
rrr ( 11 1 r area ( ) 11
11 framed_area ( ) " ) ;
return area ( x-frame_width , y-frame_width) ;

,
framed_area ( ) . ,
500 , .
, - ,
.
:
" " "
".
,
.
. , .
, .
.
1 86 5

area ( ) .
int area ( int lenqth , int width) //

if ( lenqth<=O 1 1 width <=)


error ( " Heono:llC'l'enw area ( ) " ) ;
return lenqth*width ;

area ( ) ,
framed_area ( ) .
- .
,
, ?
- , -
, .
.
,
. , ,
. ,
- ,
. ,
, , ,
.
, .
.
, ,
.
, .
, ,
, ,
. .
.
.
.
, area ( )
(.. ,
, ).
,

, ,
.
5.5. 1 87

, ? ,
.
, ,
5. 1 0.

5.5..
: ,
? "
". .
11 " " " " ;
11 ' ' (. . " " , " ")
char ask_user ( s trinq question)
{
cout << question << " ? ( n) \n" ;
strinq answer = " " ;
cin >> answer ;
if ( answer = "" 1 1 answer=="ye s " ) return ' ' ;
if ( answer ="n" 1 1 answer=" no " ) return ' n ' ;
return ' ' ; // ' '

// ;
11 - 1 ,
int area ( int lenqth , int width)

if ( lenqth<=O 1 1 width <= ) return -1 ;


return lenqth*width ;


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

.

, . . .
(, >>
1 88 5

cin) , int,
,
.
,
, .
.
int f ( int , int , int z )

int areal = area ( x , y) ;


if ( areal<=O ) rrr ( " " ) ;
int area2 = framed_area ( l , z ) ;
int r = framed_area (y , z ) ;
doue ratio = doule (areal ) /area ;
11 . . .

? ,
" ":
.

&
.
areal, area2, r ratio.
. ,
? ;
.

:
(exceptlons) .

5. 6.
, ++
: .
(
) (
), ,
. , ,
,
. ,
.
5.6. 1 89

:
, ,
return, . -
throw, , .
, ,
catch, ..
. ,
throw.
try ( ),
.
catch try.
, .
( 1 9),
.

5.6. 1 .
area ( ) .
class Bad area { } ; // ,
// a rea ()

// ;
11 Ba d a rea
int area ( int lenqth , int width)

if ( lenqth<=O 1 1 width<=O) throw Bad_area { } ;


return lenqth*width ;

, , , ,
, ,
area () throw, .
catch. Bad_area -
,
area ( ) , tch
, area ( ) . ,
( ), 9.
Bad_area { } " Bad_area
", throw Bad_area { } "
Bad_area ".

int main ( )
try {
int -1 ;
int 2;
1 90 5

int z = 4;
11 . . .
int areal area ( x ,y) ;
int area2 = framed_area ( l , z ) ;
int r = framed_area (y , z ) ;
doue ratio = areal/area ;
catch (Bad_area) {
cout << " area ( ) \n" ;

-, ,
area ( ) - main ( ) .
framed_area ( ) . -,
: main ( ) ,
throw Bad area { } , area ( )
, ( )
Bad_area, .
.
.
. , -

.

5.6.2. .
.
, .
. ++
(container).
vector,
4.6. vector ,
- size ( ) . ,
,
[O,v . size ( ) )? [low,high)
, low high-1, ..
, .

low : high :

1 "
1 . 1---_J
, :
" ?" , ,
v [O,v . size ( ) ).
!
5.6. 1 91

, .
:
vector<int> v ; //
for (int i ; cin>>i ; )
v . push back ( i ) ;
_ 11
for ( int i = ; i<=v . size ( ) ; ++i ) //
cout << "v [ " << i << " ] == " << v [ i ] << endl ;

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

, i
, v [ i ] .
, . for:
i<=v . size ( ) ,
i<v . size ( ) . , -
, .
v [ 5 J , .
,
: (off-by-one error) ,
, (range error),
,
(bounds error), .
for
? ,
.
, . for
.
.
vector<int> v ( S ) ;
int = v [ S ] ;

,
.
,
? vector
, ( ; .
4.6 1 9.4). ,
out_of_range. ,
, , -
, ,
.
192 5

i nt ain ( )
t ry {
vector<int> v ; // i n t
for { int ; cin>>x ;
v . push_back (x) ; //
for ( int i = ; i<=v . size ( ) ; ++i) //
cout << "v [ " << i <<" ] == " << v [ i ] << ' \n ' ;
catch (out_of_ranqe) {
cerr << " ! \n" ;
return 1 ;
catch ( . . . ) { //
cerr << "JJDD : - \n " ;
return 2 ;

,
, ,
5 . 5.2. ,
.
(vector : : operator [ ] )
, 1 ?
,
. vector .
.

5.6..
,
, 1 0.6. ,

, ,

. , .
. .
.
doue d = ;
cin >> d ;

, ,
cin.

1 , vector<> : : operator [ ]

,
; (. ,
) - vector<> : : at ( ) .
std_lib_facilities . h ,
operator [ ] .
19 . -
. .
5.6. 1 93

if ( cin) {
11 ,

else {
11 ,
11 -

.
- , ,
du.
,
,
, .
. .
doule some_function ( )
{
doule d = ;
cin >> d ;
if ( ! cin)
error ( " 'l' doule "
" ' some_function ( ) ' ) ;
"

11 -

! cin (" cin", .. cin ) ,


cin .
, error ( ) ,
.
error ( ) ,
? ,
, . ,
error ( )
. ,
.
,
. - (
- 7 .3) .
,
out_of_range, vector. ,
runtime_error,
. ,
. error ( )
:
1 94 5

void error ( s trinq s )


(
throw runtime_error ( s ) ;

run time_error.
.
run time_error main ( ) .
int main ( )
try (
//
return ; 11

catch ( runtime_error& ) (
cerr << " runtime error : " << e . what ( ) << ' \n ' ;
keep_window_open ( ) ;
return 1 ; // 1

e . what ( ) runtime_
error. &
catch ( runtime_error& ) (

, . .

. 8 . 5.4-8 . 5 . 6 .
.
,
cerr. cou t.
, .
cerr cout , cerr
,

, . cerr
.
.
out_of_range runtime_error,
runtime_error
out_of_range,
vector
. out_of_range, runtime_error "
",
exception.
int main ( )
try (
//
5.6. 1 95

return ; //

catch (exception& ) {
cerr << "error : " << . what ( ) << ' \n ' ;
keep_window_open ( ) ;
return 1 ; // 1

catch ( . . . ) {
cerr << " clCJDl)ee\n " ;
keep_window_open ( ) ;
return 2 ; // 2

Ji ,
tch ( ).
. . .

Ji (out_of_range runtime_error)
exception, ,
exception () .
Ji
1 3- 1 6.
, ,
main ( ) , "", .
(, Unix) . (
Windows) . ,
, , main ( ) ,

- .
error ( )
, .
.
,
error ( ) .
void error ( strinq sl , s trinq s 2 )
{
throw runtiJDe_error ( sl+s2 ) ;

,

. , error ( )
: error ( )
tch,
Ji runtime_error ( - main ( ) ) .
Ji error ( ) 7 .3
7.7. Ji ,
( " Ji").
1 96 5

&
,
,
error ( ) .

5.6.4.
3.9.2 :
, .
.
int = 2 . 9 ;
char = 106 6 ;

2, 2 . 9 ,
int, int .
, ASCII,
42 ( * ) .
1066, char
.
3 . 9.2 ,
. ( ; . 1 9.3)
, runtime_
exception,
. .
int xl = narrow_cast<int> ( 2 . 9 ) ; //
int 2 = narrow_cast<int> ( 2 . 0 ) ; //
char cl narrow_cast<char> ( 10 6 6 ) ; //
char 2 = narrow_cast<char> ( 8 5 ) ; //

< > ,
. . .

vector<int>. ,
, . ,
, .11 (template arguments).
, ""
, narrow_cast,
std_lib_faci lities . h
error ( ) . (cast)
, -
"" ( 2).
, ,

2 cast - " '',

" ". -
. .
5. 7 . r 1 97

, . ,
.

5. 7 .
,
, . ,
, .
. ,
; , ;
- if - .
,
, .
- , , .
, - .
. .
.
- .
.
int .ain ( )
{
vector<doue> temps ; //

for ( doule temp ; cin>>temp ; 11


temps . push_back ( temp ) ; 11

doule swn = ;
doule hiqh_temp ;
doule low temp "' ;
_

for ( int : temp s )

if (x > hiqh_temp) hiqh_temp = ; //


if ( < low_temp) low_temp ; //
swn + ; //

cout << "&. : "


<< hiqh_temp << 11\n " ;
cout << ". 8 : "
<< low_temp << "\n" ;
cout << " : "
<< swn/temps . size ( ) << 11 \n" ;
1 98 5

.
. (Lubbock, 'Jexas). 1 6 2005 (
).
-16 . 5 , -23 . 2 , -24 . 0 , -25 . 7 , -2 6 . 1 , -18 . 6 , - 9 . 7 , -2 . 4 ,
7 . 5 , 12 . 6 , 2 3 . 8 , 2 5 . 3 , 2 8 . 0 , 34 . 8 , 3 6 . 7 , 4 1 . 5 ,
40 . 3 ' 4 2 . 6 ' 3 9 . 7 ' 35 . 4 ' 12 . 6 , 6 . 5 , -3 . 7 , -14 . 3

:
: 4 2 . 6
: -26 . 1
: 9 . 3

.
. .
.
. 23 2005 .
7 6 . 5 , 7 3 . 5 , 7 1 . 0 , 7 3 . 6 , 70 . 1 , 7 3 . 5 , 7 7 . 6 , 85 . 3 ,
8 8 . 5 , 9 1 . 7 , 95 . 9 , 99 . 2 , 98 . 2 , 1 00 . 6 , 106 . 3 , 112 . 4 ,
110 . 2 ' 103 . 6 ' 94 . 9 ' 91 . 7 ' 8 8 . 4 ' 85 . 2 ' 85 . 4 ' 8 7 . 7

:
: 112 . 4
1D1&.11 : .
: 8 9 . 2

- . (0.0F 1 8) -

- ! ?
low_temp m . ,
,
.


. .
. " (..
), .
?

. . .
?
high_temp :
,
, .
!
5. 7 . 1 99

;
""
. , ""
. .
int main ( )
{
vector<doue> temps ; 11

doue sum ;
doue high_temp -1000 ; //
//
doue low_temp 1000 ; //
//
int no_of_temps = ;

for ( doue temp ; cin>>temp ; ) { 11


++no_of_temps ; 11
SUJD += temp ; 11
i f { x > high_temp) high_temp = ; 11
if (x < low_temp) low_temp ; 11

cout << " . : "


<< high_temp " \n" ;
cout " : "
low_temp << "\n" ;
cout " : "
<< sum/no_of_temps << " \n" ;

? ?
""? 1000 -1000?
"" ( 5 . 5 . 1 ). 1000
-1000 - ,
, . . ? ,
- 1 000F (-573)? ,
1 000F (538)?


.
. min_temp
(" ") max_temp (" ").
.
200 5

5.8.
, , ,
,
, 2 . , -34 . 56.
, . ?
. (
, , ) 21 . 65685.
-

? ,
.
,
, , .
.
. , ,
, 9. ,
21 . 65685 !
10 . 392 3 . !
,
. I ,
, ,
.
.
1 . ?
( ) .
2. ?
, " ?"
" ?" . ,
, - , .
, ,
.
- ,
.
, "
",
.
, (guesstimation3) ,
.

3 : guesstintio guess ("

") estimation - (""). - . .


5.9. 201


, 2 .
?
. .
.

( ). (
- )
.

,
, . ,
,
. ,
9 ? ? ""
""?
? . ,
2000 -
. ( )
200 / (200 9 2000),

9 . : ,
,
.


,

. ,
, .

5. 9 .
(?) . ,
()
.
, .
- , ,
- .
, ,
. (debugging),
(bugs). I,
-
202 5

, -
. .
.
(Grace Murray Hopper), COBOL (
22.2.2.2). 50 .
.
(debugglng).
.
1 . .
2. .
3. . , -
.
:

. . - . ,
- .

.
. .
, - .
, . .
(
).
.
while ( r ) { //
r
, r

?
. . - .
,
.
:
r, ?


. . ,
, .
, , ,

5. 9. 203

. .
, . .
, - .

.

5. 9 . 1 .
,
. . -
.
. .
: " error ( )
exception main ( ) . "

. .
.

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

.
.
. !.
.
,
.
. .
.
204 5

,
.
.
:
.
.
.
if, .. , ,
, .
I , ,
.
. , ,
, .
,
.
. , -
. (
, ), ,
, , .
. -
. ,
( ),
( ). ,
, " ".

.
.
?
cout << " , << name << ' \n ' ; // !

?
cout << " , " << name << ' \n ; // !

?
int f ( int )

if (> ) / * - */
else / * - */ )
// !

?
if (<= // !
= f (y) ;
5.9. 205

"":
, ,
.
?
(,
#include " std_lib_facilities . h11 )?
?
?
int count ; /* . . . */ ++Count ; // !
char ch ; /* . . . */ Cin>>c ; // - !

?
= sqrt (y) +2 // !
z = + ;

.
, 5.2.
,
, , ,
, .
, .
,
. Windows
, , ,
( - - ).
- keep_window_open ( )
s td lib facili ties . h main ( ) .
- ,
, .
,
, , ,
. ,
. ? , ,
.
, ,
, "" , .
.
for ( int i = ; i<=max ; ++j ) { // ! ()
for ( int i=O ; O<max ; ++i) ; 11 v
cout << "v [ " << i << " ] ==" << v [ i ] << \n ' ;
11 . . .
206 5

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

( cerr), :
int my_fct ( int , doule d)
{
int res = ;
cerr << "my_fct ( " << << " , " << d << 11 ) \n " ;
11 . . . . . .
cerr << "my_fct ( ) " << res << ' \n ' ;
return res ;

(.. ,
; . 9.4.3)
, :
int my_coplicated_function ( int , int , int )
11 < <
{
if ( ! ( < && < && <) ) // ! " " , & & - ""
rrr ( " '1'I f " ) ;
11

,
, ;
, , , .
assert.
,
.
-.
,
. ,
. ,
.
, ,
.
5.1 . - 207

5 . 1 . -
.
. -
,
:
. , :
int my_complicated_function ( int , int , int )
11 < <
{
if ( ! ( < && < && <) ) // ! " ", & & - ""
rrr ( " mcf" ) ;
11

,
. (
. ). .
, ,
(pre-condition) : ,
. , ,
. .
1 . (, ,
).
2. ( - ).
-
. .
int = my_complicated_function ( l , 2 , "horsefeathers") ;

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

. ,
. .
208 5

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

. , ;

,
.
int my_coplicated_function ( int , int , int )
// < <
{
if ( ! ( < && < && <) ) // ! " ", & & - ""
error ( " HeepJ.Ie 'l'l mcf" ) ;
11


int my_coplicated_function ( int , int , int )
{
11 . . .
5.1 . - 209

5.1 D. 1 .

.
- ? ,
returnl , ,
; .. - ,
- (
, ?).
5 . 6. 1 , .
11 ;
11 Ba d_ a rea
int area ( int lenqth , int width)

if ( lenqth<=O 1 1 width <= ) throw Bad_area ( ) ;


return lenqth*width ;

,
( ),
, (
).
.
int area ( int lenqth , int width)
11 .
11 : l ength wi dth
11 : ,
11
{
if ( lenqth<O 1 1 width <= )
error ( "area ( ) pre-condi tion" ) ;
int = lenqth*width ;
if ( <= ) error ( "area ( ) post-condition " ) ;
return ;

,
, ,
.

&
. ,
- .

- .
( 9.4.3),
( 4.2 5 .2), ( 26).
210 5

5 . 1 1 .
, ? ,
, ,
. ,
? . " " - :
, , .
.
,
. (testing)
7.3, 1 0 26. -
, -

.
(test case).
.
.
. .
. . -
. ,
. - . .

1 . ! @#$%
!
2. . !
. ?
, .
. .
,
. - !

.
. 7.3.
5.1 1 . 211

-../
.
.
#include " s td lib facilities . h"

int main ( )
try {
<< '1.' '1.'.n sa..r >>
keep_window_open ( ) ;
return ;

catch (exception& ) {
cerr << " : " << e . what ( ) << ' \n ' ;
keep_window_open ( ) ;
return 1 ;

catch ( ) { ...

cerr << " ! \n " ;


keep_window open ( ) ;
_
return 2 ;

, - . -
. . ,

"Successl" , ,
() ;
,
. ,
.
- "
" . ,
; , -
.
1 . Cout << " Success ! \n" ;
2 . cout << "Success ! \n ;
3 . cout << " Success" << ! \n"
4 . cout << success << ' \n ' ;
5 . string res = 7; vector<int> v ( lO ) ; v [S] res ; cout <<
" Success ! \n" ;
6 . vector<int> v ( l O ) ; v ( S ) = 7 ; if (v ( S ) ! =7 ) cout << " Success ! \n" ;
7 . if ( cond) cout << " Success ! \n" ; else cout << " Fail ! \n" ;
212 5

8 . bool = fal s e ; if () cout << " Success ! \n" ; else cout <<
" Fail ! \n" ;
9 . string s = " " ; = " fool " <s ; if () cout << " Success ! \n" ;
10 . s tring s = " " ; if ( s==" fool " ) cout << " Success ! \n" ;
1 1 . s tring s = " " ; if ( s==" fool " ) cout < " Success ! \n " ;
12 . string s = " " ; if ( s+ " fool " ) cout < " Success ! \n" ;
1 3 . vector<char> v ( 5) ; for ( int i=O ; O<v . size ( ) ; ++i) ; cout <<
" Success ! \n" ;
1 4 . vector<char> v ( 5 ) ; for ( int i=O ; i<=v . size ( ) ; ++i) ; cout <<
" Succes s ! \n " ;
1 5 . s tring s = " Success ! \n" ; for ( int i=O ; i< ; ++i) cout << s [ i ] ;
1 6 . if ( true) then cout << " Success ! \n " ; else cout << " Fail ! \n " ;
17 . int = 2000 ; char = ; if ( ==2000) cout << " Success ! \n" ;
1 8 . s tring s = "Success ! \n" ; for ( int i=O ; i<lO ; ++i) cout << s [ i ] ;
1 9 . vector v ( 5 ) ; for ( int i=O ; i<=v . size ( ) ; ++i) ; cout <<
" Succes s ! \n " ;
20 . int i=O ; int j = 9; while ( i< l O ) ++j ; if ( j<i ) cout <<
" Success ! \n " ;
21 . int = 2 ; doue d = 5/ (-2 ) ; if (d==2*x+0 . 5) cout <<
" Success ! \n " ;
22 . string<char> s " Success ! \n" ; for ( int i=O ; i<=lO ; ++i) cout
s [i] ;

23 . int i=O ; while ( i< l O ) ++j ; if ( j<i ) cout << "Success ! \n" ;
24 . int 4; doue d = 5/ (-2 ) ; if (d=2*x+0 . 5 ) cout <<
" Succes s ! \n " ;
25 . cin << " Success ! \n " ;


1 . .
2. ?
3. ?
4. ,

.
5. ?
6. ? .
5.1 1 . 21 3

7. ? .
8. ?
.
9. ? .
1 0 . ,
.
1 1 . ?
?
1 2 .
.
13. ,
(" ")?
14. ?
1 5 . .
1 6 . v [v. size ( ) ] v
? ?
1 7. ;
( area () );
, .
1 8 . ?
1 9. ?
20. .
2 1 . ?
22. ?

assertion
catch
throw



11
1 . ,
.
21 4 5

2.
.
. .
.
doule ctok ( doule ) //
( //
11
int k = + 2 7 3 . 15 ;
return int

int main ( )
(
doule = ; 11
cin >> d ; //
doule k = ctok ( " c" ) ; 11
Cout << k << ' /n ' ; 11

3. , .. -273. l 5C,
.
.
main ( ) ,
-273. 1 5.
4. . 3 ,
ctok ( ) .
5. .
.
6. .
(
4.3.3). ( 5.8) ,
.
7. 2 + + = .

= -------
2
: 2 - 4 ,
. ,
. ,
.
. ,
, .
5.1 1 . 215

? .
?
8. .
, N .
N. vector
N . :
:
3
. ( 1 ) :
12 23 13 24 15
3 ( 12 23 1 3 ) 48 .

. ,
, ,
.
9. . 8 .
,
int.
1 . . 8 . douhle
int. , ,
N- 1 ,
.
1 1 . ,
, .. ,
1 1 2 3 5 8 1 3 2 1 34.
. ,
int.
1 2 . " " .

9 (. . , , 2345, 4455),
. , 1 234,
1 359: " 1 1
", ( 1 )
() (3)
(). ,
, . .
.
1 3 . . ,
(
) .

randint ( lO )
216 5

std_lib_facilities . h. ,

.
,
randint ( l O ) srand ( n) , n - ,
. n .w
(seed),
.
1 4. ( , ) .
:
uesday 23 Friday 56 Tuesday -3 Thursday 99
vector<int>.
.
.
, Funday, ,
, n monday.
.

,
? .
- " !" .
. "
. .
, , ,
, , -
, . ! ,
,
, , ,
. . .
.
.

- .&#'


,
.
,

, , , .
.
-
, .
,

.
218 6

6.1 . 6.5. r
6.2. r
6.2. 1 . 6.5.1 .
r
6.2.2. 6.5.2.
6.3. 6.5.3.
6.3. 1 . 6.5.4.
6.3.2. 6.6.
6.3.3. 6.7.
6.3.4. 6.8.
6.3.5. ! 6.8.1 .
6.4. Token stream
6.4. 1 . : 6.8.2.
6.8.3.
6.4.2. r 6.9.

6.1 .
:
. ,
. -
. , ,
,
, .
, ,
,
. ,
.
?
.
.
.
, ,
.

.
,
.
.
.
6.2. 21 9

, .
, ,
.
: W
. ".
. . ,
:
, ,
.
.
2+3 . 1* 4


14 . 4

, ,

,
.

6.2.
? ,
. ,
. ,
.
, . .
,
.
: ( )
.
- .
, ,
.
, ,
, .
.
,
.
.

.
,
2 20 6

, . .

, .

,
.
, .
, , ,

, , ,
, , - ,
( ).

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

6.2.2.
,
.

? ,
, - , .
, -
, , - ,
.
( );
6.2. 221

, ,
, . "
?" "
?" ,
.
?
.

. .
.
.
. , ,
.
. , ,
() . ,
" 2 . 0",
.
,
? ,
. (
) , ,
, .
( ) ( )
, .
.
, .
, u .
, ,
? .

: ++.

.

, ..
, .
: ,
.
:
- ,
, . , ,
,
222 6

. ,

? . ,

,
.
,
(
).
, .
( vector), ( string) ,
(cin cout) .
.
, ,
(Token Token_strea.m) . 8 1 3- 1 5
.
.
, ,
, , , ..
.

- , .

. .. , .
. ,
, .


(. 9,
- 1 4 - 20).
,
. ,
. ,
( , ?!),
. ()
( )
, . ,
, .
:
,
;
6.. 223

,
r .
,
. ,
.

(prototype).
( ), .
. .
;
.
,
. ,
,
.
,
.

6 ..
? :
, cin cout,
(GUI) 1 6,
r .
r. , :
l : 2+2
: 4
: 2+2 *3
:
: 2+3-25/5
:

, 2+2 2+2*3,
; .
" : "
.

" , ",
.
, , >.
.
.
.

(use cases) .
224 6

.
:
read line
calculate //
write result

, , - :
(pseudo code).
. ,
. . calculate" ?
. ?
.

6.3.1 .
,
.
. - . .
. - . .
- ,
. :
ftinclude " s td lib facilities . h "
int a.in ( )
{
cout << "'l' ( + -) : " ;
int lval = ;
int rval ;
char ;
int res ;
cin>>lval>>op>>rval ; 11 - 1 + 3
if ( == ' + ' )
res = lval + rval ; //
else if (== ' - ' )
res = lval - rval ; //

cout << " Peyrrarr : " << res << ' \n ' ;
keep_window_open ( ) ;
return ;

. ,
2+2, ( 4)
. , ,
lval . . . - rval.
(-) ! . ?
- ! ,
, . .
6. 3 . 225

, .
-.
1 . .
2. (, 2 *3).
3. (,
1+2+3).
, ,
( "" )

s w i tch, if.
, 1+2+3+4 ,
; , 1, +2
2 1 ( , 3), +3
3 , 3, ..

:
#include " s td lib facilities . h "
int ain ( )
{
cout << " ( + , , * /) \n" ;
-

cout << " ( 1+2*3) : " ;


int lval = ;
int rval ;
cin >> lval ; 11

if ( ! cin)

error ( "e r " ) ;

for ( char ; cin >> ; 11


11
11 . . .
if ( ! = ' ' )

cin >> rval ;

if ( ! cin)

error ( "e r " ) ;

switch ( )

'+' :
2 26 6

lval += rval ; // : l va l l va l + rva l


break ;

case ' - ' :


lval -= rval ; // : l va l l va l - rva l
break ;

case ' * ' :


lval *= rval ; // : l va l l va l * rval
break ;

case ' / ' :

lval / = rval ; // : l va l l va l / rva l


break ;

default : // ;
cout << " : " << lval << 1 \n ' ;
keep_window_open ( ) ;
return ;

rrr ( " " ) ;

, 1+2*3,
, 9, 7, .
1-2 * 3 -3, -5, .
: 1+2*3 ( 1+2) * 3 , 1+ ( 2 *3) ,
. 1-2 * 3 ( 1-2) *3, 1- ( 2 * 3 ) ,
. ! , ,
, , .
.
.

6..2.
, (- ) ,
* ( /). , (-
) .
, ,
.
1 . .
.
1
+
2
6.3. 227

.
2. * ( /) +, -, (
) ?
3. , *?
4. ,
( 1+2*3)?
, 1-3,
4 .
, . - ,
, , ,
, ?
: ,
(tokens).
45+11 . 5/7

45
+
11 . 5
/
7
(token) - ,
, ,
. ++
.
.
++,
.
, ++.
3 . 14 , . 2742 42.
. + , -, * /. % .
( , ) .

: 12 , 12 . 3-3,

. , ,
, .
?
,
( ).
2 28 6

, ,
; , 42
- 4 2, ,
42 (.. 4 * 10+2).
- . (, ).
- : ,
? ( - )
.
(, ) ?
Token, . ? ,
: ,
,
. , int
. ,
. s tring
.
++
, char, int, douhle, string, vector ostream,
Token. - -
, - ,
. ,
, - atrix (. 24), Date (. 9)
( "Bignwn") .
, ,
: , ,
?
, ++
,
, (.. ,
).

6.3. .
? .
Token? Token
, + -, , 42
. 14 .
, ,
.
Token : Token :

kind : plus kind : number


value : value : 3.14
6.3. 229


++. . :
clas Token { //
pulic :
char kind;
doule value ;
};

Token - ( , int char) ,


.
( ) : kind value. class
", "; .
. , kind, char,
' + ' ' * '
+. .
Token t ; 11 t - Token
t . kind = ' + ' ; 11 t +
Token t2 ; 11 t2 - Token
t2 . kind = ' 8 ' ; 11
t2 . value = 3 . 14 ; 11 -

_. _
. t . kind " kind t",
t2 . value - " value t2" . Token
, int.
Token tt = t; //
if ( tt . kind ! = t . kind) rrr ( " ! " ) ;
t = t2 ; //
cout << t . value ; // 3 . 1 4

Token, ( 1 . 5+4 ) * 1 1
.

1'(' 1 1+ 1
'8'
1 .5 4 11

, (, +)
, value.
. ' 8 ' .
, .
' 8 ' ,
.
Token ++. .
, - (
), -. Token
230 6

- .
.
class Token {
puic :
char kind ; //
doue value ; // :
);

("") Token,
:
Token tl { ' + ' ) ; // tl ( t l . k i nd = ' + ' )
Token t2 { 1 8 1 , 11 . 5 ) ; // t 2 ,
/ / ( t 2 . kind = ' 8 ' t 2 . va l ue = 1 1 . 5 )


9.4.2 9.7.

6..4.
. , ! ,
. .
Token ?
Token.
Token qet_token ( ) ; 11 cin

vector<Token> tok ; //

int .ain ( )
{
while ( cin)
Token t qet_token ( ) ;
tok . push_back ( t) ;
)
11

, .
, 11 * 1 2 :
'8'

1 1

11 12


. , 11 12
, .
. 1 + 2 * 3
Token.
6.. 231

'8'
1+ 1 '8'

2
1 1 '8'

.
for ( int i = ; i<tok . size ( ) ; ++i ) {
if ( tok [i ] . kind== ' * ' ) { // !
doue d = tok [i-1] . value*tok [i+l ] . value ;
11 ?

. ? d?
? , + *
.
!
1+2*3. 1 *2+3.
1+2*3+4 . " ": 1+ ( 2 * 3 ) + 4 .
, ? ,
. .
,
.
, (
) .
, -
.
, ( . ) ,
.
. ""
.
, (
) ( ) .


,
?
. ,
. ,
. , , 12 . 5+2 .
, , ,
. ,
, , ,
! , ,
+ * 2+3* 4 .
232 6 r

" ". ,
1+2*3/ 4%5+ ( 6-7 * ( 8 ) ) ? . 2+*3
2 &3? . ,
.

6..5. !

,
. - ,
, .
.
.
while (not_fini shed)
read line
calculate 11
write result

, , ,
, ,
.
, ? ,
.
.
.
. ,
,
.
1 . 45+5/7,
- 45, +, 5, / 7? ( !)
2. ? ,
! ( ""
: "" - .)
3. 45+5/7 .
? , 4
5 4 5 (.. 4 * 10+5). (
. - .)
4. . 45+5/7 45+ ( 5/7) ,
( 4 5+5) /7?
6.4. 233

5. 5/7? . 71, .
, ,
.
? !
6. ? ,
v=7
m=9
v*

, . .
, .
7.8 , , w",
. -
.
, ,
- . w
" . ,
.
. .
, . w" ,
:
" ".
? ?
" ", .
1 , 4 .
, , 4 5
+ , - . .
, ? ,
. .
?
. ,
- , -
. .. .
!
. I ,
- , ,
.

6.4.
,
: ,
( ). ,
234 6

45+11 . 5/ 7


45
+
11 . 5
/
7

- ,
, .

. , ,
45+ 11 . 5/7 4 5+ ( 11 . 5/7) , ( 45+11 . 5) /7,
, , ?
- ,
. ,
. .
11 :
:

"+" 11
"-" 11
:
_
" * " _ 11
" / " _ 11
11 % 11 _ 11 ( )
_ :

" ( " " ) " 11
:
Ji___:

. : " -
". : "
_ ".
:
, .
6.3.2, ,
++, :
___ ( ++,
. 14 , . 2742 42):
+ , -, * , / , % ():
( , ) ().
6.4. 235

..
, .
. ,
: . .
. .
. .
( ) ,
( ) .
1-2*3, 1+2-3 3*2+4/2. .
"" .
. ?
-. ?
?
.
.
.
? .
" ". ,
.
w (parsing),
. . w
(parser syntax analyzer).
, , .
: 2 ?
1 . .
_ _
. . _
( . . 2 -
( . 'l'
___'l', .. , .
. _.
2. ( 2) / .
* %, (
/ . * %).
3. (_ 2) +,
-. (
+ -).
, 2 -
. . .
236 6 r

:

" + "
"-"
:

_
"*" _ t
_
; _
"%" _
_:

t
"(" ")"
:
___
t
___

t
2

, ,
. , , 2 - ,
2 - 'l'___'l', ,
_, ,
.
: 2+3 - ?
,
2 .
1 . ,
_ _
, _
( . , 2
( , 'l'___'l', ,
_.
2. _ ( 2) /,
*, %, (
/. * %).
. (_ 2) +,
,
, + . , ,
2 - , , .
-

+ , -,
( ,
+ -). , 2+3 + Term
.
6.4. 237

(
___ ).

2+3


:

"+"
f

"-"

t
:
_ t
_ _

'""""
"*" _

"/" _
"%" _
_:

t

1 1
"(" ")"
:
___

2 +

.
. , , 2 + 3 -
, 2 - , , 3 - ,
, + , .
, ,
,
, + *,
45+11 . 5*7.
.


, 2 2 + 3 . , 45,
11 . 5 7 ___,
, I_,
, _.
1 . 45 - , + ,
, +.
2. 11 . 5 - , * ,
_, *
.
3. 7 - _, 11 . 5*7 -
*_.
, 45+ 1 1 . 5*7 -
+ . , ,
238 6

11 . 5 * 7. 45+11 . 5 * 7 .
4 5+ ( 1 1 . 5 * 7 ) .
( ,
___ ).

" 45+1 1 . 5*7

tt
" + "
"-"
:
_
"*" _ _
t
_ _

'"'""'
"/" _

t t
"%" _
_:

"(" ")"

1 1 1
:
__ _

*
45 + 1 1 .5 7

, .
. , *_
1 1 . 5 7 . 4 5.
.
. .
2+2 45+1 1 . 5* 7.
. .
"" 4 5+ 1 1 . 5* 7
, .
, ,
, .
- , .

6.4.1 . :
,
. ,
,
. .
6.. 239

:
_ // , + + r u l es
J // , Bi rds fly bu t
11 fi sh swim

J :
" and"
"or"
"but"

_ :
"irds "
"fish"
"+ +"

:
"rules "
" fly"
" swim"

(,
, ).
- , ..

, "++ fly and blrds rules",
.

.
.
, , - "
" !

.
. ,
, 6.4 .
, , !

6.4.2.

? "". ,
. ,
. -
: .
240 6

:
_

1 1
:
"and"
"or"
"but"

-""
_ :

"' 1
"irds"
"fish"
"++" _ _

1
"
- "
: "
"rules"
"fly"

''swim"
"irds" " fly" "but" "fish" "swim"

1. .
2. ().
3. ().
4. ().
5. .

. , (
m) u
(tenninals), u (non-tenninals)
-

(productlons) .
. .
.
:
" { " ..n: " } "
..n: :

" " .
,


:
" "
""
6.5. 241

. - , ,
.
, .
-
-

. (?):
{}
{}
{, }
{,, , , }

, , (?):
{}

{, , ,,
{, , , ,
{ }
{,, , , , }

, ,
""; .

7.4 7.8. 1 .

6 .5.

. :
.
Token. . ,
. (parser).
.

6.5. 1 .
. : -
.
qet_token ( ) //
11 cin
expression ( ) // + -
11 t e rm () ge t_ token ()
ter111 ( ) // * , / %
// prima ry () ge t_ t oken ()
primary ( ) //
11 expre s s i on () ge t_ token ()
242 6

:
, ;
. -
, ,
.
?

, , get_
token ( ) , . .
primary ( ) () ,
:
qet_token ( ) // (
expression ( ) 11

?
? ,
2+3 expression ( ) 5,
. .
, : "
45+5/7 , ?"
,
.
!
,
expression ( ) -
. , 80/ .
get token ( ) :
_

, ,
. , + ( - . , get_
token ( ) Token. ,
.
1 1 , }')'I
Token qet token ( ) //
_
doue expression ( ) // + -
doue ter. ( ) // * , / %
doue priary ( ) / /

6.5.2.
expression ( ) .
:
:

6.5. 243

' + '
' - '


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

6.5.2. 1 . :
' + ' ,
expression ( ) + ( -),
,

term ( ) .
doue expression ( )
{
doue left = expression ( ) ; //
Token t = get_token ( ) ; //
switch ( t . kind) { //
case ' + ' :
return left + term ( ) ; // ,
11
' - ' :
return left - term ( ) ; // ,
//
default :
return lef t ; //

.
. : ,
, + -.
.
,
. , , + -?
,
. , +.
expression ( )
: expression ( )
expression ( ) , .
.

expression ( ) , .
,
, . pelC!Jpcu
, ,
244 6

. :
( 8.5 .8).

6.5.2.2. :
, ? ,
:
, + -.
.
doue expres ion ( )
{
doule left = Terxn ( ) ; 11
Token t = qet_token ( ) ; 11
witch ( t . kind) { 11
' + ' :
return left + expression ( ) ; 11
11 ,
case ' - ' :
return left - expression ( ) ; 11
11 ,
default :
return lef t ; 11

- - .

.
. ,
1+2 ( 1),
+ , - ( ,
2). , 3.
1+2+3 6. ,
, ,
, 1-2-3. expression ( )
1 , 2-3 (
2, 3). , 1
2-3. ,
1- ( 2-3 ) , 2 .
, 1-2-3 ( 1-2 ) -3 , , -4.
, ,
. . ,
. , 1+2+3
(6), 1+ ( 2+3) ( 1+2) +3.

? ,
6.5. r r 245

.
.
.
. ! ,
, ,
.
- .
expression ( ) . ,
+ -, w.
.
:

' + ' //
' - ' //

,
1-2-3 w 1-2. -
3, 1-2-3
1. - w 2-3. ,
, 1-2-3 ( 1- 2 ) -3, 1- ( 2 - 3 ) .
. , ,
,
, .
, ,
.
, 1-2-3 1- ( 2 - 3 ) ,
( 1-2 ) -3 .
,
, .

6.5.2.3. : ()
? (
6. 5.2): ,
+ -. ,
, , + -, ,
"" "" . .
doue expression ( )
{
doue left = term ( ) ; //
Token t = qet_token ( ) ; 11
while ( t . kind== ' + ' 1 1
t . kind== ' - ' ) { 11 + -
if ( t . kind == ' + ' )
left += term ( ) ; //
246 6

else
left -= ter ( ) ; 11
t = get_token ( ) ;

return left ; 11 : + - ;

:
+ - . , + - ,
get token ( ) .
_

. + -.
doule expression ( )
{
doule left = ter ( ) ; 11
Token t = get_token ( ) ; 11
while ( true) {
switch ( t . kind) {
case ' + ' :
left += ter ( ) ; 11
t = get_token ( ) ;
break ;
case ' - ' :
left -= ter ( ) ; 11
t = get_token ( ) ;
break ;
default :
return left ; 11 : + - ;
1 1

, - -
( 6 . 5 . 2. 1 ) .
expression ( ) expression ( ) .
,
, + - .

6.5..
.
:
_
' * ' _
' / ' _
' l/i ' _

,
. :
6.5. 247

doule term ( )
{
doule left = primary ( ) ;
Token t = qet_token ( ) ;
while ( true)
{
switch ( t . kind)
{
case ' * ' :
left *= primary ( ) ;
t = qet_token ( ) ;
break ;
case ' / ' :
left / = primary ( ) ;
t = qet_ token ( ) ;
break ;
case ' % ' :
left %= primary ( ) ;
t = qet_token ( ) ;
break ;
default :
return left;

, :
(%) .
.
5 6.3.5 - "
?". -

. ! ?
, % .
, .
% .
(. 7 . 5). % ,
:
.
, term ( ) . ,
2/0? .
,
,
.
.
.
248 6

doue term ( )
{
doue left = priary ( ) ;
Token t = get_token ( ) ;
while ( true)
{
switch ( t . kind)
{
case ' * ' :
left *= priary ( ) ;
t = get_token ( ) ;
break ;
case ' / ' :

doule d = priary ( ) ;
if (d = 0 )

rrr ( " " ) ;


}
left / = d ;
t = get_token ( ) ;
break ;

default :
return lef t ;

/ ?
.
swi tch. .

6.5..
.
_ :

' ( ' ' ) '

. , ,
.
doule priary ( )
{
Token t = get_token ( ) ;
switch ( t . kind)
{
case ' ( ' : 11 ' ( ' expre s s i on ' ) '

doule d = expres sion ( ) ;


6.6. 249

t = qet_token ( ) ;
if ( t . kind ! = ' ) ' )

error ( " ' ) ' ") ;

return d;

case 1 8 1 : // ' 8 '


return t . value ; //

default :
rrr ( " " ) ;

expres
sion ( ) term ( ) : ,
Token .

6 . 6.
.
get_token ( ) main ( ) . main ( ) :
expression ( ) .
int main ( )
try
{
while ( cin)

cout << expression ( ) << ' \n ' ;

keep_window_open ( ) ;

catch ( exception& )
{
cerr << e . what ( ) << ' \n ' ;
keep_window_open ( ) ;
return 1 ;

catch ( . . . )
{
cerr << 11 1J\n11 ;
keep_window_open ( ) ;
return 2 ;
250 6

5.6..
get_token ( ) 6.8
.

&
(
get_token ( ) ) calculatorOO . .
.

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

3
4
2

5+6. 5, :
2

3
4
2
5+6
5
, ,
.
. ?
. ? , ""
. .
.
. - - ,
!

. ,
, , .
= :
6 . 6. 251

while ( cin) cout << "= " << expression ( ) << 1 \11 1 ; 11 1

, .
:
2

4
2
5+6
= 5

! , .
.
.
2, 3
?
4 2 ,
4?
5+6 5,
11?

. ,
. ,
? : 4 2, 5+6
11, 5. . ,
1 2 3 4+5 6+7 8+9 1 0 11 12 .

1 2 4+5 6+7 8+9 10 11 12


1
4
6
8
= 10

? 2 , 3. 4 , 9 (. .
4+5)? 6 1 3 (..
6+7)? :
! , ""
? . expression ( ) .

doue expression ( )
{
doule left = term ( ) ; 11
Token t = get_token ( ) ; 11
252 6

while ( true) {
switch ( t . kind)
case ' + ' :
left += term ( ) ; 11
t = get_token ( ) ;
break ;
' - ' :
left -= term ( ) ; 11
t = get_token ( ) ;
break ;
default :
return lef t ; 11 : + - ;
11

Token, get_token ( ) ,
' + ' , ' - ' , .

. . ,
, , - .
, term ( ) .
, ""
.
expres sion () , ""
. (t).
? ,
(.
): ,
.
!
, , .
. . ,
,
.
, -

"Token_strea.m" - ts. , Token_strea.m


- get ( ) , .
- putack ( t) , t .
Token_strea.m 6.8,
, . Token_strea.m,
expression ( ) .
Token_strea.m.
doule expression ( )
{
6 . 6. 253

doule left ter ( ) ;


= 11
Token t ts . qet ( ) ;
= 11
while ( true) {
switch ( t . kind) {
case ' + ' :
left += ter ( ) ; 11
t = ts . qet ( ) ;
break ;
case ' - ' :
left ter ( ) ;
-= 11
t = ts . qet ( ) ;
break ;
defaul t :
ts . putack ( t) ; 11 t
return left ; 11 : + - ;
11

, term ( ) .
doule ter ( )
{
doule left = primary ( ) ;
Token t ts . qet ( ) ;
= 11
while ( true)
{
switch ( t . kind)
{
case ' * ' :
left *= primary ( ) ;
t = ts . qet ( ) ;
break ;
case ' / ' :
{
doule d = primary ( ) ;
if (d 0)=

{
rrr ( " " ) ;
}
left /= d ;
t = t s . qet ( ) ;
break ;

default :
ts . putack ( t) ; 11 t
return left ;
254 6

, primary ( ) .
get token ( ) ts . get ( ) ; primary ( )
.

6 . 7 .
, .
( Token_s tream)
calculatorOl . . . 2
. ?
. ,
"". - ? 3
. 2 . 2+2
. 3.
:
2

3
=2
2+2
=3

. . . putack ( ) expression ( ) term ( )


.

. .
2 3 4 2+3 2 * 3
2
3
4
5

! ! (6) .
.
, "" . ,
,
.
; ,
. ,
, <Enter>
. ,
.
? - "
". ,
,
. , " ",
6.8. 255

,
. q (
"quit" ()). main ( )
while ( cin) cout << "=" << expression ( ) << ' \n ' ; // 1

, :
doue val = ;
while ( cin) {
Token t = ts . get ( ) ;
if ( t . kind == ' q ' ) break ; // ' q '
if ( t . kind == 1 ; ' ) 11 ' ; '
cout << "=" << val << \n ' ;
else
ts . putack ( t ) ;
val = expression ( ) ;

. ,

2;
= 2
2+3 ;
= 5
3+4*5 ;
= 23
q

.
, .
. ,
, .

6.8.
.
Token_Stream. , -
- ,
. Token_Stream
. ,
.

,
( 1 . 5+4 ) * 1 1 (. 6.3.3). ,
cin
. , ,
, -
256 6

, .
. , 1 . 5+4 .
+ , 1 . 5 ?
1 . 55555? , ,
get ( )
putack ( ) . ++
, Token_stream.
, pu i c :
Token, 6.3.3.
. Token_stream
. ++ ,
, :
( pulic : ) (
private : ). , ,
,
, .
class Token strea {
_
pulic :
11
private :
11
11 ( Token s t ream)
_
};

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

.
Token_stream.
? , get ( ) putack ( ) -
- . Token_stream
Token . ,
Token_
stream, cin. ,
Token_stream :
class Token s trea
puic :
6.8. 25 7

Token_stre&111 ( ) ; // Token_ s t rea m ,


11 cin
Token qet ( ) ; 11 Token
void putack ( Token t) ; 11 Token
private :
11
1;

, Token_
stream. ,
cin , -
.
, 7 .
" putack ( ) ,
put ( ) ?
get ( ) putack ( ) : ,
. , putack ()
istream: - .
.
Token_stream .
Token_strea. ts ; // Token_ s t ream ts
Token t = ts . qet ( ) ; 11 Token t s
11 . . .
ts . putack ( t) ; 11 Token t s

, , .

6.8.1 . Token s tream


_

Token_s tream.
Token_s tream? .
Token_stream,
? ,
Token_stream. ,
.
(
). ,
Token .
class Token strea.
pulic :
Token qet ( } ; 11 (ge t () 6 . 8 . 2)
void putack ( Token t) ; //
private :
bool full ( false } ; 11 ?
Token buffer ; 11
11 pu tback ()
1;
258 6

() -. putack ( )
, .
Token_stream:
void Token_stream : : putack ( Token t)
(
buffer = t ; // t
full =true ; //

void ( "") ,
putack ( ) .
- ,
, .

_ : : _-
- putack Token
stream.
- ?
- : ( ) ,
. -
, , ,
. ,
.
.
, -
, .
,
( get ( ) )
, .
void Token_stream: : putack ( Token t)
{
if ( full) error ( "pu tack ( ) : " ) ;
buffer = t ; // t
full = true ; //

full "
Token".
, Token_stream , ..
full fal se ,
get ( ) .
full Token_stream.
6.8. 259

6.8.2.
get ( ) . Token_strea.m : :
buffer Token, get ( )
cin Token.
Token Token stream : : qet ( )
_
(
if ( full) { // Token ?
full = fals e ; //
return buffer ;

char ch ;
cin >> ch ; 11 , >>
11
switch ( ch)
case ' ; 1 : //
case ' q ' : 11
case ' ( ' : case ' ) ' : case ' + ' :
case ' - ' : case ' * ' : case ' / ' :
return Token { ch ) ; //
case ' ' .
case ' ' : case ' 1 ' : case ' 2 ' : case ' 3 ' : case ' 4 ' :
case ' 5 ' : case ' 6 ' : case ' 7 ' : case ' 8 ' : case ' 9 ' :
cin . putack ( ch) ; //
doule val ;
cin >> val ; / /
return Token ( ' 8 ' , val } ; // 8 " "
' '

default :
rrr ( " " ) ;

get ( ) . ,
Token. ,
.
if (full ) { // Token ?
full =false ; //
return buffer ;

full fal se (.. ),


.
. ,
.
error ( ) . .
default :
rrr ( " " ) ;
260 6

error ( ) 5.6.
std lib facili ties . h.
, , . .
kind Token.

.
:
' (' :' ) ' : case ' + ' :

case ' -' : ' * ' : case ' / ' :

return Token { ch ) ; //

6.8..
. .
, 123? , 100+20+3.
12 . 34? ,
12 . 345?
, , , . ++
doue. ,
, - - cin get ( ) .
1 1 .

case ' ' : case ' 1 ' : case ' 2 ' : ' 3 ' : case ' 4 ' :
case 5 ' : 6 ' : case ' 7 ' : case ' ' : case ' 9 ' :
' '

cin . putack ( ch) ; //


doue val ;
cin >> val ; //
return Token { ' B ' , val } ; // 8 " "
' '

, Token
.
, ? .
++ (. ). ,
' . ' (
). , .
, cin , -
( ),
.
:
, 123, 1 ,
cin 23, 100 23. .
. , (
), cin , Token_stream,
6.9. 261

, . ,
,
cin .
, ,
,
.
: .
: " ". ,
( ):
. ?

6. 9 .
, .
, ,
.. . .
ltinclude " s td lib facilities . h"

class Token /* */ } ;
class Token stream /* */ } ;

void Token_stream: : putack ( Token t) /* */


Token Token_stream: : qet ( ) /* */

Token_stream ts ; // ge t () pu tback ()
doue expression ( ) ; //
// primary ()

doue primary ( ) /* */ 11
doue term ( ) /* */ 11 * /
doue expression ( ) /* */ 11 + -

int main ( ) /* */ 11
11 l!

.
. , ts
, ts . get ( ) ,
error ( ) .
. :
expression ( ) term ( )
,

primary ( ) , , . expression ( ) .
(
error ( ) , ) .
262 6

cin

, :
,
. ,
, .
"" expression ( ) .
? ,
. , ,
.
, ? ,
, .
6.6 . (
6. 7) , (
) .
.
, ,
.


, , ,
, .

,
.
1 . calculator02buggy . . .
. . -
6 . 9. 263

, calculator02
buggy . . , .
2 . q .
3. ; =.
4. main ( ) .
" . -'l' !
w- J . "

5. , ,
,
.


1 . " - "?
2. -
. , .
3. ?
4. ,
?
5. " "?
6. -
.
7. ?
8. ?
9. . , ,
I_.
1 . :
, , .
( 17+4 ) / (5-1) .
1 1 . numer ( ) ?
1 2. ?
1 3 . ? ?
1 4 . ? ?
1 5 . ?
1 6. expression ( ) swi tch
?
1 7 . ""?
1 8 . puthack ( ) ?
264 6

1 9. term ( )
( ,) %?
20. - Token?
2 1 . () ?
22. Token_stream,
get ( ) ?
23. swi tch get ( ) Token_stream -
; 1 q ?
24. , ?
25. , ? ?
26. ?
27. , ?

class
private
puic -


-

1 . ,
.
2 . { } ( ) ,
{ ( 4+5) * } / ( 3+4 ) .
3 . :
! . , 7 !
7 * 6* 5 * 4 * 3 * 2 * 1 . !
* /, .. 7 * 8 ! 7* ( 8 ! ) ,
( 7 * 8 ) ! . ,
.
!
1 . : doule,
.
! int
.
6. 9. 265

4. Name_value, .
. 1 9 4 ,
vector<Name value>.
5. " " 6.4. 1 the ,
"h irds fly but the fish
swim".
6. ,
" " 6.4. 1 .
, , . ,
. , irds fly
but the fish swim . , but irds fly but
the fish swim ( ) irds fly but the fish swim . (
) - .
"" "". :
, strinq
>>.
7. .

, ,
: ! (), - (), & (), 1 () " (
).
(. 25.5). ! -
. "
. 1 ( , *
. +), x l y" z
x l (y" z ) , ( x l y) " z . & ,
" . x"y&z " (y&z ) .
8. . 1 2 5 ( " "),
, .
9. .
. , 123
1 . 2 3 .
"123 1 , 2 3 ".
-

int. ,
, . . :
5 ' 5 ' , ' ' ,
. ' 5 ' - ' 0 ' ==5.
l . -
. , . .
,
266 6

. ( 60 , 3)
,
!
( , ) =
(- )! '
! . . 4 ! - 4 *3 * 2 * 1 .
. ,
. ,
,
. ,
- ,
.
:

( , ) = (

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

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


,
.
.
. .
, . .
,
,
,


.
268 7

7. 1 . 7.6.3.
7.2. 7.6.4.
7.3. 7.7.
7 .4. 7 .8.
7.5. : % 7.8 . 1 .
7.8.2.
7 .6.
7.8.3.
7.6. 1 .
7.8.4.
7.6.2.

7 1
. .

, ,
, .
,
, "" -
. " ",
! , -,
, .

, 6.
,
, .
,
.

7 2
. .

6 ,
:
:

,
: . ,
. .
, , - .
.
,
. ,
,
. ,
7 .2. 269

: Peyn'l'a'I' : ? ?
"" .
.

2+3 ; 5*7 ; 2+9 ;

:
" 5
- 35
" 11

: 'l''I' : . :
: 2+3 ; 5 * 7 ; 2+9 ;
: 5
: : 35
: : 11
:

, - , - - .
,
,
. , : Pe
yn'l'a'I' : . -
.
.
. , -
, ,
. , .
=.
.
>.
> 2+3 ;
5
> 5*7 ;
35
>

,
main ( ) .
doue val = ;
while ( cin) {
cout << "> " ; 11
Token t = ts . qet ( ) ;
if ( t . kind == ' q ' ) break ;
if ( t . kind == ; ' )
cout << "= " << val << ' \n ' ; 11
270 7 r

else
ts . putack ( t) ;
val = expression ( ) ;

,
.
> 2+3 ; 5*7 ; 2+9 ;
5
> 35
> 11
>

,
.
:
> 2+3 ; 5*7 ; 2+9 ;
5
35
11
>

, , , , .
main ( ) .
> . =
? , !
> get ( ) , ,
get ( )
Token, , .
, ,
Token s tream.
. .
Token stream. _

.
, ,
.

7 ..
, , "
" ,- "" , ..
,
. " ",
- ,
, - .
7 .. 271

" . !".
,
. , !
: " ! ,
. !"
, r.
1+2+3+4+5+6+7+8
1-2-3-4
! +2

( 1+3 ;
( 1+) ;
1*2/3%4+5- 6 ;
() ;
1+;
+1
1++ ;
1/0
1/0 ;
1++ 2 ;
-2 ;
-2 ; ; ; ;
1234567890123456 ;
'' ;
q
l+q
1+2 ; q



,
. ,
.. ?
, .
? .

(testlng) .
. .
-
, .
26. :
" ,
?" , ..
, , . ,
,
272 7

.
, ,
"" , :
r had little l
srtvrqtiewcet7rewaewre-wqcntrretewru754389652743nvcqnwq;
! @f $ %" &* ( ) : ;


-
, . " -
", " ".
.
, -
" ".
r .
,

+1 ;
()
! +2

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

. ""
.
, main ( )
(. 5.6.3).
catch ( runtie_error& ) {
cerr << e . what ( ) << ' \n ' ;
11 keep_ window_ open () :
cout << " , \n" ;
char ch ;
for ( char ch ; cin >> ch ; ) // -
if ( ch== 1 1 ) return 1 ;
return 1 ;

, keep_window_open ( ) .
, , -
7 .3. w 273

.
.
. keep_window_
open ( ) , ,
. .
:
catch ( runtime error& )
cerr < < e . ;hat ( ) << 1 \n ' ;
keep_window_open ( 11 1 1 ) ;
return 1 ;


+1

()


,
w w ,

, ''""
.
: , ,
( !),
, - . -
.
( , Unix) ,
cin .
cout - .
. ( . 1 0) .
.
1+2 ; q

1+2 q

(3) . ,

1+2 q

,
1+2 ; q
274 7

" ".
? , main ( ) , , ;
q. "" "" ,
( 6. 7) ,
.
:
doue val = ;
while ( cin) {
cout << "> " ;
Token t = ts . qet ( ) ;
if ( t . kind == ' q ' ) break ;
if ( t . kind == ' ; ' )
cout << "= " << val << ' \n ' ;
else
ts . putack ( t) ;
val = expression ( ) ;

, expression ( ) ,
q. , .
term ( ) , primary ( ) ,
q. _,
. ,
q.
,
, main ( )
:
in t ain ( )
try
{
while ( cin)
cout << " > " ;
Token t = ts . qet ( ) ;
1 . 1
while ( t . kind == ' ; ' ) t=ts . qet ( ) ; // " " '

if ( t . kind = = ' q ' ) {


keep_window_open ( ) ;
return ;

ts . putack ( t) ;
cout << "= " << expres sion ( ) << ' \n ' ;

keep_window_open ( ) ;
return ;

catch (exception& ) {
cerr << e . what ( ) << 0 \n ' ;
keep_window_open ( 1 1 11 ) ;
return 1 ;
7 . 4. 275

catch ( . . . ) {
cerr << 11 1U \n" ;
keep_window_open ( 11 1 1 ) ;
return 2 ;

.
.

7 . 4 .
, ,
. ,
-1/2

.
,
(0-1 ) /2

.

. ,
. ,
. ,
.
-
,
.
- . ,
, ,
: ,
.
.
,
. ,
_. :
&J11 :

" ( " 8&J11 " ) "

, :
:

" ( " ) " "
276 7

"-"
"+"
. ++.
, ,
. , _,
:
doule primary ( )
{
Token t = ts . qet ( ) ;
switch ( t . kind) {
case ' ( ' : // ' ( ' ' ) '
{
doule d = expres sion ( ) ;
t = ts . qet ( ) ;
if ( t . kind ! = ' ) ' ) rrr ( " ' ) ' " ) ;
return d ;

case 8 :
' ' // ' 8 '
return t . value ; //
case ' - ' :
return - primary ( ) ;
case ' + ' :
return primary ( ) ;
default :
rrr ( " " ) ;

. .

7 5 : %
. .

, ,
- % .
, .
. .
1 . % Token.
2. doule int,
% .
, % ,
:
> 2%3 ;
= 2
> 3%2 ;
1
7 .5. : % 277

> 5% 3 ;
"" 2

. ?
> . 7% 3 . 3 ;

?
.
. , %
x%y=x-y*int (x/y) . 6 . 7% 3 . 3==6 . 7-3 . 3*int ( 6 . 7/ 3 . 3 ) , .. 0 . 1.
fmod ( )
<cmath> (24.8). term ( ) .
:
case ' % ' :

doue d = primary ( ) ;
if (d == ) error ( " % : " ) ;
left = fmod ( left , d) ;
t = ts . get ( ) ;
break ;

<cmath>
, sqrt ( ) ( ) . abs () (
) . log () ( ) pow ( , ) ( ).
%
. . ,
. . .
.
%
(3 . 9 . 2 5.6.4),
narrow cast:
case ' % ' :

int il narrow_cast<int> ( left) ;


=

int i2 narrow_cast<int> (primary ( ) ) ;


if ( i2 0 ) error ( " % : " ) ;


=

left =il%i2 ;
t ts . get ( ) ;
=

break ;

. .
278 7

7 6
. .

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

7 6 1
. . .

. . ' 8 '
Token, .
,
; ,
.
, .
case ' 0 ' : 11 ' 0 '
return t . value ; 11
case ' - ' :
return -priary ( ) ;

, ,
' ' , ' 8 ' , ,
. . -
' 8 ' . Token,
, ;
1 8 1 "
", 4.3. 1 .
. .
const char nWDer = 1 8 1 ; // t . k i nd==number , t -

const . .
; , nu.mer= ' '
. nu.mer
' 8 ' .
primary ( ) . , :
case nWlle r :
return t . value ; //
case ' - ' :
return -priary ( ) ;
7 .6. 279

.
, .
, , -
. .
Token_stream : : get ( ) , ,
:
case 1.
1

case 1 0 1 : case ' 1 ' : case ' 2 ' : case 1 3 1 : case ' 4 ' :
case ' 5 ' : case 1 6 1 : case ' 7 ' : case 1 8 1 : case ' 9 ' :

cin . putack ( ch) ; //


doule val ;
cin >> val ; //
return Token (numer , val ) ;

,
. , ' ( ' ' + ' .
, . ' ; ' "" (
" ") ' q ' ""
. ' ' ' ' ?

, :
const char quit = ' q ' ; 11 t . k i nd==qui t , t -
//
const char print = ' ; ' ; // t . kind==pri n t , t -
//

main ( ) :
while ( cin)
cout << "> " ;
Token t = ts . get ( ) ;
while ( t . kind == print) t=ts . get ( ) ;
if ( t . kind == quit) {
keep_window_open ( ) ;
return ;

ts . putack ( t ) ;
cout << "= " << expression ( ) << ' \n ' ;

"" "",
. , ,
main ( ) , , . ,
, "
" ' ' ( "exit").
main () .
280 7

"> " "= "


"" ? ,
main ( ) ,
? , ?
,
.
const strinq prompt "> " ;
const strinq result " = " ; 11 ,
11


, .
:
while ( cin) {
cout << prompt ;
Token t = ts . qet ( ) ;
while ( t . kind ==print) t=ts . qet ( ) ;
if ( t . kind == quit) {
keep_window_open ( ) ;
return ;

ts . putack ( t) ;
cout << result << expression ( ) << 1 \n ' ;

7 6 2
. . .

,
.
:
expression ( ) , term ( ) primary ( )
, get ( )
. main ( ) ,
.
1 . main () :
, "" .
2. main ( ) .

(. 4.5. 1 ) . main ( )
. .

calculate ( ) .
7 .6. 281

void calculate ( ) //
{
while ( cin)
cout << prorpt ;
Token t = ts . qet ( ) ;
while ( t . kind == print) t=ts . qet ( ) ; 11
if ( t . kind == quit) return ;
ts . putack ( t) ;
cout << result << expression ( ) << ' \n ' ;

int main ( )
try {
calculate ( ) ;
keep_window_open ( ) ; 11
return ;

catch ( runtime_error& ) {
cerr << e . what ( ) << endl ;
keep_window_open ( 11 11 ) ;
return 1 ;

catch ( . . . ) {
cerr << " \n" ;
keep_window_open ( 11 11 ) ;
return 2 ;

, ,
.

7 .6..
:
switch ( ch) {
case q : case ' ; ' : case ' % ' : case ' ( ' : case ' ) ' :
' '

case ' + ' : case ' - ' : case ' ' : case ' / ' :
return Token { ch } ; //

, ' q ' , ' ; ' ' % ' ,


. . ,
. , /
case
. Token_stream : : get ( )
:
Token Token_stream : : qet ( )
11 c i n Token
282 7

if ( full) { // Token
full =fal se ;
return buffer ;

char ch ;
cin >> ch ; 11 , >>
switch ( ch) 11
case quit :
case print :
case 1 ( ' :
' ) 1 :
case ' + ' :
case , _ , :
case 1 * 1 :
case ' / ' :
case 1 % 1 :
return Token { ch } ; //
case 1 1 . /!
//
11 :
case ' : case 1 ' : case
' ' ' 2 ' : case ' : case 4 ' :
' '

case ' 5 ' : case 6 ' : case


' ' 7 : ' 8 ' : ' 9 ' :
'

cin . putack ( ch) ; //


doue val ;
cin >> val ; //
return Token { nwaer , val } ;

default :
rrr ( " neJCcea " ) ;

. case
, .
, get ( )
.
; , .
. .
, ,
' q ' qui t.

qui t . .

.
.
. ,
- , ,
. : .
7 .6. 283

7 6 4
. . .


. - .
. ,
,
,
.
1 . ( . ) .
2. ( ).
. ( ).
: ,
.
. , ,
, .
.
= + ; //
11

,
, ,
.
,
.
: ,
,
(. 5.9. 1 ) .
. - :
, ,
( ) ,
.
.
/*

2 0 0 7 .
2 0 0 6 .
2 0 0 4 .
(bs @ cs . tamu . edu) 2 0 0 4 .

.
284 7 r

cin ; - cou t .

:
q

:

+
-

:
_
* _
/ _
% _

_ :

)
- _
+ _

:
___

cin Token s t ream t s .


*/

,
/ * * / .
,
.
, .
:
, (,
).
calculate ( ) ,
.
7.8. 1 .
7 . 7 . 285

7 . 7 .
,
? ,
?
? , ,
, . ,
. ,
,
,
.

main ( ) .
, calculate ( )
,
.
void calculate ( )
{
while ( cin)
try {
cout << propt;
Token t = ts . get ( ) ;
while ( t . kind == print) t=ts . get ( ) ; //
if ( t . kind == quit) return ; //
ts . putack ( t) ;
cout << result << expression ( ) << ' \n ' ;

catch (exception& )
cerr < < e . what ( ) < < ' \n ' ; //
clean_up_mess ( ) ;

while try,
.
-.
" "?

,
.
Token_stream. , , -
,
. .
1++2 * 3 ; 4+5 ;
286 7

, 2 * 3 ; 4+5
Token stream cin , +
. .
1 . Token_s tream.
2. Token_stream ,
.
( 4+5 ; ),
- 2 * 3 ; , 4+5 ; .
, - .
. .
.
, ,
. . .
get ( ) . ,
clean up mess ( ) :
_ _

void clean_up_ess ( ) //
{
while ( true) { // ,
//
Token t = ts . get ( ) ;
if ( t . kind == print) return ;

. . ?
:
l@ z ; 1+3 ;
@ catch while.
clean_up_mess ( ) .
, . get ( ) z .
( z
), catch ( . . . )
main ( ) .
1+3.
try catch,
.
, .
, .
Token stream ,
_

.
get ( ) , . ,
7. 7. 287

. , .
- Token_s tream.
class Token stream
puic :
Token qet ( ) ; 11
void putack ( Token t ) ; 11
void iqnore ( char ) ; 11
11
private :
bool full { false } ; 11 ?
Token buffer ; 11 ,
11 pu tback (}
};

ignore ( ) Token_stream,
.
ignore ( ) - ,
Token_stream ,
.
, ,
, ( ,
). , :
void Token stream : : iqnore ( char )
_
11

11 :
if ( full && c==buffer . kind)
full = fal se ;
return ;

full = false ;

11 :
char ch = ;
while ( cinch)
if ( ch==c) return ;

.
, . ;
cin, .
clean_up_mess ( ) :
void clean_up_ess ( )
{
ts . iqnore (print) ;
288 7

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

7 . 8 .
,
.
; ?
.
.
,
, pi , .
-
. .
,
. ,

.

7 .8. 1 .
,
( . ) ,
.
Variae:
clas Variale
puic :
tring name ;
doule value ;
};

name
Variae. value - .
name.
Variae ,
, name ?
,
: Variale.
7 .8. 289

vector<Variale> var_tale ;

var_t
Variae, ,
. get_value ( ) ,
name value.
doule qet_value ( strinq s )
1 1 . s

for ( const Variale& v : var_tale)


if (v . nae == s) return v . value ;
error ( 1 1qe t : n 11 s) ;

: Variae
var_tae (
) , name s .
name s , value
.
set_val ue ( ) .
value Variae.
void set_value ( s trinq s , doule d)
11 s Va ri a e d

for (Variae& v : var_ta.le)


if (v . nae ==s) (
v . value = d ;
return ;

error ( 1 1 se t : n n 11 s) ;

,
Variae var_t.
Variale var_tale?
, ,
? , ++.
doule var = 7 . 2 ;

,
doule, .
.
var = 7 . 2 ;

. ,
.
290 7

varl = 7 . 2 ; // va rl
varl = 3 . 2 ; // var2

! var2 = . 2 ; , "" (
). , . . .
, ++.
( ) .
doule, -
, - -
let.
let var = 7 . 2 ;

r :
:

l

w

:
'
:

'n :
" let" 11 = 11

- ( ) .
( calculate ( ) ),
.

'I'. , :
doule s tatement ( )
{
Token t = ts . get ( ) ;
switch ( t . kind) {
case let :
return declaration ( ) ;
default :
ts . putack ( t ) ;
return expression ( ) ;

expression ( ) calculate ( )
statement ( ) .
void calculate ( )
{
while (cin)
7 . 8. 291

try {
cout << prompt ;
Token t = ts . qet ( ) ;
while ( t . kind == print)
t=ts . qet ( ) ; //
if ( t . kind== quit) return ; //
ts . putack ( t) ;
cout << result << stateent ( ) << \n ' ;

catch (exception& ) {
cerr << e . what ( ) << ' \n ' ; //
clean_up_ess ( ) ;

declaration ( ) .
? , let ,
- = . .
? var_t
vector<Variae> Variale name
.
get_value ( ) set_
value ( ) . , ,
. .
let vl 7;
let vl = 8 ;


. . ,

let vl 7;
let v2 ;

Variale var val


.
1. var_tale Variale
var.
2. (var,val) var_tale.
,
is_declared ( ) define_name ( ) ,
.
bool is_declared ( strinq var)
11 va r va r_ ta l e ?

for ( const Variae& v : var_tae)


if ( v . name var) return true ;
==
292 7

return false ;

doule define_name ( strinq var , doule val)


11 (va r , va l ) va r_ ta e

if ( i s_declared (var) ) error (var , " '111111 ) ;


var_tae . push_back (Variale (var , val ) ) ;
return val ;

Variae vector<Variale>
: - push_back ( ) .
var_tale . push_back (Variale (var , val ) ) ;

Variae (var , val )


Variae, push_back ( )
var_tae. , ,
let , declara tion ( ) .
doule declaration ( )
11 , "l e t "
11 : =
11 ,
11

Token t = ts . qet ( ) ;
if ( t . kind ! = name)
error ( " ' " ) ;
strinq var_name = t . name ;

Token t2 = ts . qet ( ) ;
if ( t2 . kind ! = ' = ' )
error ( " ' " var_name) ;

doule d = expression ( ) ;
define_name (var_name , d) ;
retur d ;

, ,
. ,
,
let v = d/ ( t2-tl) ;

v .
, calculate ( ) ,
statement ( ) .
.
.
7 .8. 293


(symbol taes) .
map ( 2 1 .6. 1 ) .

7 .8.2.
, , , .
. - . -
. -
. ' = ' , .
case Token_stream : : qet ( ) (
7.6.3). let ? , ,
,
qet ( ) . ? .
const char n. = ' ' ; 11
const char let = ' L ' ; 11 l e t
const string declkey = " let" ; 1 1 l e t

Token Token_stream: : get ( )

if ( full ) {
fullfal se ;
return buffer ;

char ch ;
cin >> ch ;
switch ( ch)
11
default :
if ( i salpha ( ch ) ) {
cin . putack ( ch) ;
string s ;
cin >> s ;
if ( == declkey)
return Token ( let) ; 1 1 l e t
return Token { na.s , s } ;

rrr ( " " ) ;

. isalpha ( ch) .
" ch ?";
isalpha ( )
std_lib_facilities . h.
1 1 .6.
:
294 7

( - ).
putack ( )
>>.
. ; Token
. Token { name , s } .
. Token
. doule, s tring
.
kind; . Token { ' * ' } .
kind ; . Token { numer , 4 . 321 } .
kind ; , Token { name , "pi " } .
, ,
, :
class Token {
pulic :
char kind ;
doue value ;
s trinq name ;

11 kind ch
Token ( char ch) : kind { ch ) { }

11 k i nd va l u e
Token ( char ch , doue val) : kind { ch } , value { val } { }

11 kind
Token ( char ch , strinq n) : kind { ch } , name { n } { }
};


. 9 (
9.4.2. 9. 7).
let ' L ' ,
. .
doule, var, i - .
declkey. s .
.
, . :
let = 3 . 4 ;
let = 2 ;
+ * 2;

. ,
. :
7 . . 295

let = 3 . 4 ;
let = 2 ;
+*2 ;

? , .
, . "
" ( 7 .8. 1 ) .
? ? . ? ,
. ? +? ?"
.
s tring >>.
, . , , +*2 ;
-
. , , .
?
, ,
get ( ) . :
, . ,
:

al
Z12
asdsddsfdfdasfdsa434RTHD12345dfdsa8fsd88 8fadsf

:
la
as s
#
as*
car


++.
defaul t get ( ) .
default :
if ( isalpha ( ch) )
strinq s ;
s += ch ;
while ( cin . qet ( ch) &&
( isalpha (ch) 1 1 isdiqit ( ch) ) ) s+=ch ;
cin . putack ( ch) ;
if ( s == declkey)
return Token { let } ; 11
return Token { nae , s } ;

rrr ( " " } ;


296 7

s string
s,
. s+=ch ()
ch s.
while ( cin . qe t ( ch) && ( isalpha ( ch) 1 1 isdiqit ( ch) ) s+=ch ;

ch ( - get ( )
cin) , . ,
ch s . -
get ( ) >>. ,
.

7 ...
. ,
,
, pi .
? main ( ) calcula te ( )
calculate ( ) .
main ( ) , - .
int main ( )
try (
11
define_nae ( "pi " , 3 . 141592 6535 ) ;
define_nae ( " " , 2 . 7182818284) ;

calculate ( ) ;

keep_window_open ( ) ; 11
return ;

catch ( exception& ) (
cerr << e . what ( ) << 1 \n ' ;
keep_window_open ( " -- 11 ) ;
return 1 ;

catch ( . . . ) (
cerr << 111JDD \n" ;
keep_window_open ( " -- " ) ;
return 2 ;

7 8 4 ?
. . .

- . ,
,
7 .8. 297

. , .
. "" (. . 2),
-
(. . 3).

. . ,
.
1 . :
.
2. .
.
45%,
.
, ,
, .
, 50/
.
. .
, . -
, .. , ,
,
.


1 . calculatorOBbuggy . .

2. .
3. (
. ) . :
.
4. :
. ?
? , ,
" " .
5. ,
.
6. k 1000.
7. sqrt ( ) .
sqrt ( 2+6 . 7 ) . , sqrt () -
298 7

; . sqrt ( 9) 3.
sqrt ( ) ,
std_lib_facilities . h.
.
8.

.
9. pow ( , i ) .
" i "; , pow ( 2 . 5 , 3)
2 . 5*2 . 5*2 . 5 . i .
.
% .
1 0. " " let #.
1 1 . " " q exi t.
qui t,
let 7.8.2.


1 . ,
? .
2. "1+2 ; q". .
?
. nwner?
4. main ( ) .
main ( ) ?
5 . ?
.
6. ?
7. narrow_cast?
8. ?
9. ?
1 0. % ()
?
1 1 . i s_declared ( ) ?
1 2. let
.
?
7 .8. 299

1 3. , ,
, .
14. ?
1 5. ?
1 6. ?
1 7. ,
?
1 8. ?
.
1 9. ?
20. , ?
2 1 . ?

1 .
.
2. =,

let. ,
.
3. ,
. : Variae ,
,
set_value ( ) .
(
pi ),
, const pi = 3 . 14 ; .
4. gt_vlu ( ) , set_value ( ) , is_declared ( ) define_name ( )
var t. Symol_t
_

var_t vector<Variae> - get ( ) ,


set ( ) , is_declared ( ) define ( ) .
Symol_t.
5. Token_stream : : get ( ) , ,
, Token (print) .
300 7


' \n ' .
i s space ( ch) , true,
ch .
6. .
( )
.
7 . q h quit help .
8. 7.6.4 (
,
): ,
4+4 ; 5-6 ; , ,
7.8. . ,
, .
9. (
). .
1 . ,
; ,
. : narrow_cast ( 7.5).
1 1 . ,
4 5.
, . ,
-
.

, ,
. ,
, .
.
( ).
. i
;
.
:

,, -.,.,."....-.
k.

u

-
++ . ,


++ . ,
,
,

.
302 :

8. 1 . 8.5.5.
8.2.
8.2 . 1 . 8.5.6.
8.2.2.

8.2.. 8.5.7.

8.3.
8.5.8.
8.4. 8.5.9. nstr-
8.5. 8.6.
8.5.1 . 8.6.1 .
8.6.2.
8.5.2.
8.7.
8.5..
8.7.1 . usinq

usinq
8.5.4.

8.1 .
,
, . .
, ,
,
. :
,
, .
.
.
.
, ,
" .
,
.
. .
.
- .
, .

. , ,
- . ,
. ++ ,
.2. 303

; ,
.
,
, ,
. ,
.
,
. - -
.
. .
, , ,
, . ,
++ , ,
( 27), Java #, .
, , ,
, f, g, .
, ,
.
(
), ,
. ,
.
,
,
.
, ,
++ (
, ) . ISO ++
1 300 ; The ++ Programming
Laguage 1 300 ,
(
++ ).
,
,
, .

8.2.
(declaration) - ,
( 8.4),
(,
)
304 :

() (.
).
.
int = 7 ; 11 i n t
const doue c d = 8 . 7 ; //
//
doue sqrt (doule ) ; 11 dou e ,
1 1 doue
vector<Token> v ; 11 - Token

++.
.
int main ( )
{
cout << f ( i ) << ' \n ' ;

,
: cout. f i
. , cout, ,
s td_lib_facilities . h.
.
ftinclude " std li. facilities . h" // co u t

int main ( )
{
cout << f ( i ) << ' \n ' ;

.
.
.
,
"- ". ,
, ;
, .
,
: , cout
<<;
ftinclude. ;
, ,
. cout.
, "" .
- f i .
:
8.2. 305

#include " s td lib facilities . h" // co u t

int f ( int) ; 11 f

int ain ( )
{
int i = 7 ; 11 i
cout << f ( i ) << ' \n ' ;

,
, (. 2.4),
f ( ) ; , ,
f () .
, ,
(definition). .
int = 7 ;
vector<doue> v ;
doue sqrt (doue d ) { / * . . . */ }

,
.
, ;
,
, - .
doue sqrt (doule) ; //
extern int ; // "extern "
11 ,

,
, ,
, ,
.
, .
, .
, .
.
doue sqrt (doue d) { / * . . . */} //
doue sqrt (doue d) { / * . . . */} // :

int ; 1 1
int ; // :

, ,
, , ;
, .
306 t :

,
, .
int = 7 ; //
extern int ; 11
extern int ; //

doule sqrt ( doule) ; /!


doule sqrt ( doule d) { / * . . . */} //
doule sqrt ( doule ) ; 11 sqrt
doule sqrt ( doule ) ; // sqrt

int sqrt ( doule ) ; // :


// sqrt

?
sqrt,
doule (int
doule).
extern,
, , .
. ,
,
, (.
8.4 8.6.2).
: :

doule sqrt(doue d) doue sqrt(doue d)


{
11
doue sqrt(doue d) 11
11 d

extern int ;

int x = 7;
extern lnt ;

++ ,
?
, ,
(), , ,
, () .
, (
). (
),
( ). ,
, -
.2. 307

,
, - .

.
;
.
( ),
.
8.3.
6: expression ( )
term ( ) , , . primary ( ) ,
expres sion ( ) .
++ ,
, .
doue expression ( ) ; 11 ,

doule priary ( )
{
11 . ..
expression ( ) ;
11 . . .

doule term ( )
{
11 ...
primsry ( ) ;
11 . . .

doule expression ( )
{
11 ...
term ( ) ;
11 . . .

;
,
. ,
. expression ( )
primary ( ) . .
.
?
, (
), , ?
308 :

, "" ,
. ++ ,
(
; . 9.4.4). ,
( ): , ,
,
,
. " "
, .
,
. ( ),
, , "-".
"-" , .
,
, ( )
.

8.2. 1 .
++.
.
.
.
(. 8 . 5) .
(. 8. 7).
( ; . 9).
(. 1 9).

8.2.2.
, (
) . .
int ; //
dou.le d = 7 ; 11

vector<int> vi ( 10 ) ; // ()
vector<int> vi2 { 1 , 2 , 3 , 4 } ; // { }

ISO ++.
, ,
const .
8.2. 309

cont int =7 ; // =
cont int 2 { 9 } ; // { }
cont int ; // : '

, ,
:
. , -
; , ,
. .
void f ( int z)
{
int ; //
// . . .
7; //

11 . . .

, ,
. ,
? .
void f ( int z )
{
int ; //
// . . .

if ( z>x) {
11
}
11
= 7; //

11 . . .

, z>x
. z>x

.
z>x -
, -
.
,
, ,
. . " " (,
) -
. ,
, -
- . .
. ,
310 8 :

, ;
. , ,
.

{ } .
"". ,
, =.
( ) ,
(. 1 7.4.4).

8.2..
, ,
string, vector .
vector<strinq> v ;
strinq s ;
while ( cin>>s) v . push_back ( s ) ;

.
.
, ,
. ,
v (.. ), s (
" "). , ,
(default constructor).
, ++
. (. 8.4)
,
. ,
,
, (
). , !

. .
?
,
;
. , ,
. , cout
sqrt ( ) - .
. I ,
- , ++ . ,
8.. 311

(header) :
-

, (header file).

#include. ,
(. 6 7).
. token . h,
, Token Token _

stream:
token.h:

// :
class Token { /* . . . / } ;

class Token_atream { /* . . / } ;
.

token .cpp:

#include "token.h'"
/ "''"'''"'
//:
#include "token .h'"
vold Token_atream:: putback(Token t)
// :
{
buffer = t;
Token_stream ts;
full = true;

Token t = ta.get();

ts. putback(t);

Token Token stream


token . h. token . . ++
h ,
.

. .
++ .

.
, #include "file . h"
file . h , .
. f . h.
11 f . h
int f ( int) ;

user .
11 user .
#include " f . h "

int g ( int i )
31 2 :

return f ( i ) ;

user .
. :
int f ( int) ;
int q ( int i )
{
return f ( i ) ;

#include
,
(preprocessing) ( . 1 7).

. .
, ,
.
. , , Token_
stream : : putack ( ) .
Token Token_s tream : : putack ( Token t)
{
buffer . push_back ( t) ;
return t ;

. ,
, ( #include)
Token_stream : : putack ( ) .
, , putack ( )
Token, buffer
Token, vector<Token>,
push_back ( ) . ,
,
.
:
Token t = ts . qett ( ) ; 11 : ge t t
11 . . .
ts . pu tack ( ) ; 11 :

; token . h
, .
s td_lib_facilities . h
, cou t, vector sqrt ( ) ,
, error ( ) .
8. 4. 31 3

. 1 2. 8

.
.
, ,
(, -
, ).

8.4.
(scope) - .

(.. ),
,
. .
void f ( )
{
q() ; 11 : g () ( )

void q ( )
{
f() ; 11 : f ()

void h ( )
{
int = ; 11 : ( )
11
int = ; 11 :
q() ; 11 : g ()

, ,
. . f ( )
g ( ) , "
. h
. , , ,
, - , f ( )
g ( ) .
,
.
(global scope):
, .
31 4 :

(namespace scope):
.
( 8. 7).
(class scope): .
( 9.2).
(local scope):
{ . . . } .
: . for.
-
. .
. .
void f ( int ) 11 f ;
11 f

int z = +7 ; 11 z

int q ( int ) 11 g ;
11 g

int f = +2 ; 11 f
return 2 *f ;

.
R :

'D

f ( )
g ( ) .
.

: f ( )
f ( ) .
g ( ) g ( ) .
(clash) .
f g ( ) (
) f ( ) .
.
.
8.4. . 31 5

int ma.x ( int , int ) // ,


//
-

return (>=) ? : ;

int as ( int ) 11
// ()

return (<) ? - : ;

max ( ) s ( ) ,
. ? :
{ (arithmetic if) (conditional expres
sion). (>=) ? : , >=, -
.
.:
int ax ( int , int // ,
11
-

int 111 ; 11
if ( >=)
111 = ;
else
111 = ;
return 111 ;

.
.

, .
, , ,
, ,
. :
.
.
,
,
( ).
11 r, i v
class y_vector
{
vector<int> v ; // v
11
pulic :
31 6 :

int larqest ( )

int r = ; // r (
// )
for ( int i = ; i < v . size ( ) ; ++i) // i
r = max ( r , as ( v [ i ] ) ) ; // for

11 i
return r ;
)
11 r
};
// v

int ; // -
int ;

int f ( )

int ; // ,
//
= 7; //

int = ; //
11
11
++ ; //

++ ; // f ()
return ;

I , .
" !"
,
: , , z.
. .
, , ,
.
. , ,
, .
? ? ?
? .
, ,
! (.
) - . .
, 6 7,
: ts na.mes.
8.4. 31 7

, ++.
, .
: - ( 9.4.2).
class {
pulic :
void f ( ) ;
void q ( ) // -
{
11
}
11
};

void C : : f ( ) // -
{
11 . . .

.
: - ( - ).
class {
puic:
struct
11
};
11
};
; ,
.
: .
void f ( )
{
class L {
11
};
11

; ,
, .

: (
).
void f ( )
{
void q ( ) //
{
11 . . .
31 8 :

}
11

++ ; -
.
( - ).
void f ( int , int )
{
if {>)
11
}
else {
11
{
11

11
}

,
.
++ - name
space,
( 8. 7).
,
.
:
11
s truct {
void f ( int ) {
s truct {
int f ( ) { return 1 ; } int m; } ;
int m ;
m=x ; 1112 ;
return f (lll2 . f ( ) ) ;
int m ; void g ( int m) {
if (m) f (m+2 ) ; else {
g (m+2 ) ; } }
( ) { } void m3 ( ) {
}

void main ( )
; a . f (2) ; }
};

.
, -
8.5. 31 9

(
). , "
", (
).
.

8. 5 .
.
-, ,
. ++ (, + *), -
,
, (, for if),
.
, .

, , . ,
.

8.5. 1 .
++
.
,
. :
doue fct ( int , doue d) ; 11 fc t ( }
doue fct ( int , doue d)
{ return a*d; } // fc t

(,
), , ,
.
(parameters). ,
, , :
int current_Power ( ) ; //

, ,
void, :
void increase_Power ( int level ) ; //

void " ".


, . ,
.
.
320 :

// s vs ;
// vs [hin t }
// ; -1 ,
//
int y find (vector<tring> v , //
_
tring , int hint) ;
int y_find ( vector<tring> , //
s tring , int) ;

r
,
. -
my_find ( ) , :
, .
r .
.
int y_find (vector<string> vs , tring , int hint)
// s vs , h i n t
{
if (hint < 1 1 vs . size ( ) <= hint) hint = ;
for (int i = hint ; i < vs . size ( ) ; ++i ) // ,
// h i n t
i f (vs [ i ] ==s) return i ;
if ( 0 < hint) { // r ,
11 h i n t
for ( int i = ; i < hint ; ++i )
if (vs [ i ] ==s ) return i ;

return -1 ;

hint ,
n, ,
. ,
my_find ( ) , ,
hint,
. hint ,
" " my_find ( )
hint. (
, ),
my_find ( ) .
r. , .
int y_find ( vector<string> vs , string s , int) // 3 -
//
for (int i = ; i < vs . size ( ) ; ++i )
if (vs [ i ] ==s) return i ;
8.5. 321

return -1 ;

ISO
++.

8.5.2.

return.
f ( ) // f { )
{
V v;
11 . . .
return v ;

= f() ;

- ,
v.
V v;
11 . . .
t (v) ; // t v
, - .
. . .
. " ":
doue _s ( int ) // :
{
if ( < 0 )
return - ;
else if ( > 0 )
return ;
// : = = ,


, "" = 1 ,
, ,
. . ,
return error ( )
.

1 , >= , ,


,
, . - . .
322 :

main ( )
. " " main ( )
return , .
, , return

, :
void print_until_s (vector<string> v, string quit)
{
for ( int s : v) {
if ( v [ i ]
== quit) return ;
cout << v [i ] << ' \n ' ;

, " " vid-.


" " return ; .

8.5..

,
. f ( ) , -
. .
11 (
11 )
int f ( int )

= x+l ; //

return ;

int main ( )
{
int = ;
cout f (xx) << ' \n ' ; 11 : 1
cout << << ' \n ' ; 11 : ; f ()

int = 7 ;
cout << f () << ' \n ' ; 11 : 8
cout << << ' \n ' ; 11 : 7 ; f ( )

, x=x+l
f ( ) ,
.
.
8.5. 323

: :

: 1
1



: :

: 1 7
1

7

.
.

8.5-'.
, ,
, int, doue
Token (. 6.3.2).
(
), (, )
(, )?
. ,
,
,
, . ,
,
:
void print (vector<doule> v) // ; ?
{
cout << " { " ;
for ( int i = ; i < v . si ze ( ) ; ++i ) {
cout << v [ i ] ;
if ( i ! =v . size ( ) -1) cout << " " ;

cout << " } \n" ;

print ( ) :
void f ( int )
{
vector<doule> vdl ( l O ) ; 11
vector<doue> vd2 ( 1000000 ) ; 11
vector<doue> vd (x) ; 11
// . . . vdl , vd2, vd . . .
print (vdl ) ;
print (vd2 ) ;
print (vd) ;
324 :

, print ( )
doue (, 80 ), -
doue (, 8 ),
. " -
?" ,
. ,
. ,
, ,
, -
, .
- print ( )
"" . . ""
(reference) , :
void print ( const vector<doule>& v) //
{ // U1
cout << { 1 1 ;
11

for ( int i = ; i < v . size ( ) ; ++i ) {


cout << v [ i ] ;
11 11 .
if ( i ! =v . size ( ) - 1 ) cout << '

cout << 11 ) \n 11 ;

& , const
print ( ) .
, . .
, , ,
. . "
" , . print ( )
, .
void f ( int )
{
vector<doule> vdl ( l O ) ; //
vector<doule> vd.2 ( 1000000 ) ; //
vector<doule> vd (x) ; 11
11 . . . vdl , vd2 , vd . . .
print (vdl ) ;
print (vd.2 ) ;
print (vd ) ;

.
8.5. 325

v:

vd2

vd2:

:
. ,
,
print ( ) , - ,
.
void print ( const vector<doue>& v) //
{ //
11 . . .
v [ i ] = 7 ; // : v ( )
-

11 . . .

-
. my_find ( ) ( . 8 . 5 . 1 ),
.
.
int .y_find (vector<strinq> vs) ; 11 :
s trinc;r s ) ; 11

,
. my_
find ( ) , .
// : ,
int .y_find ( const vector<strinq>& vs , const strinq& s ) ;

8.5.5.
, ,
? . ,
init ( ) ,
.
void init (vector<doule>& v) //
{
for (int i ; i < v . size ( ) ; ++i ) v [ i ] = i ;

326 8 :

void g ( int )
{
vector<doule> vdl ( lO ) ; 11
vector<doule> vd2 ( 1000000 ) ; //
vector<doule> vd (x) ; //
init (vdl ) ;
ini t (vd2 ) ;
init (vd ) ;

. ini t ( ) .
r. . (..
). (..
). .
. - .
. . int& -

int.
:
int i = 7 ;

int& r = i ; // r i
-

r = 9; // i 9
i = 10 ;
cout << r << ' ' << i << 1 \n ' ; // : 1 0 1 0

. r
i .
.
:
vector<vector<doule> > v; // do ue

. v [f () ] [g () ]
. , v [f () ] [g () ]
, .
. :
doule val = v [ f (x) ] [q (y) ] ; // v [ f (x) } [g (y) }
val .
v [ f () ] [ g () ]

v [ f () ] [g () ] ? .
doule& var = v [ f (x) ] [ g (y) ] ; // v [ f (x) } [g (y) }

. v [ f (x) ] [g (y) ]
var:
var = var/2+sqrt ( var) ;
8.5. 327

- ""
.
. .
11
int f ( int& )

= x+l ;
return ;

int main ( )
{
int = ;
cout << f (xx) << ' \n ' ; 11 : 1
cout << << ' \n ' ; 11 : 1 ; f ()
11

int = 7 ;
cout << f {yy) << ' \n ' ; 11 : 8
cout << << ' \n ' ; 11 : 8 ; f {)
11


.
:

( )

:
7

8.5.3.
, -
:
, . , -
- .
. ,
doue:
void swap (doue& dl , doue& d.2 )
{
doue temp = dl ; 11 dl t emp
dl d.2 ; 11 d2 dl
d2 = temp ; 11 dl
11 d2
328 :

int main ( )
{
doue = 1 ;
doue = 2 ;
cout << " =" << << " == "

<< << ' \n ' ; 11 : x==l ==2


swap (x , y) ;
cout << " =" << << " == "

<< << \n ' ; / / : ==2 y==l

swap ( ) ,
,
swap ( ) .

8.5.6.

,
?
.
void f ( int , int& r, const int& cr)
{
++ ; //
++r ; // , r
++cr ; 11 : cr-

,
: ,

. :
void q ( int , int& r , const int& cr)
{
++ ; 11
++r ; 11 , r
int cr ; // , cr

int main ( )

int = ;
int = ;
int z ;
q (x , y , z ) ; 11 ==; y==l ; z==O
q ( l , 2 , 3 ) ; // : r :
q (l ,y , 3) ; 11 cr ,
8.5. 329

, ,
, . .
2 ( , rvalue , ..
-

), . .
r f ( ) lvalue (.. ,
).
, lvalue
. ,
. g ( l , y , 3)
int cr g ( ) ,
:
g ( l , y , 3 ) ; 11 : i n t _ compi ler_genera ted = 3;
11 g ( l , y, compi ler_genera ted)

. ,
(temporary object) .
,
. .
1 . .
.
2. . ,
. .
3. . , .
.
4. . -
.
, u
.
int, doue
. ,
, ,
.
, ,
. .
int incrl ( int ) ( return a+l ; 11
void incr2 ( int& ) ( ++ ; } 11 ,
11

int = 7 ;
= incrl (x) ; 11
incr2 (x) ; 11
330 :

-
? .

(, )
.
, ( ++
return
).
.
void larqer (vector<int>& vl , vector<int>& v2 )
// :>t vl
// vl v2;
// :>t v2
11

if (vl . size ( ) ! = v2 . si ze ( ) )
error ( " larqer ( ) : " ) ;
for ( int i=O ; i < vl . size ( ) ; ++i )
if (vl [ i ] < v2 [ i ] )
swap (vl [ i ] , v2 [ i ] ) ;

void f ( )
{
vector<int> vx ;
vector<int> vy ;
// vx vy
larqer ( vx , vy) ;
11 . . .

-
larger ( ) .
,
. ,
, .
,
, .
. Fortran - ,

, - .
, ,
Fortran, , .
.
8.5. 331

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

8.5. 7 .

, .
.
void f ( T ) ;
f (y) ;
= ; //
11 ( . 8 . 2 . 2)

f () , = ;
.
.
void f (doue ) ;

void q ( int )
(
f (y) ;
doule = ; //
11 (. 8 . 2 . 2)

,
int doue.
f ( ) . doue,
f ( ) , ,
.
,
( 3 . 9.2). ,
, . -
doule .
int, .
void ff ( int ) ;

void qq (doule )
332 :

ff () ; // , ?
int = ; 11 , ?

doue
int, .
void qqq ( doule )
{
int xl = ; 11
int 2 int (x) ;
int static_cast<int> (x) ; 11 (1 7 . 8)

ff (xl) ;
ff ( x2 ) ;
ff (x) ;

ff (x) ; //

ff ( int (x) ) ;
ff ( s tatic_cast<int> ( x) ) ; 11 (1 7 . 8)

,
.

8.5..
?
expression ( ) , term ( ) primary ( ) , 6 7,
:
,
. !
: ,
. ,
ts Token_s tream
: ts .
. ,
Token_stream&.
.
, expression ( ) , :
(ts) (left t).
doule expression ( Token_stream& ts)
{
doule left = term ( ts ) ;
Token t = ts . qet ( ) ;
11 . . .
8.5. 1 333

term ( ) expression ( ) , i
, (d) ,
( case ' / ' ) .
doule term ( Token_stream& ts)
{
doule left = primary ( ts ) ;
Token t = ts . get ( ) ;
11
case ' / ' :

doule d = primary ( ts ) ;
11 . . .

11

primary ( ) term ( ) ,
, left.
doule primary ( Token_stream& ts)
{
Token t =ts . get ( ) ;
switch ( t . kind) {
case ' ( ' :

doule d = expression ( ts ) ;
//
1
11

,
:
, . ,
,
, ,
, .

,
. ,
expression ( ) ,
.
334 :

expression(): ts
left
'

" " .
,

. 11 11
(functton acttvation record) .
. .

.
expression ( ) term ( ) .
term ( ) .

expression(): ts
left
t



term(): ts
left
t
d

, term ( )
d, .
,
. . (
)
. d
, case ' / ' .
term ( ) primary ( ) .
.
.5. 335

expressio(): ts
left
t

term(): ts
left
t

d

primary( ) : ts
t
d

, primary ()
expression ( ) .

expressio(): ts
left
t

term(): ts
left
t
d

primary(): ts
t
d

expression(): ts
left
t


336 8 :

expression ( )
,
expression ( ) . ,
, left t
. ,
( ) , 11
(recursive). ,
,
( ).
, , ,
(stack of actlvatlon records),
(stack) , . ,
, . ,
expression ( )
primary ( ) , .

expression(): ts
left
. . ..
t

term(): ts
left
t

d

primary(): ts
t
d

primary ( ) term ( ) ,
, .
8.5. 337

expression(): ts
left
t



term(): ts
left
t
d

. . (call
stack), ,
-

" - ".
,
++, , .
, ? , ;
,
" " " ",
, .

8.5. 9 . ns tr-
,
.
,
. ,
, , ,
.
,
constexpr. ,
,
:
constexpr doule l 10 ;
= //
constexpr doule l .;
=

constexpr Point scale ( Point p) { return { xscale*p . x , yscale*p . y ) ; ) ;

, Point
, . ,
scale ( ) Point,
, xscale yscale,
:
338 :

void user ( Point pl)


(
Point 2 ( 10 , 10 } ;

Point scale (pl) ; // : == ( 1 00, 8 ) ;


//
Point 4 = scale (p2 ) ; // 4 == ( 1 00 , 8 )

constexpr Point pS scale (pl ) ; 11 : sca l e (p l )


//
constexpr Point = scale (p2 ) ; 11 == ( 1 00 , 8 )
11 . . .

constexpr ,
- , ,
. , r
(, 2),
, (, pl).
, , constexpr.
, ( ,
) . ++ 1 1 , ,
constexpr, , return
( scale ( ) ): ++ 1 4
. ntr-
, . .
, ,
.
, :
int qob = 9 ;

constexpr void bad ( int& arq) / / :


( //
++arq ; // :
//
qlob = 7 ; // :

, nstr-
" " (
), .

8.6.

. " "
8.6. 339

, , ..
, .
. , .. ,
, ,
. .
strinq proqram_name " silly" ;
vector<strinq> v ; 11 v -
void f ( )
{
strinq s ; // s - f
while ( cin>>s && s ! ="quit" )
{
strinq stripped ; // s t ripped -
strinq not_letters ;
for ( int i=O ; i < s . s i ze ( ) ; ++i) // i
11
if ( isalpha ( s [ i ] ) )
stripped += s [ i ] ;
else
not letters += s [i ] ;
v . push_back ( stripped) ;
11
1
11

, progra.m_na.me v,
main ( ) . ,
, .
(.. progra.m_na.me
v) , (..
v progra.m_na.me).
- f ( ) .

s; . s
. , f ( )
. , while,
stripped not_letters.
stripped not_letters,
stripped. .
( , not_
letters stripped) ,
. ,
qui t, , stripped
not_letters .
340 :

, for, i.
, for, i
, v . push_back ( s tripped) ; .
, ( )
(
), ,
. ,
, .

8.6. 1 .
,
.
. , -
. :
,
. .
v [ i ] = ++i ; 11
v [++i ] = i ; 11
int = ++i + ++i ; 11
1 1
cout << ++i
<< i ' \n ' ; //
f ( ++i , ++i) ; 11

,
: , ,
,

. - :
.
, = ()
. ,
,
. v [ ++i ] = i
.

8.6.2.
( :
8 . 7)
, . .
// fl .
int xl 1;
int yl = 1+2 ; // yl 3
8.6. 341


main ( ) .
,
, . ,
, ,
( 8.4).
,
. .
11 f2 . cpp
extern int yl ;
int 2 1+2 ; // 2 2 5

:
, ,
.
fl .
f2 , 2 5 (
.

). ,
f2 fl ,
. .

2 2 ( ,
,
).
;
, ,
.
.
( ) ?
Date,
. -:
const Date default_date ( 1970 , 1 , 1 ) ; // - 01 . 01 . 1 9 70

, default_date
? ,
.
, .
.
const Date default_date ( ) //
1
return Date ( 1970 , 1 , 1 ) ;

Date ,
defaul t_date ( ) . ,
default_date ( ) ,
342 :

Date .
.
:
const Date& default_date ( )
{
static const Date dd ( 1970 , 1 , l ) ; // dd
!/
return dd ;

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

8. 7 .
(
8.4). .
( 9).
.
,
.
, .
, .
r
. .
,
(namespace). .
Color, Shape. Line, Function Text ( 1 3) .
namespace Graph_li
struct Color /* */ } ;
s truct Shape /* */ 1 ;
struct Line : Shape /* */ } ;
struct Function : Shape /* */ } ;
struct Text : Shape /* */ } ;
11 ...
in t qui_ain ( ) /* */
. 7 . 343

, - ,
. - Text,
- , .
Graph_lib : : Text, - Text.
,
Graph_lib, Text. Graph_
lib ; . "
" Graphics - .
, Text
. .
Graph_lib, .
, , -
TextLib.
namespace TextLib
class Text /* */ } ;
class Glyph /* */ } ;
class Line /* */ } ;
11 . . .

,
.
Text Line. ,
, .
.
; . Text -
Graph_lib : : Text, TextLib : : Text. ,
-

( )
, : : , . . (fully
qualified name).

8. 7 . 1 . us inq us inq
.
, ++
s td :
#include<strinq> // s t ri n g
#include<iostream> // ios t ream

int main ( )
{
std : : strinq name ;
std : : cout << " . , \n" ;
std : : cin >> name ;
344 :

std: : cout << " 8'1' , " << name << ' \n ' ;

string
cout,
- std : : string std : : cout. :
, s tring
std : : string, cout - s td : : cout ..
using std : : string ; // s t ri n g s td : : s t ri n g
using std : : cout ; // co u t s t d : : co u t
11 . . .

using.
"", . ,
.
""
: "
, std".
, using.
using namespace s td ; //
11 s t d

.
#include<string> 11 s t ri n g
#include<iostrea> 1 1 ios t ream
using namespace s td ; 11 s td

int main ( )

s tring name ;
cout << " , \n " ;
cin >> name ;
cout << " , " << name << ' \n ' ;

in - std : : cin, string - o std : : string


.. std_lib_facilities . h,

std.
using
, ,
,
std. ,
using, , ,
,
.
8. 7 . 345

using ,
using (
) - . .
using std
std_lib_facilities . h. .
:
llinclude " s td lib facilities . h"
int ain ( )
{
strinq name ;
cout << ". , \n" ;
cin >> name ;
cout << " , << name << ' \n ' ;
"

,
std.

1 . : my . h, my . use . . my . h
:
extern int foo ;
void print_foo ( ) ;
void print ( int) ;

my . cpp llinelude my . h
std_l ib_facilities . h print_foo ( ) (
foo eout) print ( int i) (
cout i ) .
use . cpp #inelude
my . h, main ( ) .
foo 7
print_foo ( ) . 99
print ( ) . , use . cpp
#inelude std_lib_faeilities . h,
.
.
Windows. .
use . my . use .
{ ehar ; ein >> ; } . : ein
#inelude <iostream>.
2. : swap_v ( in t , int) . swap_r ( i nt& , int&) swap_
er (const int& , eonst int&) .
346 :

{ int t ; t = , = ; =t ;

- .
, .
int = 7 ;
int 9 ;
swap_? (x , y) ; // v, r cr
swap_? ( 7 , 9 ) ;
const int = 7 ;
const int = 9 ;
swap ? ( cx , cy) ;
_
8W&p_? ( 7 . 7 , 9 . 9 ) ;
doule dx 7.7;
doule dy 9 . 9 ;
swap_? (dx , dy) ;
swap_? ( 7 . 7 , 9 . 9 ) ;

?

, ,
. , 8.6.
. , ,
. z, . main ( ) ,
, .
int main ( )
{
: : var = 7 ;
: : print ( ) ; // va r
using namespace ;
var " 9 ;
print ( ) ; // va r
{
using Z : : var ;
using Z : : print ;
var 11;
print ( ) ; / / va r Z

print ( ) ; // va r
: : print ( ) ; // va r


var print ( ) .
var cout.
8. 7 . 347


1 . , ?
2.
?
3.
?
4. 6 ,
?
5. int ; -
?
6. -
?
7. ?
8. ?
9. ?
1 . ?
1 l . .
.
1 2. ,
?
1 3.
?
1 4. ,
?
1 5. , m
?
1 6. swap ( ) ?
1 7. , vector<doue>,
?
1 8.
. ?
1 9. && 1 1 ?
20. ++ :
, ,
, ?
2 1 . ?
22. ?
348 :

23. ?
24. ?
25. using.
26. using ?
27. s td.

const
constexpr
extern
namespace
return



-

-

usinq usinq

1 . - 7,
( 8. 5.8). ,
Token_stream ( 7.8.2)
istream& . , ,
(, ),
. 11:
i s tream.
2. print ( ) . cout
. : "
vector.
3.
. 2. ,
fibonacci ( , , v , n) ,
int, v vector<int>,
n - , v:
v [ O ] , v [ l ] - . -
,
. 7 . 349

. .
1 2. 1 . 2, 3, 5, 8, 1 3, 2 1 , . "

. fibonacci ( )
. .
4. int ,
.
fibonacci ( ) .
5. ,
vector<int>. , 1 , 3, 5, 7, 9
9, 7, 5, 3, 1 . ,
,
vector, vector
.

. 11: swap.
6. 5 vector
<string>.
7. vector<string> name,

vector<doue> age.
(name [ i ] , age [ i ] ) . ( sort (name . egin ( ) , name . end ( ) ) )
(name [ i ] , age [ i ] ) .
, age,

name. 11: name
age
name. ,
.
8. , vector
<doue>. price weight, .
price [ i] *weigh t [ i ] . weigh t .
size ( ) price . size ( ) .
=

9. maxv ( ) ,
, .
1 . ,
, ,
. .
struct,
.
?
350 :

1 1 . print_until_s ( ) 8.5.2.
.
? . print_until_
ss ( ) , ,
qui t.
1 2 . , vector<string>
vector<int>.
. .
.
.
? ?
1 3 . ,
(, void f ( const int) ; )? ?
?
? ; .
. .

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

. ,
, . ""
.
:

r : la -
- r (?...! .//-.;_)


: ++ .
,

, - .

Date.

.
352 9 :

9. 1 . , 9.5 .
9.2. 9.5. 1 . ""
9.3. 9.6.
9.4. 9.7.
9.4 . 1 . 9.7. 1 .
9.4.2. - - 9.7.2.
9.7.3.
9.4.3. 9.7.4. -
9.4.4. - 9.7.5.
9.4.5.
9.4.6. 9.8. Date

9 1 ,
. .

++ , char, int doule


( .8).
, ,
(, + *)
, .
, ,
, (user
defined types UDT). -
-

, ++,
ISO (, string, vector ostream,
1 0), , ,
Token Token_s tream (. 6 . 5 6.6).
, ,
Shape, Line Text ( 1 3) .
, ,
,

, , :
, .
, , ,
.
, vector [ ] size ( ) ( 4.6. 1 .4.8),
ostream <<. Token_stream
- get ( ) (
-

6.8), Shape add ( Point) set_color ( ) ( 14.2).


-

? ,
.
, -
9. 1 . , 353

-
. .
? ? , .
. ,

, , ,
.
, int;
, s tring;
,
Token Token_stream. .
r. ,
.
. . .
: ""
(
) , .
, -. -, ,
, ;
.
.
- " " .
, "" ""
. " ?"
.
++.
++
: .
, -
, , .
. (class) -
() .
, , ( 1 7.5).
- . . ,
, "" .
, , , ,
. , ,
, , , , , .
++ ( )
,
.
(. 6 7).
354 9 :

9 2
. .

- .
, . ,
,
(members) . ,
. .
class {
puic :
int m ; // -
i n t m.f (int v) { 11 -
int old = m; m.=v ; return old ;

};

.
-, .
-,
.
. , :
var ; / / va r
-
var . m = 7 ; // va r
int = var . m.f ( 9 ) ; /! - m f () va r

, .
, int,
..
-, mf ( ) ,
var . m. (
- m) . -
, - . ,
var . mf ( 9) m mf ( ) var . m.

9
. .

, . -
,
. - , -
. .
puic : . -
priva te : . :
class { 11
pulic :
11 :
9 3
. .
355

// - ( )
11
11
11 ( priva te)
private :
// :
// - (
11 )
11
11
11
1;

. ,

class {
int mf { int) ;
11 . . .
1;

class {
private :
int mf ( int) ;
11 . . .
1;

; //
int = x . mf ( ) ; // : mf
11 (. . )


. -
, , :
class {
int m;
int mf ( int} ;
pulic :
int f ( int i ) m=i ; return mf ( i ) ; }
1;

;
int = x . f ( 2 ) ;


( )
( ).

356 9 :

. . ,
, .
,
. struct
class, :
struct {
int m;
11 . . .
};

class {
pulic :
int m;
11 . . .
};

.
; ,
( 9.4.3).

9 .4.
,
. , - -

. :
(, . 1 9 1 963 )
. (
, , . .
..). . .

9 4 1
. . .

?
: , ".
, .
.
11 Da te ( ?)
struct Date {
int ; //
int m; //
int d ; //
};

Date today ; // Da te ( )
9 .4 . 357

Date, today,
int.
Date:

42
;:
d:
,
Date, -
.
, Date; ?
, ,
today ( Date),
. i ,
. , Date,
. .
11 2 0 2004
today . y 2004 ;
=

today . 111 20 ;

today . d " 2 ;

.
? , ,
! , ,
; ?
Date ;
. -3 ;
=

. 111 13 ;
=

x . d = 32 ;

, , .
? ?
Date ;
. = 2000 ;
. 111 " 2;
y.d 29;

? ?
, ,
.
,
.

. Date
Date. , ; :
358 9 :

11 :
void init_d&y (Date& dd , int , int m, int d)
{
11 , (y , m , d) ,
11 , , dd

void add_d&y (Date& dd , int n)

11 dd

Date.
void f ( )
{
Date tod&y ;
init_d&y ( tod&y , 12 , 2 4 , 2005 ) ; 11 ! ( 1 2 - Ui
11 2 0 0 5 - . )
add_d&y ( tod&y , l ) ;

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

9 .4.2. -
Date,
.
, . , ,
Date << ( 9 . 8):
void f ( )
{
Date tod&y ;
11 . . .
cout << today << ' \n ' ; // today
11 . . .

init_d&y ( tod&y , 2008 , 3 , 30 ) ;


11 . . .
Date tomorrow;
9 . 4 . 359

tomorrow . y today . y ;
=

tomorrow . m today . m ;
=

tomorrow . d today . d+l ;


= 11 today
cout << tomorrow << ' \n ' ; 11 tomorrow

"" today.
ini t_day ( ) . ,
"-" , add day ( ) -
_

( ), tomorrow
. - . .
. . .
. .
Date
"", d
: today
.
. . .

. . .
.
u-'-!" . . ,
, :
11 Da t e ,
//
//
struct Date {
int , m, d ; // , ,
Date ( int , int m, int d) ; 11
11
void add_day (int n ) ; 11
};

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

.
Date y_irthday ; 11 :
Date today ( 12 , 2 4 , 2007 } ; 11
Date las t ( 2000 , 12 , 3 1 } ; 11 ( )
Date next = { 2014 , 2 , 14 } ; // ( )
Date christmas =

Date ( 1976 , 12 , 24 } ; 11 ( )
360 9 :

my_irthday ,
. today
,
( { 12 , 24 , 2007 } 2007- -

24- 1 2- ).
last
- ,
Da te, { }
.

, ,
.
,
( Da te { 1 97 6 , 12 , 2 4 } ) ,
=.
, .
""
.
last . add_day ( l) ;
add_day ( 2 ) ; 11 : Da t e ?

, - add day ( )
Date (
). - ,
9.4.4.
++98
, :
Date last ( 2000 , 12 , 31 ) ; // ( )


{ } , ,
() ,
.
, :
int ( 7 } ; // Ok ( )

{ } =:
Date next = { 20 14 , 2 , 14 } ; // ( )

9. 4 . .
: , -
- add_day ( ) ? . -
9 .4 . 361

? , ""
.
Date birthday { l960 , 12 , 31 } ; // 31 1 9 60
++irthday . d ; / / ' !
// bi rthda y . d == 32

Date today { l970 , 2 , 3 } ;


today . m = 14 ; // ' !
11 toda y . m == 14

, Date , - -
- ; .
, . -
Date ,
.
; - -. ,

- - -
.
, Date,
-,
. , :
11 ' ' Da t e ( )
class Date
int , m, d ; // , ,
pulic :
Date ( int , int m, int dl ; 11
void add_day ( int n ) ; // Da te
int month ( ) { return m;
int day ( ) { return d ;
int year ( } { return ;
1;

:
Date irthday { l 970 , 12 , 30 } ; 11
irthday . m = 14 ; // : Da t e : : m
cout << irthday . month ( ) << endl ; //

" Date" -
.
. ;
, , ,
. -
.
. (state),
- .
362 9 :


.
. ,
.
, ,
.
. .
.
, ,
(invariant). Date (W
Da te .
") :
, ,
. .
Date - . .
, ,
. .
. , ,
. struct.

9 -
. . .

Date
.
-. ,
Date,
.
11 Da t e ( )
class Date {
pulic :
Date ( int , int m , int d) ; //
void add_day ( int n) ; /! Da t e
int month ( ) ;
11 . . .
private :
int , m, d; 11 , ,
};

.
. ,
. .
, ,
, -.
, ,
.
9 4
. .
363

,
; .
.
, ,
. u_ :
: .
Date : : Date ( int , int mm., int dd) //
: { ) , m{mm) , d { dd ) / /

void Date : : add_day ( int n)


{
11 . . .

int onth ( ) // : Da t e : :

return m; // - ,

: { } , m { mm } , d{dd}
().
:
Date : : Date ( int , int mm., int dd) //
{
= ;
m = mm ;
d = dd ;


, ,
. ,
.
: { } , m { mm } , d{dd } .
.
int ; // . . .

11 . . .
= 2 ; // . . .


int { 2 } ; //

- .
11 Da t e (
11 )
364 9 :

class Date {
pulic :
Date ( int , int m, int dd)
: { } , m{ m} , d { dd }

11 . . .

void add_day ( int n)


{
11 . . .

int month ( ) { return m; }

11 ...
private :
int , m, d; // , ,
};

,
. add_day ( )
.
.

.
month ( ) .

, Date : : month ( ) ,
.
.
, month ( )
m, , m
() month ( ) .
, . ,
,
, .
-
.
m11 (inline), . .
.

, month ( ) .
-
, .
,
.
9 4
. .
365



.
. ,

-.
: -
,

. , , ,
.
, - .

9. , . 5 .
Date.
class Date {
11 . . .
int month { ) { return m ; }
11 . . .
private :
int , m, d; // , ,
};

void f (Date dl , Date d.2 )


{
cout << dl . month ( ) << ' ' << d2 . month ( ) << ' \n ' ;

Date : : month ( ) ,
dl . m, d2 . m?
-

Date : : month ( ) : !
Da te : : mon th ( ) "", ?
- , Date : : month ( ) ,
, ,
. , m
dl m, - d2 . m.
.

1 7. 1 0.

9 .,.6.
?
?
5.6 , "-
", -
366 9 :

Date.
Date - .
Date .
Date.
11 Da t e ( )
class Date {
pulic :
class Invalid }; //
Date ( int , int m, int d) ; 11
11 . . .
private :
int , m, d; // , ,
bool is_valid ( ) ; 11 , t rue
};

is_
valid ( ) .
, ,
. , ,
:
Date : : Date ( int , int llllD. , int dd)
: { } , m{ llllD. } , d { dd } // -

if ( ! is_valid ( ) ) throw Invalid { } ; //

bool Date : : i s_valid ( ) // true


{
if (m<l 1 1 12<m) return false ;
11

Da te :
void f ( int , int )
try {
Date dxy { 2009 , x , y } ;
cout << dxy << ' \n ' ; 11 << . 9 . 8
dxy . add_day ( 2 ) ;

catch (Date : : Invalid)


rrr ( " " ) ; // error ()
// 5 . 6 . 3

, << add day ( )


Date.
Dt 9.7
, ,
: .
9.5. 367

9 . 5.
enum (enumeration)
-

, (
) :
enum class Month {
j an=l , f , r , apr , , jun , jul , auq , sep, oct , nov , dec
};

''" .
class enum class ,
. . . , j an,
n th : : j an.
,
jan,
. ,
,
. ,
Month ,
. ,
enum class Month {
j an=l , f=2 , ma.r= , apr=4 , ma.y=S , jun= ,
jul=7 , auq=B , sep=9 , oct=lO , nov=l l , dec=12
};

.
, , ,
; ,
, "" .
, . .
,
:
enum class Day {
monday , tuesday , wednesday ,
thursday , friday , saturday , sunday
};

monday=O sunday== .
.
Month :
Month m = Month : : fe ;

Month m2 = f ; 11 : feb
. = 7 ; 11 :
11 i n t Mon th
368 9 :

int n = m; // :
11 Mon th i n t
onth n = onth ( 7 ) ; 11 ()
11 i n t Mon th

onth , ""
int. onth
, int
onth. ,

onth bad = 9999 ; 11 :


11 Mon th

onth ( 9999) ,
. ++

, ; ,
! ,
onth { 9999 } , ,
Month; int
.
, .
,
.
onth int_to_month ( int )
{
if ( < int (Month : : j an) 1 1 int (onth : : dec) < )
rrr ( " " ) ;
return Month (x) ;

int (Month : : jan)


onth : : j an. , :
void f ( int m)
(
onth n = int_to_month (m) ;
11 . . .

? ,

. ,
aop aepa (up, down; yes , no, maybe; on, off; n, ne, e, se, s , sw, w, nw)
(red, u. green, yellow, maroon, crimson,
k).
9 .5. 369

9 . 5.1 . " "


enum class,
(scoped enumeratlon), "
, ,
"
int, :
enum onth { // "cl a s s "
j an=l , f , .r , apr , . , j un , jul , aug , sep , oct , nov , dec
};

onth m = f ; // : feb
Month m2 = Month : : fe ; // :
m = 7; // :
11 i n t Mon th
int n = . ; // : t h
11 i n t
Month IDID. = onth ( 7 ) ; // i n t
11 Mon th ()

, " ,
enwn class . " ,
. ,
. ,
Month iostream ( 1 1 .2. 1 ),
, , dec
, .
int
(
int) . , :
void my_code (onth .)
{
if (. == 1 7 ) do_som.ething ( ) ; // 1 7 - ?
if (. monday) do_som.ething_else ( ) ; //
==

11 ?

onth enwn class,


. monday " enwn,
enwn class, , ,
, .
enum
class , ""
enwn: enwn class ++ 1 1 .
370 9 :

9 6
. .


, ++.
(operator overloadlg). ,
.
.
enu.m class Month
Jan=l , F , &r , Apr , & , Jun , Jul , Auq , Sep , Oct , Nov , Dec
1;

Month operator++ (onth& m) 11

m = (m.==Dec) ? Jan : Month ( int (m) +l ) ; //


return m;

? : ":
m Jan, (m--Dec) , Month (int (m) + 1 )
.
. Month :
Month m = Sep ;
++m; // Oc t
++m; // Nov
++m; // Dec
++m; // Jan ( )

,
Month ,
.
? :
vector<strinq> month_t ;

ostream& operator<< (ostream& os , Month m)


(
return os << month_tl [ int (m) ] ;

, month_t
, , , month_t [r]
"arch" - (.
1 0. 1 1 .3).
,
, ++,
+, -, *, /, %, [ ] , ( ) , ", ! , &, <. <=, > >=. -
; , , -
9. 7 . 371

** $=, ++ .

; , -.
<= (" ").
+,
! ("").
,
.

.
int operator+ ( int , int) ; 11 :
11 +
Vector operator+ ( const Vector& , const Vector &) ; 11
Vector operator+= ( const Vector& , int) ; 11


, ,
. , ,
: + ;
* - ; [ ] - ; ( ) -
.. , , :
, +
, . ,

. ,

. .
.
,
+, -, * , / ,
, =, ==, ! =, <, [ ] ( ) .

9 . 7 .
,
. ++
s truct,
.
?
?
, ,
++ .
372 9 :

.
.
.
( )
(. 1 4.2.4).
.
-
(. 9. 7.4).
(. 1 7. 5).
. 5.5, ,
.
: "
. ".
, . ,
,
. ,
, , - , .
.
, . ,
, ! .
, .
. , ,
.

, ++.

9 . 7 1
. .
Date 9.4.3,
int. .
Date dl { 4 , 5 , 2005 } ; // 4 , 2005
Date d2 { 2005 , 4 , 5 } ; // 5 4 ?

( )
. (
) .
. - ,
: , 4 / 5 ClllA 5 . - 4 .
,
- .
Month.
9 . 7 . 1 373

enum class onth {


j an=l , f , mar , apr , may , jun , jul , aug , sep , oct , nov , dec
};

// Da t e ( Mon th)
class Date
pulic :
Date ( int , onth m, int d) ; /!
11
11 . . .
private :
int ; 11
Month m ;
int d ; //
};

Month .
. , Month
. , ,
, , .
.
Date dxl { l998 , 4 , 3 } ; 11 : 2 -
11 Mon th
Date dx2 { 1998 , 4 ,Month : : r } ; 11 : 2 -
11 Mon th
Date dx2 { 4 ,Month : : r , 1998 } ; 11 :
11 1 998
Date dx2 {onth : : mar , 4 , 1998 } ; 11 : 2 -
11 Mon th
Date dx3 { 1998 , Month : : mar , 30 } ; 11

.
mar : Month : : mar.
Month . mar. Month (Month - ),
mar - ( -
). : : ,
( 8. 7). . () -
.
, ,
. ,
. ,
. ,
.

? ,
, Month; , ,
.
374 9 :

,
.
, , (
Date). - :
class Year // [mi n , max)
static const int in = 1800 ;
static const int = 2200 ;
pulic :
class Invalid { ) ;
Year ( int ) : { ) { if ( x<in 1 1 max<=x) throw Invalid { ) ; )
int year ( ) { return ; )
private :
int ;
);

class Date {
pulic :
Date ( Year , Month m, int d) ; 11
11
11 . . .
private :
Year ;
Month m ;
int d ; / /
);

.
Date dxl { Year { l 998 ) , 4 , 3 ) ; 11 :
11 - Mon th
Date dx2 { Year { 1 998 ) , 4 , onth : : mar ) ; 11 :
11 - Mon th
Date dx2 { 4 , Month : : mar , Year { l998 ) ) ; 11 :
11 - Yea r
Date dx2 {Month : : mar , 4 , Year { l 998 ) ) ; 11 : - Mon th
Date d { Year { l 998 ) , Month : : mar , 3 0 ) ; 11


.
Date dx2 { Year { 4 ) , Month : : mar , 1998 ) ; // :
11 Yea r : : Inva l i d


? , ,
Date.
Year.
, .
.
,
9 . 7 . 375

, .
, ,
, . , " -
".
static const
min max.
. -
static ,
,
. ,
, constexpr
const.

9. 7 . 2.
, -
. ,
: , ,
(..
).
.
,
?
Date onth :
, :
. .
, . , Date

, .
Date holiday { 1 978 , Month : : jul , 4 } ; //
Date d2 = holiday ;
Date d = Date { 1 978 , Month : : jul , 4 } ;
holiday = Date { 1 978 , onth : : dec , 2 4 } ; //
d = holiday ;

, . Date { 1978 , Month : : dec , 24 }


Date,
, :
cout << Date { 1978 , Month : : dec , 24 } ;


. ,
, .
376 9 :

?
(. 1 8.3).
(.
1 4.2.4).

9.7. .

. . ++
, ,
. , Date :
: Date ( int , Month , int) , ,
Date . .

. .
Date dO ; 11 :
Date dl { } ; 11 :
Date d2 ( 1998 } ; 11 :
Date d ( l , 2 , 3 , 4 } ; 11 :
Date d4 ( 1 , 1 1 jan 11 , 2 } ; 11 :
Date dS ( l , Month : : j an , 2 } ; 11 :
11
Date d ( dS } ; 11 :
11 JJI1

, ,
Date, - Date.
;
.
, ?"
.
strinq sl ; 11 : " "
vector<strinq> vl ; // :


. , vector string
,
.
{ }
. , ,
:
strinq sl = strinq ( } ; // :
1 1
""
vector<strinq> v l = vector<strinq> ( } ; // :
//
9. 7 . 377

.
string sl ; // : " "
vector<string> vl ; // :

, int doue,
. int{ } -
, doue { } - . .
, , -
. ,
string vector.
string ;
for ( int i=O ; i<s . size ( ) ; ++i) // :
11 !
s [ i ] = toupper ( s [ i ] ) ; 11 :
// !
vector<string> v ;
v . push_back ( "bad" ) ; // :
// !

s v ,
, (
: . 1 7.5) , .
. ,
! ,
. ,
. (. 9.4.3) .
, . .
:
string sl = " " ;
vector<string> vl { } ;

.
string 11 11 .
vector . { }
.
. u
, .

.
' .
Date
, ( ,
).

1 " . - .. .
378 9 :

class Date {
pulic :
11 . . .
Date ( ) ; 11
11 . . .
private :
int ;
onth . ;
int d ;
};

r .
XXI .
Date : : Date ( )
: { 2001 } , .{Month : : j an } , d { l }

.
:
class Date
pulic :
11 . . .
Date ( ) ; //
Date (year , Mon th , day) ;
Date ( int ) ; // 1
11 . . .
private :
int { 2001 } ;
Month . {Month : : jan} ;
int d {1} ;
);


:
Date : : Date ( int ) 11 1
: { }
{
if ( ! is_valid ( ) ) throw Invalid { ) ; //

Date ( int) (m) (d),


(Mon th : : j an 1).
, ,
uuuu (in-class initializer).
, ,
, r ( ).

9 . 7 . 379

, ,
8.6.2.
const Date& default_date ( )
{
static Date dd{ 2001 , Month : : j an , 1 ) ;
return dd ;

static , dd
,
defaul t_date ( ) .
defaul t_date ( ) . default_
date ( ) Date.
Date : : Date ( )
: y { default_date ( ) . year ( ) ) ,
m{ default_date ( ) . month ( ) } ,
d{ default_date ( ) . day ( ) }

,
, ; ,
. default_date ( ) .
Date,
Date :
vector<Date> birthdays ( lO) ;


.
vector<Date> birthdays ( lO , default_date ( ) ) ; 11
11 Da t e
vector<Date> birthdays2 = { 11
11 Da t e
default_date ( ) , default_date ( ) , default_date ( ) ,
default_date ( ) , default_date ( ) , default_date ( ) ,
default_date ( ) , default_date ( ) , default_date ( ) ,
default_date ( )
};

,
. vector<int>
( 1 8.2).

9 . 7 . 4. -
,
(, "" ), - ; ,
380 9 :

, .
,
const, :
void some_function (Date& d , const Date& s tart_of_term)

int = d . day ( ) ; 11
int = start_of_term. day ( ) ; // ( ?)
d . add_day ( ) ; 11
s tart_of_term. add_day ( ) ; 11

, d - ,
s tart_of_term - ; , some_function ( )
s tart_of_term.
? , ,
s tart_of_term (const) .
day ( ) day start_of_term?
, Date s tart_of_term .
day ( ) , .
day ( ) Da te.
, , day ( )
Date, .
,
.
, -
: , .
. .
clas s Date
pul i c :
11 . . .
int day ( ) const ; // :
11
Month DLOnth ( ) con s t ; // :
11
int year ( ) con s t ; 11 :
11

void add_day ( int n ) ; // :


11
void add_DLOnth ( int n ) ; // :
//
void add_j'ear ( int n ) ; // :
//
private :
int ; //
Month . ; 11
int d ; / /
9. 7 . 381

1;

Date d ( 2000 , onth : : jan , 2 0 } ;


const Date cd ( 2001 , onth : : fe , 2 1 } ;

cout << d . day ( } << " - "


<< cd . day ( ) << ' \n ' ; 11
d . add_day ( l ) ; 11
cd . add_day ( l ) ; // : cd -

const -
, -
. - ,
:
int Date : : day ( } const
(
++d; // :
11 -
return d ;

, .
,
.

9 . 7 . 5.
( ) ,
. ,
,
(.. -),
. ,
.
, "
" ; ,
- , ,
: ,
. ,
, .
Date! , ,
. :

next_Sunday ( ) , next_workday ( ) .. -
,
, .
.
382 9 :

. ,
. .
.
Date, ,
. 1 1 900 ,
(. . ).
-.
(helper functions).
Date next_Sunday ( const Date& d)
{
// d , d . day () ,
// d . mon th () d . yea r () ;
// Da te

Date next_weekday ( const Date& d) /* . . . */ 1

bool leapyear ( int ) { /* . . . */

bool operator == ( const Date& , const Date& )


{
return a . year ( ) = b . year ( )
& & . 1110nth ( )== . 1110nth ( )
& & a . day ( ) == b . day ( ) ;

bool operator ! = ( const Date& , const Date& )


{
return ! ( ;

-11,
11 .
, ,
: .
,
.
. (
:
leapyear ( ) ) .
(. 8. 7):
namespace Chrono {
enum class onth { / * . . . */ } ;
class Date { / * . " */ 1 ;
bool is_date ( int , Month m, int d) ; 11 t rue
11
Date next_Sunday ( const Date& d) { / * . . . */ }
9.8. Date 383

Date next_weekday ( cont Date& d) { /* . . . */ )


bool leapyear ( int ) { / * . . . */ } // . . 1 0
bool operator== ( cont Date& , cont Date& ) {
/* . . . */ }
11 . . .

== ! = .
. ! = ,
=

, ,
,
.
, i s_date ( ) ,
Da te : : check ( ) ,
Date. ,
, Date ,
, "30 2008 " , "30
2008 " - . , ,
(, "30 1 066 "),
( ) Date.

9 .8 . Date
, ,
Date. ,
. . . , (,
).
Chrono . h.
1 1 Chrono . h

namepace Chrono
enum clas Month
j an=l , f , mar , apr , may , jun ,
jul , aug , sep , oct, nov , dec
};

class Date {
pulic :
class Invalid { } ; //
Date (int , Month m, int d) ; //
11
Date ( ) ; 11
//

1 1 :
int day ( ) const { return d; }
Month month ( ) const { return m ;
384 9 :

int year ( ) const ( return ;

// :
void add day ( int n) ;
_
void add_month ( int n) ;
void add_year ( int n) ;
private :
int ;
onth m ;
int d ;
};

bool is_date ( int , Honth m, int d) ; // true


bool leapyear ( int ) ; // t r ue
bool operator- -= ( const Date& , const Date& ;
bool operator ! = ( cons t Date& , const Date& ) ;
ostream& operator<< ( os tream& os , const Date& d) ;
istream& operator>> ( istream& i s , Date& dd) ;
} // Ch rono

Chrono . .
11 Chrono . cpp
ftinclude " Chrono . h"
namespace Chrono (
// - :
Date : : Date ( int , Honth mm, int dd)
: ( } , m{mm} , d ( dd }

i f ( ! i s_date (yy , mm , dd) ) throw Invalid( } ;

const Date& default_date ( )

static Date dd ( 2001 , Honth : : j an , 1 } ; // XXI


return dd;

Date : : Date ( )
: y ( default_date ( ) . year ( ) } ,
m( defaul t_date ( ) . month ( ) } ,
d { default_date ( ) . day ( ) }

void Date : : add_day ( int n)

11 . . .

void Date : : add_month ( int n)


(
9.8. Date 385

11 . . .

void Date : : add_year ( int n)


(
if (==onth : : fe && d2 9 &&
! leapyear (y+n) ) ( //
m = n th : : mar ; // 2 9 1
d = 1;

y+=n ;

11 :
bool is_date ( int , onth m, int d)
(
11
if ( d<=O) return fals e ; // d
if (m<onth : : j an 1 1 onth : : dec<m) return fal se ;

int days_in_month 31 ; // 31
switch (m) {
case Month : : fe : //
days_in_month = ( leapyear (y) ) ?2 9 : 28 ;
break ;
case Month : : apr : case onth : : jun :
case onth : : sep : case onth : : nov :
days_in_month = 30 ; 11 30
break ;

if (da.ys_in_month<d) return false ;


return true ;

bool leapyear ( int )

// . . 10

bool operator== ( const Date& , const Date&


{
return . year ( ) b . year ( )
&& . mon th ( ) b . month ( )
& & a . day ( ) . da.y ( ) ;
--

bool operator ! = ( const Date& , const Date&


{
return ! ( =) ;
386 9 :

ostream& operator<< (ostream& os , const Date& d)

return os << ' ( ' << d. year ( )


<< , ' << int ( d . month ( ) )

<< ' , ' << d . day ( ) << ' ) ' ;

istream& operator>> ( istrea.m& i s , Date& dd)

int , m, d ;
char chl , ch2 , h , ch4 ;
is >> chl >> >> ch2 >> m >> h >> d >> ch4 ;
if ( ! i s ) return is ;
if ( chl ! = ' ( ' 1 1 ch2 ! = ' , ' 1 1
h ! = ' , ' 1 1 ch4 ! = ' ) ' ) { / /
is . clear ( ios_base : : failit) ; //
return is ;

dd = Date (y , Month (m) , d) ; 11 dd


return is ;

enum class Day {


sunday , monday , tuesday , wednesday ,
thursday , friday , saturday
};

Day day_of_week ( const Date& d)


{
11 . . .

Date next_Sunday ( const Date& d)

11 . . .

Date next_weekday ( const Date& d)

11 . . .

11 Ch rono

, >> << Date,


1 0.8 1 0.9.
9 Date
. . 387



Da te,
. Date
today, 1 9 1 963 .
Date tomorrow ,
today
add_day ( ) . today tomorrow,
<<. 9.8.
.
. ,
[ l , l 2J, -
[ 1 .3 1 J .
, (2009. 1 3 ,-5).
1 . 9.4. 1 .
2. 9.4.2.
3. 9.4.3.
4. 9 . 7 . 1 .
5. 9.7.4.


1 . ?
2. ?
3. , Date,
?
4. Date . init_
day ( ) ?
5. ? .
6. ,
? ?
7. ?
. (
).
8. ?
9. , -
const?
1 .
?
388 9 :

11
class
const
enwn
struct


1 . .
9. 1 (, ).
2. Name_pairs, (.
), - string, -
doue.
vector<string> ( name) vector<doue> ( age).
read names ( ) ,
_

. read_ages ( ) ,
.
print ( ) , (name [ i ] ,age [ i J ) (
) . name.
sort ( ) name

age.
"" -. (
. ).
. Name_pair : : print ( ) ()
ra tor<< ! = Name_pair.
=

4. "" 8.4.
.
, ;
" ".
5.
Book.
. Book
ISBN. ,
. ,
, . .
. , ,
. .
Book; . ISBN
9 .8. Date 389

n-n-n-x, n - ; - . ISBN
string.
6. Book. == ,
ISBN . ! =
ISBN. << ,
ISBN .
7. Book Genre.
. . .
.
Genre
Book -.
8. Patron .
, .
. ,
. , .
,
(bool ) , , .
9. Library. k
tron. Transaction
Book, Patron Date .
Transaction. .
.
( ) . .
.
, .
, . ,
.
, . ,
Transaction
Transaction. . ,
, .
1 0. leapyear ( ) 9.8.
1 1 .
Date, . next workday ( ) (
. . ,
) week_of_year ( ) ( .
1 . - ) .
1 2 . Date ,
1 1 970 ( ),
long int.
9 . 8 . .
390 9 :


(, ,
. .. ).
1 3.
Rational. :
, 5/6 ( , . 83333).
. , , .
. ,
doue. Rational?
1 4 . Money ,
,
4/5 (0, 5 : , 0,5,
).
long,
, $ 1 23.45. ,
long int.
1 5. n, (
). ,
long int.
. , n*n
, USDl . 23+D5 . 00 ,
.
(USD) (D).
1 6. >>.
(. USDl . 23 DS . 00)
Money. <<.
1 7. , Rational
. Money.
1 8 . , Rational
, doue.


. .
. , .
++
.
. (
) - ,
. :
. ,
.
L\ aC\ b
\\

\!\

----=-=
392 10

./1- -
4.#- 4.4.- 1

t ' J.,,
- ?. ttr ( ?. )


++ :
-. ,
, ,
-
.
:
, ,
.
.
.
.
394 1 D

1 0. 1 . 1 0.8. ,
10.2. -
10.3. 10. 9. ,
1 0.4.
10.5. 10. 10.
10.6. - 10. 1 1 .

1 0.7.
10.11 . 1 .
10.7. 1 .
10.11 .2.

10.7.2.
10.11 ..

1 0. 1 .
.

. 4. 1 -
.
, ,

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

, . , .

- ,
-.

.

, -
-.
.

(),
1 D.1 . 395

-.
-
. , ,
.
1 . -
.
2. .

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

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

. ,

396 1 D

.
: -
-
.
1 iostream
.

. -
1 2- 1 6.

1 0.2. -
++
istream ostream - .
istream cin
ostream cout,
(
iostream).
ostream .

.
" -" (. , ,
).
ostream .

" - "
( 1 2 ,34) ostream

1 23

- , os tream

, . ostream
,
. -
1 D.2. - 397

,
.
istream .

.
"-" (. , ,
).
istream .

" - "
( 1 2,34) istream

1 23

ostream.
istream .
. istream,
, , , .
<Enter> ( / ).
. . <Enter>.
<Backspace>.
-
. . .
. -, , ,
. . ..
ostream
.
.
. istream
. ostream. ,
. 1 1 .2, ,
, - 1 1 ..2. .
, .
. ,
- .
398 1

1 0. .
.
.
-
.
-
.
, .
: 1: 2:

: , .
. , ,
.
. ,

( 1 1 ..2).
. .
, ,
. ( 1 1 .2 1 1 .).
ostream .
,
. istream : . -
.

iostream
( ) ( )

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

.
.
,
1 0.4. 399

1 . ;
2. ( );
3. ;
4. ( ) .
,
1 . ;
2. ( ) ;
3. ;
4. ( ).
,
ostream, ,
, cout, istream, ,
, cin. , ,
( 1 1 .3.3), ,
. ,
ostream istream.

1 0.4.
,
. ifstream -

istream , ofstream ostream


-

, fstream iostream,
, .
,
:
cout << " , 'l' : " ;
strinq iname ;
cin >> iname ;
ifstream i s t { iname } ; // i s t
- i n a me
if ( ! ist) error ( " name) ;
" ,

ifstream , name,
. ! ist ,
.
. istream. ,
, >> Point,
:
vector<Point> points ;
for ( Point ; ist>>p ; )
points . push_back (p) ;
400 1

, ofstream.
.
cout << "'l' : " ;
strinq oname ;
cin >> n ;
ofstream ost { n } ; // os t
-

if ( ! ost) rrr ( " O'l'JCP'l' 11w " , n) ;

ofstream , name,
. ! ost ,
.
, ostream. :
for ( int : points )
ost << ' ( ' << . << ' , ' << . << 11 ) \n" ;

.
. .
; . .
,
. - .
,
. ,
.

ostream i stream,
. :
void fill_from_file (vector<Point>& points , strinq& name)
{
ifstream ist { name } ; 11
if ( ! ist) error ( 11 11 O'l'JCP'l' 11 " , name) ;
11 ... i s t . . .
//

open ( ) close ( ) ( . 7. 1 )
.
. -
, . . :
ifstream ifs ;
11 . . .
ifs >> foo ; 11 :
11 . . .
ifs . open (name , // . . .
ios_base : : in) ; // . . .
11 . . .
ifs . close ( ) ; //
1 0.5. 401

11 " .
ifs >> bar ; 11 :
11 . . .

, ,
. , ,
, :
fstream fs ;
fs . open ( " foo " , ios_base : : in) ; 11
11 close ()
fs . open ( " foo " , ios_base : : out) ; 11 : i fs
if ( ! fs) error ( " :110 " ) ;

.
open ( ) close ( ) ?
,
. ,
. ,
,
, ,
iostream ( ++).
1 1 , ,
,
, .
, , ,

.
,
, .

1 0. 5 .
,
. ,
, .
60 . 7
1 60 . 6
2 60 .
59 . 22

(, ).
23,
. : ,
(, ,
402 1 D

), , (
, ) .
.
Reading.
s truct Readinq { //
int hour ; // [ 0 , 23}
doue teperature ; //
};

:
vector<Readinq> temps ; //
int hour ;
dou.le temperature ;
while ( is t >> hour >> temperature) {
if (hour < 1 1 23 < hour) rrr ( " " ) ;
teps . push_back (Readinq{ hour , temperature } ) ;

. istream ist
( ifstream) , ,
(cin) istream. ,
, , istream
. , , - ,
ist istream
. :

.
, .
, ,
. ,
(ofstream)
ostream. , , ,
.
for ( int i=O ; i < teps . si ze ( ) ; ++i )
ost << ' ( ' << temps [ i ] . hour << ' , '
<< teps [ i ] . temperature << " ) \n" ;

,
(, ).

,
.
#include " s td lib facilities . h"

s truct Readinq { 11
1 0.6. - 403

int hour ; // [ 0 , 23]


doue temperature ; //
};

int main ( )
{
cout << " : " ;
strinq iname ;
cin >> iname ;
ifstrea ist { iname } ; // i s t i name
if ( ! ist) rrr ( " w " , iname) ;

strinq n ;
cout << " : " ;
cin >> n ;
ofstream ost { n } ; // os t
if ( ! ost) rrr ( " w .n " , n) ;

vector<Readinq> temps ; //
int hour ;
doue temperature ;
while ( ist >> hour >> temperature) {
if (hour < 1 1 23 <hour) rrr ( " " ) ;
temps . push back ( Readinq {hour , temperature ) ) ;
_

for ( int i=O ; i<temps . si ze ( ) ; ++i )


ost << ' ( ' << temps [ i ] . hour << ' , '
<< temps [ i ] . temperature << " ) \n" ;

1 0.6 . -
, .

? ? -
, ( ,
, ..), - ,
, - ,
, ..
!
i s tream ,
(stream state).


good
eof ( ) ( )
fail ( ) ( , , )
bad ( ) ( , )
404 1

, fail ( ) bad ( )
,
- .
:
, fail ( ) . ,
( ) .
, .
bad ( ) , ,
,
. bad ( )
fail ( ) :
.

int i = ;
cin >> i ;
if ( ! cin) { // ()
if ( cin . bad ( ) ) error ( "cin " ) ; // !
if ( cin . eof ( ) ) {
11
11

if ( cin . fail ( ) ) { //
cin . clear ( ) ; //
11

! cin " cin "


" cin - ", " cin
good ( ) ".
" ". cin .
clear ( ) fail ( )
.

fai l ( ) , , , .
fail ( ) ,
, ;
clear ( ) , cin . clear ( ) cin
good ( ) .

.
, ,
* (<Ctrl+Z>
Windows <Ctrl+D> Unix) :
1 2 4 5 *

:
void fill_vector ( is tream& ist, vector<int>& v , char terminator)
// i s t v
//
1 D . 6. - 405

for ( int i ; ist >> i ; ) v . push_back ( i ) ;


if ( ist . eof ( ) ) return ; // :

if ( ist . bad ( ) ) error ( " ist " ) ; 11 !


if ( i st . fail ( ) ) { //
ist . clear ( ) ; // ,
11
char ;
i s t >> ; // ( -
// )
if ( ! = terminator) { //
i s t . unqet ( ) ; //
i s t . clear ( ios_base : : failbit) ; //
// fa i l ()

, ,
, . ,
- , , fill_vector ( ) ,
fail ( ) .
, , ,
,
fail ( ) . i s t . clear (ios_base : : failit) .

clear ( ) : clear ( )
() iostream,
() . fail ( ) . ,
, .
ist, unqet ( ) ; ,
fil l_vector ( ) ,
. unqet ( )
putack ( ) ( 6.8.2, . 7 . 3),
. , ,
.
fill_vector ( ) ,
, fail ( ) eof ( ) .
, runtime_error,
error ( ) , , -
istream d ( ) .
.
, ,
bad ( ) , - .
, istream
.
406 1

11 i s t ,
// ba d
ist . exceptions ( i s t . exceptions ( ) ( ios_base : : badit) ;

, :
ist bad ( ) ,
ios_base : : failure. exceptions ( )
. .
bad ( ) :
void fill_vector ( is tream& ist , vector<int>& v, char terminator)
11 i s t v
11

for ( int i ; ist >> i ; ) v . push back ( i ) ;


_
if ( is t . eof ( ) ) return ; // :

11 good () , ba d () , eof () ; , fa i l (}
i s t . clear ( ) ; // ,
11
char ;
ist >> ; // ( -

11 )
if ( ! = terminator) { //
ist . unget ( ) ; //
i s t . clear ( ios_base : : failbit) ; 11
11 fa i l ()

ios base, ,
iostream, , badit, ,
failure, .
: : , ios_base : : badit ( .7.2) .
iostream:
. , iostream
,
,
(
.
). , iostream,
h ++ Programmig Language (Stroustrup) Stan
dard ++ IOStreams and Loca/es (Langer).
ostream ,
istream: good ( ) , fail ( ) . eof ( ) bad ( ) . .
,
, . .
, r,
,
1 .7. 407

, ,
.

1 . 7 .
. , ,
.
,
, .
.
.
-
.
, ,
. ,
,
, .
1 1 0 ().
cout << " 1 10 : \n " ;
int n = ;
while ( cin>>n) { 11
if ( l<=n && n<=lO) break ; 11
cout << " , " << n
<< " 'l' [ 1 , 10 ] ; \n" ;
}
11 . . .

, .
break ( .6),
.
cout << "'l' 1 10 : \n" ;
int n = ;
while ( cin>>n && ! ( l<=n&&n<=lO ) ) //
cout << " , << n "

<< " [ 1 , 1 0 ] ; \n" ;


11 . . .

, "" .
, ""?
, ,
. t
6 ( t
6), ,
n, .
. ( -
408 1

) (
<Ctrl+Z> Windows <Ctrl+D>
Unix). n,
. ,
, .
1 . , ,
?
2. , (
)?
3. , (
- , )?
?
?"
.
1 . .
2. , -
(, ).
3. .

, - ,
.
, . ..
, ,
. ,
, , ,
. ,
.
.
.
. , ,
"" ; ""
. ,
.
,
; ,
. ,
,
, :
, .
cin ( cin . clear ( ) ),
1 0. 7 . 409

(
<Ctrl+Z>?) .
, , ,
,
, -
; . . ,
. ,
.
( ). .

1 0 . 7 . 1 .
,
.
.
cout << " 1 1 0 : \n " ;
int n = ;

while ( true )

cin >> n ;
if ( cin) ( // ;
if (1 <= n && n <= 1 0 ) break ;
cout << "'1' , " << n
<< " [ 1 , 10 ] ; \n" ;

else if (cin . fail ( ) ) ( // ,


//
cin . clear ( ) ; // good ( ) ;
/ /
cout << " ; \n " ;
for ( char ch ; cin>>ch && ! isdigit ( ch) ; ) //
/ * * / ; //
if ( ! cin) error ( "Boa " ) ; 11
cin . unget ( ) ; / / ,
11
else (
error ( "Boa " ) ; // eof bad:
11
)
11 , ,
11 [ 1 , 1 0 ]

.
,
. ,
,
410 1

. ? .
.
.
.
.
"" .
.
,
. ,
. -
"" (.. ) .
void skip_to_int ( )
{
if ( cin . fail ( ) ) // ,
//
cin . clear ( ) ; // good () ;
for ( char ch ; cin>>ch ; ) { //
if ( i sdiqit ( ch) 1 1 ch == ' - ' ) {
cin . unqet ( ) ; 11 ,
11
return ;

error ( " " ) ; // eof bad

skip_to_int ( ) .
:
cout << " 1 10 : \n" ;
int n = ;
while ( true) {
if ( cin>>n) { // ;
if ( l<=n && n<=lO ) break ;
cout << " , " << n
<< " .w [ 1 , 10 ] ; \n" ;

else
cout << "'l' , ; \n" ;
skip_to_int ( ) ;

}
// , ,
11 [ 1 , 1 0 }
1 0.7. 411

,
, .
?
: " :
in t, - ".
int qet_int ( ) ; 11 cin i n t

int qet_int ( int low , int hiqh) ; 11 c i n i n t


11 [ l o w , h i gh }

,
. .
int qet_int ( )
(
int n = ;
while ( true )
if ( cin >> n) return n ;
cout << " ; \n" ;
skip_to_int ( ) ;

get_int ( ) ,
, .
get int ( ) ,
( get_i nt ( )
) .
get int ( ) ,
get_int ( ) :
int qet_int ( int low , int hiqh)

cout << " "


<< low << " " << hiqh << " JU : \n" ;
while ( true) (
int n = qet_int ( ) ;
if (low<=n && n<=hiqh) return n ;
cout << " , " << n
<< " . [ "<< low
<< '<< hiqh << " ] ; \n " ;
'

get_int ( ) ,
. cin ,
, .

.
41 2 1

int n = qet_int ( l , 10 ) ;
cout << " n : " << n << endl ;

int m = qet_int ( 2 , 0 0 ) ;
cout << " m : " << m << endl ;

.
(, ) .
get_int ( )
.

1 . 7 .2.
get_int ( ) -
. .
, .
. get_int ( ) .
:
int strenqth = qet_int ( l , 10 , "Bee " ,
" , " ) ;
cout << " : " << strenqth << ' \n ' ;

int altitude = qet_int ( 0 , 50000 ,


" " ,
" , " ) ;
cout << " : " << altitude << " \n" ;

:
int qet_int ( int low, int hiqh ,
const strinq& qreetinq ,
const strinq& sorry)

1 . 1
cout << qreetinq << " : [ " << low << << hiqh << " ] \n" ;
while ( true) (
int n = qet_int ( ) ;
if ( low<=n && n<=hiqh) return n ;
1 . 1
cout << sorry << " : [ " << low << << hiqh << " ] \n" ;

,
.
,
(, . ,
. , ).
.
, :
get in t ( ) "".
1 . . . 41 3

,
, ,
"" . , .
,
, -
. , .
,
. ,
error ( ) (.
5. 6.3); , .

1 . . ,
<< , ,
.
,
.
- .
, ,
<< .

.
<<.
. ,
,
<<.
,
.
Date 9.8,
. , .
ostream& operator<< (ostream& os , const Date& d)
{
return os << ' ( ' << d . year ( )
<< ' , ' << int ( d . onth ( ) )
<< ' , ' << d . day ( ) << ' ) ' ;

. "30 2004 "


(2004 , 8 , 30 ) .
, ,

.
41 4 1

9.6 , ,
,
. . -
<< Date,
cout << dl ;

dl Da te,
operator<< ( cout , dl ) ;

, ostream&
operator<< ( ) .
"" . ,
.
cout << dl << d2 ;

<<.
.
cout << dl << d2 ; // opera tor<< (cou t , dl ) << d2 ;
11 opera tor<< (ope ra t o r<< (co u t , dl ) , d2) ;

, dl cout,
d2 ,
.
dl d2 .
.

1 . 9 . ,
>>
. ,
.
Date 9.8,
, <<.
.
i stream& operator>> ( i s tream& i s , Date& dd)
{
int , m, d ;
char chl , ch2 , h , ch4 ;
is >> chl >> >> ch2 >> m >> h >> d >> ch4 ;
if ( ! is ) return i s ;
if ( chl ! = ' ( ' 1 1 ch2 ! = ' , ' 1 1 h ! = ' , ' 1 1 ch4 ! = ' ) ' )
/ /
i s . clear ( ios_base : : failbit) ;
return i s ;
1 0.1 . 41 5

dd = Date { y , Date : : Month (m) , d } ; // dd


return is ;

>> , (2004 , 8 , 20 ) ,
Date . ,
, .
, .
>> ,
( , , ) , fail ,
eof bad, Date .
istream - clear ( ) .
, ios base : : failbi t fail ( ) .
_

Date
; . ,
operator>> () ,
, :
,
. (2004 ,
8 , 30 } . , } ,
,
, unget ( )
. operator>> ( )
Date, ( 2 0 0 4 , 8 , 32 ) , Date
,
operator>> ( ) .

1 0 . 1 .
1 0 . 5 , .
(. 1 0. 6)
, .
, .

. , ,
ist istream.
for (y_type var ; ist>>var ; ) { //
// r va r

11 - va r
}
11 ba d ;
11 .
i f ( i s t . bad ( ) ) error ( " noxo O'l'OIC " ) ;
41 6 1 D

if ( is t . fail ( ) )
// ?
}
11 ,

, ,
, ,
, , .
1 0.6, , istream
JUi failure .
.
// - : i s t
i s t . exceptions ( i s t . exceptions ( ) l ios_base : : badit) ;

-
.
for (y_type var ; i st>>var ; ) { //
11 va r

// - va r

if ( i st . fail ( ) ) 11 ' 1 ' - /


ist . clear ( ) ;
char ch ;
if ( ! ( i s t >> ch && ch == ' 1 ' ) )
rrr ( ": " ) ;

}
// ,


- , . . ,
error ( ) .
,
,
, , JUi
, .. .
.
, . ,

.
. .
11 - : i s t
ist . exceptions ( i s t . exceptions ( ) l ios_base : : badit) ;

void end_of_loop ( i s tream& i s t , char term, const string& essage)


1 D.1 1 . r 41 7

if ( i s t . fail ( ) ) { // term
11 /
ist . clear ( ) ;
char ch ;
if ( ist >> ch && ch -- term) return ; //
error (essage ) ;


for (y_type var ; ist>>var ; ) { //
// va r

// - va r

// ,
end of loop ( i s t , ' 1 ' , " " ) ;
// :

end of loop ( ) , ,
; fail ( ) . , r

.

1 0. 1 1 .
" " .
,
. ,
,
.
, .
{ year,
, , 1 900,
} .
, .
{ month,
, j an,
} .
.
( ,
, , ) .
41 8 1

.
year 1990 1
{year 1991 { onth jun 1 1
{year 1992 { onth j an ( 1 61 . 5 ) 1 ( onth f ( 1 1 6 4 ) ( 2 2 65 . 2 1 1 1
{year 2000
{ month f (1 1 68 1 (2 3 66 . 6 6 ) ( 1 67 . 2 ) 1
{ onth dec ( 15 15 - 9 . 2 ) ( 1 5 14 -8 . 8 ) ( 14 - 2 ) 1

.
.


(, HTML L), -
, .
, , .
,
.
, , ,
,
.
,
. ,
, (
HTML L) . ,
. , .
, -
,
, . , .

1 0.1 1 .1 .
? .
, Year, onth Reading,
. Year Month
;
, ,
, ,
.. ,
Year onth
: Month , Year -
. Reading? ,
().
Reading ( , )
1 0. 1 1 . 41 9

Month. , : ,
. ,
- ,
.

.
- , .
- ,
.
Year ,
1 2 Month, Month ,
-

30 Day, Day 24 (
-

).
. . Day, Month Year -

. .
Month Day
Year , ,
. , " "
, .
const int not_a_readinq = -7777 ; //

,
, "
". .
const int not_a_month = -1 ;

:
struct Day {
vector<doule> hour { vector<doule> ( 2 4 , not_a_readinq) } ;
};

. Day 24 ,
not_a_reading.
struct Month { !/
int month { not_a_month } ; // { 0 , 1 1 } ( 0)
vector<Day> day { 32 } ; // { 1 , 3 1 }
};

da ta [ ] .
,

struct Year { /!
int year ; //
vector<onth> month { 12 } ; // { 0 , 1 1 } ( 0)
};
420 1

, - , Month Year
month year .
" " (
, 2 4 , 32 12). ,
. -
( )
. ,
, , "
";
(. 7.6. 1 ) . 32
, , ; 32
"" .
,
struct Day {
vector<doule> hour { 24 , not_a_reading } ;
};

, , - ! -
(24 -1).
, int
,
( ) ( 1 8.2) .

1 0. 1 1 .2.
Reading ,
:
struct Readinq
int day ;
int hour ;
doule teperature ;
};

istream& operator>> ( istream& i s , Reading& r )


1 1 i s r
// : ( 3 4 9. 7
11 ,

char chl ;
if ( i s >> chl && chl ! = ' ( ) { // Rea di n g ?
'

is . unqet ( ) ;
i s . clear ( ios_base : : failbit) ;
return i s ;

char ch2 ;
1 0. 1 1 . 421

int d;
int h ;
doule t ;
is >> d >> h >> t >> ch2 ;
if ( ! is 1 1 ch2 ! = ) )
error ( "noxa " ) ; 11
r . day = d ;
r . hour = h ;
r . teperature t;
return is ;

, . ,
fail ( ) .
:- . ,

, error ( ) .
: Month , ,
: : Reading,
(: >> : Reading).
istrellill& operator>> ( istrellill& is , Month& )
11 Mon th is

11 : { mon th feb . . . }

char ch = ;
if ( i s ch && ch ! = ' { ' ) {
i s . unget ( ) ;
is . clear ( ios_base : : failbit) ; 11 Mon th
return i s ;

string onth_arker ;
strinc;r m;
is >> onth arker >> m;
if ( ! is 1 1 onth_arker ! = "onth " )
error ( " Heaepoe onth" ) ;
. onth = onth_to_int (m) ;

Readinc;r r ;
int duplicates = ;
int invalids = ;
for (Readinc;r r ; is >> r ; ) (
if ( i s_valid ( r ) ) {
if (. day [r . day] . hour [ r . hour] != not_a_reading)
++duplicates ;
. day [r . day] . hour [ r . hour] = r . teperature ;

else
++invalids ;
422 1

if ( invalids)
error ( " Heepre onth , " , invalids ) ;
if (duplicate s )
rrr ( " 111 1D1 Month , "
duplicate s ) ;
end_of_loop ( is , ' } ' , " Month " ) ;
return is ;

month_to_int ( ) :
, j un,
[ , 1 1 ] . end_of_loop ( )
1 0. 1 .
Readinqs
( - ).
>> Month
Readinq, .
constexpr int iplausie_min -200 ;
constexpr int iplausie_ax = 200 ;

l i s_valid ( const Readinq& r )


1 1
{
if ( r . dsy <1 1 1 l<r . day) return fal se ;
if ( r . hour<O 1 1 23<r . hour) return false ;
if ( r . teperature < iplausie_min 1 1
iplausie_max < r . teperature)
return false ;
return true ;

, Year. >>
Year >> Month.
istream& operator>> ( istream& i s , Year& )
11 Yea r is
// : ( r 1 9 72 . . . J

char ch ;
is >> ch ;
if ( ch ! = ' { ' )
is . unqet ( ) ;
is . clear ( ios : : failbit) ;
return i s ;

strinq year_marker ;
int ;
i s >> year_marker >> ;
i f ( ! is 1 1 year_marker ! ="year " )
1 0.1 1 . 423

rrr ( " Year" ) ;


y . year = ;
while ( true) {
Month m; //

if ( ! ( i s >> m) ) break ;
y . month [ m . month] m;
=

end_of_loop ( is , ' } ' , " Year" ) ;


return is ;

"'' , "
", . .
, -
for (Month m; is >> m;
y . month [m. month] = m;

, .
. ,
. , operator>> ( i s tream& i s ,
Month& m) m ;
Reading. ,
is >> m
m. , Month
.
is >> m,
. Month.
m ,
.
, operator>> ( i s tream& is , Month& m)
m .
for (Month m; i s >> m; ) {
y . month [m. month] m; =

m = Month { } ; / / " "

.
11
cout << "'l' .n\n" ;
string iname ;
cin >> iname ;
ifstream ist { iname } ;
if ( ! ifs) rrr ( " l'l' .n " , iname) ;

// bad ()
ifs . exceptions ( ifs . exceptions ( ) l ios_base : : badit) ;
424 1

11
cout << " w:r \n " ;
s trinq n. ;
cin >> n. ;
ofstream ost { ona.me } ;
if ( ! ofs) rrr ( " 'l' w 11 , n.) ;

// :
vector<Year> ys ;
while ( true ) {
Year ; //
// Yea r
if ( ! ( ifs>>y) ) break ;
ys . push_back (y) ;

cout << " " << ys . size ( ) << " I \n" ;

for (Year& : ys ) print_year (ofs , y) ;

print_year ( )
.

1 0.1 1 ..
>> Month .
.

.
if, .:
i f ( s = " j an" )
1D = 1 ;

else if ( s== " fe " )


ID = 2 ;

;
. ,
,
.
vector<string>.
.
vector<strinq> month_input_t = {
" jan" , 11 f 11 , "mar" , " apr " , " . " , " jun" ,
" jul " , "auq" , " sep " , " oct." , "nov" , "dec"
};

int onth_to_int ( s trinq s )


// s ? ,
1 0.1 1 . 425

11 ( 0 , 1 1 } , - -1
{
for ( int i=O ; i<12 ; ++i ) if (onth_input_tl [ i ] ==s) return i ;
return -1 ;

. ++
- map<string , i nt>
2 1 .6. 1 .
, .
int.
. ,
string int
int string.
vector<strinq> onth_print_tl = {
"January" , " Feruary" , "&rch" , "April " ,
"& " , "June" , " July" , "Auqust" , " Septeer" ,
"October" , "Novemer" , "Decemer"
};

strinq int_to_month ( int i)


11 ( 0 , 1 1 }
{
if ( i<O 1 1 12<=i) rrr ( " " ) ;
return onth_print_tl [ i ] ;

,
? , ?
,
- . - , - .
. , ,
. - .
- .
(
) .
. .
. -
.
426 1

1 . . (. 1 0.4).
Point, -
.
2. 1 0.4,
(, ).
Point original__points .
3. original__points, ,
.
4. ofstream mydata . txt.
Windows . txt
(,
WordPad).
5. ofstream, ifstream
mydata . txt. myda ta . txt
processed__points.
6. .
7. -
! , ( )
.


1 .
?
2. istream?
3. ostream?
4. ?
5. ?
6.
.
7. .
8. .
9. .
1 0.
.
1 0. 1 1 . r 427

,
.
( ).
.
1 1 . ?
1 2 . ?
1 3. () ?
1 4. ,
clear ( ) istream.
1 5. << >>
?

d ( ) iostream
uffer istream
clear () ofstream
close ( ) open ( )
eof ( ) ostream
fail ( ) unget ( )
good ( )
ifstream

1 . , ,
.
2. , ,
Reading, 1 0. 5 .
50 .
store_temps . cpp. - raw_
temps . txt.
3. , raw temps . txt,
. 2. ,
. temp_stats . .
4. s tore_temps . cpp . 2,
f .
temp_stats . cpp,

.
5. print_year ( ) , 1 0. 1 1 .2.
428 1

6. Roman int (
int) << >>. Roman_int
as_int ( ) , int, . ,
r Roman int, cout << " " << r
<< 11 11 << r . as_int ( ) << ' \n ' ; .
7. 7,
, , XXI + CIV cxxv. =

8. ,
,
; ,
.
9. , ,
, ,
, .
1 0 . 7 from .
. to .
( , )
. , 7 .3,
. ,
.
1 1 . . ,
.
, "bears : 17 elephants 9 end" , 26.


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

"" .

' ,
' ?- #. -

,f 9 (Uk/ E)
-

,
-, 1 0 ,
.
, ,
,
.
.

.
430 1 1

11 . 1 . 11 ..1 .
11 .2. 11 ..2.
1 1 .2.1 . 11 ...
11 .2.2. 1 1 .4.
11 .2.3. 11 .5. .t
11 .6.
11 .2.4. 1 1 .7.
11 .2.5.
1 1 .. 11 .8.

1 1 . 1 .
iostream ( ++,
-)
. "" ,
.
, ,
1234 .
: 1 , 2, 4 .

. . ,
(, ) ,
. , ,
,
. .
, ,
( ).
,
.
.
,
-
, ,
, ,
. ,
, .
,
.
1 1 .2. 431

1 1 . 2.
.
,
. , 1 . 25 (
) 1 . 24 6 7 0 4 7 7 ,
( 1 2 5)
.

( 1 . 2467) 1 . 25 (
, . . ) .


. (ostream)
.
<<.
,
, . . .
.
( , );
-; ""
true ( vrai or sandt) , 1,
,
ASCII (, Unicode) . ,
,
. ,
,
, Standard ++ IOStreams d Locales
(Langer); 2 1 h ++ Programmtng Language
; 22 27 ISO ++.

.

1 1 .2 . 1 .
(
8). (
1 0) ( 1 6).
, .2. 1 . 1 .

.
, .
,
.
,
432 11

,
( ),
(
). -
++ ( 1 970- ),
. .
, ( ) 1234
, :
cout << 1234 << 11 \t (decial ) \n "
< < hex < < 1 2 3 4 < < 11 \t ( hexadecial ) \n"
<< oct << 1234 << 11 \ t ( octal ) \n" ;

' \ t ' " ".


:
1234 (decial )
4d2 (hexadecial )
2 322 ( octal)

<< hex << oct .


<< hex ,
, << oct
,
. .
cout << 1234 << ' \t ' << hex 1234
<< ' \t ' << oct 1234 ' \n ' ;
cout << 1234 ' \n ' ; 11
//

:
1234 4d2 2 322
2322 //

,
: , oct, hex dec ( )
, " " (sticky). -
, . hex oct

(manipulators).


,
. .

.
1 1 .2. 433

, ,
. , ,
, 1 1
1 1 , 9 (.. 1 1 )
1 7 (. . 11 ).
,
, .
.
cout << 1234 << ' \t ' << hex << 1234
<< ' \ t ' << oct << 1234 << ' \n ' ;
cout << showbase << dec ; //
cout << 1234 << ' \t ' << hex << 1234
<< ' \ t ' << oct << 1234 << ' \n ' ;

:
1234 4d2 2 322
1234 Ox4d2 02322

. .
, ( ).
++.
.
cout << 1234 << ' \t ' << Ox4d2 << ' \t ' << 02322 << ' \n ' ;

:
1234 1234 1234

, showbase
, oct hex. noshowbase
showbase,
,
.
, .


oct
dec
hex
showbase
noshowbase
434 1 1

1 1 .2.2.
>> ,
,
.
int ;
int ;
int ;
int d ;
cin >> >> hex >> >> oct >> >> d ;
cout << << ' \t ' << << ' \ t ' << << ' \ t ' << d << ' \n ' ;


1234 4d2 2 322 2322


1234 1234 1234 1234

, oct, dec hex


, .


, ,
. :

1234 1234 1234 1234
. . ,
.


, >>.
, ,
cin . unsetf ( ios : : dec) ; 11 ( . .
11 )
cin . unsetf (ios : : oct) ; 11
11 (. . 1 2 )
cin . unsetf ( ios : : hex) ; 11
11 (. . 1 2 )

- unsetf ( ) ( ),
. ,
cin >> >> >> >> d ;


1234 Ox4d2 02322 02322
1 1 .2. 435


1234 1234 1234 1234

1 1 .2..
,
(. , ).
, ,
.
ios tream , . .
cout << 1234 . 56789 << " \ t\t (o ID) \"
<< fixed << 1234 . 56 7 8 9 << 11 \ t (11) \ 1 1
<< scientific << 1234 . 5 6 7 8 9 << " \ t ( ) \n" ;

:
1234 . 57 ( )
1234 . 567890 ( )
1 . 234568+003 ( )

fixed, s cientific defaul tfloat


.
defaultfloat ,
( (general format)).
:
cout 1234 . 56789 ' \t '
fixed << 1234 . 5 6789 << ' \ t '
scientific << 123 4 . 56789 << ' \n ' ;
cout 1234 . 56789 ' \n ' ; // " "
cout defaultfloat << 1234 . 5 6789 11
<< \t ' << fixed << 1234 . 56789
<< ' \ t ' << scientific << 1234 . 5 6 7 8 9 << ' \n ' ;

:
1234 . 57 1234 . 567890 1 . 234568+003
1 . 234568+003 // " "
1234 . 57 1234 . 567890 1 . 234568+003


fixed
scientific ;
(1 , 1 0), . .

defaul tfloat fixed scientific
defaultfloat
436 1 1

1 1 .2.4.

defaultfloat. ,
( defaul tfloat).
, - . .
1234 . 567 1234 . 57
1 . 2345678 1 . 23457
, , 4/5: 4 -
, 5 9 . ,
-

,
1234567 1234567 ( )
1234567 . 1 . 234 57+ 0 0 6
ostream , 1234567 .
fixed, ,
scientific,
. , defaultfloat
scientific fixed,
defaultfloat,
.


,
1234567 . 89, defaul tfloat, - fixed,
- scientific.
?

,
setprecision ( ) , :
cout < < 1234 . 5 6789 < < \t '
<< fixed << 1234 . 5 6789 << ' \ t '
<< scientific << 123 4 . 56789 << ' \n ' ;
cout << defaultfloat << setprecision ( 5 )
<< 1234 . 5 6789 < < ' \t '
<< fixed << 1234 . 5 6789 << 1 \t '
<< scientific << 1234 . 5 6789 << ' \n ' ;
cout << defaultfloat << setprecision ( 8 )
<< 1234 . 5 6789 << ' \t '
<< fixed << 1234 . 56789 << ' \ t '
<< scientific << 1234 . 56789 << ' \n ' ;

(
) :
1 1 .2. 437

1234 . 57 1234 . 5 6 7 8 90 1 . 2 345 68+003


1234 . 6 1234 . 5 6 7 8 9 1 . 23457+003
1234 . 5679 1 23 4 . 5 6 7 8 9000 1 . 2 3456789+3

, .


defaul tfloat
scientific
fixed

, (
defaul tfloat , ),
. ,
, : "
"

1 1 .2.5.

. .
. .
(fields). ,
setw ( ) :
cout << 123456 << ' 1 ' //
<< setw ( 4 ) << 123456 << ' 1 ' // 1 23 4 5 6
11 4
<< setw ( 8 ) << 123456 << ' 1 ' // 8
<< 123456 << " 1 \n" ; 11 " "

:
123456 1 12345 6 1 123456 1 12345 6 1

123456. -
, ,
. 123456 ,
, . ? ,
1 1234 1 1 3456 1 ,
,
, ,
. -
, .
, " ".
(, ) -
, .
438 1 1


:
cout << 12345 << ' 1 ' << setw ( 4 ) << 12345 << ' 1 '
<< setw ( B ) << 12345 << ' 1 ' << 12345 << " l \n" ;
cout << 1234 . 5 << ' 1 ' << setw ( 4 ) << 1234 . 5 << ' 1 '
<< setw ( S ) << 1234 . 5 << ' 1 ' << 1234 . 5 << " l \n " ;
cout << "asdfq" << ' 1 ' << setw ( 4 ) << "asdfq" << ' 1 '
<< setw ( B ) << " asdfq" << ' 1 ' << " asdfq" << " l \n " ;

:
12345 1 12345 1 12345 1 12345 1
1234 . 5 1 1234 . 5 1 1234 . 5 1 1234 . 5 1
asdfq l asdfq l asdfq l asdfq l

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

&
. , ,
.
,
.

1 1 ..
++ - ,
. 1 0.3, -
, .
: 1: 2:

, .
iostream
. .
,
. , istream,
, ostream,
.
1 1 . . 439

1 1 .. 1 .
.
ifstream , ofstream -
. .
.


ios_s : : ( )
ios base : : ate ( )
ios_s : : inary ( )
ios s : : in
ios s : : out
ios s : : trunc

:
ofstream ofl { namel } ; 11 i o s_ ba se : : o u t
ifstream ifl { name2 } ; // i o s_ ba se : : in
ofstream ofs { name , ios_ase : : app } ; // ofs t ream
// ios base : : o u t
fstream fs{ "myfile " ,
ios_ase : : in 1 ios_ase : : out } ; /! ,

1 - (
. 5 . 5), .
,
.

.
, ,
good ( ) .
if { ! fs} // :


.
ifstream ifs { " redunqs " } ;
if ( ! ifs) // "readings "

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

ofstream ofs ( "no-such-file" } ; / /


11 "no - s u ch - fi l e "
ifstream ifs ( "no-file-of-this-name " } ; // : i fs
/ / good ()

"" .
"" .
, istream,
- , ostream.

1 1 ..2.
1 23
. .
int n = 123 ;
strinq s 11 12 3 11 ;
=

123 () .
, , ,
int (4 , .. 32
). 123 12345,
- .
123 .
11 12 34 5 11 ,
( string).
,
, , .

1 23 :
12131 ? ? 1 1? ? ?

1 2345 :
121314 5
1 1? ? ?

1 23 : 1 23

1 2345 : 1 2345

, -
, ,
1 23456 1 23 456.
.
.

1 23456 :
12131415161 111
1 23 456 : l2ll 141sll 1

(, int)
1 1 .. 441

(, s tring)
. ios tream
; . istream
.
ostream
. ,
istream ostream .
- (blnary 1/0).
ios_base : : inary. ,
,
. , ,
.
int ain ( )

// i s t ream
cout << " \n" ;
strinq iname ;
cin >> iname ;
ifstream ifs { inae , ios_base : : binary } ; // ,
//
11 -
if ( ! ifs) rrr { " !'l' " , inae) ;

// os t ream
cout << " \n" ;
strinq n ;
cin >> n ;
ofstream ofs { onae , ios_base : : binary } ; // ,
//
// -
if ( ! ofs) error ( " !'l' " , oname) ;

vector<int> v ;

// :
for (int ; ifs . read (as_ytes {x) , sizeof ( int) ) ; ) 11
v . push_back (x) ; 11

11 ... v

// :
for (int : v)
ofs . write ( as_bytes (x) , sizeof (int) ) ; 11
return ;

ios_base : : inary
:
442 1 1

ifstream ifs { name . c_str ( ) , ios_base : : binary } ;


oftream ofs { name . c_str ( ) , ios_base : : binary } ;

,
. -
-
>> <<.
,
(, " asdf" , s, d, f,
123 1 , 2 , 3).
,
. inary,
( - ) , .
, inary ,
.
int? ,
; .
int ( )
, .
.
ifs . read ( as_ytes ( i ) , sizeof ( int) ) //
ofs . write (as_bytes ( v [ i ] ) , sizeof ( int) ) //

wri te ( ) ostream read ( ) istream


( as bytes ( ) )
(), sizeof.
, ,
. , int
1234, (
) 0 0 , 0 0 , 0 4 , d2 :
-

i: 00 00 04 d2

as_bytes ( ) .
( ,
, 1 7.8 1 9.3):
template<class >
char* _t (& i) //
//

void* addr z &i ; //


//
1 1 .3. 443

return static_cast<char*> ( addr) ; 11

static_cast
,
. 1 7 1 8.
, .
. ,
read ( ) wri te ( ) .
. .
,
-
. ,
.
,
:
.
-.
ios tream, Ul\.1,
-
. .
-, -
, .

1 1 ...

.
. , ,
,
.
. ,
.
, , /
("read/get posltion"), . ,
/ ("write/ put posltion").

: : 6

.
fstrea. fs { n } ; //
if ( ! fs) error ( " " , n) ;
444 1 1

fs . seekq ( S) ; // (g "ge t ")


11 ( )
char ch ;
fs>>ch ; 11
cout << " CllOn - " << ch << ' ( ' << int (ch) << 11 ) \n" ;
fs . seekp ( l) ; /! ( "pu t ")
/!
fs<< ' ' ; / /

, seekg ( ) seekp ( )
.
.
: .
, (
seekg ( ) seekp ( ) ) .
.

1 1 .4.
istrea.m
ostrea.m
string. istrea.m,
string, istringstrea.m, ostream,
s tring, ostringstrea.m. ,
istringstream .
doule str_to_doule ( strinq s)
/! s
//

istrinqstream i s { s } ; // s

doule d ;
i s >> d ;
i f ( ! is) error ( "OlllC\ a doule : 11 , s ) ;
return d ;

doule dl = str_to_doule ( " l2 . 4 " ) ; 11


doule d2 str_to_doule ( " l . 34e-3 " ) ;
doule d = str_to_doule ( " twelve point three " ) ; 11 error ()

,
istringstream, eof ( ) .
, istringstream
; istringstream
istream.
ostringstream, , .
, .
1 1 .4. 445

(
1 6. 5), :
void my_code (strinq lael , Temperature t)
{
11 . . .
ostrinqstream os ; //
os << setw ( B ) << lael << " : "
<< fixed << setprecision ( S ) << t . t << tep . unit ;
someobject . display ( Point ( l00 , 100) , os . str ( ) . c_str ( ) ) ;
11 . . .

- str ( ) ostringstream
string, ostringstream.
c_str ( ) - string,
-

, .
stringstream ,
- . ,
string str_to_doue ( )
(. ) .
, my code ( ) ,
. , 1 1 . 7 stringstream
. ,
stringstream -
.
ostringstream
.
int seno = qet_next_numer ( ) ; //
ostrinqstream name ;
nas << "myfile" << seno << " . loq" ; 11 , myfi l e 1 7 . l og
ofstream loqfile { na.e . str ( ) } ; // myfi l e 1 7 . l o g

istringstream string,
, .
, ostringstream
string, .
stringstream,
: ss . s tr ( )
ss, ss . str ( s )
ss s . 1 1 . 7 ,
ss . str ( s ) .
446 1 1

1 1 . 5 . ,
>>
, .
, int >> .
, ,
string >> ,
(whltespace). istream
:
strinq n. ;
cin >> n. ; 11 : Denn i s Ri t ch i e
cout << n << ' \n ' ; 1 1 : Den n i s

,
, ?
getline ( ) :
strinq name ;
qetline ( cin , name) ; 11 : Denn i s Ri t ch i e
cout << name << ' \n ; 11 : Den n i s Ri t ch i e

. ?
: " ,
>>". : "
". , ,
>>. , ,
- , :
strinq first_name ;
strinq second_name ;
strinqstream ss ( name) ;
ss >> first_name ; 11 Denn i s
ss >> second_name ; 1 1 Ri t ch i e

first_name second_name
.

,
.
. ,

, :
,
.

,
.
1 1 .6. 447

string command ;
getline ( cin , command) ; 11
stringstream ss { command ) ;

vector<string> words ;
for ( s tring s ; ss>>s ;
words . push_back ( s ) ; //

. .
. .

1 1 .6.
, , ,
, .
, ,
.
. , .
, .
7.8.2. . 1+4 *x<=y/ z * S
.
1 + 4 * <= / z * 5

>>. ,
, <=
( < = ).
z* (
* ).
:
for ( char ch ; cin . get ( ch) ; )
if (isspace ( ch) ) { // ch ,
11 (
11 )

if (isdigit (ch) ) {
11

else if (isalpha ( ch) )


11

else
11

istream : : get ( ) .
. >>.
448 1 1

get ( ) istream,
.
:
? ? .
.


isspace () ( ' ' , ' \ t ' , ' \n ' ..)?
isalpha () ( ' ' ' z ' , ' ' z ' ) ( : ' ' )?
" " '

isdigit (c) ( 1 1 " 1 9 1 )?


isxdigit (c) (
' ' " ' f ' 1 1 " 1 F 1 )?
isupper () ?
islower () ?
isalnum (c) ?
iscntrl () (ASCll 0"3 1 1 27)?
ispunct (c) , , , ,

isprint ( ) (ASCll 1 1"1
)?
isgraph () isalpha ( ) isdigi t ( )
ispunct () ( : )?

,
( 1 1 ) . , isalnum () isalpha ( )
1 1 isdigi t () ; , ,
.
,
, .


toupper ( )
tolower ()

, , ,
. , Right, right rigHT, .
, (, rigHT
<Caps Lock>).
tolower ( ) ,
: right.
string.
void tolower ( string& s) //
1 1 . 7. 449

for (char&x : ) = tolower (x) ;

string,
(. 8 . 5 . 5).
,
, .
tolower ( ) , toupper ( ) ,
, -
,
.

1 1 . 7 .

iostream .
. ,
istream , ,
,
, >> . , ,
?
4.6.3,
. , ,

planned , the guet arrived ; then ,

planned,
the
guet
arrived;
then ,

: "planned , " "arrived ; "


. , ,
.

. ?
, ,
"" .
trinq line ;
qetline (cin , line) ; 11 l in e
for (char& ch : line) 1 1
witch (ch) {
450 1 1

case ' ; ' : case 1 1 . case 1 , 1 .. case ' ? ' : case ' ! ' :
' '
ch = ;

strinqstream ss ( line) ; 11 i s tream s s l i n e


vector<strinq> vs ;
11
for ( strinq word ; ss>>word ;
vs . push_back ( word) ;

, :

planned
the
quests
arrived
then

. .
, ?

. ? ?
, ?
ps . whi tespace ( 11 ; : , 11 ) ; // , ,
//
for ( strinq word ; ps>>word ; )
vs . push_back ( word) ;

. , ps?
. .
-. .
, .. .
. .
as . not

not
as
not

, .
istream >>.
, istream, ,
, .

(, ..) ;
"
" . ,
1 1 .7. 451

; , .
Punct s tream.
class Punct stream { // i s t rea m ,
//
pulic :
Punct_strea. ( i stream& is)
: source { is } , sensitive { true } { 1

void whitespace (const strinq& s ) 11 s


{ white = ; 1 //
void add_white (char } //
{ white += ; 1 11
bool is_whitespace (char ) ; //
// ?

void case_sensitive (bool ) sensitive = ; 1


bool is_case_sensitive ( ) return sensitive ;

Punct_strea.& operator>> ( s trinq& s ) ;


operator bool ( ) ;
private :
istream& source ; //
istrinqstrea. buffer ; //
strinq whi te ; //
bool sensitive ; // ?
1;

, , -
istream , " "
, istringstream .
, , Punct_stream
: case_sensi tive ( ) ,
, , .
, Punct_stream

n i tes doq !

man
ites
doq

Punct_stream istream,
, source. ,
,
. Punct_stream,
cin, ,
452 11

, ,
. :
Punct_stream ps ( cin) ; // ps cin
ps . whi tespace ( " ; : . " ) ; // ,
11
ps . case_sensitive (false) ; 11

,
>>. .
, istream
line. ,
, ( ' ' ) .
istringstream buffer.
buffer >>.
.
, buffer,
, .
Punct_stream& Punct_strea. : : operator>> ( s trinq& s)
(
while ( ! (buffer>>s) ) 11
// buffer
if (buffer . bad ( ) 1 1 ! source . qood ( ) ) return * this ;
buffer . clear ( ) ;

strinq line ;
qetline ( source , line) ; 11 l i n e
1 1 so urce

11
for ( char& ch : line)
if ( is_whitespace ( ch) )
ch '= ' ; //
else if ( ! sensitive)
ch = tolower ( ch) ; //

buffer . str ( line) ; //

return * this ;

.
.
while ( ! (buffers ) )

buffer istringstream ,
buffer>>s s ,
w "; .
1 1 .7. 453

, buffer
. , buffer>>s
, . . ! (buffer>>s ) ,
buffer source. ,
buffer>>s ;
buffer .
while ( ! (buffer>>s ) ) { //
if (buffer . bad ( ) 1 1 ! source . qood ( ) ) return *this ;
buffer . clear ( ) ;

// buffer

buffer bad ( )
. :
buffer
. buffer,
" ", ,
- " " (eof ( ) ) buffer:
, buffer .
,
. ,
.
strinq line ;
qetline ( source , line) ; // l i n e
// so urce

//
for (char& ch : line)
if ( is_whitespace ( ch) )
'
ch ' ;
//
else if ( ! sensitive)
ch tolower ( ch) ; //

buffer . str ( line) ; 11

buffer,
. i s_whi tespace ()
Punct_stream, .
tolower ( ) - ,
.
(. 1 1 .6).
line istringstream.
buffer . s tr ( line) ;
: " buffer
istringstream line".
454 1 1

. ""
source getline ( ) .
,
! source . good ( ) .
. >> * this (
1 7. 1 0).
. . :
,
.
bool Punct_strea.. : : is_whitespace ( char )
(
for ( char w : white)
if ( c==w) return true ;
return false ;

. istringstream
(.
) , ,
.
.
Punct_strea.. : : operator bool ( )
(
return ! ( source . fail ( ) 1 1 source . bad ( ) ) && source . qood ( ) ;

istream
>>. .
while (ps>>s) ( / * . . . * / }

.
ps>>s. .
ps>>s Punct_stream,
Punct_stream l .
operator l ( ) Punct
,

stream. - operator l ( )
Punct_stream l . , true,
Punct_stream .
.
int 111&i n ( )
11
11 , ,
//

Punct_strea.. ps ( cin} ;
1 1 . 7. 455

ps . whitespace ( " ; : , . ? ! ( ) \ 11 { } <>/ &$@#% "' * 1 " ) ; // \ "


11 "
ps . case_sensitive ( false) ;

cout << " , cnoa\n" ;


vector<strinq> vs ;
for ( strinq word ; ps>>word ; )
vs . push_back ( word) ; //
sort (vs . beqin ( ) , vs . end ( ) ) ; 11
11
for ( int i=O ; i<vs . si ze ( ) ; ++i ) //
if ( i==O 1 1 vs [ i ] ! =vs [i-1] ) cout << vs [ i ] << ' \n ' ;

.
if ( i==O 1 1 vs [ i ] ! =vs [i-1 ] )

.
here are only two kinds of lanquaqes : lanquaqes that people complain
aout, and lanquaqes that people don ' t use .

:
aout
and
are
complain
don ' t
kind
lanquaqes
of
only
people
that
there
two
use

don ' t, dont?


whi tespace () .
: Punct_stream
istream, . ,
rdstate ( ) ,
eof ( ) , >>.
. ,
Punct_stream , istream.
Punct_i stream,
istream? ,
.
456 1 1

( - - ,
).
Punct_stream?
?
?
, ! !" , ! ,
?!" :
;
,
;
;
,, .
,
. .
. -

.
,
,
.

1 1 . 8.
- .
. ,
, . .
12 . 35.
12 , 35. ,
++
, -.
. ? .
?
, .
,
( , (Langer) Standard ++
IOStreams and Locales (Stroustrup) h ++ Program
ming Laguage). .
locale ( );
,,
.
1 1 .8. 457

;
iostream
streamuf. , iostream,
,
streamuf .
iostream iostream
, 38
h ++ Programmi.g Langua.ge .
++
- printf ( ) / scanf ( ) ,

. 27.6, . 1 0.2
(Kernlghan) (Ritchie) h Programming
Langua.ge, .
(
) -;
( -) .
1 1 1 .
- .
. .
1 2- 1 6 .

1 . Test_output . cpp.
irthyear .
2. irthyear .
.
3. .
4.
? , .
5. .
6. - ? ?
.
7. . 2
.
8. .
..
cin >> >>oct >> >> hex >> >> d ;
cout << << 1 \t ' << << ' \t ' << << 1 \t ' << d << ' \n ' ;
:
458 1 1

1234 1234 1234 1234

.
9. ,
1234567 . 8 9 : defaultfloat, - fixed
scientific.
? ?
1 0 . , , ,
.
, .

r
1 . - ?
2. << hex?
.
? ?
4. ,
.
5. ?
6. ,
.
7.
?
8. ?
9. , setprecision ( ) setw ( ) .
1 . ?
1 1 . "
": h, scientific, setprecision, showbase, setw?
1 2. .
1 3. .
.
1 4. ,
stringstream.
1 5. ?
1 6. ,
?
1 7. , , ?
1 8. isalnum () ?
1 1 .8. 459

defaultfloat
fixed
noshowbase
scientific
setprecision
showbase

1 . .
.
2. ,
, ,
. : getline ( ) .
3. . .
, Once upon time ! nc pn tm ! .
;
.
4. multi_inpu t . cpp,
,
,
;
.
:
43 () 67
0123 ( ) 83
65 ( 'l' ) 65

5. .
,
1 1 .6. ,
(, - , -
).
6. , .
( . ) , ( ; ) . ( , ) ,
(?), (-), ( ' ) .
, ( 11 ) .
. 11 - don ' t use the as-if rule . 11
11 don t use the as if rule 11
7. ,
don ' t do not, can ' t - cannot .. ;
460 1 1

( ,
"
do not use the as-if rule 11 );
.
8.
( .
1 1 .7). .
,
. .
9. - 1 1 .3.2 :
.
.
,
.
1 0. vector<string> spli t ( const string& s ) .
s,
.
1 1 . vector<string> split ( const string& s , const
s tring& w) , s,
. .
"" .
w.
1 2 . .
, asdfghjkl lkjhgfdsa. :
.
.
1 3. ( .
). . Norwegian Blue parrot
parrot Blue Norwegian. .
.
1 4 . .
(.
1 1 .6) .
1 5. . ,
. ,
, .
.
1 6. . .
,
.
, .
1 1 .8. 461

. ,
7 5 5 7 1 1 7 5 :

5
7 2
117

,
.

, ,
, . ,

, , -
.

f- -, .
/, 19() - 1t: .

- l .s"f.., (./,,., .... )f

(
,
) ,
,
, ,
. Line , Lines ,
Polygon , Axis Text , Shape .
Shape
,
.
. 1 3
, 1 4
-

1 - CUIA C alvln and Hob


bes. - . .
464 1 2

1
1 2. 1 . 1 2.7.2.
1 2.2. 1 2.7.3.
1 2.3. 1 2.7.4.
1 2.4. 1 2 .7.5.
r 1 2.7.6.
1 2.7.7.
1 2.7.8.
1 2.5.
1 2.7.9.
1 2.6. Shape
1 2 .7.10.
1 2.7. 1 2.8.
Shape
1 2.8. 1 .
1 2.7. 1 .
main

1 2. 1 . ?
-
(graphtcal user iterface - GUI)?
. ?
, ,
,
. , ? , -
, ,
, .
.
.
, ,

.
. .
,
,
,
. ( )
1 5.
11.
. ,
( )
. .
!
1 2.2. 465

11 .
,
. ,
, ,
. ,
, , ,
, ,
.
, 1 3 ,
.
- 1111 .


. -
.

, ()

,
.
-
.
, -
,
(
22),
,
,
- .
. 11 .
.
, ,
. ,
, , " ",
.

1 2 .2.
- ios tream
. ,
,
466 1 2

. ,
.
, rotf, leX, Word, HTML L (
). .
<hr>
<h2>

</h2>
:
<ul>
<li><></> , EPddd , . . . </li>
<li><></> , Eiddd , . . . </li>
<li><>n</> , ESddd , . . . </li>
</ul>
<> . . .
<>

HTML, (<h2> . . .
</h2>). (<ul> . . . </ul>) (<li> . . . </li>)
(<>). ,
. I -
, ,
,
, .
(, ,
), .

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

.
.
,
, . "" -
,
. , ,
" ( - "") ", " ",
" " () "
, ", ,
, .
1 2.3. 467



attach() draw()

" "

attach()

,
, ..
" ", ,

, . ,

.
.

1 2. .
- ,
. .
. , :
#include " Siple_window . h" 11
11
#include "Graph . h" //
//
int ma.in ( )
{
using namespace Graph_lib ; //
11 Graph_ l i b
Point tl { 100 , 100 } ; //

Siple_window win { tl , 600 , 400 , "Canvas" } ; //

Polygon poly ; 11 ()

poly . add ( Point { 00 , 200 } ) ; //


poly . add ( Point { S0 , 100 } ) ; 11
poly . add ( Point { 400 , 200 } ) ; 11

poly . set_color ( Color : : red) ; // poly


468 1 2

win . attach (poly) ; 11 poly

win . wait_for_button ( ) ; //
11

, .

, .

.
#include " Simple_window . h" 11
//
#include "Graph . h" 11
11

main ( ) ,
Graph_lib.
usinq namespace Graph_lib ; //
11 Graph_ l i b

.
.
1 2.3. 469

Point tl ( l00 , 100 } ; 11

.
Simple_window win ( tl , 600 , 400 , "Canvas " } ; //

Simple_window,
Graph_lib. Simple_window
win: . win Simple_window.
-

win tl,
, 600
400. , , , .
, ,
. Canvas
. ,
Canvas .
.
Polyqon poly ; 11 ()

poly . add ( Point ( 00 , 200 } ) ; 11


poly . add ( Point ( S0 , 100 } ) ; //
poly . add ( Point ( 400 , 200 } ) ; //

poly, .
Polygon
: ,
. , .
,
.
,
.
poly . set_color ( Color : : red) ; // po ly

poly win.
win . attach (poly) ; 11 poly

, .
(, Simple_window) (
poly), (Color : : red)
win, .
.
win . wait_for_button ( ) ; 11
11


, .
470 1 2

wai t_for_utton ( ) ,
, Next Simple_window.
,
. ,
. .
.

, .
Next? Simple_window. 1 6
Simple_window "" Window,
, , ,
.
Next
,
(" ").
,
, .
Microsoft Windows,
.
, (
). ,
.
, .
( - Canvas).
1 2. 4. 471

1 2.4.



. -

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

, ,


.

, , FLTK (Fast
Light Tool it, "full tick") www . fl tk .
org. .
(Windows, Unix, , Linux .).
,
.
.
, ,
. ,
600
++,
FLTK 370 .
- www . fl tk . org, .
.
, 1 2- 1 6,
.
, .
FLTK. . ,
( ) .
" "
.
472 1 2

/GUI
( - FLTK)


(Windows, Linux)



.
,
,
, ( ,
1 6).

1 2. 5 .
- .
. - .
.

.
. .

. .
(0 ,.""'---- (200,0) --

(50,50)

(0, 1 00) (200, 1 00)


!
1 2.6. Shape 473

, ,
. ,
( , )
, - , .
:
1 024768, 1 280 1 024, 1 450 1 050 1 600 1 200.

,
. , . ,
. ,

Simple_window win ( tl , 600 , 400 , " Canvas " } ;

, ,
600 , - 400,
599 399 . ,
, (canvas). 600400
"" , .. , ,
; ,
.

1 2.6. Shape

.

Wlndow [ Llne_style ) Color )

, , ,
. , . ,
Polygon , Shape;
, Polygon Shape.
:
Simple_window, Window;
Shape, Text, Polygon, Line, Lines, Rectangle, Function .. ;
474 12

Color, Line_style, Point;


Axis .
( 1 6)
:
Button, In_box, enu ..

,
Spline, Grid, Block_chart, Pie_chart ..


.

1 2 . 7 . Shape

: Simple_window, Window, Shape , Text,
Polygon, Line, Lines, Rectangle, Color, Line_style, Point, Axis.
- ,
, .
.
,
.
, ,
, . ,
.

1 2. 7 . 1 . main
,
.
#include "Window . h " 11
#include " Graph . h "

#include " Siple_window . h" // Nex t
#include "Graph . h "

, , , Window . h ,
. Graph . h - ,
( ) .
Graph_lib.
using namespace,
Graph_lib .
1 2. 7 . Shape 475

using namespace Graph_lib ;

, main ( ) ,
( ), .
int main ( )
try
{
// . . . . . .

catch (exception& )
11
return 1 ;

catch ( . . . )
11
return 2 ;

main ( ) ,
exception. , ,
std_lib_facili ties . h

<s tdexcept>.

1 2. 7 .2.
(. 5,
5.6.3), main ( ) :
Point tl { l00 , 100 } ; //

Simple_window win { tl , 600 , 400 , "Canvas " } ;


// t l
11 : 600400
// : Ca n va s
win . wait_for_button ( ) ; //

Simple window, . .
Next, . ,
Simple_window
Simple_window . h, Window . h. ,
:
Point{ 1 0 0 , 100 } . ,
. , Point ,
-


(, ). :
Simple_window win { Point { l00 , 100 } , 600 , 400 , "Canvas " } ;
476 1 2

( 1 00, 1 00) ,
. 600 - , 400 -
, "Canvas" - , .
,

. win . wait_for_button ( ) .
.

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

. , ,
Microsoft ++,
(, Borland GNU).
1 2. 7 . Shape 477


, .

( )
. ,
.

1 2. 7 ..
,
- . ?
, - ,
- , .
, , . ,
, . ,

, :
,
. . .
Axis xa {Axis : : x , Point (20 , 300) , 280 , 10 , " axis " ) ; // Axi s
11 Ax i s - Shape
11 Ax i s : : x
11 - (2 0 , 3 0 0 )
// - 2 8 0
11 1 0
// " " -
win . attach () ; // wi n
478 1 2

win . set_lael ( " Canvas i 2 " ) ; 11


win . wait_for_button ( ) ; 11 !

: Axis ,
.

, Axi s : : .
, " axis". ,
, . ,
- .
-
, "- "
, , _max-bottom_
margin, " ", 300 (.
4.3. 1 1 5. 6.2).
,
" Canvas i2 " - set label ( )
Window.
.
Axis ya {Axis : : y , Point { 20 , 300 ) , 280 , 10 , " axis " } ;
ya . set_color ( Color : : cyan) ; 11
ya . lael . set_color ( Color : : dark_red) ; 11
win . attach (ya) ;
win . set_lael ( "Canvas i " ) ;
win . wait_for_button ( ) ; 11

, ,
, -.
1 2. 7 . Shape 479


. ,
.
. ,
, .

1 2. 7 .4.
? ,
. ,
, .
Function sine { sin , 0 , 100 , Point { 2 0 , 150 } , 1000 , 50 , 50 } ;
11
11 sin () [ 0 , 1 0 0 ) ( 0 , 0) (20, 1 5 0 ) ,
11 1 0 0 0 ;
11 5 0 )f(

win . attach ( sine} ;


win . set_la.el ( "Canvas # 4 " ) ;
win . wait_for_button ( ) ;

Function sine ,
sin () .
1 5.3. ,
(
Point) ,
, .
480 1 2

Canvas #4 f:Jrg:ll)
.. 1

. .
,
.

1 2. 7 .5.
.
1 5.
, .
,
(, )
. Polygon
, .
, ,
.
sine . set_color ( Color : : ue) ; 11

Polyqon poly ; 11 Polygon - Shape


poly . add ( Point ( 00 , 200 } ) ; 11
poly . add ( Point ( S0 , 100 } ) ;
poly . add ( Point ( 400 , 200 } ) ;

poly . set_color (Color : : red) ;


poly . set_style (Line_style : : dash) ;
win . attach (poly) ;
win . set_lael ( "Canvas lt5 " ) ;
win . wait_for_button ( ) ;
1 2 .7 . Shape 481

(sine) .
, . ,
1 2.3,
. .
Polygon . ,
, .. ( - 1 3. 5) .
.

1 2. 7 . 6.
- , -
- .
( ) -
. .
( ,
). .
. ,
.

.
. ,
Rectangle,
Polygon. Rectangle
. .
Rectanqle r { Point { 200 , 200 } , 100 , 50 } ; // , ,
win . attach ( r) ;
482 12

win . set_lael ( "Canvas # 6 11 ) ;


win . wait_for_button ( ) ;

Canvas #6 l/Qlg]
s

x axls

, , , ,
, Rectangle
. Closed_polyline,
Rectangle (
Open_polyline, ).
Closed_yolyline poly_rect ;
poly_rect . add ( Point { l00 , 50 } ) ;
poly_rect . add ( Point { 200 , 50 } ) ;
poly_rect . add ( Point { 200 , 100 } ) ;
poly_rect . add ( Point { l00 , 100 } ) ;
win . attach (poly_rect) ;

::u poly_rect
. poly_rect
Rectangle "" .
, .
poly_rect . add ( Point ( 50 , 75 ) ) ;

.
1 2 . 7 . Shape 483

, Rectangle
, ,
, . -
, , Rectangle
.
484 1 2

1 2. 7 . 7 .
,
. .
r . set_fill_color ( Color : : yellow) ; 11
poly . set_style ( Line_style (Line_style : : dash , 4 ) ) ;
poly_rect . set_style (Line_style (Line_style : : dash , 2 ) ) ;
poly_rect . set_fill_color (Color : : green) ;
win . set_lael ( " Canvas # 7 " ) ;
win . wait_for_button ( ) ;

,
(poly) , (
). poly_
rect ( ).

poly_rect,
, .
( 1 3.9).
( ).

1 2. 7 .8.
,
,
- -
.
1 2. 7 . Shape 485

, , , ,
,
Text.
Text t { Point { lS0 , 150 } , " Hello , qraphical world ! " } ;
win . attach ( t) ;
win . set_lael ( "Canvas 118 " ) ;
win . wait_for_button ( ) ;

, ,
.
: , ,
".
.
,
.
, : Axis (.
1 2. 7.3) Text. ,
.
t . set_font ( Font : : ties_bold) ;
t . set_font_size ( 20 ) ;
win . set_lael ( "Canvas 119 " ) ;
win . wait_for_button ( ) ;

"Hello , graphical world ! " 20


Times.
486 1 2

1 2. 7 . 9 .
.
Ia.qe i i { Point { l00 , 50 } , " imaqe . jpq" } ; // 4 0 0 2 1 2
win . attach ( ii) ; // jpg
win . set_lael ( "Canvas #10" ) ;
win . wait_for_button ( ) ;

image . jpg - ,
.
1 2. 7. Shape 487


. ,
.
ii . move ( l00 , 200) ;
win . set_lael ( " Canvas #11 " ) ;
win . wait_for_button ( ) ;

, , ,
. , ,
.

1 2. 7 . 1 .

Circle c { Point { l00 , 20 0 } , 50 } ;
Ellipse e { Point ( l00 , 200 ) , 75 , 25 } ;
e . set_color ( Color : : dark_red) ;
ark m{ Point ( l00 , 200 } , ' x ' } ;

ostrinqstream oss ;
oss << " screen size : " << _ ( ) << " * " << _max ( )
<< " ; window size : " << win . x_max ( ) << " * " << win . y_max ( ) ;
Text sizes ( Point ( l00 , 20 } , oss . str ( ) } ;

Imaqe cal ( Point ( 225 , 22 5 } , " snow_cpp . qif" } ; // 320 * 2 4 0 , gi f


cal . set_mak ( Point ( 40 , 40 } , 200 , 150 ) ; 11

win . attach (c) ;


488 1 2

win . attach (m) ;


win . attach (e) ;

win . attach ( sizes) ;


win . attach ( cal) ;
win . set_lael ( "Canvas #12 " ) ;
win . wait_for_button ( ) ;

, ?
?

, ,
. ,
, : . -
, ,
, ostringstrea.m (.
1 1 .4) .

1 2 . 8 .
,
. , Shape
, .
,
, , .
main ( ) ,
, ,
1 2.8. 489

, FLTK
( ).
. . .
(main ( ) ..).
(Window, Shape, Polygon ..).
FLTK.
++.
, .
.

.

Point.h:

struct Point ( . };
FLTK

// :
class Window (".};
Graph.h:

// :
struct Shape ( ". } ;

// GUI:
window.cpp: struct ln_box ( ". } ;

Graph.cpp: GUl.cpp:
Simple_window.h:
Ko GUI
// :
class Simple_window ( " . } ;

chapter 1 2.:

#include "Graph.h"
#include "Simple_window.h"
int main() ( ". }

,
.
490 1 2

1 2.8. 1 .

.

Point . h Window . cpp
Window . h Graph . cpp
Simple_window . h GUI . cpp
Graph . h

GUI . h
1 6
.

-
"Hello , World ! -
"

.
1 . , Simple_window
600400 window, ,
. ,
FLT. ,
Graph . h Simple_Window . h,
Graph . Window . .
2. 1 2.7,
.
3. , (,
, )
.


1 . ?
2. ?
3. ?
4. ?
5.
( )?
6.
?
1 2.8. 491

7. ?
8. ?
9. ?
1 . ?
1 1 . ,
?
1 2. ,
.
1 3. ?
1 4. ,
?
1 5. ?
1 6.
.
1 7 . , Window.
. ?
1 8. , Shape,
. ?

GUI GUI
HTML
JPEG
XML
FLTK

;
Simple_window.
1 . Rectangle
Polygon. Polygon ,
Rectangle - .
2. Rectangle 1 0030
"Howdy!"
3. 1 50 .
. .
492 1 2

4. - ,
.
5.
,
, - .
6. , ,
? , ,
? , .
7. , : ,
. ,
.
8. . ,
.
9. .
.
1 0 . 1 2.8.
1 1 . , .

, .
, .. :
N- (N+ l ) -. :
<cmath> (
24.8, . 9.2)
1 2 . - ,

rn ,n > .

u,
, . ,
, . .
, , m, n N . N
, , , m n.
(
" "). N
( ,

N-1 , .. ).
1 3. .
.
1 2.8. 493


.
, -
, - . -
, .
, ,
Circle Polygon.
, , ,
. ,
(
).
,
. , -

.
: ,
, .

51, NA, ,

NA,
tJ

1 2 ,

.
.
,
,
Point, Color , Polygon Openyolyline ,
.
,
, .
496 1 3

1 3. 1 . 1 3 . 10.
1 3.2. Point Line
1 3.3. Lines 1 3. 1 1 . Text
1 3.4. Color 1 3 . 1 2. Circle
1 3.5. Line_style 1 3. 1 3. Ellipse
1 3.6. Openyolyline 1 3 . 1 4. arkedyolyline
1 3.7. Closedyolyline 1 3 . 1 5 . arks
1 3.8. Poly9on 1 3. 1 6. ark
1 3.9. Rectan9le 1 3. 1 7. Ima9e

1 . 1 .

. ""
, . .
.
.
. !

.
J.l. . .
. .
- .

.
,
. ,
.
.

. 1 4
.

. ,
.
++.

.
1 3. 1 . 497


Color ,
Line_style
Point
Window
Line , ,
Point
Open_polyline ,
Point
Closed_polyline Open_polyline, ,

Polyqon Closed_polyline,

Text
Lines , Point
Rectanqle - ,

Circle ,
Ellipse ,
Function ,
Axis
ark , (, )
arks , (, )
arked_polyline Open_polyline ,
Imaqe

Function Axis 1 5. 1 6
.


Window ,
Simple_window Next
Button , ,

In , ,

Out , ,

Menu Button

.
498 1


Point . h Point
Graph . h
Window . h Window
Simple_window . h Simple window
_

GUI . h Button GUI


Graph . cpp Graph . h
Window . ccp Window . h
GUI . cpp GUI . h

,
,
Shape Widget.

Shape Widget
Vector ref ,

, , .
, ,
- (

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

1 3 .2. Point Line



. ,
. ,
-
1 .2. Point Line 499

(, ). 1 2.5,
( ) max ( ) ( );
_

( ) _max ( ) (
).
Point . h, Point
-

int ( ):
struct Point {
int , ;
};

bool operator== ( Point , Point ) { return . == . && . =- . ;


bool operator ! = ( Point , Point ) { return ! ( --) ; }

Graph . h Shape,
1 4, Line.
struct Line : Shape { // Line - Shape ,
11
Line ( Point pl , Point 2) ; 11 Line
11 Poi n t
1;

Line Shape.
-

" : Shape". Shape (base class) Line.


Shape ,
Line. ,
Line Openyolyline. , (. 1 4).
Line Point.
"" ( #include ,
1 2 . 3),
.
//
constexpr Point { 100 , 100 }

Simple_window winl { x , 600 , 400 , " wo lines " } ;


Line horizontal { x , Point { 200 , 100 } } ; //
Line vertical { Point { 150 , 50 } , //
Point { 150 , 150 } } ;
winl . attach (horizontal ) ; //
winl . attach (vertical) ;
winl . wait_for_button ( ) ; 11

, .
, ,
, Line .
, ,
Line vertical { Point { 150 , 50 } , Point { 150 , 150 } } ;
500 1

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


, ,
, . Line
.
Line : : Line ( Point pl , Point 2) //
{
add (pl) ; 11 pl
add (p2 ) ; 11 2

, . ?
Line ? Shape.
1 4 , Shape ,
, , , ,
add ( ) , Shape Point.
, Line
. ".
,
.
1 3.3. Lines 501

Simple_window
t tach ( ) . "",
,
Shape.

1 .. Lines
, . ,
, ,
, , , , ,
, ..
" " Lines .
struct Lines : Shape { 11
Lines ( ) { } 11
11 :
Lines (initializer_list<Point> lst) ;
void draw_lines ( ) const;
void add ( Point pl , Point 2 ) ; 11 ,
}; 11

Lines ,
Point. .
1 3. 2
, :
Lines ;
x . add ( Point { l00 , 100 } , Point { 200 , 10 0 } ) ; 11 :
11
x . add ( Point { l50 , 50 } , Point { l50 , 150 } ) ; 11 :
11

(
), Line.
, , -
.
Line
Lines
, . Lines,
,
. .
, Lines, .
, ,
Line, .
.
,
502 1

. ,
Lines grid.

int size = win . x_max ( ) ; / /


int y_size = win . y_max ( ) ;
int x_qrid = 80 ;
int y_qrid = 40 ;
Lines grid;
for ( int x=x_qrid ; x<x_size ; x+=x_grid)
grid. add ( Point { x , 0 } , Point { x , y_size } ) ; 11
for ( int = y_grid; y<y_size ; y+=y_grid)
grid. add ( Point{ O ,y} , Point { x_size , y } } ; 11

,
_max ( ) _max ( ) . ,
. , .
.
, .
.
Lines . -
Lines? Lines .
add () , ,
, .
void Lines : : add { Point pl , Point 2)
{
Shape : : add (pl) ;
Shape : : add (2 ) ;
1 .. Lines 503

Shape : : ,
add (pl ) (
) add ( ) Lines,
Shape.
draw lines ( ) ,
add ( ) .
void Lines : : draw_lines ( ) const
{
if (color ( ) . visibility ( ) )
for (int i=l ; i<numer_of_yoints ( ) ; i+=2)
fl_line (point ( i-1) . x , point ( i-1) . ,
point ( i ) . x , point ( i ) . ) ;

. Lines : : draw_lines ( )
( 1 ) ,
fl_line ( ) .
(visility) Color ( 1 3.4),
-

, , ,
.
1 4, draw_lines ( ) "
". , ,
add ( ) Lines .
Shape numer_of__points ( ) point ( ) (.
1 4.2), .
Shape. -
504 13

draw_lines () const (. 9 . 7 . 4),


.
Lines
( ):
, add () .
, .
, initializer_list
, .
( 1 8. 2)
Lines, , 1 , 2, . . , "

:
Lines = {
{ Point { l00 , 100 } , Point { 200 , 100 } } , //
{ Point { 150 , 50 } , Point { 150 , 150 } } //
};
:
Lines = {
( ( 100 , 100 } , {200 , 100 } } , //
( ( 150 , 50 } , ( 150 , 150 } } //
};
:
void Lines : : Lina ( initializer_lit<pair<Point , Point>> lst)
{
for ( auto : lt) add (p . first , p . econd) ;

auto pair
<Point , Point>. first second
. initializer_l i s t pair
(. .6.4 .6.3 ).

1 .4. Color
Color , .
-

:
grid . set_color (Color : : red) ;

, grid,
. ,
:
Color
.
13.4. Color 505

struct Color {
enWll Color_type
red =FL_RED ,
u =FL_BLUE ,
green =FL_GREEN ,
yellow =FL_YELLOW ,
white =FL_WIE ,
k =FL_BLACK ,
magenta =FL_AGENA,
cyan =FL_CYAN ,
dark red =FL_DAR_RED ,
dark_green =FL_DAR_GREEN ,
dark_yellow =FL_DAR_YELLOW,
dark u =FL_DAR_BLUE ,
dark_agenta=FL_DAR_AGENA,
dark_cyan =FL DAR CYAN
};

enWll Transparency { invisile = , visie=255 } ;

Color (Color_type } : c { Fl_Color ( cc} } , v{visile} { }


Color (Color_type , Transparency vv)
: c { Fl_Color (cc) ) , v { vv } { }
Color (int ) : c { Fl_Color ( cc) } , v{visile} { }
Color (Transparency vv) : c { Fl_Color ( ) } , v { vv } { } //
11

int as_int ( ) const { return ; }

char visibility ( ) const { return v ; }


void set_visibility (Transparency vv) { v=vv ; }
506 1

private :
char v; //
Fl Color ;
);

Color .
Fl_Color FLT.
Fl_Color Color_type.
.
( ) .
.
. Color : : dark_lue.
"" ,
(
0-255; , Color ( 99)
- ).
1 3.9.
RGB (Red, Green , Blue - ,
, ). .
. .
"RGB color" ,
, http : //en . wikipedia . org/wiki/RGB_color_model www .
rapidtaes . com/we/color/RGB_Color . htm. (. . 1 3 1 4.)
Color,
Color_type,
int. .
,
, , Color
,
. ,
.
Fl_Color, FLT.
.
RGB ( ).
as int ( ) .
, as_int ( ) -,
Color, .
v,
Color : : visie Color : : invisie, .
: " ". ,
, .
1 .5. Line_style 507

1 3.5. Line style


_
,
. . - ,
. Line_style :
grid. set_style (Line_style : : dot) ;

, grid,
, .

, .
() , .
Line_style :
struct Line_style {
enum Line_style_type {
solid =FL_SOLID , 11 ------

dash =FL_DASH , // - - - -
dot =FL_DOT , 11
dashdot =FL_DASDOT , 11 -
dashdotdot=FL_DASDODOT , 11 - . . - . .
) ;

Line_style (Line_style_type ss) : s { s s } , w{ O } { }


Line_style (Line_style_type lst, int ww) : s { lst} , w{ww} { }
Line_style (int ss) : s { s s } , w{ O } { }
int width ( ) const { return w; }
int style ( ) const { return s ; }
private :
int s ;
508 1

int w;
};
,
Line_style, ,
Color. ,
F int.
?
. FLTK
Fl_linestyle,
.
int , , .
, ;
, ( ,
).
, .
- ,
, ,
.
Line_style u" :
(, i ) (
). .
. ,
:
qrid . set_style (Line_style (Line_style : : dash , 2 ) ) ;
.


t 1 ' 1 1
1 1
1 1 1 1 1 t
1 f 1 1 t

'1 1 1
---------}-------}------------------}--------}--------- -------------


1 t 1
1 1 !
1 ' ' 1 1 1
1 1 1
--------- -------- --------- --------- ------ -+- ---------r -------- - -- -

1
t 1
1 -1 -1 1
1
' 1 1 1 \"
1 1

:
! 1 1,
-------- -------- --------- -------- --------- -------- --------- ---

1 1 : : : l
1 1 1
-------------------------------------------------------------}.----

4-1
11
1 1 1
1 '
1 1 1 1 1 1
1 1 1 '
--------- r - ---- -------- ---------} ---------------------
--"-----

1 t 1 1
i 1 1 1 t 1
1 1
j '( j 1 1 1 1

1
-------------------------------------------------------------------
1 1 J f 1 J
1


1
'

1 1
1 1 ' t 1 J
-------- --------- ------- -- ------- ----- ------------- --------- --

1
1
t1
J
1
1
1
1 f
' 1 1
J
1 '
1

1
---------}-----------------}---------}-----------------}------------
1
t
1
1
1 1
1

1 '
t

1
1 1 t 1 1 1 1

:
-----------------1- --"----- -------- --------- -------- --------} --

! i : : ; 1
1 1 1
1
1 1 1
1 1 1 1 1 1
1 3.6. Open_polyline 509

, .
.
. Lines, Open_polyline
Polygon.
, Line.
.
horizontal . set_color (Color : : red) ;
vertical . set_color (Color : : green) ;
, .

!
f 1

1 3.6. Open_Polyline
Open_polyline ,
, .
poly "", polylie -
, . .
Open_yolyline opl = {
{ 100 , 100 } , { 150 , 200 } , { 250 , 250 } , { 300 , 200 }
};
, ,
:
. Open_polyline ,
-

"Connect the Dots" (" ").


Open_polyline .
510 1 3

struct Open_yolyline : Shape { //


usinq Shape : : Shape ; // Shape ( . 1 6)
void add (Point ) { Shape : : add (p) ; )
);
-. . ,
, Shape.
add ( ) Openyolyline
add ( ) Shape (.. Shape : : add ( ) ).
draw_lines ( ) . Shape
.
.
using Shape : : Shape using-.
, Openyolyline .
Shape. Shape
( 9.7.3) ( 1 8.2).
using-
Openyolyline.
Lines,
add ( ) .

1 3.7. Closed_Polyline
Closedyolyline Openyolyline,
, . ,
Closedyolyline ,
Openyolyline 1 3.6.
1 3.7. Closed_polyline 511

Closed_yolyline cpl =
{ 100 , 100 } , { 150 , 200 } , { 250 , 250 } , { 300 , 200 }
};

, ,
1 3.6, .

Closed_Polyline .
struct Closed_yolyline
: Open_yolyline { //
using Open_yolyline : : Open_yolyline ; //
11 Open_polyline ( . 1 6)
void draw_lines ( ) const ;
};

void Closed_yolyline : : draw_lines ( ) const


{
Open_yolyline : : draw_lines ( ) ; 11 ,
// -
if (2<numer_of_yoints ( ) & & color ( ) . visibility ( ) )
fl_line (point (numer_of_yoints ( ) -1) .,
point (numer_of_yoints ( ) -1) . ,
point ( O ) .,
point ( O ) .) ;

using- ( . 1 6) , Closed_Poly
line . Open_Polyline. Closed_Poly
line draw_lines ( ) ,
, .
512 1 1

, ,
Closed_polyline Open _yolyline.
" " ("programming
difference"). ,
(Closed_yolyline) (Open_yolyline).
, ?
fl_line () FLTK.
int, .
. , ,
, FLTK
.
fl line () ,
.
FLTK
, .

1 .8. Polygon
Polygon Closed_yolyline.
, Polygon
. , Closed_yolyline, ,
, ,
.
cpl . add ( Point ( l00 , 250) ) ;
.
1 3.8. Polygon 513

Closed_
polyline . Polygon ,
Closed_polyline,
? .
Polygon - Closed_polyline,
. ,
, Polygon - Closed_polyline,
Point, ,
Polygon.
Polygon :
struct Polyqon : Closed_yolyline { //
//
usinq Closed_yolyline : : Closed_yolyline ; //
!/ Closed_polyl i n e
void add (Point ) ;
void draw_lines ( ) const ;
};

void Polyqon : : add (Point )


1
/! ,
11 ( )
Closed_yolyline : : add(p) ;

draw_lines ( )
Closed_polyline, .
, add ( ) .
,
N2, - Polygon, N .
N (N - 1)/2 intersect ( ) . ,

, Polygon
. ,
Polygon, 24 ,
24 (24 - 1)/2 = 276 intersect ( ) . ,
, 2000
, 2 .
,
.

:
Polyqon poly =
{ 100 , 100 } { 150 , 200 } , { 250 , 250 } , { 300 , 200 }
/

1;
51 4 1

, : Polyqon,
( ) : Closed_Polyline.

, : Polyqon
. .
. Polyqon : : add () .
.
.
.
: Polyqon ,
- " " - .
. ,
,
: Polyqon. add ()
,
,
: , .

1 3.9. Rectangle
.
(
, , , ,
),
( , ).
,
1 .9. Rectangle 51 5


, . .
struct Rectanqle : Shape {
Rectanqle (Point :, int ww, int hh);
Rectanqle (Point , Point ) ;
void draw_lines ( ) const;

int heiqht ( ) const return h ;


int width ( ) const return w ;
private :
int h ; //
int w; //
};

(
) , .
i :
Rectanqle : : Rectanqle ( Point :, int ww, int hh)
: w{ww} , h{hh )

if (h<=O 1 1 w<=O)
error ( "Ollla : : " ) ;
add(ry) ;

Rectanqle : : Rectanqle ( Point , Point )


: w{ y . x-x . x} , h { y . y-x . y }

i f (h<=O 1 1 w<=O)
error ( "Ollla : 1111) ;
add (x) ;

h
w ( ; . 9.4.4)
Shape (
add ( ) ). ,
- .
,
,
, .
, , ,
, . Polygon
Circle. " " - ..
-
, .
set_fill_color ( )
516 1

( Shape
).
Rectanqle rectOO { Point { lS0 , 10 0 } , 200 , 100 } ;
Rectanqle rectll { Point { 50 , 50 } , Point { 250 , 150 } } ;
Rectanqle rectl2 { Point { S0 , 150 } , Point { 250 , 250 } } ; // rec t l 1
Rectanqle rect2 l { Point{ 250 , 50 } , 200 , 100 } ; 11 rec tl 1
Rectanqle rect22 { Point ( 250 , 150 } , 200 , 100 } ; 11 rec t21

rectOO . set_fill_color (Color : : yellow) ;


rectll . set_fill_color ( Color : : ue) ;
rectl2 . set_fill_color (Color : : red) ;
rect21 . set_fill_color (Color : : qreen) ;
.

.
; rectOO .
(. 1 4. 2.3).
.
rectll . move ( 400 , 0 ) ; // rect21
rectll . set_fill_color (Color : : white) ;
winl2 . set_lael ( "rectanqles 2 " ) ;

.
,
rectl l . , , ""; .
.
1 .9. Rectangle 51 7
-

rectangles 2 (Q)

, :
. , :
. . :
Window ( .3)
( Window : : put_on_top ( ) ). ,

win12 . put_on_top (rect00) ;
win12 . set_lael ( "rectanqles 3 " ) ;
.
518 1 111

, .
. . ,
.
rectOO . set_color (Color : : invisile ) ;
rectll . set_color ( Color : : invisie) ;
rect12 . set_color (Color : : invisile ) ;
rect21 . set_color (Color : : invisile) ;
rect22 . set_color (Color : : invisile ) ;
.

rect22 .
invisie. .
-
, - - draw_lines ( ) Rectangle
.
void Rectanqle : : draw_lines ( ) const
{
if (fill_color ( ) . visibility ( ) ) { //
fl_color ( fill_color ( ) . as_int ( ) ) ;
fl_rectf (point ( O ) . x , point ( O ) . y , w , h) ;

if (color ( ) . visibility ( ) ) { //
fl_color ( color ( ) . as_int ( ) ) ;
fl_rect (point ( O ) . x , point ( O ) . y , w, h) ;
1 3.1 D. 519

. FLTK
(fl_rectf ( ) ) (fl_rect ( ) )
. .

(
) .

1 .1 .
.
. .
,
256 , ; .
256 1 6 1 6.

256 ,
. , ""
(.) , -

(i,J1. .
.
vector<Rectangle>. , . ,
(),
.
1 4., :
, :
teplate<class > class Vector ref
puic:
520 1

11
void push_back (T&) ; 11
void push_back (T*) ; //

& operator [ ] ( int i) ; 11 :


const & operator [ ] ( int i ) const ; //

int size ( ) const;


};
vector
.
Vector_ref<Rectanqle> rect;
Rectanqle x{ Point{ l00 , 200 } , Point{ 200 , 300 } } ;

11
rect . push_back (x) ;

11
rect . push_back (new Rectanqle { Point { 50 , 60 } , Point { 80 , 90 } } ) ;

//
for ( int i=O ; i<rect . size ( ) ; ++i) rect [ i ] . move ( l0 , 10) ;

new 1 7, Vector_ref
. ,
.
new ( Rectangle)
( { Point{ 50 , 60 } , Point{ 80 , 90} } ) .
,
.
Rectangle Vector_ref
. ,
, 256 .
Vector_ref<Rectanqle> vr ;

for (int i = ; i<l ; ++i )


for ( int j = ; j<l ; ++j )
vr . push_back (new Rectanqle { Point { i*20 , j *20 } , 20 , 20 } ) ;
vr [vr . size ( ) -1 ) . set_fill_color (Color { i *l+j } ) ;
win20 . attach (vr [vr . si ze ( ) -1 ] ) ;

Vector_ref. 256
Rectangle, Window
1 6 1 6. Rectangle , 1, 2, 3, 4
..
.
1 .1 1 . Text 521

1 . 1 1 . Text
, .
. "" Closed_yolyline
1 3.8
Text t ( Point (200 , 200) ," closed polyline that isn ' t polyqon" ) ;
t . set_color (Color : : u) ;
. .
522 1 3 11

Text , ,
Point.
. , ,
.
;
, . string,
Text (.
1 2. 7 . 7 1 2. 7.8) (. 1 1 .4).
Text .
struct Text : Shape {
11
Text (Point , const strinq& s )
la{ s } ,
{ add (x) ;

void draw_lines ( ) const ;


void set_lael (const strinq& s ) { l s; }
strinq lael ( ) const { return l ; }

void set_font (Font f) { fnt = f ; }


Font font ( ) const { return fnt ; }

void set_font_size ( int s) { fnt_sz = s ; }


int font_size ( ) const { return fnt_sz ;
private :
strinq ls; //
Font fnt { fl_font ( ) } ;
int fnt sz { (fl_size ( ) <14 ) ? 14 : fl_size ( ) } ;
};
, 1 4
, FLTK ,
. ,
.
FLTK,

,
.
, ,
.
Text - draw_lines ( ) ,
, .
void Text : : draw_lines () const
{
fl_draw ( la . c_str ( ) , point ( O } . x , point ( O ) .) ;
1 3.1 2. Circle 523

, r,
(. Openyolyline Circle).
set_color ( ) . ,
. - color ( ) .
. .
.
class Font { //
puic :
enum Font_type {
helvetica =FL_HELVETICA ,
helvetica bold =FL_HELVETICA_BOLD,
helvetica_italic =FL_HELVETICA_ITALIC ,
helvetica_old_italic=FL_HELVETICA_BOLD_ITALIC ,
courier =FL_COURIER,
courier bold =FL_COURIER_OLD,
courier italic =FL_COURIER_ITALIC ,
courier bold italic =FL_COURIER_BOLD_ITALIC ,
tilD8S =FL_TIES ,
times bold =FL_TIES_BOLD ,
ti&s i talic =FL_TIES_ITALIC ,
times bold italic =FL_TIES_BOLD_ITALIC ,
symol =FL_SYOL ,
screen =FL_SCREEN,
screen bold =FL_SCREEN_BOLD ,
zapf_dingbats =FL ZAPF DINGATS
};

Font (Font_type ff) : f { ff } { }


Font (int ff) : f { ff } { }

int as_int ( ) const { return f ; }


private :
int f ;
};
Font
Color (. 1 3. 4) Line style (. 1 3. 5).

1 . 1 2. Ci rcle
. .
, Circle Ellipse. Circle
.
struct Circle : Shape {
Circle (Point , int rr) ; //

void draw_lines ( ) const ;


524 1 3

Point center ( ) const ;


int radius ( ) const { return r ; }
void set_radius (int rr)
{ //
set_point ( O , Point { center ( ) . x-rr , center ( ) . y-rr} ) ;
r=rr ;

private :
int r ;
};
Circle :
Circle cl { Point { l00 , 200 } , 50 } ;
Circle c2 { Point{ 150 , 200 } , 100 } ;
Circle c3 { Point{ 200 , 20 0 } , 150 } ;

,
.

Circle ,
. ,
. .
. FLTK
. ,
(. )
. .
Circle : : Circle ( Point , int rr) //
: r (rr)
1 .1 . Ellipse 525

add (Point { p . x-r , p . y-r } } ; //

Point Circle : : center ( ) const

return {point ( O ) . x+r , point ( O ) . y+r } ;

void Circle : : draw_lines ( ) const

if (color ( ) . visibility ( ) )
fl_arc (point ( O ) . x , point ( O ) . y , r+r , r+r , 0 , 360) ;

fl arc () . _

. .
- ,
,
. ,
360 , fl arc () _

( ): . . 1 .

1 3. 1 3. Ellipse
,
. . . ,
.
.
struct Ellipse : Shape {
11 ,
Ellipse (Point , int w, int h) ;

void draw_lines ( ) const;

Point center ( ) const;


Point focusl ( ) const;
Point focus2 ( ) const;

void set_major (int ww)


{ //
set_Point ( O , Point{ center ( ) . x-ww , center ( ) . y-h } ;
w=ww ;

int major ( ) const { return w; }

void set_inor (int hh}


{ //
set_Point ( O , Point ( center ( ) . x-w , center ( ) . y-hh } ) ;
526 1

h=hh ;

int llli. nor ( ) const { return h ; }


private :
int w;
int h;
1;

Ellipse :
Ellipse el { Point { 200 , 200 } , 50 , 50 } ;
Ellipse e2 { Point { 200 , 200 } , 100 , 50 } ;
Ellipse e3 { Point { 200 , 200 } , 100 , 150 } ;

.

E l l ipse, maj or ( ) = =
minor ( ) , .

. Ellipse,
:
Point Ellipse : : focusl ( ) const
{
if (h<=w) // :
return { center ( ) . x+int ( sqrt (doule (w*w-h*h) ) ) ,
center ( ) . } ;
else // :
return { center ( ) . ,
center ( ) . y+int ( sqrt (doule (h*h-w*w) ) ) } ;
1 3.14. Marked _polyline 527

Circle Ellipse?

. . ,
- , .
, Circle Ellipse.

( ;
).
. , ,
Circle Ellipse, ,
, -
set maj or ( ) set minor ( ) . ,
_ _

( ),
set major ( ) major ( ) ! =minor ( ) , -
_ ,

, .
, (
major ( ) ! =minor ( ) ), ( major ( ) -inor ( ) ).
( Ellipse), .
, Circle
.
, :
. ,
, -
, -
.
, . -
. , -
. ,
,
. ,
.

1 . 1 4. Marked_polyline
.
, ,
. arked_polyline.
.
arkedyolyline mpl { " 1234 " } ;
mpl . add ( Point { l00 , 100 } ) ;
mpl . add ( Point { lS0 , 200 } } ;
mpl . add ( Point { 250 , 250 } ) ;
mpl . add ( Point { 00 , 2 00 } ) ;
528 1 3

marked polyline [QI

arked_yolyline :
struct &rked_yolyline : Open_yolyline {
arked_yolyline (const strinq& m) : mark {m)
{
if (111- "") mark = " * " ;
-=

arked_yolyline ( const strinq& m,


initializer list<Point> lst) ;
void draw_lines ( ) const;
private :
strinq mark ;
};
Open_yolyline,
" Point, .
, - . , draw lines ()
:
void &rked_yolyline : : draw_lines ( ) const
{
Open_yolyline : : draw_lines ( ) ;
for ( int i=O ; i<numer_of_;ioints ( ) ; ++i )
draw_mark (point ( i ) , mark [i%mark . size ( ) ] ) ;

Open_yolyline : : draw_lines () ,
.
1 3. 1 5. Marks 529

, : mark
[ i%mark . size ( ) ] ,
, ,
arked_polyline. % (
). draw lines ( ) _

draw mark ( ) _ .

void draw_ark ( Point , char )


{
constexpr int dx = 4 ;
constexpr int dy = 4 ;
strinq m{ 1 , } ; //
fl_draw (m. c_str ( ) , y . x-dx , y . y+dy) ;

dx dy
. m .
, ,
Open_polyline:
arked_polyline (const strinq& m, initializer list<Point> lst)
: Open_polyline { lst} , ark {m}

if (m==" " ) mark = "*" ;

.
, draw lines ()
_

.
. .

arked_polyline mpl { " 1234" , { { 100 , 100 1 , { 150 , 200 1 ,
{ 250 , 250 } , { 300 , 200 } } } ;

1 .1 5. Marks
.
arks. . ,
. .
arks { "" , { { 100 , 100 1 , { 150 , 200 1 , { 250 , 250 1 , { 300 , 200 1 1 1 ;
:
, arks
,
. (, ).
.
530 1

arks - arked_polyline
.
struct &rks : &rked_polyline {
arks ( const strinq& m) : &rked_polyline (m}
{
set_color (Color{ Color : : invisile } ) ;

arked_polyline ( const strinq& m, initializer list<Point> lst)


: &rked_polyline {m, lst}

set_color (Color { Color : : invisile } ) ;

};
: arked polyline { m }
arked_polyline arks.
, (9.4.4).

1 3. 1 6. Mark
Point Window.
-, .
,
, , 1 3. 2 ,
arks. "",
arks, .
, ,
1 3. 1 2, :
1 3. 1 6. Mark 531

ark ml { Point { l00 , 200 } , ' x ' } ;


ark m2 { Point { lS0 , 200 } , ' y ' } ;
&rk m. { Point { 200 , 200 } , ' z ' } ;
cl . set_color (Color : : ue) ;
c2 . set_color (Color : : red) ;
c . set_color (Color : : qreen) ;

. .

ark arks,
-

.
struct ark : arks {
ark ( Point , char ) : arks { strinq { l , c } }
{
add (xy) ;

1;
string { 1 , } string,
, .
ark arks
, .
, ?
" "?
. .
, ,
.
?
, .
5 32 1 3

.
, , , + * , .

1 . 1 7. Image
.
, . ,

. ,
(rita_path . gif), ".
.

Hurricane Rita
Sep tember 21 , 2005
4 CDT Wednesday
NWS TPCINatlonal Hurrlcane Center
Advlsory 1 7
Current Cent.er Locatlon 24.4 N . W
Sustalned Wlnd 165 mph
Current Movement W at 1 3 mph
Current Center Locatlon
Forecast Center Posltlons
Sustalned wlnd > 73 mph
D Sustalned wind < mph
:::::.,. Potential Day 1 - Track Area
Potentlal Day 4-5 Track Area
Hurrlcane Watch
Troplcal Storm Watch


, (ri ta . jpg).
Iage rita { Point{ 0 , 0 } , "rita . jpq" } ;
Image path { Point { 0 , 0 } , "rita_yath . gif" } ;
path . set_mask ( Point{ S0 , 250 } , 600 , 400) ; 11
1 .1 7. lmage 533

win . attach (path) ;


win . attach (rita) ;

set_mask ( ) ,
.
600400 ri ta_path . gif ( path)
,
(50,250) . - ,
.
, ,
. path
"", , rita.
.
: JPEG GIF.
enwn class Suffix
enwn Encodinq ( none , jpq , qif } ;
};

Image.
struct Imaqe : Shape (
Imaqe (Point , strinq file_name , Suffix Suffix : : none) ;
Imaqe ( ) ( delete ; }
void draw_lines ( ) const ;
void set_mask ( Point , int ww , int hh)
( w-ww ; hhh ; . ; = . ; }
private :
int w , h ; // " "
534 1 3

11 (, )
int , ;
Fl_Ima.qe* ;
Text fn ;
};
Image
, ,
( ) .
(, ,
), Image Bad image.
Bad_image :
struct Bad_ima.qe : Fl_Ima.qe
Bad_ima.qe ( int h , int w) : Fl_Ima.qe { h , w , 0 } { )
void draw ( int , int , int,
int , int , int) { draw_epty (x,y) ; }
};
,
Image
.
// , ,
11 ,
Ima.qe : : Imaqe ( Point , strinq , Suffix )
: w { O } , h { O } , fn { xy , " " }

add (xy) ;
if ( ! can_open ( s ) ) { // s ?
fn . set_lael ( " :w \ " "++ ' " ' ) ;
= new Bad_ima.qe ( 0 , 20 ) ; 11 " "
return ;

if ( == Suffix : : none) = qet_encodinq ( s ) ;

switch (e) { //
case Suffix : : jpq :
= new Fl_JPEG_Ima.qe { s . c_str ( ) } ;
break;
case Suffix : : qif :
= new Fl GIF_Ima.qe { s . c_s tr ( ) } ;
break ;
default : //
fn . st_ll ( " n '1'II \ 11 11 +s+ 1 11 1 ) ;
= new Bad_imaqe { 0 , 20 } ; // " "
1 3. 1 7 . lmage 535

I ,
, (Fl_JPEG_Image Fl_GIF_
Image) . new
. ( new
1 7)
FLTK . FLTK
, s . _s tr ( )
s .
can_open ( ) , .
.
bool can_open ( const strinq& s)
11 , s
11

ifstream ff ( s . c_str ( ) ) ;
return ff ;

. -
. .
, ,
.
, get_
encoding ( ) :
.
map ( 2 1 .6).

1 . Simple_window 800 1 .
2 . 88 800800
( 1 1 00
).
3. , ,
, ( Rectangle) .
4. 200200 ( JPEG
GIF) (
). ,
200 , ,
set mask ( ) .
. .
536 1

5. 1 1 00 .
. Next.
wai t for button ( ) .
. .


1 . -
?
2 .
,
?
3.
?
4. ?
5. Line -
?
6. Point?
7. Line_s tyle.
8. Color.
9. RGB?
1 0. Line
Lines. ?
1 1 . Shape?
1 2 . Closed_polyline
Point?
1 3 . , Shape.
Window?
1 4 . Rectangle Polygon
Point ()?
1 5. Polygon Closed
polyline?
1 6. :
?
1 7. Triangle (
Rectangle)?
1 8. Shape ?
1 9 . Shape ?
1 3. 1 7. lmage 537

20. Text?
2 1 . ?
22. Vector_ref ?
23. Circle Ellipse?
24. , Image
, ,
?
25. ?

GIF
JPEG
Vector ref



n11
. ,
,
.
1 . Arc, . : fl_arc ( ) .

2. . ,
.
3. Arrow, .
4. n ( ) , s ( ) , ( ) , w ( ) , center ( ) , ne ( ) , se ( ) , sw ( )
nw ( ) . Rectangle
Point. "
",
. , nw (r) - (-)
Rectangle r.
5. . 4 Circle Ellipse.
.
.
6. . ,
1 2.6. .
538 1

.
.
7. RGB ( ).
8. Reqular_hexagon ( ).

.
9. Regular_hexagon
( ).
1 0. Regular_polygon.
. ( )
.
1 1 . 300200 .
400 300 ,
. . .
.
.
1 2. .
( .
Next) .
1 3. 1 3. 1 , .
.
1 4. . -
.
1 5. .
1 6. .
1 7.
.
1 8. Poly, .
. .
: .
1 9. Star.
.
. .

1 2 , .
" " :
.


l, 1
- (V)

, ,
: ,
,


. ,

.
,
,
-
: ,
.
,
,

. ,
"
"
.
540 14

1 4. 1 . 14.3.
1 4. 1 . 1 . 1 4.3. 1 .
1 4. 1 .2 . 1 4..2. -
1 4 . 1 ..
1 4 . 1 .4. 1 4.3.3.
1 4 .2 . Shape 1 4.3.4.
1 4 .2 . 1 . 1 4..5.
1 4 .2.2. 1 4.4. -
1 4 .2..
1 4.2.4.

1 4. 1 .

? .
" " ,
?

1 4.1 . 1 .
- .

. -
, , ,
. ,
.
-
.
, ,
. .
Window - , .
Line - , .
int - .
Color - .
Shape -
.
, Shape. ,
, .. .
; ,
14.1. 541

. :
Shape .

;
.
.
,
.
, .
,
. ,
,
. ,
.

. , .

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

.
.
,
.
, ,
.
,
,
. ,
. ,
542 1 4

FLTK, ,
( r ) . ,
, .
1 7 1 8 .
,
.

. .
, Open_polyline, Closed_polyline, Poly
gon, Rectangle, arked_polyline, arks ark
( Polyline");
.
,
. .
Shape. .

. .
", ,
, .

1 .1 .2.
.
- , ,
. ,
, ,
.
.
. , ,
, , -
. . ,
. :
.
Point.
Line ln ( Point { l00 , 200 } , Point{ 00 , 40 0 } ) ;
ark ( Point { lOO , 200 } , ' ' ) ; 11 " "
Circle c (Point { 200 , 200 } , 250) ;

. , Point.
. . ,
, . .
void draw_line ( Point pl , Point 2) ; 11 pl 2 ( )
void draw_line (int xl , int yl , int 2 , int 2 ) ; 11 (x l , yl ) (2 , 2)
14.1. 543

,
,
.
Point
: . .
draw_rectangle ( Point { l00 , 200 } , 300 , 400) ; //
draw_rectangle ( 100 , 200 , 300 , 400 ) ; //


, . .
? , ( 1 00,200)
(300,400)? , ( 1 00,200),
300 400? ,
( )? Point,
.
, ,
(. ,
).

.
.
, , ,
, add ( ) , ,
, draw_lines ( ) .
( ), ,
( ).
, ,
.
(generic); 1 9-2 1 .

1 4. 1 . .
. ,
, "
(attach) Shape Window, -
(add) Line Shape?"
" -",
? . .
.
Open__polyline opl ;
opl . add (Point { l00 , 100 } ) ;
opl . add ( Point { lS0 , 20 0 } ) ;
opl . add ( Point { 250 , 250 } ) ;
544 14

opl . opl ,
add ( ) ;
. .
. ,
:
win . attach ( opl) ;
win opl;
win opl , .
opl , win
. , win opl,
. opl,
win opl
. attach ( )
add ( ) .

Window:

Open_polyline:
( 1 00, 1 00)
( 1 50,200)
(250,250)

add ( )
(), attach ( ) -
( ).
Window,
,
add ( ) , t tach ( ) .
Window. .
, , ,
, .
void f ( Simple_window& w)
{
Rectanqle r { Point{ l00 , 200 } , 50 , 30 } ;
w . attach (r) ;
11 r !

int main ( )
{
Simple_window win { Point{ l00 , 100 } , 600 , 400 , "Moe J" } ;
1 4. 1 . 545

11 . . .
f (win) ; // !
11 . . .
win . wait_for_button ( ) ;

f ( ) wait_for_
button ( ) , r win , win
. 1 7
, ,
, ,
" " wai t_for_button ( ) .
Vector_ref,
1 3 . 1 0 .4.
, f ( ) .
Window (
8 . 5 . 6). :
attach (r) const Window,
attach ( )
Window, r.

1 4. 1 .4.
. ,
, : " ?" "
?" ,
.
puic private,
,
protected. .
- , , lael string;
, ,
. . ,
, , lael,
, . .
struct Circle
11 . . .
private :
int r ; //
1;

Circle c { Point{ l00 , 200 } , 50 } ;


.r = - 9; // OI\ ? - :

// Ci rcl e : : r
546 14

1 3,
- .
"" ,
Circle.
,
.
, ,
"" ,
, .
(.. Window)
.
,
, ,
.

1 4.2. Shape
Shape ,
Window .
,
Window, , .
.
, ,
. Line_style Color
( , ).
Point
, .
, ,
, , .
, .
, .
class Shape { // ,
pulic :
void draw ( ) const ; //
virtual void move (int dx , int dy) ; //
// +=dx +=dy

void set_color ( Color col) ;


Color color ( ) const ;
void set_style (Line_style sty) ;
Line_style style ( ) const;
1 4.2. Shape 547

void set_fill_color (Color col ) ;


Color fill_color ( ) const ;

Point point ( int i ) const ; 11


int nuer_of_yoints ( ) const ; 11

Shape ( const Shape&) = delete ; 11


Shape& operator= ( const Shape&) 11
delete ;

virtual shape ( )
protected :
Shape { } ;
Shape (initializer_list<Point> lst) ; //
virtual void draw_lines ( ) const ; //
void add ( Point ) ; //
void set_yoint (int i , Point ) ; // poin t s [ i } =p ;
private :
vector<Point> points ; 11
11
Color lcolor { fl_color ( ) } ; 11
// ( )
Line_style ls { O } ;
Color fcolor { Color : : invisile } ; //

};

, II
r
. - .
, ,
.
.

1 4.2. 1 .
Shape:
protected :
Shape { } ;
Shape (initializer_list<Point> lst) ; //

protected. ,
,
Shape ( : Shape). , Shape
II ,
Line Open_polyline. protected : ,
-

Shape :
Shape ss ; // : Shape
548 14

Shape ,
.
, -
Shape , , ,
,
. ,
Shape,
, ,
, Circle Closed_
polyline. ! ?
- : "
?" , Shape,
. ,
.
Shape
.

.
FLT. .
FLTK, .
Shape, Color Line_style.
vector<Points> .

, add ( )
Shape:
Shape : : Shape (initializer_list<Point> lst)
{
for ( Point : lst) add (p) ;

(abstract),
. -
- -
W1. (pure vlrtual functlon),
1 4. 3 . 5 . ,
,
(concrete). ,
.
, .
-
. ?
? :
-

. "Canon EOS 6000" -


1 4.2. Shape 549

,
(
). , - () ,
canon EOS 6000" - () ,
( ) - .

virtual shape ( ) { )
.
, 1 7. 5 . 2 .

1 4.2.2.
Shape - .
private :
vector<Point> points ; 11
11
Color lcolor{ fl_color ( ) } ; 11
11 ( )
Line_style l s { O } ;
Color fcolor {Color : : invisile } ; //

-
, . ,
,
.
.
- Shape ,
.
. -
, : ,
, ( ) set_X ( )
, :
void Shape : : set_color ( Color col )
{
lcolor = col ;

Color Shape : : color ( ) const

return lcolor ;

,
. . .
,
550 14 r

.
- . ,
const. ,
Shape (. 9.7.4).
Shape points Point.
.
Point points add ( ) :
void Shape : : add ( Point ) //
{
points . push_back (p) ;

, points .
Shape ,
- - , Shape -
-.
,
, - -
. .

.
, Shape, Circle Polygon,
", . Shape " ",
. ,
, . .
Circle Rectangle
. .
? (. 1 2.7.6.)
Lines (
; . 1 3.3).
Open_Polyline arks
.
Polygon -
add ( ) , ( 1 3.8).
add ( )
protected (..
), ,
. add ( )
puic (.. ) private (..
Shape).

.
1 4.2. Shape 551

set_
point ( ) . "",
, . ,
Regular_hexagon,
. .
. ,
,
. set_Point ( )
, ,
Shape.
. Mutae_rectangle,
Rectangle ,
.
points private,
. ,
.
void Shape : : set_.Point ( int i , Point ) //
{
points [ i ] = ;

Point Shape : : point (int i) const

return points [ i ] ;

int Shape : : numer_of_.Points ( ) const

return points . size ( ) ;

-
:
void Lines : : draw_lines ( ) const
11 ,

for (int i=l ; i<numer_of_.Points ( ) ; i+=2)


fl_line (point ( i-1) . x , point ( i-1) . y , point ( i ) . x ,point (i ) . ) ;


. ?
? ? ,
(inline).
numer_of_Points ( )
. points .
size ( ) .
552 1 r

, , .
Shape:
struct Shape 11
// ( , )
Shape ( ) ;
Shape (initializer_list<Point>) ;
void draw ( ) const ; /!
virtual void draw_lines ( ) const ; 11
virtual void move ( int dx , int dy) ; 11
virtual shape ( ) ;

vector<Point> points ; //
Color lcolor ;
Line_style ls ;
Color fcolor ;
};

- (pri va te :
protected : )? I ,

.
( . 9.4.3).
, Shape.
Shape :
Fl_Color lcolor ;
int line_style ;
, ( ,
int,
, Fl_Color )
.
,

( lcolor
line_style) .
,
. , s . add ()
, s . points . push_back () .

1 4.2..
, Shape.
void draw ( ) const ; //
// dra w_ l i nes
virtual void draw_lines ( ) const; //
1 4.2. Shape 553

Shape - .
Shape
, (.
1 4.4) : - Shape.
FLTK .
.
draw ( ) ,
draw_lines ( ) .
draw_lines ( ) .

draw ( ) .
FLTK. ,
draw_lines ( ) . ,
. .
void Shape : : draw ( ) const
{
Fl_Color oldc = fl_color ( ) ;
//
// }'I!
fl_color (lcolor . as_int ( ) ) ; //
fl_line_style ( ls . style ( ) , ls . width ( ) } ; //
draw_lines ( ) ;
fl_color (oldc) ; 11
fl_line_style ( O ) ; 11

, FLTK
.
. ,
, .
.
.
, Shape : : draw ( )
.
draw_lines ( ) , ,
. ,
draw_lines ( ) .
.
, draw_
lines ( ) . , ,
- Shape , -
.
Shape .
vector<Point>. -
554 1 4

. , (..
) ,
- .

. : ,
Shape, .
. Text, Rectangle Circle
, . .
. ,
"", . .
Circle , , ,
. Circle
, , .
, - .
Circle draw_lines ( ) ,
draw_lines ( )
Shape. virtual
Shape : : draw_lines ( ) :
struct Shape (
11 . . .
virtual void draw_lines ( ) const ;
11
11 dra w l i nes ()
11 . . .
};

struct Circle : Shape


11 . . .
void draw_lines ( ) const ; // " " Shape : : dra w_ l ines ()
11 . . .
};
. draw_lines ( ) Shape
- Circle,
Circle, - Rectangle,
Rectangle. virtual
draw_lines ( ) :
Shape, draw_
lines ( ) ( , draw_lines ( ) Shape) ,
draw_lines ( ) Shape.
1 3 , Text, Circle, Closed_polyline
.. ,
, (overriding).
, ,
Shape, draw_lines ( ) protected. ,
1 4.2. Shape 555

" " -
draw ( ) , - " ,
draw ( ) , Shape.
,
1 2. 2 . , , Window.
Window Shape - draw ( ) .
draw ( ) draw_lines ( )
. gui_main ( )
.

Shape
draw_lines() draw()
Clrcle
draw_llnes() draw()
Window

draw()
Shape attach()
draw_lines() gui_main()
Square
draw_llnes()

gui_main ( ) ?
; wait_for_button ( ) ,
.
move () Shape ;
.
void Shape : : move ( int dx, int dy) // +=dx + =dy
(
for (int i = ; i<points . size ( ) ; ++i ) {
points [ i] . x+=dx ;
points [ i] . y+=dy ;

draw_lines ( ) , move ( ) ,
.
Shape.
Axis (. 1 2. 7.3 1 5.4).
move () Shape;

556 1 4

. . .
Shape, move ( ) .

1 &.2.4.
Shape
:
Shape (const Shape&) =delete ; //
Shape& operator= ( const Shape& ) =delete ;

,
:
void my_fct (Open_polyline& , const Circle& )
{
Open_polyline 2 = ; // :
11
vector<Shape> v ;
v .push_back (c) ; 11 :
11
// . . .
= 2 ; 11 :
11 Shape

!
push_back ( ) ;
( push_back ( )
). ?
,
. -
my_fct ( ) . Circle
v, Shape
; Circle ,
Shape , sizeof ( Shape) < si zeof (Circle) .
-

v . push_back () , Circle ""


. v
; Circle ( r),
.

Shape: polnts Circle: polnts


color color
ls ls
fcolor fcolor
1 4.3. 557

2
:
&rked_Polyline mp { "" 1 ;
Circle ( , 10) ;
my_fct (mp , c) ; 11 Open_polyl ine
11 Ma rked_polyl ine

Open_polyline
mark, string.
.

. , -

=delete, Shape.
( ) - ,
.
, .
, Shape
, . Shape
Window, . , Window
Shape,
. , ,
,
. .
,
,
. .
clone ( ) . , clone ( )
,
, Shape.

1 4..

; , ( )
, ,
.
,
.
. .
.
, Circle Shape;
. Circle Shape
558 14

Shape Circle.
( Circle)
( Shape) .
(inheritance),
.
(subclass), -
(su perclass).
. ++

,

. , Window
draw_lines ( ) Shape,
Circle, draw_lines ( )
Circle, draw_lines ( ) Shape.
11 (run-time
polymorphlsm), (dyamlc dispatch)
(run-tlme dispatch),

, .
. .
,
, .
(encapsulation).
, -
-
(object-oiiented programming). , ++
-
. , 20
2 1 , ++
. ++ - -
Simula67, ,
- (
22).
! ?
?
,
.
.

.
.
14.3. 559

Open_polyline
. ,
(Shape). .

.

Rectangle
Marked_polyline Closed_polyllne

Marks Polygon

Mark

1 . . 1 .
?
9.4. 1 . : -
. ,
- -
, :
Shape: Circle:
points points
lcolor lcolor
ls ls
fcolor fcolor

Circle - Shape (
, Shape)
Shape. , Circle
r,
-.
560 14

,
" Shape:
,
draw lines ( ) Shape.
. vt (
), vptr (
, ).
1 7 1 8; .

.
. :

draw_lines ( ) - .
vt,
move ( ) .
; vt (
). .
. draw_lines ( ) , ,
draw_lines ( ) vt, .
. .
Circle, Circle : : draw_lines ( ) .
. , Open_polyline,
vt . Shape,
Shape : : draw_lines ( ) . ,
Circle move ( ) .
x . move ( ) . Circle,
Shape : : move ( ) . .
, vptr
vt
.
. - .
1 4.3. 561

Shape .
Shape , Open_polyline
. " Shape",
- .
vt
. , vt
.
,
. ,
-
.
. ,
(, Circle : : draw_
lines ( ) ),
vt ,
(overridlng). , Circl e : : draw_lines ( )
Shape : : draw_ lines ( ) .
vt ?
. -
? . (
) , .
- , . ,
, "
". ? ?
? ?
, .
(
).
,
.
. .

1 ,.3.2.
, ,
:
struct Circle : Shape { /* . . . */ } ;

,
s truct, (. 9.3),
. -
:
class Circle : puic Shape { pulic : / * . . . */ } ;
562 1 4 r

Circle ,
,
. , ,
. .
puic,
. .
class Circle : Shape { puic : /* . . . */ } ; // ,
Shape
Circle, - Shape
Circle. .
.
, .

virtual ,
, virtual .
struct Shape {
11 . . .
virtual void draw_lines ( ) const ;
virtual void move ( ) ;
11 . . .
};

virtual void Shape : : draw_lines ( ) const { / * . . . */ } //


void Shape : : move ( ) { / * . . . * / } // OI-i

1 4.. .
,
,
, :
struct Circle : Shape {
void draw_lines ( int) const ; 11 ,
11 ( i n t ?)
void drawlines ( ) cons t ; 11 ,
11 ( ? )

void draw_lines ( ) ; 11 , ( cons t ?)


11 . . .
};

,
Shape : : draw_lines ( ) ( ),
.
. ,
.
1 4.. 563

draw_lines ( ) ,
,
.
struct (
virtual void f ( ) const ( cout << 11 : : f 11 ; }
void q ( ) const ( cout << 11 : : q 11 ; } //
};

struct D :
void f ( ) const ( cout << 11D : : f 11 ; } // B : : f
void q ( ) ( cout << 11D : : g 11 ; }
};

struct DD : D (
void f ( ) ( cout << 11 DD : : f 11 ; } 11 D : : f
11 ( con s t )
void q ( ) const ( cout << 11DD : : g 11 ; }
};


f ( ) . .
, f ( )
q ( ) , , ,
( , ).
void call ( const & )
11 D , ca l l ()
-

11 D. DD - -

11 D, D ,
-

// ca l l () DD

b.f() ;
b.q() ;

int main ( )

;
D d;
DD dd;

call (b) ;
call (d) ;
call (dd) ;

b.f() ;
b.q() ;

d. f ( ) ;
d.g() ;
564 14 r

dd . f ( ) ;
dd . g ( ) ;

:
: : f : : g D : : f : : g D : : f : : g : : f : : g D : : f D : : g DD : : f DD : : g
, , ,
.
, ,

. , .
, .
, ,
override.
struct {
virtual void f ( ) const { cout << 11 : : f 11 ;
void g ( ) const { cout << ": : g 11 ; } //
};
struct D :
void f ( ) const override 11 B : : f
{ cout << "D : : f 11 ;
void g ( ) override // :
1 cout << "D : : g 11 ; 11 B : : g
};
struct DD : D {
void f ( ) override // : D : : f
{ cout << "DD : : f 11 ; 11 ( con s t)
void g ( ) const override 11 :
{ cout << "DD : : g " ; // D : : g
};

override
, .

1 4..4.
++ .
.
(private) .
priva te, r
.
(protected).
protected, r
, .
1 4.. 565

. (pu\ic).
puic, .
.

.- - - - - - - - - - - - - - - - - - - - - --------- ---------- ---- ---- -


: :
1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - ;

private, protected
puic.
D private,
, puic protected,
D.
D protected,
, puic protected,
D , D.
D puic, ,
puic, .

.
. ,
++
++, ISO ++.
(..
); (
, , , )
.

1 4 . .5.
- ,
.
; .
, -
566 14

.
( rl. , " .)
.
.
"" ( . ),
" " (
) "" (
) .
( ).
1 4.2 . 1 , ,
protected. ,
-
: ,
. .
class ( 11
pulic :
virtual void f ( ) = ; //
virtual void 9 ( ) = ;
);

; 11 : -

= ,
: : f ( ) : : g ( ) "", ..
.
, .
"":
class Dl : pulic (
pulic :
void f ( ) override ;
void 9 ( ) override ;
);

Dl dl ; 11

, ,
:
class D2 : pulic (
pulic :
void f ( ) override ;
11 g ()
};

D2 d2 ; // : D2 - ( )

class D pulic D2 (
1 4.4. - 567

pulic :
void g ( ) override ;
) ;

D d ; 11 ok


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

1 4.4. -
, Circle
Shape Circle
Shape. ,
( ).
11 . .
Shape ( , ),
Circle (
Shape) .
11 u11. Circle
-. (,
-). Shape.
, (.. ,

),
. , Never_do_this,
Shape .
Shape : : draw lines ( ) ,
. 1 00 . "
" , , ,
Never_do_this Shape,
(.. ),
Shape. !
,
,
(""; -
Shape) ("";
- . Shape).
568 14


,
(. Circle). -
, (
- Shape).
,
: " "
Shape : : draw ( ) , . .
draw_lines ( ) Shape,
. " ", Shape
, . , FLTK

!
attach ( ) .
Window Shape (
Window : : attach ( ) Shape&; . .3).
, Shape ,
Shape ,
.
, .
. - " J" .
: -
. , .
.
(. Shape
),
(. 1 7- 1 9 vector;
).
- .
.
.
. Shape.
, .
-
,
Shape
-
.
.
,
(. 1 4.3.5).
1 4.4. - 569

, ,
,
. -
.
1 . 81 vf ( )
f ( ) . 81 .
, (,
"81 : : vf ( ) ) . . 81
"

.
2. D l , 8 1 ,
vf () . D l
vf ( ) f ( ) .
3. 81 (.. 81 &)
D l . vf ( )
f ( ) .
4. f ( ) Dl . 1-3 .
.
5. 81 pvf ( )
. 1 -4. .
6. D2, Dl,
pvf ( ) . D2
f ( ) , vf ( ) pvf ( ) .
7. 82 pvf ( ) .
D2 1 - string -,
pvf ( ) ; D21 : : pvf ( )
string. D22,
D21, ,
int. f ( ) 82 &
,

pvf ( ) .
f ( ) D21 D22.


1 . ?
2. ?
3. ?
4. Shape?
5 70 14

5. , -
?
6. ?
7. ?
8. private?
9. -
?
1 0. ?
1 1 . ?
1 2 . ?
1 3 . , ?
1 4. ?
1 5.
?
1 6 . ?
1 7.
?
1 8. - ?
1 9. - ?
20. ?
2 1 .
?
22. - ?

11
private
protected
puio

-



1 . , Smiley () Frowny (),
Circle .
1 4.4. - 571

, Smiley Frowny,
.
2. Shape. ?
3.
. ?
4. Immoile_Circle,
Circle, .
5. Striped_rectangle,

(, ).
,
.
6. Striped_circle, Stri
ped_rectangle.
7. Striped_closed__polyline,
Striped_rectangle ( ).
8. Octagon, .
. -
( . Shape) .
9. Group, Shape
Group.
: Vector_ref. Group, ,
,
.
1 0 . Pseudo_window, Window.
.
, . ,
- ,
. .
( ),
Simple_window.
1 1 . Binary_ tree, Shape.
, (levels==O ,
, levels==l ,
, levels==2 , ,
levels= ,
, , . , ..).
.
( ).
5 72 14

P.S.
(, , ).
1 2. Binary tree ,
. Binary_
tree ,
. (. ).
1 . Binary tree ,
( ). ,
(, , ,
, ). ,
,
.
1 4. Binary_tree , .
,
Binary_tree. ; .
, , ,
" lrrlr" (
"1", "r") .
1 5. .
I terator, next ( ) .
doue* (. 1 7) .
I tera tor Vector-i tera tor Li s t-i tera tor .
next ( ) Vector_i terator
vector<doule>. List_
i terator list<doule>.
Vector_iterator vector<doule>,
next ( )
. .
. .
void print ( I terator&) .
vector<doule> list<doule>.
1 6 . Controller,
: on ( ) , off ( ) , set_level ( int) show ( ) .
Controller .
, show ( )
, ,
.
Shape; ""
.
Controller.
14.4. - 573

1 7. . ++,
exception, runtime_error out_of_range (. 5.6.3),
(
what ( ) . ,
).
++
.



. . - ,
,
, (
)
.
, . . , -
. ,
( private protected),
( ), -
(
)
.

-
- 4./1 (V)

,

, .

. ,

.

,
.
576 1 5

1 5 . 1 . 1 5 .5 .
1 5 .2. 1 5 .6.
1 5.. Function
1 5 . . 1 . r 1 5 .6. 1 .
1 5 . .2. r 1 5 .6.2.
1 5... - 1 5.6..
1 5.4. 1 5.6.4. r

1 5. 1 .
.
,
, -
. - .
,
. .
.
. ,
.
-
, .

1 5. 2.
,
. .
.
, .
, ,
- ,

doue one (doule) { return 1 ; }

. :
1.
, .
, one ( ) .
. 1 ; .
( , ) = ( , 1 ) .
15.2. 577

,
, - ,
.
doule slope (doule ) { return /2 ;

. .
, /2. . ( , ) = ( , /2 ) .
( 2 , 1) .
- .
,
.
doule square (doule ) { return ; )

( ),
, .
(0 , ) , . . ( , ) == ( , ) . ,
,
, - ( 0 , 0) .
,
.
constexpr int xmax = 600 ; //
constexpr int = 400 ;

constexpr int x_oriq = xmax/2 ; // ( 0 , 0)


constexpr int y_oriq = ymax/2 ;
constexpr Point oriq{x_oriq , y_oriq} ;
5 78 15

constexpr int r min -10 ; 11 [ -1 0 , 1 1 )


constexpr int r = 11 ;

constexpr int n_.Points = 400 ; 11

constexpr int scale 30 ; 11


constexpr int y_scale 30 ;

Simple_window win { Point { l00 , 100 } , xmax , ymax , "Function graphing" } ;

Function s { one , r_min , r_max , orig , n_.Points , x_scale , y_scale} ;


Function s2 { slope , r_lllin , r_max , orig , n_.POints , x_scale ,y_scale } ;
Function s 3 { square , r_min , r_max , orig, n_.POints , x_scale ,y_scale} ;

win . attach ( s) ;
win . attach (s2 ) ;
win . attach ( s3) ;
win . wait_for_button ( ) ;

,
" ". ,
,
, .
.
Function: s, s2 s.
Function s { one , r_min , r_max , orig , n_.Points , x_scale , y_scale } ;
Function s2 { slope , r_min , r_max , orig , n_.Points , x_scale , y_scale } ;
Function s 3 { square , r_min , r_max , orig , n_.Points , x_scale , y_scale } ;
Function ,
( doule,
doule) .
( ).
( orig) Function,
( , ) .
,
, .
,
.
.
, 1 5.3. .

. ,
, . ,
, .
.
Text (. 1 3. 1 1 ) .
1 5.2. 579

Text ts { Point { lOO , y_oriq-40 } , "one" } ;


Text ts2 { Point { lOO , y_oriq+y_oriq/2-20 } , "x/2 " } ;
Text ts { Point{x_oriq-100 , 20 } , "x*x" } ;
win . set_lael ( " Function qraphinq : lael functions " } ;
win . wait_for_button ( ) ;


, ,
Next.
.
, /2
( 0 , 0 ) ( 0 . 5 , . 25) , one
/2 ( 2 , 1 ) , ;
, .
Axis
( 15 .4).
constexpr int xlenqth xmax-4 0 ; 11
constexpr int ylenqth ymax-40 ;

Axis {Axis : : x , Point { 20 , y_oriq} ,



xlenqth , xlenqth/x_scale , "one notch 1" } ;
Axis (Axis : : y , Point { x_oriq , ylenqth+20 } ,
ylenqth , ylenqth/y_scale , "one notch 1" } ;
580 15

xlength/x_scale ,
,
1 , 2, 3 .. ( , )
. , .
, , ( 1 5.6),
. , ,
.
x . set_color (Color : : red) ;
y . set_color (Color : : red) ;
, .
,
- ,
. ,
. ,
.
, .
-
(, ). :
" - ".

1 5.. Function
Function
.
1 5.3. Function 581

struct Function : Shape {


11
Function (Fct f , doue rl , dou.le r2 , Point oriq ,
int count = 100 , dou.le xscale 25 , dou.le yscale
25) ;
};

Function Shape,
Shape.
f,
count , [ r l , r2 ) .
Function : : Function (Fct f , dou.le rl , dou.le r2 , Point ,
int count , dou.le xscale , dou.le yscale)
11 f (x) [rl , r2) ,
11 coun t . (0, 0)
11 ;
11 xsca l e ;
11 ysca l e

i f (r2-rl<O) rrr ( " " ) ;


if (count <) error ( "He nn'81'l' count " ) ;
dou.le dist (r2-rl ) /count ;
dou.le r rl ;
for (int i ; i<count; ++i )
add ( Point {xy . x+int (r*xscale) , xy . y-int (f (r) *yscale) ) ) ;
r + dist ;
582 1 5

xscale yscale
. ,
.
, Function
, ,
,
.
. ( Shape)
. Function,
,
. (. . 2).
Fct,
-? s td : : function .
w" . Fct ,
.
doue.

1 5..1 .
xscale
yscale Function.
D (default arguments).
.
.
Function s { one , r_lllin , r_ax , orig , n_J1oints ,
x_scale , y_scale } ;
Function s2 { slope , r_lllin , r_ , orig , n_J1oints ,
x_scale } ; // ysca le
Function s { square , r_lllin , r_max , orig ,
n_J1oints } ; // xsca le , ysca l e
Function s4 { sqrt , r_llli n , r_ , orig } ; // coun t , xsca l e ,
1 1 ysca l e

:
Function s { one , r_llli n , r_max , orig , n_J10ints ,
x_scale , y_scale } ;
Function s2 { slope , r_lllin , r_ax , orig , n_J1oints , x_scale , 25 } ;
Function s { square , r_lllin , r_max , orig , n_J1oints , 25 , 25} ;
Function s4 { sqrt , r_llli n , r_. , orig , 100 , 25 , 25 } ;

, ,
.

.
1 5 .3. Function 583

struct Function : Shape { //


Function (Fct f , doue rl , doule r2 , Point oriq,
int count , doule xscale , doule yscale) ;
11 :
Function (Fct f , doule rl , doule r2 , Point oriq ,
int count , doule xscale) ;
// :
Function (Fct f , doule rl , doule r2 , Point oriq, int count) ;
// coun t ; :
Function (Fct f , doule rl , doule r2 , Point oriq) ;
) ;


.
, , , -
.
,
.
, :
struct Function : Shape {
Function (Fct f , doule rl , doule r2 , Point oriq ,
int count = 100 , doule xscale , doule yscale) ; 11
);

,
:
struct Function : Shape {
Function (Fct f , doule rl , doule r2 , Point oriq ,
int count = 100 , doule xscale=25 , doule yscale=25) ;
};

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

1 5..2.
- (cos)
( , ,
) /2 .
doule slopinq_cos (doule ) { return cos (x) +slope (x) ;

.
584 1 5


Function s4 { cos , r_ID.in , r_max , oriq , 400 , 30 , 30 } ;
s4 . set_color (Color : : ue} ;
Function sS { slopinq_cos , r_ID.in , r_max , oriq , 400 , 30 , 30 } ;
x . lael . move ( -160 , 0 ) ;
x . notches . set_color ( Color : : dark_red) ;
,
( ) .
, ,
.
Function fl { loq , 0 . 00000 1 , r_max , oriq , 200 , 30 , 30 } ; // l n ()
Function f2 { sin , r_ID.in , r_m.ax , oriq , 200 , 30 , 30 } ; // s i n ()
f2 . set_color (Color : : ue) ;
Function f3 { cos , r_ID.in , r_m.ax , oriq , 200 , 30 , 30 } ; 11 cos ()
Function f4 { exp , r_ID.in , r_m.ax , oriq , 200 , 30 , 30 } ; 11 ()

log ( 0 ) (
), log
. :
.
, cos ( ) . sin ( ) sqrt ( ) .
<cmath>
.
24.8 .9.2.
1 5.3. Function 585

1 5... -
,
- . ++
-, ,
. . sloping_cos
:
Function s5 { [ ] (doue ) { return cos (x) +slope (x) ; } ,
r_in , r_ax , oriq , 400 , 30 , 30 } ;
[ ] (doue ) { return cos () +slope ( ) ; }
-, . . ,
, . [ ]
- (lambda lntroducer) .
- , (
) ( ).
-.
doue,
-

cos () +slope ( ) . ,
:
Function sS { [ ] (doue ) -> doue { return cos (x) +slope (x) ; } ,
r_in , r_ax , oriq , 400 , 30 , 30 } ;
-
. , -
, .
586 15

- , ,
, , (
, ).
, .
-
- : .
1 5.5, 2 1 .4.3.

1 5.4.
, , Axis (
, 1 5.6.4),
. Axis ,
. Axis
( ) .
.
struct Axis : Shape {
enum Orientation { , , z } ;
Axis (Orientation d , Point , int lenqth ,
int numer_of_notches=O , strinq lael = " " ) ;

void draw_lines ( ) const override ;


void move (int dx , int dy) override ;
void set_color (Color ) ;

Text lael ;
Lines notches ;
};
lahel notches ,
. , ,
. label
move ( ) . Axis - ,
.
Axis ,
numer_of_notches .
Axis : : Axis (Orientation d , Point , int lenqth ,
int n , strinq l) : lael ( Point { 0 , 0 } , la)

if (lenqth<O ) rrr ( " " ) ;


switch (d) {
case Axis : : :

Shape : : add () ; //
Shape : : add (Point{ xy . x+lenqth , xy . y } ) ;
1 5.4. 587

if (O<n) //
int dist = lenqth/n ;
int = xy . x+dist;
for ( int i = ; i<n ; ++i) (
notches . add ( Point { x , xy . y } , Point { x , xy . y-5 } ) ;
+= dist ;

lael . move (length/3 , xy . y+20 ) ; 11


break ;

case Axis : : y :
{
Shape : : add () ; // ,
Shape : : add (Point ( xy . x , xy . y-length } ) ;
if (O<n) { //
int dist = length/n ;
int = xy . y-dist;
for ( int i = ; i<n ; ++i ) {
notches . add ( Point { xy . x , y } , Point{ xy . x+S , y} ) ;
-= dist ;

lael . move (xy . x-10 , xy . y-length-10) ; //


break ;

case Axis : : z :
error ( "Oc z " ) ;


, ,
, ,
. ,
Shape. Axis ( Shape : : add ( ) ),
(notches).
;
, .
. ,
, .

Orientation.
Axis ,
Axis , :
void Axis : : draw_lines ( ) const
{
Shape : : draw_lines ( ) ;
588 1 5

notches . draw ( ) ; //
11
lael . draw ( ) ; 11
//

notches label draw ( ) ,


draw_lines ( ) ,
, .
Axi s : : Shape , .
, ,
,
, :
void Axis : : set_color (Color )
{
Shape : : set_color (c) ;
notches . set_color (c) ;
lael . set_color (c) ;

Axis : : move ( )
Axis :
void Axis : : .ove ( int dx, int dy)
{
Shape : : .v (dx , dy) ;
notches . .ove (dx , dy) ;
lael . .ove (dx , dy) ;

1 5. 5 .
:
"" . -
( ),
.
, ,
, .


2 4
= I +x+ - + - + - + K
21 31 41
, < ;
, ,
.
1 5.5. 589

.
, , , . .
:
() = //
expl (x) = 1 //
2 () = l+x // ; pow (x , 1 ) /fa c (l ) ==x
() = l+x+pow ( x , 2 ) /fac (2)
4 () = l+x+pow ( x , 2 ) /fac (2) +pow ( x , 3 ) /fac ( 3 )
expS (x) l+x+pow ( x , 2 ) /fac (2) +pow (x , 3 ) /fac ( ) +pow (x , 4 ) /fac ( 4 )

, .
pow ( , n ) ,
-

". ,
, .
int fac (int n) // fa ctori a l (n) ; !

int r = 1 ;
while (n>l)
r*=n ;
--n ;

return r ;

fac ( ) . 1 .
fac ( ) , - :
doue term (doue , int n) // -
return pow (x , n) /fac ( n) ;

term ( ) , n
:
doule expe (doule , int n) //
{
doule swn = ;
for (int i=O ; i<n ; ++i) swn+=term(x, i) ;
return swn;

.
"" ( ( )
), ,
( ) :
Function real_exp { exp , r_min , r_max , oriq , 200 , x_scale , y_scale } ;
real_exp . set_color (Color : : ue) ;
590 1 5

( ) ?
, Function
, ( ) .
++, , ,
.
-:
for ( int n = ; n<SO ; ++n)
ostrinqstream ss ;
ss << " approxiation ; n==" << n
win . set_lael ( ss . str ( ) ) ;
11 :
Function { [n] (doue ) { return expe (x , n ) ; } ,
r_min , r_ax , oriq , 200 , x_scale , y_scale } ;
win . attach (e) ;
win . wait_for_button ( ) ;
win . detach (e) ;

- [ n ) , -
n. ,
Function ( , n ) n
Function.
detach () .
Function for.
, Function,
, .
Window ,
. detach () ,
Window .
, "
" .
, ( ) 1, "
" ( , 1) .
, ,
( () ==) .
-

Next, , .
, .
, .
1 5.i 591

one natc h. "" 1

one notr-h "" 1

expl () =1,
.
( , 1 ) ,
.
592 15

(l+x),
, r ( , 1 )
.

(l+x+pow (x , 2 ) /fac ( 2 ) ) .

,
, -3.
1 5.5. 593

.
, .
,
: ,
.

, -
.
, -
594 1 5

.
int
, doue
. ,
, , ,
,
doule,
. , fac ( ) ,
int. fac ( )
, doule, .
. 1 1 5
24.2.

, , , ,
.
,
, .

1 5.6.
.

-
.
,
.
.
.
.
2008 .
-
, :
;
;
;
.
.
, " ", .
, , ,
.
1 5.6. 595

, ,
. ,
,
, .
, .

.

(
)
. ,
,
.
, .
(r , 'l' 'l') . .
(r , ICOJIQC'l'BO 'l' , ICOJIQC'l'BO I , IQ'l' 'l') .
,
. (r , 'l' 'l') , (r ,
'l' I) (r , IQ'l' 'l') .
( , ) . ,
, ,
. .
(year , s teel_production) ,
(steel_production) , (year),
.
Open_polyline (. 1 3.6).
596 1 5

(, ,
( , 1) ,
arks (.
1 3. 1 5).
,
.

1 5.6. 1 .

:
( 1 960 : 30 64 6 )
( 1970 24 69 7 )
( 1980 : 23 68 9 )

-
( 0- 1 5), - ( 1 5-64), -
( 65 ). -
. ,
. ,
.
, Distribution
.
struct Distribution {
int year , young , micldle , old ;
};

istream& operator>> ( istream& is , Distribution& d)


11 : ( : )

char chl ;
char ch2 ;
char ch3 = ;
Distribution dd;

if (is >> chl >> dd . year


>> ch2 >> dd . young >> dd . middle >> dd . old
ch3) {
if ( chl ! = ( ' 1 1 ch2 ! = ' : ' 1 1 ch3 ! = ' ) ' )
'

is . clear ( ios_base : : failbit) ;


return is ;

else
return is ;
d = dd;
1 5.6. 597

return is ;

,
1 . - ,
, . Distribution
>>.
" ", "
". Distribution
, .
, " ". ,

. ""
, (, ,
, , ),
.
Distribution,
.
strinq file name = " j apanese-aqe-data . txt" ;
ifstream ifs { file_name } ;
if ( ! ifs) rrr ( " l'l' " , file_name) ;

11

for (Distri.ution d ; ifs>>d; ) {


if ( d . year < base_year 1 1 end_year < d . year)
error ( "o " ) ;
if (d. younq + d . iddle + d . old ! = 100)
rrr ( "w conaCYJO'l'C" ) ;
11

. japanese-age-data . txt
. . ""
,
,
, . ,
japanese-age-data . txt
string, .
.
. >>
,
.
598 15

1 5.6.2.
?
1 5.6. ,
Open_polyline - .
.
"" .

. ,
, ( ) .
. . ,
2008, .
.
.
,
. - .
, -
, .
.
.
constexpr int XIDAX = 600 ; 11
constexpr int 400 ;
11 :
constexpr int xoffset = 100 ; 11
constexpr int yoffset = 60 ; 11

constexpr int xspace 40 ; 11


constexpr int yspace = 40 ;
11
constexpr int xlength xa.x-xoffset-xspace ;
constexpr int ylength = ymax-yoffset-yspace ;

()
( ).


, .

1 5.6..
,
. ,
. .
,
.
15.6. 599

xmax

yspace

ymax ylength
xoffset xspace
xlength

yoffset

constexpr int base_year = 1 960 ;


constexpr int end_year = 2040 ;

constexpr doue xscale = doule (xlenqth) / (end_year-base_year) ;


constexpr doule yscale = doule (ylenqth) /100 ;
(xscale yscale)
-
, .
,
doue (. 4.3.3).
,
(1960), xscale
xoffset. .
, .
( , ),
, .
class Scale { 11
int s ; 11
int v ; 11
doule l ;
pulic :
Scale (int , int vb , doule s ) : s () , vbase (v) , scale ( s )
{ )
int operator ( ) ( int v) const
{ return s + (v-vbase) *scale ; } // . 2 1 . 4
};

,
,
. :
600 1 5

Scale xs {xoffse t , base_year , xscale } ;


Scale ys {ymax-yoffset , O , -yscale } ;

,
ys , ,
, ,
. xs
. ys .
-

1 5.6..
, .
.
Window win { Point { l00 , 100 } , , m , "Aqing Japan" } ;

Axis {Axis : : x , Point{ xoffse t ,ymax-yoffset} , xlength ,


(end_year-base_year) /10 ,
"year 1960 1970 1980 1990 "
"2000 2010 2020 2030 2040 " } ;
x . lael . ove ( -100 , 0) ;

Axis {Axis : : y , Point { xoffse t , ymax-yoffset} , ylength , 10 ,


" % of population" } ;

Line current_year { Point { xs (2008) ,ys ( 0 ) } , Point { xs (2008) , ys ( 100) } } ;


current_year . set_style (Line_style : : dash) ;

Point{ xoffset , ymax-yoffset } ,


( 1960 , ) . ,
. . ,
.
.
base_year end_year, ,
, .
" " .
, .
. . ,
.
,
, .
.
"year 1960 1970 1980 1 990 "
"2000 2010 2020 2030 2040"
.
:
1 5.6. 601

"year 1960 1970 1980 1990 2000 2010 2020 2030 2040"

,
.
current_year ,
. ,

xs ys.
, .
Open_polyline .
Openolyline children ;
Openolyline adults ;
Openolyline aqed;

for (Distribution d; ifs>>d; ) (


if ( d . year < base__year 1 1 end__year < d . year)
error ( " o " ) ;
if (d . younq + d. iddle + d . old ! = 100 )
error ( "poe'l'I :r" ) ;
const int = xs ( d . year ) ;
children . add ( Point( x ,ys ( d . younq) } ) ;
adults . add ( Point ( x , ys ( d . iddle ) ) ) ;
aqed. add (Point ( x , ys ( d . old) } ) ;

xs ys
. " ", Scale,

-
.
,
.
Text children_lael ( Point ( 20 , children . point ( O ) . y ) , "aqe 0- 14 " } ;
children . set_color (Color : : red) ;
children_lael . set_color (Color : : red) ;

Text adults_lael ( Point ( 2 0 , adults . point ( 0 ) . y ) , " aqe 15-64 " ) ;


adults . set_color (Color : : ue) ;
adults_lael . set_color (Color : : u) ;

Text aqed_lael ( Point ( 20 , aqed . point ( 0 ) . ) , "aqe 65+" ) ;


aqed. set_color (Color : : dark_qreen) ;
aqed_lael . set_color ( Color : : dark_qreen) ;

Shape
Window
(. 1 4.2.3).
602 1 5

win . attach (children) ;


win . attach (adults) ;
win . attach (aqed) ;

win . attach (children_lael ) ;


win . attach (adults_lael) ;
win . attach (aqed_lael ) ;

win . attach (x) ;


win . attach (y) ;
win . attach (current__year) ;

qui_ain ( ) ;

main ( ) ,
Scale Dis tribution,
, Distribution.
, , .

Aging Japan (QJ


" of populetfon

yeer 1 960 1 970 1 980 1 990 2000 201 2020 2030 20-40


3
, .
1 . 600600 " ".
2 . , .
-
FLTK.
. Graph . Window . .
1 5.6. 603

4. 400 " l = = 20
" 20 .
(300,300).
5. .
Shape
.
l . doue one (doule ) { return 1 ; }
[- 1 0, l l J (.) (300,300),
400 ( ).
2. , 20
. .
3.
.
4. doule slope (doule ) { return /2 ; } .
5. Text
" / 2 " .
.
6. doule square (doule ) { return * ; } .
7. ( ).
8. .
9. sloping_cos ( ) ,
slope ( ) ( ) .
, .
1 . struct Person, name string
age int.
2. Person,
"GoofY" 63 ( cout) .
3. (>>) (<<) Person;
Person (cin)
(cout).
4. Person, name
age.
5. Person
- name ( ) age ( ) ,
.
6. >> <<
Person.
604 1 5

7. . .
age [, 1 50), na.me
; : " ' [ ] * & " % $ # @ ! .
error ( ) . .
8. Person
(cin) vector<Person>; (cout).
.
9. Person , na.me
first_na.me second_na.me.
.
>> . .


1 . ?
2 . .
?
. ?
.
4. ?
5. ? ?
6. , . ?
7. ?
8. ?
9. ?
1 0.
?
1 1 . ?
1 2.
?
1 3. ?
, ?
1 4. ?
?


1 5.6. 605

1 . ,
.
int fac (int n) { return n>l ? n*fac (n-1 ) : 1 ; } // !

, , fac ( 4 ) .
. , 4 > 1 , 4 *fac ( 3 ) , , ,
4 * * f ( 2 ) , , 4 *3*2 *fac ( 1 ) , .. 4 * 3 * 2 * 1 .
, . ,
, (recursive).
, 1 5.5, (iterative),
( while).
, fac ( )
, fac ( )
, 1 , 2, 3, 4 20.
fac ( ) ?
2 . Fct, Function,
, ,
. Fct ""
, ,
..
3. Fct .
,
. ,
.
4. sin ( ) . cos ( ) , sin () + cos () sin () *
*sin () +cos () *cos () . .
5. "" ( 1 5. 5) 1-1/3+1/5-1/7+1 / 9-1/11+ . . .
(Leibniz) rt/4.
6. .
vector<doue>,
N
"", .. .
7. ,

. .
8. ,
( ) :
( 1 70,7), ( 1 75,9), ( 1 80,23), ( 1 85, 1 7), ( 1 90,6), ( 1 95, 1 ) .
? . .
606 1 5

.
.
9.
,
. , ClllA
.
,
. .

, .
1 0 .
?
(, ).
1 1 .
(, , , ;
"")
. , , , ..

.
, ,
. ,
- .
, ?
" "? :
. ;
, ,
, .
-!

(grap
hical user interface - GUI)
,
, ,

.
-
.
,
.
, , ,

.
"
" .
.
,
1 7 1 8 . II
.
608 1 6

1 6. 1 . 1 6.4. Button
Widget
1 6.2. Next 1 6.4. 1 . Widqet
1 6.. 1 6.4.2. Button
1 6.3. 1 . 1 6.4.. In Out
- -

1 6.3.2. 1 6.4.4. enu


1 6.3.3. - 1 6.5.
1 6.6.
1 6.7.
1 6.8. G U l-

1 6. 1 .
. .
, ,
, -
.
.
,
,
(. ). .
.
/ ,
. (.
). .
.
iostream ++ (. 1 0 1 1 ).
.
(. 1 2- 1 5).
.
r
. , ,

( . .
, ..). ( )

.
. ,
"" Windows/Mac,
.
1 6.2. Next 609

-.
(markup language), HTML,
(scripting language).
,
.
(
).
,
,

.


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

.
,

.

1 6.2. Next
Next,
1 2- 1 5?
. ,

. ,
, .
, ,
,
.
1 2- 1 5 :
610 16

// / ,

11 win
win . wait_for_button ( ) ;

11 / ,

11 win
win . wait_for_button ( ) ;

// / ,

11 win
win . wait_for_button ( ) ;

, wait_for_button ( ) .
, ,
.
,
( ).
:
11 / ,
11
cin >> var ; //

11 / ,
//
cin >> var ; //

11 / ,
//
cin >> var ; //

. cin >> var,
, "" ,
. (
),
, : . -
( ..).
- , .
, (,
"- Next'").
, , .
, -
.
,

; , -
1 6.3. 611

,
, ,
.
", ,
"; . ",
, - ,
,
". ,
.
"" - . -
, ,
.

1 6..
"" (..
)

.
, -
"". ,
(callback functlons),
. .
.
.
,
.

.
,
.
. - Window,
( Simple_window . h) Simple_window,
next_button.
struct Siple_window : Graph_lib : : Window {
Siple_window ( Point , int w , int h , const strinq& title) ;

void wait_for_button ( ) ; 11
private :
Button next_button ; 11 Nex t
bool button_pushed; 11
61 2 1 6

static void next (Addre ss , Addre ss ) ; 11


11 nex t_bu t ton
void next ( ) ; 11 ,
11 nex t_but ton
};

. Simple_window Window
Graph_lib.
. Graph_lib :
: Window, ( FLTK)
.
Window ..
Simple_window.
Simple_window : : Simple_window ( Point , int w , int h , const strinq& title)
: Window { xy , w , h , title } ,
next_button { Point{ x_.ax ( ) -70 , 0 } , 70 , 20 , "Next" , c_next } ,
button_yushed{false }

attach (next_button) ;

. Simple_window
(). (w, h) (ti tle) Window
Graph_lib .
next_button (Point (x_max ( ) -70 , 0 ) ;
- ). (70, 20). ("Next")
(c_next).
, Window:
.
attach ( ) next_button
Simple_window; . .
.
.
button_pushed -
; .
next ( ) .
. .
pri va te. .
struct Simple_window : Graph_li : : Window {
Simple_window (Point , int w , int h , const strinq& title) ;
void wait_for_button ( ) ; 11
11 . . .
};

. .
.
1 6.. 61 3

1 6.. 1 .
_next ( ) - .

, . -

, ,
(callback function).
c_next ( ) (_ - "callback").
, , -
, . , _
next , Next.
next "".
, .


_ R

/GUI

/GUI

"" .
,
FLTK, . .
.
. - ,
.
next ( ) . _next ( )
Simple_window ;
- "- " c_next ( ) ,
Next.
(
) ,
.
++. , Simple_window
614 16

Button. .
. , ,
,
.
.
- , :
static void c_next (Addre ss , Address) ; 11
11 nex t b u t ton
s tatic , c_next ( )
. ..
, . -
, .
,
,
-. Address , _next ( )
. w- ". .
++, ,
. . ,
w-".
.
,

( Widget).
. ,
. - ,
Widget; _next ( )
Simple_window.
:
void Siple_window : : c_next (Addre ss , Addre ss pw)
// Simple_ window: : t () pw
{
reference_to<Siple_window> (pw) . next ( ) ;

reference_to<Simple_window> (pw) .
, pw,
Simple_window; .
reference_to<Simple_window> (pw)
Simple_window. 1 7 1 8
. reference_to (,
) . 1 .
- Simple_window
,
1 6.3. 61 5

,
- . - next ( ) .
_next ( ) ,
, ,
, -
,
,
.
_next ( )
- next ( ) .
next ( ) , ( -
).
,
, , "
": c_next ( ) -
- , next ()
. , ( "")
, ; , .
1 6. 5- 1 6.7. , .
Simple_window.
Simple_window next_button
.
next_button
,
c_next ( ) .
_next ( )
- next ( ) .
next ( )
.
. ,
,
( ) . ,
.
,
.
.

.
61 6 1 6

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

.

1 6. .2.
. next () Simple_window
() ? ,
,
. .
. , , next ( )
.
1 1 / ,
11

win . wait_for_button ( ) ; //

// /
. wa i t for
button ( ) .
void Simple_window : : wait_for_button ( )
11 :
// ( )
11 ,
11 b u t ton_pushed t rue .

while ( ! bu ttonyushed) Fl : : wai t ( ) ;


buttonyushed = false ;
Fl : : redraw ( ) ;

.
FLTK ,
. - .
FLTK wa i t ( ) . wa i t ( )
,
, . ,
Microsoft Windows
1 6.3. 61 7

,
. , Window
. Fl : : wai t ( ) ,
. . wai t ( )
- , ,
- .
, - Next. wai t ( )
_next ( ) (
). wait_for_button ( )
next ( ) button_
pushed true. :

void Siple_window : : next ( )


{
button_pushed = true ;

, - button_pushed.
bool button_pushed; //
11 fa l se
wai t_for_button ( )
button_pushed redraw ( ) ,
.
.

1 6... -
, Widget
: -
, - :
struct Siple_window : Graph_lib : : Window {
Siple_window{ Point , int w, int h , const strinq& title } ;
void wait_for_button ( ) ; 11
private :
Button next_button ; // "Nex t "
bool button_pushed; //
static void c_next (Addre ss , Addre ss ) ; 11
11 nex t bu t ton
void next ( ) ; 11 nex t_ b u t ton
);

- ( 1 5 . 3 . 3) ,
_next ( ) .
Simple_window:
Siple_window : : Siple_window ( Point , int w, int h ,
const strinq& title)
61 8 1 6

: Window { xy , w , h , title } ,
next_button { Point { x_ax ( ) -70 , 0 } , 70 , 20 , "Next" ,
[ ] (Addre ss , Addre ss pw)
{
reference to<Simple_window> (pw) . next ( ) ;

} 1

button_Pushed{ false }
{
attach (next_button) ;

1 6.4. Button Widget


, .
struct Button : Widqet {
Button (Point , int w, int h , const strinq& lael ,
Callback ) ;
void attach (Window&) ;
};
Button Widget
, w h, lael
. , , -
- (, ),
Widget.

1 6.4. 1 . Widqet
(wtdget) .
-

, (control) .
-



. Widget :
class Widqet {
// Widge t Fl_ widge t ,
-

// F1_ widge t ;
// ,
11 FLTK
pulic :
Widqet ( Point , int w, int h , const strinq& s ,
Callback ) ;

virtual void move ( int dx , int dy) ;


virtual void hide ( ) ;
virtual void show ( ) ;
virtual void attach (Window&) ;
1 6.4. Button Widget 61 9

Point loc ;
int width ;
int heiqht;
strinq lael ;
Callback do_i t ;

protected:
Window* own ; // Widge t
11 Window
Fl_Widqet* pw; // Widge t FLTK
1;
Widget ,
Button ( ,
Widget, enu; . 1 6.7).
hide ( ) Widget .
show ( ) Widget .

Widget .
Shape, move ( )
Widget , ,
, attach ( ) .
, attach ( )
(. 1 4.3.5): , Widget,
,
Window. attach ( )
. attach ( ) Window
attach ( ) . ,
- ,
.
,
.

Widget

Window

Widget
620 1 6

, Window ,
Widget . 1 4.4.
-
Window
Widget. Widget ,
Window .
.
-. own pw
, protected.
Widget (Button,
Menu ..) GUI . h.

1 6.4.2. Button
Button - Widget, .
, , - .
class Button : puic Widqet
puic:
Button ( Point , int ww, int hh , const strinq& s ,
Callback )
:Widqet { xy , ww , hh , s , c } { }

void attach (Window& win) ;


};
. attach ( ) ()
FLT. (
, , 1 7 1 8). .
Widget .
,
( )
. ,
,
. ,

. , ,
,
, .

1 6.4.. In Out
,
Widget.
struct In_box : Widqet {
In_box ( Point , int w, int h , const strinq& s )
1 6.4. Button r Widget 621

:Widqet { xy , w , h , s , 0 } { }
int qet_int ( ) ;
strinq qet_strinq ( } ;

void attach (Window& win) ;


};

struct Out_ox : Widqet (


Out_ox (Point , int w , int h , const strinq& s )
:Widqet { xy , w, h , s , 0 } {
void put (int) ;
void put (const strinq&) ;

void attach (Window& win) ;


};
In_ , ,
get_string ( )
- get_int ( ) .
, , get string ( )
, .
strinq s = some_inox . qet_strinq ( ) ;
if (s == " " ) {
11

Out_box ,
. In_
put ( ) , .
In_box Out_box 1 6. 5 .
get_floating_point ( ) , get_
complex ( ) . ,
s trings tream -
, (. 1 1 .4) .

1 6.4.4. Menu
.
struct enu : Widqet {
enum Kind { horizontal , vertical } ;
enu (Point , int w, int h , Kind kk , const strinq& lael ) ;
Vector ref<utton> selection ;
ind k ;
int offset;
int attach (Button& ) ; 11
int attach (Button* ) ; 11 ,
11 new
622 1 6

void show ( ) //

for (Button& : selection) b . show ( ) ;

void hide ( ) ; //
void move (int dx , int dy) ; //

void attach (Window& win) ; // win


};

, Menu .
, Point .

. 1 6. 5 1 6. 7 .
( ) - Widget,
Menu ttach ( ) . ,
enu attach ( )
,

. Menu
Vector_ref ( 1 3. 1 0 .4).
(pop-up menu), (
1 6. 7).

1 6.5.

,
, , .
1 6.5. 623


( ; . 1 3. 6),
. ,
(. ) next next ;
Next point.
current (, ) , ,
.
current (, ),
,
( current (, ))
(, ), (, )
.
. ,
Quit .
,
: ,
. , ,
.
.

. .
struct Lines_window : Window {
Lines_window (Point , int w , int h ,
const strinq& title ) ;
Open_J?olyline lines ;
private :
Button next_button ; // (nex t_ x , nex t_y) l i nes
624 1 6

Button quit_button ;
In next_x ;
In_box next_y;
Out_ox xy_out ;

void next ( ) ;
void qui t ( ) ;
};
Open_polyline.
- Button, In_box
Out_ox, -,
. -
, .
Lines_window .
Lines_window : : Lines_window ( Point , int w, int h, const strinq& title)
:Window { xy , w , h , title } ,
next_button{ Point{ x_ax ( ) -150 , 0 } , 70 , 20 , "Next point" ,
[ ] (Addres s , Addre ss pw) {
reference_to<Lines_window> (pw) . next ( ) ;
} 1

quit_button{ Point{ x_max ( ) -70 , 0 } , 70 , 20 , "Quit" ,


[ ] (Address , Addre ss pw) {
reference_to<Lines_window> (pw) . quit ( ) ;
} 1

next_x{ Point{x_max ( ) -310 , 0 ) , 50 , 20 , "next : " } ,


next_y{ Point{x_max ( ) -2 10 , 0 } , 50 , 20 , "next : " } ,
xy_out{ Point { l00 , 0 } , 100 , 20 , "current ( , ) : " }

attach (next_button) ;
attach (quit_button) ;
attach (next_x) ;
attach (next_y) ;
attach (xy_out) ;
attach ( lines) ;

, ,
.
Quit .
FLTK, .
void Lines_window : : quit ( )
{
hide ( ) ; 11 FLTK

Next point:
, Open_polyline, r
.
1 6.5. 625

void Lines_window : : next ( )

int = next_x . get_int ( ) ;


int = next_y . get_int ( ) ;
lines . add (Point { x , y } ) ;

11
ostringstream s s ;
ss << ( 1 << << , 1 << <<
1 1 1
) ;
1

xy_out . put ( ss . str ( ) ) ;

redraw ( ) ;

. get_int ( )
In_box: os trings tream
I Out_box; -
s tr ( ) ostringstream.
, redraw ( ) , I :
,
redraw ( ) Window.
?
main ( ) .

#include "GUI . h"

int main ( )
try {
Lines_window win{ Point { l00 , 10 0 ) , 600 , 400 , "lines" } ;
return gui_main ( ) ;

catch (exception& ) {
cerr << "1JDD : " << . what ( ) << ' \n ' ;
return 1 ;

catch ( . . . ) {
cerr << "- JU\n" ;
return 2 ;

, , ! main ( )
win gui_main ( ) .
, if swi tch, - ,
6 7, -
gui_main ( )

run ( ) FLTK. . .
run ()
while (wai t () ) ;
626 16

,
. .
. .
?

1 6.6.
:
:
, - . ,
, .
"", ,
- . , wai t ( )
.
wai t ( ) ,
,
. FLTK
wai t ( )
,
.
"" .




----

" "



1 6. 7. 627

" "
,
. ,
. ,
,
.
( - 26).
.
. ,
.
, ,

. ,
,
, , ,
. ,

.
,
?
, , ,
1 6. 5 . next ( ) Lines_window.
Next point,
In_ox (next_x next_y), - lines
Out_ox (xy_out). , ,
, : ,
.. ,
.

1 6. 7.
,
" ",
. .
- lines.
color_menu .
struct Lines_window : Window {
Lines_window (Point , int w , int h , const strinq& title) ;

Open_J?olyline lines ;
enu color_enu ;

11 :
628 16

static void c_red (Addre ss , Address ) ;


11 :
static void c_lue (Address , Address) ;
11 :
static void c_ack (Address , Address) ;

11 :
void redyressed ( ) change ( Color : : red) ;
void uyressed ( ) change ( Color : : ue) ;
void ackyressed ( ) change ( Color : : ack) ;

void change (Color ) lines . set_color (c) ;

11 . . . , . . .
};


"" - .
.
.
,
-.
.
color_menu, .
Lines_window : : Lines_window ( Point , int w , int h ,
const string&title}
:Window ( xy , w , h , title } ,
// . . . . . .
color_menu( Point ( x_AX ( ) -70 , 40 } , 70 , 20 ,
enu : : vertical , "color" }

11 . . . . . .
color_menu . attach (new Button ( Point ( 0 , 0 } , 0 , 0 ,
"red" , c_red} ) ;
color_menu . attach (new Button( Point ( 0 , 0 } , 0 , 0 ,
"lue" , _u } ) ;
color_menu . attach (new Button ( Point ( 0 , 0 } , 0 , 0 ,
"lk" , _k } ) ;
attach ( color_menu) ;

, (
ttach ( ) ) / .
Menu : : attach ( ) ,
. .
.
1 6.7. 629

, ,
. . . ,
, ,
. color menu.
, ,
, , color menu .
, .

color menu .
color menu .
630 1 6

, color menu . .
. u,
. color menu .

.. . color menu
,
. Lines window
.
struct Lines window : Window
Lines_window (Point , int w , int h , const string& title) ;
1 6. 7. 631

private :
11 :
Open_polyline lines ;

11 :
Button next_button ; // (nex t_x , nex t_y) l ines
Button quit_button ; //
In next_x;
In_box next_y ;
Out_box xy_out ;
Menu color menu ;
Button menu_button ;

void chanqe (Color ) { lines . set_color (c) ;

void hide menu ( ) { color menu . hide ( ) ; menu_button . show ( ) ;

11 , :
void red_pressed ( ) { chanqe ( Color : : red) ; hide_menu ( ) ; }
void ue_pressed ( ) { chanqe ( Color : : u) ; hide_menu ( ) ; }
void ack_pressed ( ) { chanqe (Color : : ack) ; hide_menu ( ) ; }
void enu_pressed ( ) { menu_button . hide ( ) ; color_menu . show ( ) ; }
void next ( ) ;
void qui t ( ) ;

11 :
static void c_red (Address , Address) ;
static void c_ue (Address , Address) ;
static void c_ack (Address , Address) ;
static void c_menu (Address , Address) ;
static void c_next (Addres s , Address) ;
static void c_quit (Addres s , Address) ;
};

. , ,
. . . ,
, ,
. , . ,
.
.
Lines_window : : Lines_window ( Point , int w, int h ,
const strinq& title)
: Window{ xy , w, h , title } ,
next_button { Point{ x_max ( ) -150 , 0 } , 70 , 20 ,
"Next point" , c_next} ,
quit_button { Point { x_max ( ) -70 , 0 } , 70 , 20 ,
"Quit" , c_quit} ,
next_x{ Point {x_max ( ) -310 , 0 } , 50 , 20 , "next : " } ,
next_y{ Point {x_max ( ) -2 10 , 0 } , 50 , 20 , "next : " } ,
xy_out{ Point { lOO , O } , 100 , 20 , "current ( ,) : " } ,
632 16

color_menu{ Point{ x_max ( ) -70 , 30 } , 70 , 20 ,


enu : : vertical , "color" } ,
menu_button { Point{x_max ( ) -80 , 30 } , , 20 ,
" color menu" , c_menu}

attach (next_button) ;
attach (quit_button) ;
attach (next_x) ;
attach (nextJ) ;
attach (xy_out) ;
xy_out . put ( "no point" ) ;
color_menu . attach (new Button { Point { 0 , 0 } , 0 , 0 , "red" , c_red) ) ;
color_menu . attach ( new Button { Point { 0 , 0 } , 0 , 0 , "lu" , _u) ) ;
color_menu . attach (new Button { Point { 0 , 0 } , 0 , 0 , "lk" , _k) ) ;
attach (color_menu) ;
color_menu . hide ( ) ;
attach (menu_button) ;
attach (lines) ;

,
-. .

. ()
,
.

1 6.8. GUl-

, : ,
. ,

. main ( ) .
int main ( )
{
Lines_window{ Point { l00 , 10 0 } , 600 , 400 , " lines " } ;
return qui_ain ( ) ;

? , ,
; ,
- .
?
.
(, ,
).
1 6.8. GUl- 633

. ,

.
.
.
.
r
.
, ,
- ,
.
.
(). -
. .
.
, ? (. 1 6.5).
int main ( )
{
Line_window win{ Point { l00 , 100 } , 600 , 400 , "lines " } ;
return qui_main ( ) ;

w" win Lines_window.


, .
, , ,
. .
, .
,
. ,
. ?
.

.
( ) .

. .

,
-
"" ( .
++).
634 1 6

, ,
Shape Widget -
, -
. ,
, .
11
void load_disaster_menu (enu& m)
{
Point oriq { 0 , 0 } ;
Button l { oriq , 0 , 0 , 11flood11 , c_flood} ;
Button b2 { oriq , 0 , 0 , "fire" , c_fire } ;
11 . . .
m . attach (l ) ;
m . attach (b2 ) ;
11 . . .

int main ( )

11 . . .
enu disasters { Point{ l00 , 100 } , 60 , 20 , Menu : : horizontal ,
"disasters " } ;
load_disaster_menu (disasters ) ;
win . attach (disasters) ;
11 . . .

.
load_di sas ter_menu, - .
1 8.6.4 (
n.
8 . 5 . 8 . ,
load_di sas ter_menu ( )
, di sasters
() . , ,
.
,
new.
11
void load_disaster_menu (enu& m)
{
Point oriq { 0 , 0 ) ;
m . attach (new Button { oriq , 0 , 0 , 11flood11 , c_flood} ) ;
m . attach (new Button { oriq , 0 , 0 , "fire " , c_fire } ) ;
11 . . .
, (, ,
) .

1 .
FLTK ( .)
2. Graph_l ib.
1 6. 5 .
3. .
, 1 6.7, .
4. .
. .

r
1 . ?
2. ?
3. ?
4. ?
5. -
++?
6. ?
7. ?
8. ?
9. "FLTK"?
1 0. GUI ?
1 1 . .
?
1 2 . .
1 3. ?
1 4. ?
1 5. ?
1 6. ?
1 7. ?
1 8.
.
636 16

1 9.
,
-?

11

/ -
-


1 . y_window, Simple_window,
, , next qui t.
2. ( _window) 44
.
,
( ).
. Image Button;
. ..
" "
s td_lib_facilities . h:
#include<random>
inline int rand_int ( int min, int max)

static default_random_engine ran ;


return uniform_int_distribution<> {min , max } (ran) ;

[min,max).
4. "". "". "
" "". (
) .
, .
5. ,
Next.
,
.
6. " ", .. .
. .
- ,
1 6.8. GUl-nporpa 637


(, ).
. : clock ( ) , sleep ( ) .
7. ,
, . Start Stop.
8. .
.
(, ) .
9. 7 ,
, .
1 0. ,
(. sin ( ) log ( ) )
,

- .

. ,
(, GUI
).

, .
, , , ,
,
/ ,
.
, GUI
-, ,
,
..
, , .
,
. ,
; /
.
111


lf vector.'
- /.uJU- (& .!)



++, STL.

STL . ,

,
STL,
.
- ,
.


STL: vector .
642 1 7

1 7 . 1 . 1 7 .6.
1 7.2. 1 7.7.
1 7.3. , 1 7.8. : void*
1 7.3. 1 . sizeof
1 7.4. 1 7.9.
1 7.9. 1 .
1 7.4. 1 .
1 7.9.2. , -
1 7.4.2.
1 7.4.3. 1 7.9.3. :
1 7.4.4. 1 7.9.4.
1 7.4.5. 1 7.9.5 .
1 7.4.6. 1 7 . 10. this
1 7 5
. . 1 7.10. 1 .
1 7 .5 . 1 . r
1 7.5.2.

1 7 . 1 .

++ vector.
.
, push
back ( ) , ,
s i ze ( ) , ,
.
- , , ( ,
)
. s tring
, , list
map, 20.

. , -
, - . .
vector<doue> v . push_back ( 2 . 3 )
2 . 3 doue
v (v . size ( ) ).
, push back ( ) ; ,
, - .
,
vector, ,
1 7 . 1 . 643

. ,
,
++.
, vector,
.
,
vector,
. map,
. ++
( 20 2 1 ) . .
,
.
,
++,
.
- sort ( ) .
vector
.
vector.
. ,
vector,
vector, ++.

.
,
. .
1 7. ? ,


?
(). ,
( ) .
1 8. ?
? ,
.
1 9.
?
?
++ .
,
,
, ( )
644 1 7

.
.
, . ?
vector s tring :
. , , vector string,
,
. ,
, .
. .. ,
vector , ? ,
,
.
, - (
, ..), .
,
.
,
.
"
", , .
" ",
(
) . ,
.
++, .
, (
)
. ++, ,
.
I ,
, ,
-
,
, .

1 7.2.
vector
.
vector<doule> aqe ( 4 ) ; 11
aqe [0 ] =0 . 33 ; // d
aqe [ 1 ] =2 2 . 0 ;
1 7 .2. 645

aqe [ 2 ] 2 7 . 2 ;
q [ ] "54 . 2 ;

, rr acca vector
doule . 33, 22 . , 2 7 . 2 54 . 2 .
, 1 , 2 3.
++ .
,
, ++.
rracca vector .
, aqe .
() size- 1 . , aqe
aqe . size ( ) - 1 . aqe
.
age:
4
age[O]: age[ 1 ] : age[2]: g[]:
0.33 22 . 27. 2 54.2
" " ?
? ,
rr acc vector. , rrac
ca -
. ,
?
vector, :
rr acc .
, . ?
, .
.
clase vector {
int size , aqeO , aqel , aqe2 , q ;
11 . . .
};
, ,
.
age:
slze: age[O] : age[ 1 ] : age[2] : age[3] :
4 0.33 22 .0 27. 2 54. 2
646 1 7

,
push_back ( ) ,
: ,
. ,
, .
. vector,
push_back ( ) , ,
vector . ,
,
. -
. ++ , ,
(pointer). * ,
doule* doule.
vector.
// doue
class vector {
int sz ; 11
doue* elem; 11 ( doul e)
pulic :
vector ( int ) ; /! : s
11 doul e ,
11 elem s s z
i n t size ( ) const { return sz ; )
//
};
vector,
"" . "" (
"") - ""
++.

1 7 ,
. .


. .
(address) ,
. .
, 1 ..
-

2 220_ ,

1''-- -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
, -. , :
int var = 17 ;
1 7.. . 647

.
int, var
1 7 . ,
. . , . , ,
in t,
int int*.
int* ptr = &var ; // ptr va r

,
&. , var ,
4096 ( 2 12), ptr
4096.

1 2 ptr 2 1 2 2 20- 1
@
1 1 1 1_-_-_-_-_-_-_ -_-_ -_-_-
_-_-_-_-_-_ -_ -_-_-_ ?:_-_-_------ -0
,
,
.
, .
, :
int = 17 ;

int* pi = & ; // i n t

doue = 2 . 71828 ;
doule* pd = & ; // do ue

, ,
, *, :
cout << "pi==" << pi << " ; pi==" << *pi << 11\n" ;
cout << "pd== " << pd << " ; pd==" << *pd << 11\n" ;

*pi 1 7 , *pd -
doue, 2 . 7 1 8 2 8 . pi pd ,
. ,
(),
, ;
( .2. 1 . 1 ) .

:
*pi = 27 ; 11 Ofi: 2 7 i n t ,
11 pi
*pd = . 1415 9 ; 11 : 3 . 1 4 1 5 9
11 dou e , pd
648 1 7

*pd = *pi ; 11 : i n t ( *p i )
1 1 doue ( *pd)

, ,
,
. " int?" . -

, .
, int
( ) .
.
int i = pi ; // : in t * i n t
pi = 7 ; // : i n t i n t *

char (.. char* ) int


(.. int* ) . :
= pi ; 11 : i n t * cha r *
pi ; 1 1 : cha r * i n t *

pi?
. char int:
.
char ehl = ' ' ;
ehar eh2 = ' ' ;
ehar h = 1 1 ;
char eh4 = ' d ' i
int* pi = &h ; 11 h , ,
11 cha r . : cha r *
11 in t *
11 , ,
*pi 12345 ; 11 ,
*pi 678 90 ; 11 i n t

,
, , , .
h : pi:

,
12345 , &h.
, . . ch2 ch4 .
( )
pi l *i=7890
67890 . ,
,
.
1 7.. . 649

,
int ,
reinterpret_cast ( -
1 7.8).
.
.

. ,
, vector. ,
, (.
25). ,
,
. -
,
. ,
1 8 1 9 ,
, vector.

1 7 . . 1 . sizeof
in t? ?
sizeof.

void sizes ( char ch , int I , int* pi )


{
cout << " char "
<< sizeof ( char) << 1 1
<< si zeof ( ch) << ' \n ' ;
cout << " int "
sizeof ( int) << 1 1
<< sizeof ( i ) << ' \n ' ;
cout << " int* "
<< sizeof ( int* ) <<
<< sizeof (p) << ' \n ' ;

, sizeof ,
: sizeof
, - .
sizeof ,
sizeof ( char) ,
1 . , char ,
sizeof .


. ,
.
bool, doue .
650 1 7 11


++. sizeof ( int)
. 8-
, int 32 .
int 1 6 ,
int 64 .
vector? .
vector<int> v ( l000 ) ; // 1 0 0 0
cout << " vector<int> ( lO O O ) "
<< si zeof (v) << 1 \n ' ;

:
vector<int> ( l0 0 0 ) = 20


(. 1 9.2. 1 ) . , sizeof
.

1 7 .4.
vector,
1 7. 2 . I)J,e vector ?
elem . ?
, ++,
1 ( )
(
). , ,

( ) .
;
.
. .


1 7 .4. 651

++ (
(heap)) new:
doue* = new doule [ 4 ] ; // 4 doue
11


doue
.
.
.
:

new . .
new (),
. .
, new, *:
char* q = new doule [ 4 ] ; // : doule *
11 cha r *

new doue,
doue char, (
) doue
char.

1 7 .4. 1 .
new (allocation)
(free store).
new .

.
.
.
652 17

new ,
:
int* pi new int ; // i n t
int* qi = //
new int [ 4 ] ;
11 i n t ( )
doule* pd = new doule ; //
11 doue
doule* qd = new doule [n] ; //
/ / doue

,
. , ,
,
. n 2, .

pi: __:-----!

qi: >---- !

-

pd:
qd:
.
.
p i = pd; // : dou e * in t *
pd = pi ; // : i n t * doul e *

? ,
int doue, .
[ ] . ,. ,
. , qi [ 2 ]
, int qi [ ] , qd [ 2 ]
, doue
qd [ O ] . int doule,
, , qi ,
qd,
.
.
:
(type errors).
1 7.4. 65 3

1 7 4 2
. . .

*.
[ ] :
doule* = new doule [ 4 ] ; //
11 d
doule = * ; //
//
doule = [2 ] ; //
//

vector.
. . [ 2 ] ;
[] . [ ] , *
-

[ ] * .
* = 7.7; 11
11
[2 ] = 9 . 9 ; 11
11

. .
.
.
doule = * ; // ,
* = 8 . 9 ; // ,

[ ] .
( ,
), .
doule = [ ] ; //
//
[3] = 4 . 4 ; //
11
doule = [ ] ; // { ] - , *

. . -
.
[] : [ 1 ) : [2] : [] :
8.9 9.9 4. 4

vector.
654 17

1 7 .. .
, , ,
,
. .
doule* pd = new doule [ 3 ] ;
pd [2 ] 2.2;
pd [ 4 ] 4.4;
pd [-3) -3 . 3 ;
, pd?
pd [ 4 ] ?
pd, "" "" .
; .
, .
pd [ -3 ] .
doue ,
pd.

pd :

pd [-3] : pd [ -2] : pd[O] : pd [1] : p d[2] : pd[3] : pd [4] :


-3.3 2.2 4.4

, ,
pd [ -3 ] pd [ 4 ] . .
,
doule, pd. ,
, . .
. ""
" - ", "
". ;
. , .

, ,
, .
, ,
,
. ,
, -
""
. , .
, .
: ,
1 7.4. 655

, ,
.
(" ") .
,
. ,
vector,
new, , vector

.

. ,
doue* doue*
, .
, . .
doue* = new doue ; // doue
doule* q = new doule [ lOOO ] ; // do u e

q [ 700] = 7 . 7 ; 11
q = ; 11 q ,
doule d = q [ 700] ; 11 !

. q [ 7 0 0 ]
,
.


q
q
:_-_-_-:_-_-_-_-_-_-_-_-_-:_-_-_-_-_-
! -
, , "
?" , ,
. , -
vector . ,
++, ,
" ",
.

, . . ,

.
656 1 7

1 7 4 4
. . .

. , - ,
; .
, , ,
. .
doule* ; // :
//
doule* pl new doule ; //
// dou e
doule* 2 new doule (S . 5 ) ; //
// dou e 5 . 5
doule* 3 = new doue [S] ; //
// dou e

,
. .
* = 7.0;
7 . .
, .
, .
, :
.
,
,
-
. , ,
, - , ,
.
- .
.
.
, new ,
.
, -
, 2 :
*2 5 . 5.
( ) , .
, [ ] !
,

,
new, :
doue* 4 new doule [ S ] ( 0 , 1 , 2 , 3 , 4 } ;
doule* 5 = new doule [ ] ( 0 , 1 , 2 , 3 , 4 } ;
1 7.4. 657

4 doue,
. . 1 . . 2 . , 3 . 4 . . 5;
,
.
,
, ,
. ,
(
). , ,
.
. ,
. .
.
.
.
, :
* pxl new ; // ,
//
* 2 new [ 1 7 ] ; // 1 7 ,
11

, ,
:
* pyl = new ; // :
* 2 = new { 13 } ; // : ( 1 3 }
* 3 = new [ 1 7 ] ; // :
* 4 = new [ 1 7 ] ( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 } ;

new ,
(
). .

1 7 .4.5.
,
,
nullptr.
doue* = nullptr ; //


(null pointer).
(.. , -)
nullptr:
if ( != nullptr) //
658 1 7

,
(, )
, delete (.
1 7.4.6). ,
. nullptr,
if , .
if () // ;
1 1 p O ! =n u l lp t r

, ,
" ",
.
.
- , .
, ; :
, ,
? ,
?
nullptr ++ 1 1 ,
() NULL
nullptr. /
,
nullptr.

1 7 4 6
. . .

new .
,
, .
.

. .
doule* calc ( int res_size , int ) //
{
doue* = new doue [m.ax] ;
doue* res = new doule [res_size] ;
11
11 res
return res ;

doule* r = calc (l00 , 1000) ;


calc ( ) "
" , doule,
1 7 .4. 659

. , calc ( 100 , 1000)


,
doue.
, , delete.
, delete
, new:
doule* calc ( int res_size , int )
// , res ,
//

doule* = new doue [m.ax] ;


doule* res = new doule [res_size] ;
//
// res
delete [ ] ; // ,
return res ;

doule* r = calc ( l00 , 1000 ) ;


11 r
delete [ ] r ; // ,

,
:
.
delete :
delete , new
;
delete [ ] ,
new .
.
- .
.

int* = new int ( S ) ;


delete ; // : ,
// new
11 . . .
delete ; // : ,
11

delete .
,
,
delete
.
1 7


, ,
: (
) .
.
.
(
),
:
int* = ;
delete ; // :
delete ; // - -

?
, ,
? .
(automatic garbage collection)
(garbage collection) . , -
.
,
. (.
www . stroustrup . com/C++ . html). ,
"", ,
.
? ,
"",
.
, ( . 25).
, -
,
. , , .

. .

(Unix, Windows - ),
. ,
,
, , ,
. ,
, ,
, .
1 7 .5. 1 661

1 7 5
. .

, .

.
11 dou e
l vector {
int z ; 11
doul111 * 111 1 111111 ; 11
pulic :
vector {int ) //
: z { } , 11 s z
111 l111111 { n111w doul111 [ ] } 1 1 elem
{
for ( int iO ; i< ; ++i ) 11
111 l 111111 [ i ] O ; 11

int iz111 ( ) cont { r111 turn z ; } //


11 . . .
};
, sz.
, vector
, size ( ) .
new, ,
new, elem.
,
( . ). vector
, .
, vector
.
new. . 1 7.4,
delete. .
void f ( int n)
{
vector v (n} ; 11 doue
11 . . .

f ( ) v,
, . clean_
up ( ) - vector :
void f2 ( int n )
{
vector v (n) ; 11 ,
11 i n t
// . . . v . . .
662 1 7

v . clean_up ( ) ; // clean () elem

.
, , ,
delete.
clean_up ( ) : .
.
, , ,
, . -
(destructor) .
,
, -
. ,
. , , ,
, .
.
class vector {
int sz ; 11
doule* l; //
pulic :
vector ( int s ) 11
: sz { s } , 11 s z
ele{ new doule [ s ] } 11 elem
{
for ( int i=O ; i<s ; ++i} //
ele [i ] =O ; //

vector ( ) 11
{ delete [ ] l; } //
};

:
void f ( int n )
{
doule* = new doule [n] ; //
11 d
vector v (n) ; // (
11 d }
11 . . v
.

delete [ ] ; // ,
// d
// vector , v

delete [ ]
! vector,
new,
delete [ ] .
1 7.5. 663

vector. , vector
, . .

. .
. ,
- . , .. ,
ios tream? ,
. .. .
, "" - ,
.

1 7 .5.1 .
,
, :
struct Custoer {
string name ;
vector<string> addresses ;
11 . . .
};

void s_fct ( )

Customer fred;
11 fred
11 fred

some fct ( ) fred


, : ,
name addresses.
, .
: "
Customer. ".

.
( )
( ,
). . : "
. " (
, delete ..).
664 17

1 7 5 2
. . .

,

++. .
-
, .

.

, .
"-"
vector, .
1 9. 5 .
.
.
Shape* fct ( )
{
Text tt { Point { 200 , 200 } , "Annemarie" } ;
11 . . .
Shape* = new Text { Point { l00 , 100 } , "Nicholas " } ;
return ;

void f ( )
{
Shape* q = fct ( ) ;
11 . . .
delete q;

- .
, . .
, .
fct ( ) tt Text (. 3. 1 1 ), ,
. Text string,
. - s tring
, vector.
tt :
Text, 1 7. 5 . 1 .
Text, fct ( ) ? f ( )
, q Text:
. Shape.
delete q Text?
1 7 .6. 665

1 4. 2 . l , Shape
. Shape .
, .
delete q. delete q,
. , , .
delete q -Shape ( ) Shape.
-Shape ( ) ,
(. 1 4.3. 1 )
. Shape, -
-t ( ) . Shape : : -Shape ( ) ,
Text : : -t ( ) Text,
string, .
: ,
.
.
1 . , , ,
.
2. , , ,
new.
3.
new,
, , ,
.
,
delete.
. .


.
, ,
, .
, .

1 7 6
. .

vector .
.
- get ( ) set ( ) .
11 doue
class vector {
666 17

int sz ; //
doule* elem; //
pulic :
vector ( int s ) : sz { s } , elem{ new doule [ s ] } //
{ / * . . . */ }
vector ( ) { delete [ ] elem; } 11
int size ( ) const { return sz ; 11

doule qet ( int n ) const { return elem [n) ; 11


void set ( int n , doule v) { elem [n ] =v ; } 11
);

get ( ) set ( ) ,
[ ] elem.
, doue,
.
vector v ( S ) ;
for ( int i=O ; i<v . size ( ) ; ++i )
v . set ( i , l . l * i ) ;
cout << "v [ " << i << " ] == " << v . qet ( i ) << \n ' ;

:
v [ O ] ==O
v [ l ] ==l . l
v [2 ] ==2 . 2
v [ ] == .
v [ 4 ] =4 . 4

vector . .
get ( ) set ( ) ,
( ).
, , ,
, .

.

1 7. 7.
,
. ,
vector ,
char:
vector* f ( int s )
{
vector* new vector ( s ) ; 11
11 *
1 7. 7. 667

return ;

void ff ( )
{
vector* q = f ( 4 ) ;
11 *q
delete q; 11

, , vector
delete, vector.
vector* = new vector ( s ) ; 11
delete ; //

vector
new :
vector;
vector,
;
vector .

vector, delete
:
vector; ,
, ( ),
, ;
, vector.
,
(. . 8.5.8). () vector,
:
vector<vector<doule>>* = new vector<vector<doule>> ( lO ) ;
delete ;

delete vector<vector
<doue>>. , .
vector<doue>. ,
.
delete ( ,
, , vector) , ,
(destroy) ,
(deallocate).
, , "" new

delete. (. . ,
668 1 7

Vector_ref 1 3. 1 .4) ,
new . delete -

.
,
, ? ,
. ().
.
vector v ( 4 ) ;
int = v . size ( ) ;
doue d = v . qet ( ) ;

-> ()
:
vector* = new vector ( 4 ) ;
int = p->size ( ) ;
doue d = p->qet ( ) ;

. () . -> ()
- -.
, int doue, , ->
. "" ""
(member access operators).

1 7. . : void*
,
. .
, (, ,
* [ ] ) .
,
.
, .
, ,
,
(, ,
,
++). , ,
,
.
.
.
, .
1 7.8. : void 669

,
,
( ) .
void* " ,
". ,
. -
,
. ,
( 1 6.3. 1 ),
(, new) .
void , , ,
void " ".
void v; // : void
void f ( ) ; 11 f (} ;
11 f () void

void* ,
:
void* pvl = new int ; // !\ : i n t * voi d *
void* pv2 new doule [ lO ] ; / / : doul e * voi d *

,
void* , .
void f (void* pv)
{
void* pv2 = pv ; // ( void*
11 )
doue* pd = pv ; // :
11 void* dou e *
*pv = 7 ; 11 : void *
11 ( , pv,
// )
pv [ 2 ] = 9 ; // : voi d *
int* pi = static cast<int*> (pv) ; // :
_
11 . . .

static_cast
, , void* doue* ( .5.7).
static_cast -
( ) ,
.
( ). , static_cast,
(explicit type conversion)
(cast).
670 1 7

++ ,
static_cast.
reinterpret_cast
, , int doue*.
const cast const.
.
Reqister* in = reinterpret_cast<Reqister*> ( Oxff) ;

void f ( const Buffer* )


{
Buffer* = const_cast<Buffer*> (p) ;
11 . . .

- ,
reinterpret_cast. .
(. OxFF)
Register (,
). , .
.

in : OxFF

OxFF :

const cas t
const const Buffer* . ,
, .
s tatic_cast
const,

static_cast. ,
. : ,
? ,
? .
, .
, , static_
cast.
. reinterpret_cast.
.
1 7.9. 671

1 7. 9.
(reference)
.
.
- ,
, .
, ,
new & .
, ,
* [ ] .
,
, .

.
(
. );
(
, ).
.
.
int = 10 ;
int* = & ; // &
* = 7 ; /!
11 *
int 2 = * ; 11
int* 2 = &2 ; 11
11 i n t
2 = ; 1 1 2
= &2 ; //

.
int = 10 ;
int& r = ; // & ,
r = 7; 11
// r ( * )
int 2 = r; // r
11 ( * )
int& r2 = 2 ; 11 i n t
r2 = r ; /!
11 2
r = &2 ; // ll :
11 ( i n t * i n t & )
672 1 7

; ,
, -
. ,
. 1 7.9.3.
, .
-,
.

1 7 . 9 . 1 .
,
, :
int incr_v ( int ) { return x+l ; 11
11
void incr_p ( int* ) { ++* ; } 11
11 (
11 )
void incr_r ( int& r) { ++r ; } 11

? ,
( , ) :
int = 2;
= incr_v (x) ; 11 incr_ v () ;
11

, in t.
" (.
1 8. 3.4), .

? ,
, .
.

, . .
int = 7 ;
incr_p ( &x) ; 11 &
incr_r (x) ;

& incr_ ( &)


, .
incr_r (x) . . "".
.
,
, .
, . . nullptr. .
1 7.9. 673

incr_p ( O ) ; // : i ncr_p ()
int* = ;
incr_p (p) ; // : i n cr_p ()

, . incr_ ( )
:
void incr_p ( int* )
{
if (p=nullptr)
rrr ( " incr_p ( ) " ) ;
++* ; //
// ,

, incr_ ( )
, . 5 , ,
.
, (, incr_r ( ) ) ,

, .
" " ( )
,
. :
, p==nullptr .
, :
.

.
, "
" ( nullptr),
( nullptr!).

.
(. 8 . 5 . 6.)

1 7 . 9 .2. ,
1 4. 3 , ,
Circle, Shape.
:
Circle* Shape* .
Shape Circle.
.
void rotate ( Shape*s , int n) ; // *s
674 17

Shape* = new Circle { Point { l00 , 100 } , 40 } ;


Circle c { Point { 200 , 200 } , 50 } ;
rotate (p , 35) ;
rotate ( &c , 45) ;

.
void rotate (Shape&s , int n) ; 11 s

Shape& r = ;
rotate ( r , 55) ;
rotate ( *p , 65 ) ;
rotate ( c , 75} ;
-
(. 1 4.3, 1 4.4).

1 7.9.. :

. , ,
.
- . ,
.

norse_gods

Frela Odln Thor

(douy-linked list).

. ,
, . (singly-linked list).
, .
:
struct Link {
strinq value ;
Link* prev ;
Link* succ ;
Link ( const strinq& v, Link* = nullptr , Link* s = nullptr)
: value { v } , prev{p} , succ { s } { }
};
. Link,
. succ,
- prev. ,
1 7.9. 675

, .
:
Link* norse_qods = new Link { "Thor" , 0 , 0 } ;
norse_qods = new Link { "Odin" , 0 , norse_qods } ;
norse_qods->succ->prev = norse_qods ;
norse_qods = new Link { "Freia" , 0 , norse_qods } ;
norse_qods->succ->prev = norse_qods ;

, Link .
: - , , ,
, -
. , , ,
succ prev .
,
.
Link* insert (Link* , Link* n) // ()

n->succ = ; //
p->prev->succ = n ; 11
n->prev = p->prev; //
//
p->prev n ; 11
return n ;

,
Link
. , .
, ,
Link, ,
.
. . .
insert ( ) .
, , n. p->prev nullptr.
. ,
insert.
Link* insert (Link* , Link* n) // ;

{
if (n==nullptr) return ;
i f (p==nullptr) return n ;
n- >succ = ; //
if (p->prev) p->prev->succ = n ;
n->prev p->prev ; //
11

p->prev = n ; //
676 1 7

return n ;

:
Link* norse_qods = new Link { "hor" } ;
norse_qods = insert { norse_qods , new Link { "Odin" } ) ;
norse_qods = insert (norse_qods , new Link { " Freia" } ) ;
,
prev succ, .
, -

. ,
, ,
nullptr, ()
-

insert ( ) .
,
(. 1 5. 3 . 1 , .9. 2),

.

1 7. 9 .4.
list,
20.4. ,
, Link,
, " " ,
.
Link,
, ?
, .
.
insert: .
add: .
erase: .
find: .
advance: - .
:
Link* add (Link* , Link* n) // ;
{
11 , insert (. . 1 1 )
1 7. 9. 677

Link* erase (Link* ) 11 * ;


{ 11
if (p==nullptr) return nullptr ;
if (p->succ) p->succ->prev = p->prev;
if (p->prev) p->prev->succ = p->succ ;
return p->succ ;

Link* find(Link* , const strinq& s ) // s ;


// n ul lp t r ,

while () {
if (p->value s ) return ;
= p->succ ;

return nullptr ;

Link* advance (Link* , int n) //

// n u l lp t r ,
// ; -
-

{
if (p==nullptr) return nullptr ;
if (O<n) 1
while (n-- )
if (p->succ nullptr) return nullptr ;
p->succ ;
"'

else if (n<O) {
while (n++)
if (p->prev nullptr) return nullptr ;
p->prev;

return ;

n+ + .
, ,
.

1 7 . 9 . 5 .
.
Link* norse_qods = new Link { " hor" } ;
norse_qods insert (norse_qods , new Link{ "Odin" } ) ;
norse_qods insert (norse_qods , new Link { " Zeus" ) ) ;
norse_qods = insert (norse_qods , new Link { "Freia" } ) ;
678 1 7

Link* qreek_qods = new Link{ 11Hera11 } ;


qreek_qods = insert (qreek_qods , new Link { 11Athena11 } ) ;
qreek_qods insert (qreek_qods , new Link { 11ars11 } ) ;
qreek_qods = insert (qreek_qods , new Link { 11Poseidon11 } ) ;

" ". : - ,
, - , ( -
). :
Link* = find (qreek_qods , 11ars 11 ) ;
if () p->value = 11Ares11 ;

, find ( )
nullptr. , , , ( ,
greek_gods),
- , .
.
Link* = find (norse_qods , 11Zeus 11 ) ;
if () {
erase (p) ;
insert (qreek_qods , p} ;

? (,
).
erase ( ) norse_gods? ,
, ,
.
Link* = find (norse_qods , "Zeus 11 ) ;
if () {
if (p=-norse_qods) norse_qods = p->succ ;
erase (p) ;
qreek_qods = insert (qreek_qods , p ) ;

:
, .
- , .
.
void print_all ( Link* )
{
cout << 11 { 11 ;
while () {
cout << p->value ;
if (p--p->succ) cout << 11 11 '.

cout << 11 } 11 ;
1 7 .1 . this 679

print all (nore_gods) ;


cout<<" \n" ;
print_all (greek_god ) ;
cout<<"\n" ;

:
Freia, Odin , Thor }
{ Zeus , Poseidon , r , Athena , Hera

1 7 . 1 . thi s
, ,
, Link*
, .
. Link (
), ? ,
, -
? .
class Link
puic :
string value ;
Link ( const tring& v, Link* = nullptr , Link* s nullptr)
: value { v} , prev{ p } , succ { s } { }

Link* inert (Link* n) ; 11


Link* add (Link* n) ; 11
Link* erase ( ) ; 11
Link* find (const string& ) ; //
const Link* find ( const string& s ) const ; //
11 (. 1 8 . 5 . 1 )
Link* advance (int n) const; //
/!
Link* next ( ) const { return u ; }
Link* previous ( ) const { return prev ; }
private :
Link* prev ;
Link* succ ;
);

. ,
Link,
-. ( ) next ( )
previous ( ) , . -
succ prev
. , ()
; " ".
680 1 7

Link : : insert ( ) ,
.
Link* Link : : insert (Link* n) // ;
{
Link* = this ; //
if (n==nullptr) return ; //
if (p==nullptr) return n ; //
n->succ = ; //
if (p->prev) p->prev->succ = n;
n->prev p->prev; //
11

p->prev n; //
return n ;

,
Link : : insert ( ) ?
. - this -
. .
this .
Link* Link : : insert (Link* n) // this;
{
if (n==nullptr) return this ;
if ( this==nullptr) return n ;
n->succ = this ; 11 this
if ( this->prev) this->prev->succ = n ;
n->prev = this->prev; // this
11
this->prev = n ; // th i s
return n ;

,
this , .
Link* Link : : insert ( Link* n) // thi s ;
{
if (n==nullptr) return this ;
if ( this==nullptr) return n ;
n->succ = this ; // this
if (prev) prev->succ = n ;
n->prev = prev ; // this
11
prev = n ; // this
return n ;

,
thi s . ,
1 7 1 . this
. 681

, .
.
, this
: , -.
- .
, this
-. .
struct S {
11 . . .
void mutate ( S * )
{
this = ; // 011 : this
11 . . .

) ;

1 7 1 0 1
. . .

, ,
.
Link* norse_qods = new Link { " Thor" ) ;
norse_qods = norse_qods->insert (new Link { "Odin" ) ) ;
norse_qods norse_qods->insert (new Link { " Zeus " } ) ;
norse_qods norse_qods->insert (new Link { "Freia" } ) ;

Link* qreek_qods = new Link { "Hera" } ;


qreek_qods qreek_qods->insert (new Link{ "Athena" } ) ;
qreek_qods qreek_qods->insert (new Link { "ars " } ) ;
qreek_qods qreek_qods->insert (new Link { " Poseidon" } ) ;

. ,
"". . .
Link* = qreek_qods->find ( "ars " ) ;
if () p->value = "Ares " ;

.
Link* 2 = norse_qods->find ( " Zeus " ) ;
if (2) {
if (p2==norse_qods ) norse_qods = p2->next ( ) ;
p2->erase ( ) ;
qreek_qods = qreek_qods->insert (p2 ) ;

.
void print_all (Link* )
{
682 1 7

cout << " { " ;


while () {
cout << p->value ;
if (p=p->next ( ) ) cout << " ";

cout << " ) " ;

print_all (norse_qods) ;
cout<< " \n" ;

print all (qreek_qods ) ;


cout<< " \n" ;
, :
Freia , Odin , hor }
Zeus , Poseidon , Ares , Athena , Hera )
: , in8ert ( )
-, ,
? , ,
9 . 7 . 5.
. .
. ,
.
. Li8t,
. list
20.4.


.

vector.
1 . , -
int, new.
2. cout int.
3. , ( delete [ ] ).
4. print_arraylO (o8tream& 08 , int* ) .
08 ( ).
5. ,
int; 1 00, 1 0 1 , 1 02 .. ;
.
1 7 .1 . this 683

6. ,
int: 1 00, 1 0 1 , 1 02
.. : .
7. print_array (os tream& os , int* , int n ) ,
os ( n ).
8. ,
int: 1 00, 1 0 1 , 1 02 .. ;
.
9. ? ( , .)
1 . 5, 6 8, vector, ,
print_vector ( ) print_array ( ) .
.
print_array ( ) .
1 . int,
7 pl.
2. pl int,
.
3. .
int: l , 2, 4, 8 .. :
2.
4. 2 ,
.
5. int*
2 .
6. pl 2 .
7. 2 .
8. pl 2, ,
.
9. . .
1 0. ,
int: 1 , 2. 4, 8 .. :
pl.
1 1 . ,
int, 2.
1 2. , pl,
, 2 .
1 3. 1 0- 1 2, vector, .
684 1 7

r
1 . ?
2 . .
.
. ? ? -
?
4. ?
5. ? ++ ?
6. , -
? ?
7. 7
8. ?
9. ?
1 . 7
1 1 . ? ?
1 2. ( )?
1 3 . ? ?
14. ?
1 5. ?
16. ? ?
1 7 . ?
1 8 . ?
1 9. this ?

delete
delete [ ]
new
nullptr
this : ->
void* : *
: [ ]
: &


1 7 . 1 0. this 685

1 .
? : .
2 . int, doue l?
, sizeof,
.
3. void to_lower ( char* s ) ,
- s . ,
"Hell o , World ! " "hello , world ! " .
. -
, ,
, ,
.
4. char* strdup ( const char* ) , -
.
.
5. char* findx ( const char* s , const char* ) ,
- s .
6. , , ,
new, .
11 (memory exhaustion). , .
:
,

. .
, ?
7. . cin
, .
, ( ! ).
std : : string .
8. . 7 ,
std : : string, ( string ,
).
9. : ( )
( )? (
)
? , .
1 0 . . 7 .
; . ,
686 1 7

( )? .
, ?
1 1 . 1 7. 1 0. 1 , " ".
1 2. find ( ) ?
1 3 . Link 1 7. 1 0. 1 ,
struct God. string: ,
, . . God{ "" ,
" " , " " , "" } God{ " " , "" , "
'l' " , " r" } . print_
all ( ) . .
- add ordered ( ) . ,
new,
. Link God,
: ()
-
.
1 4. 1 7. 1 0 . 1
: , prev
Link? ?
?
.

.
,
vector? , , -
vector , ,
. ,
,
. ,
. ,
, ,
++ ( ,
). ,
, . .
. .
, ,
,
: ,
"- ".

,
.


.
,
.
,
: ,
,
, .
, ,
.
688 1

1 8. 1 . 1 8.6.
1 8.2. 1 8.6. 1 .
1 8..
1 8.3. 1 . 1 8.6.2.
1 8.3.2. 1 8.6.3.
1 8.3.3. , 1 8.6.4.
1 8.7. :
1 8.3.4. 1 8.7. 1 .
1 8.4. string
1 8.4. 1 . 1 8.7.2.

1 8.4.2.
1 8.7.3.

1 8.5.
1 8.5 . 1 .

1 8. 1 .
,
. ,
. ,
, -
. ,
.
,
-
,
.
, ,
.
.
,
;
;

(, , ..).
,
++ ( ; . 22.2. 5 27).
1 7 vector,
,
1 8.2. 689

"" ,
.
.
, . ,
?
?
? ?
. ?

, vector string,
, . .
. ,
,
++ .
, vector

++.
(string, vector, list, map .)

. .

1 8.2.
vector, 1 7:
class vector {
int sz ; 11
doue* elem; 11
puic :
vector (int s ) 11
: sz { s } ,
elem{new doule [ s ] } //
{ / * . . . */ }
vector ( ) //
{ delete [ ] elem; 11
11 . . .
1;
,
, ?
. :
vector vl = { 1 . 2 , 7 . 89 , 12 . 34 } ;
. ,
. ,
:
690 1 8 r

vector v2 (2) ; //
v2 [ 0 ] 1.2;
v2 [ 1 ] = 7 . 8 9 ;
v2 [ 2 ] = 12 . 34 ;

vl, w" v2
(
). push_ack ( )
.
vector v ; //
v2 . push_back ( l . 2 ) ;
v2 . push_back ( 7 . 89 ) ;
v2 . push_back ( 12 . 34) ;

.
.
? { }
initializer_list<T>
, . .
.
class vector (
int sz ; 11
doue* elem; //
puic :
vector (int s) // (s - )
: sz ( s } , elem( new doule [ s z ] } //
(
for ( int i = ; i<sz ; ++i )
ele [i] = . ; 11

11
vector (initializer_list<doule> lst)
: sz ( lst . size () } ,
elem( new doule [ s z ] } //
(
// s td: : copy () ; ( . 5 . 2)
( lst . eqin ( ) , l st . end ( ) , l) ;
}
11
};
(.5.2).
,
( - initiali zer_list)
,
( - ,
elem) .

1 0.3. 691

vector vl = { 1 , 2 , 3 } ; // 1 . 0 , 2 . 0 , 3 . 0
vector v2 ( 3 ) ; // ( 0 . 0)
.
. - . ,
, :
vector v1 { 3 } ; // 3 . 0
vector v2 ( 3 ) ; // .

, .
. { }
ini tializer_list.
- -
= { } ,

vector vll = { 1 , 2 , 3 } ; // 1 . 0 , 2 . 0 , 3 . 0
vector v12 { 1 , 2 , 3 } ; // 1 . 0 , 2 . 0 , 3 . 0
, .
, ini tializer_list<doule>
. : ini tia
lizer list ,
_

r-" (. .6.4).

1 8..
.
class vector {
int s z ; 11
doue* elem; 11
pulic :
vector ( int s ) 11
: sz { s } ,
elem{ new doule [s] } //
{ / * . . . */ }
vector ( ) //
{ delete [ ] elem; 11
11 . . .
};
.
void f (int n)
{
vector v (3 ) ; //
v . set ( 2 , 2 . 2 ) ; // v [2 ] 2 . 2
vector v2 = v; // ?
11 . . .
692 18

v2 v (.. =
); , v2 . size ( ) =v .
size ( ) v2 [ i ] ==v [ i ] i [ O : v . size ( ) ). ,
f ( ) .
() vector ,
vector. -
vector . ,
, .
. ? ,
. ,
, .
"
-". .
, Point, -
. , ,
. ,
v . sz==v2 . sz v . elem=v2 . elem,
:

. . 2.2

, v2 v;
v. .
v . set ( l , 99) ; // v [ l } 9 9
v2 . set ( 0 , 88 ) ; 11 v2 [ 0 } 88
cout << v . get ( O ) << ' ' << v2 . get ( l ) ;
88 9 9 . ,
. v v2,
,
v [ ] v2 [ 1 ] . ,
"", "" " ",
, , vector. .
f ( ) , .
v v2;
v
delete [ ] elem;
v21
, v v2, elem ,
.
(. 1 7.4.6).
1 .. 693

1 ..1 .
, ? :
,
.
.
, , . ,
, ( constructor)
. ,
(. .
vector : vector ( const vector& ) ; ).
,
vector . ,
( ) ,
. . .
const, (.
8.5.6). , vector.
class vector {
int sz ;
doue* ele111 ;
puic :
vector (const vector&) 11
11 . . .
1;
(sz)
( elem)
.
vector : : vector {const vector& arg)
11 ,
: sz { arg . sz } , ele111 { new doule [arg . sz ] }

copy (arg , arg+sz , ele111) ; 11 s td : : copy () ; . . 5 . 2


.
vector v2 = v;
v2,
vector v.
.
v: 3 1 2.2

v2 : 3 1 2.2
694 1


. , vector
, v,
v2, .
v . set ( l , 99) ; 11 v [ l ] 9 9
v2 . set ( 0 , 88 ) ; 1 1 v2 [ 0 ] 8 8
cout << v . ge t ( O ) << ' ' << v2 . get ( l) ;
.

vector v2 = v;


vector v2 (v) ;

v () v2 ( )
,
, ,
, .

1 8. .2.

, . ,
, -
(. 1 8.3. 1 ) .
.
void f2 ( int n)
(
vector v ( ) ; 11
v . set ( 2 , 2 . 2) ;
vector v2 ( 4 ) ;
v2 = v ; 11 : ?
11 . . .

, v2 v (
vector). vector
.
; , ,
sz elem v2 sz elem v
.
:
7
1 .. 695

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

. .

f2 ( )
, f ( ) 1 8.3 ,
: ,
2
(v v ),
delete [ ] . , ,
v2, .
.
(. 18.3. 1 ) .
.
class vector {
int s z ;
doue* elem;
pulic :
11 :
vector& operator= ( const vector&)
11 . . .
};

vector& vector : : operator= ( const vector& )


11
{
doule* = new doule [ a . sz] ; //
copy (a . elem , a . elem+a . sz , p) ; 11
delete [ ] elem; 11
elem = ; 11
sz = a . sz ;
return *this ; // (. 1 7 . 1 0)

, ,
.
vector.
doule* = new doule [a . sz ] ; //
copy ( a . elem, a . elem+a . sz ,p) ; //
vector:
delete [ ] elem; 11

elem .
elem = ; //
s z = a . sz ;
.
696 1



.----- delete[]

vector ,
.
,
,
, .
, ,
vector .
vector v ( lO) ;
v=v ; 11
,
, .

1 . . . .

. ,
: ( ) ,
(, , ).
(shaHow ) .
,
.
.
I. (deep )
, ,
.
, vector. string . . ,

,
.
.
int* = new int{ 7 7 } ;
int* q = ; //
* = 8 8 ; 11 i n t ,
11 q

.
1 8.3. 697

: q:
( )

.
int* = new int{ 77 } ;
int* q = new int{ *p } ; // i n t ,
11 ,
* = 88 ; // i n t ,
11
.

, .
vector .
,
elem. vector, vector
. ,
. ,
(, ). .
(pointer semantics) u (reference
semantics). . ,
(. s tring vector) . ,
(value semantics), . . . -
. .
,
. . .
".
".

1 . .4 .
,
.
. . .
vector fill (istream& is)
{
vector res ;
698 1 8

for (doue ; is>>x ; ) res . push_back (x) ;


return res ;

void use ( )
{
vector vec = fill ( cin) ;
// . . vec . . .
.

res
use ( ) . res fill ( )
vec .
? !
res .
res fill ( ) .
?
:

res: i 1 00000 :::,..-----


"" res
vec. , , vec
res , .
res
res vec . res
fill ( ) vec. res
( ) - :

1 00 doue
fill ( )
.
++?
:
class vector (
int sz ;
doue* elem;
1 .. 699

puic :
vector (vector&& ) ; 11
vector& operator= (vector&&) ; 11
11 . . .
};
&& rvalue". ,
. ,
, . . (vector&&) ,
(const vector&&) .
, ".
,
. .
vector : : vector (vector&& )
: sz { a . sz ) , l.{ . l.} // elem sz
{
a . sz = ; //
a . elem = nullptr ;

vector& vector : : operator= (vector&& ) //

delete [ ] elem; 11
elem = . l.; // elem sz
sz = a . sz ;
. l. = nullptr ; 11
a . sz = ;
return *this ; 11 (. 1 7 . 1 0)

,
,
. .
vector fill (istream& is)
{
vector res ;
for (doue ; is>>x ; ) res . push_back (x) ;
return res ;


. ,
(res) , ,
.
,

. -
- , .
700 1 8

vector* fi112 (istream& is)

vector* res = new vector ;


for (doule ; is>>x ; ) res->push_back (x) ;
return res ;

void use2 ( )
{
vector* vec = fill ( cin) ;
// . . . vec . . .
delete vec ;

vector.
1 7.4.6, , ,
,
.

1 8.4.
, ,
,
-
. .
.
.
( ).
( ).
(
).
(
).
.

,
, , :
strinq s { " cat . jpq" } ; / / s " t . jpg"
Iaqe ii { Point { 200 , 300 } , // Poi n t ( 2 0 0 , 30 0 ) ,
"cat . jpq" } ; //
// ca t . jpg


. string
,
1 8.4. 701

vector .
( . 9.4.3).
,
. , ,
.
, , ,
.
.
, ?
,
.
, .
vector.
. int, string vector<int> ,
.
vector<doue> vi (lO) ; 11 1 0 dou e ,
// .
vector<strinq> vs (lO) ; // 1 0 s t ring,
11 " "
vector<vector<int>> vvi ( 1 0 ) ; // 1 0 ,
//
// vecto r { }

, .
:
? :
. ,
int doue, ( doue
. ). string
11 11 vector .
, { } . , doue { }
. , string { } " " , vector<int> { } vector,
-

int.
. .
- , "- " ,
. .
new, ,
delete delete [ ] .
vector ;
, . ,
, - ( ,
) . , (

).
702 1

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

. ,
( - ,
), (
) , (
, . )
. vector.
, ,
(.
1 7. 5.2).

1 8.4. 1 .
, r,
r . .
.
class l {
puic :
complex (doue) ; // doue complex
complex (doue , doule) ;
11 . . .
};
l z l 3 . 18 ; / / : 3. 18 (3 . 1 8 , 0)
l z2 complex { l . 2 , 3 . 4 ) ;
.
,
. , vector, ,
, int, .,
int vector:
class vector
11 . . .
1 8.4. 703

vector (int) ;
11 . . .
};

vector v = 10 ; // 1 0 d ?
v = 20 ; // v 20 doue
void f ( const vector&) ;
f ( lO) ; // ? f
11 1 0 doue
, , . ,
.
explici t
. -
.
class vector
11 . . .
explicit vector ( int) ;
11 . . .
};

vector v = 10 ; // : i n t vector
v = 20 ; // : i n t vector
vector v0 ( 10 ) ; 11

void f ( const vector&) ;


f ( lO) ; // : i n t vector
f (vector (lO) ) ; 11
, -
- vector
explici t. ,
explici t ; , .
,
explici t.

1 8. 4 .2.

. ,
vector ( 2) ; vector

new.
, .
,
. -
.
704 1 8

,
.
, .
, ;
, ,
delete.
.
; ,
new (
), .
? ,
,
. .
struct { //
int val ;

void out (const strinq& s , int nv)


(
cerr << this << "->" << s << 11 " << val
<< 11 ( " << nv << " ) \n" ;
1
( ) ( out ( "X ( ) " , 0 ) ; val=O ; 1 //
X ( int v) ( val=v; out ( "X ( int) " , v) ; 1
11 I1 :
X ( const & ) ( val=x . val ; out ( "X (X&) " , x . val) ; }
& operator= (const & ) //
(
out ( 11X: : operator= ( ) " , a . val) ;
val=a . val ; return * this ;

- ( out ( "-X ( ) 11 , ) ; //
1;

,
.
qlob ( 2 ) ; //
( ) ( return ;

2 ( ) { = ; return ; 1

& ref_to (X& ) { return ; 1

* ake ( int i) ( a (i ) ; return new ( ) ; }


struct { ; ; } ;
int main ( )
(
1 8.4. 705

loc { 4 ) ; 11
loc2 { loc) ; 11
loc = X{ S ) ; 11
loc2 = copy ( loc) ; 11
loc2 = 2 ( 1) ;
l { ) ;
& r = ref_to (loc) ; 11
delete k ( 7 ) ;
delete k ( ) ;
vector<X> v ( 4 ) ; 11
loc4 ;
* = new { 9 ) ; 11
delete ;
* = new X [ S] ; 11
delete [ ] ;

&
: -
, .
, ,
.


" ". ( ) 2 ( ) .
() . ;
- .
, , -
. ,
,
.
"", .
, , ,
.
( 1 8.3.4).
" "?
, .
. ,
. .
,
, .
, .
. , . ,
706 1 Belfrop

.
,
,
. ,
.
,
,
; " -
" (leak detectors). , , ,
, .

1 8.5.
(. 1 7 .6)
- set ( ) get ( ) .
. : v [ i ] . i
- operator [ ] .
() .
class vector {
int s z ; 1 1
doue* elem; //
pulic :
11 . . .
doule operator [ ] ( int n )
{ return elem [n ] ; } //
};
, , , .
(operator [ ] ( ) ) ,
, .
vector v ( lO) ;
int = v [ 2 ] ; //
v [] = ; // : v [} l va l ue

v [ i ] v . opera
tor [ ] ( i ) , i- v. i
vector v [ ]
, ,
.


vector,
,
v [ ] = ; .
1 8.5. 707

operator [ ]
:
class vector {
int sz ; 11
doue* elem.; //
puic :
11 . . .
doule* operator [ ] ( int n )
{ return &elem [n ] ; ) //
);

:
vector v ( l O ) ;
for ( int i=O ; i<v . size ( ) ; ++i )
11 , -
*v [ i ] = i ;
cout << *v [ i ] ;

v [ i ] v . opera tor [ ] ( i )
i- v. ,
* ,
. , set ( )
get ( ) . ,
.
class vector 1
11 . . .
doule& operator [ ] ( int n )
{ return elem [n] ; } //
};
.
vector v ( lO) ;
for ( int i=O ; i<v . size ( } ; ++i ) { // !
v [i ] = i ; // v { i }
11 i -
cout << v [i ] ;

:
v [ i ] v . opera tor [ ] ( i )
i- v.

1 8.5. 1 .
operator [ ] ( ) , , :
:
708 1 8

void f ( const vector& cv)


{
doue d = cv [ l ] ; // ,
cv [ l ] = 2 . 0 ; // ( )

, - vector : : operator [ ] ( )
vector.
, - ,
. ,
const (. 9.7.4).
.
class vector {
// . . .
doule& operator [ ] ( int n ) ; //
doule operator [ ] ( int n) con s t ; //
};
, doue&
const, doue.
const doue&,
, doue ,
(. 8. 5.6), .
.
void ff ( const vector& cv , vector& v)
{
doule d = cv [ l ] ; // ( [ } )
cv [ 1 ] = 2 . ; // ( [ } )
doule d = v [ l ] ; // ( [ } )
v [l] = 2 . 0 ; // ( [ } )

vector
, operator [ ] ( ) const
.

1 8.6.
(array)
, .
-
. .
(.
).
(
).
1 8.6. 709

( ).
( , , -
).
, ,
vector . s td : : vector
. -

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

; ,
.
.
.
const int = 100 ;
int qai [ax] ; // ( 1 0 0 i n t ) ; " "
void f ( int n)

char lac [ 20 ] ; // ; " "


//
int lai [ 60 ] ;
doue lad [ n ] ; // :
11 . . .

:
. ,
,
.
vector .
, ,

( [ ] ):
void f2 ( )
{
char lac [ 2 0 ] ; // ; " "
//
lac [ 7 ] '' ;
*lac '' ; 11 l a c [ O} = ' b '
lac [ - 2 ] ' ' ; // ? ?
lac [ 2 0 0 ] ' ' ; 11 ? ?
710 1

, , ,
. [ ] ,
,
. f2 ( ) , lac [-2]
lac [ 2 0 0 ] (
). .
.
, "
" .
- , lac
, lac [200 ] -
? , , , , -
. ,

, (, )
.

1 8.6. 1 .
. .
doue ad [ lO ] ;
doue* = &ad [S ] ; // a d [ 5 ]

doule, ad [ 5 ] .

ad :

.
* = 7 ;
[2 ] = ;
[ -3 ] = 9 ;

ad : 7 6
1 8.6. 711

,
, . ,
,
.
( ,
; . 1 7 . 4.3). ,
( ) .
,

.
+= 2 ; //

ad : 6


-= 5; //

ad : 9 7 6

+, -. +=
-=

(pointer arithmetic). ,
, , -
.
+= 1000 ; // : ,
11 1 0
doule d = * ; // :
11 ( )
* = 12 . 34 ; // : ,
11

, ,
, .
.
71 2 1 8


++
--
. ,
ad :
for (doule* = &ad [ O ] ; p<&ad [ lO ] ; ++) cout << * << ' \n ' ;
:
for (doule* = &ad [ 9 ] ; p>=&ad [ O ] ; --) cout << * << ' \n ' ;


. , ("")
. &ad [ 9 ] , &ad [ l O ] ? >=. >?
( ),
. ,
vector,
.
,
.
,
, .
.
++ ?
,
:
doue* pl = &ad [ O ] ;
doue* 2 = 1+7 ;
doue* = &1 [ 7 ] ;
if (2 ! = ) cout << ":110 ! \n" ;

.
.
,
, . ,

, .

1 8.6.2.
:
char ch [ lOO J ;

ch, . . sizeof ( ch ) , 1 00.


:
1 8.6. 71 3

char* = ch ;

&ch [ ] , si zeof ()
- 4 ( ,
1 001).
. ,
strlen ( ) , ,
.
int strlen ( const char* ) //
11 s t rlen ()

int count = ;
while ( *) { ++coun t ; ++ ; }
return coun t ;

strlen ( ch) ,
strlen ( &ch [ ] ) . , ,
, .
,
,
. .
int strlen ( const char [ ] ) //
11 s trlen ()

int count = ;
while ( [count] ) { ++coun t ; }
return coun t ;

char lots ( 100000 ] ;

void f ( )
{
int nchar strlen ( lots) ;
11 . . .

( ) ,
1 00 . ,
strlen ( ) , .
char [ ] char*p.
strlen ( lots) s trlen ( &lots [ ] ) .
-

, .
? . ,
, ( .
8.5.3-8. 5.6), .
71 4 1 8

, ,
, ,
.
char ac [ lO ] ;
= new char [ 20 ] ; // : -
11
& [ ] = new char [ 2 0 ] ; // :
//

- , .

.
int [ 1 0 0 ] ;
int [ 1 0 0 ] ;
11 . . .
= ; 11
int z [ l O O ] = ; 11

, . .
. :
for ( int i=O ; i<lOO ; ++i )
x [ i ] =y [i ] ; 11 1 0 0 in t
me.cpy ( x , y , lOO*sizeof ( int) ) ; 11 l OO * s i zeof (in t )
copy (y , y+lOO , x) ; // 1 0 0 in t

,
. ,
++, ( - 27. 1 .2).
, ( , ;
27.5) .
,
vector .
vector, ,
:
vector<int> x ( lOO ) ;
vector<int> ( 10 0 ) ;
11 . . .
= ; 11 1 0 0 i n t

1 8.6..
char
, :
char [ ] = "eorn" ; //
1 8.6. 71 5

. ,

! lo r
.
.

: l l ln 1 1
, ,
. ,
, (C-style string)
-.
.
char* = "Howdy " ; //
//

10 1... 1 1
:

11
char, , - ' '
- .
- . :
. ,
:
int strlen ( const char* ) // s t rlen ()

int n = ;
while (p [n] ) ++n ;
return n ;

s trlen ( ) ,
,
<string . h> ( 2 7 . 5 . 1 1 .3). ,
s trlen ( ) .
; .
+ l char.

,
, .
.
int ai [ ] = { 1 , 2, 3, 4 , 5, 6 } ; // i n t
int ai2 [ 10 0 ] = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ; // 9 0
11
71 6 1 8

doue ad [ lO O ] = { } ; //
char chars [ ] = { ' ' , '' , '' } ; // !

, ai
( ), chars (
), - " "
. ,
( ).
,
( ai2 ad),
,
.

1 8.6.4.
, .
, . ,
, ,
, ,
, .
. .
.
.
.
.
, .
,
, ,
;
. ( ),
, , .
, . .
.
int* = nullptr ;
* = 7 ; // !
, ,
- .

.
, , ,
:
1 8.6. 71 7

int* = fct that can return a nullptr ( ) ;


_ _ _ _ _
if ( == nullptr) {
11 -

else {
11
* = 7 ;

void fct that can receive a nullptr ( int* )


_ _

if ( = = nullptr) {
11 -

else {
11
* = 7 ;

, ,
, (. 1 7.9. 1 ) ,
- (. 5.6 1 9. 5).
.
int* ;
* = 9 ; // !

, ,
.
.
int a [ lO ] ;
int* = &a [ l O ] ;
* = 1 1 ; / / !
a [ l O ] = 12 ; // !



. vector.
(
),
.
.
int* = new int ( 7 ) ;
11 . . .
delete ;
11 . . .
* = 13 ; !! !
71 8 1

delete . ,
* .

.
"" new, " "
delete: new delete
. Vector_ref (
.4).
.
int* f ( )
{
int = 7 ;
11 . . .
return & ;

11

int* = f ( ) ;
11 . . .
* = 15 ; // !

f ( ) . ,
* .
. , .

. . ,
( . 1 7.5. 1 ) .
,
, .
.
vector& ff ( )
{
vector ( 7 ) ; // 7
11 . . .
return ;
11

11

vector& = ff ( ) ;
11 . . .
[ 4 ] = 15 ; // !


.
.
.
1 8.7. : 71 9

,
. -
, , new delete.
, " " -
. , RAII (WResource Acqui
sition ls Initialization" w ": .
-

1 9. 5),
.

1 8. 7. :
!
. (palindrome) - ,
. , r petep
malaualam , ida homesick - .
, .
, ,
.
, , - -
.
.
.
. ,
- , -
,
.

1 8. 7 . 1 . s tring
,
string ,
.
l is_yalindrome ( const string& s )
1
int first = ; //
int last = s . length ( ) -1 ; //
while ( first < last) { //
if (s [ first] ! = s [ last] ) return fal se ;
++first ; //
--last ; //

return true ;
720 1 8

true, ,
.
, , ,
,

. , ,
, ,
.
is_palindrome ( ) .
int main ( )
{
for ( s trinq s ; cin>>s ; ) {
cout << s << 11 i s 11 ;
if ( ! i s_yalindrome ( s ) ) cout << 11 not11 ;
cout << 11 palindrome\n 11 ;

, , string,
, s tring .
,
, . is _

palindrome ( ) , ,
getline ( ) (. 1 1 . 5).
, ah ha as dJ.fd sa
.

1 . 7 .2.
string ( vector)
?
bool i s_yalindrome ( const char s [ ] , int n)
// s

int first = ; 11
int last = n-1 ; //
while ( first < last) //
if ( s [first] ! =s [ last] ) return false ;
++first ; //
--last ; //

return true ;

is_palindrome ( )
. (
) :
1 8.7. : 721

istream& read_word ( i stream& is , char* buffer , int )


// -1 buffer

is . width (max) ; 11 >>


// -1
is >> buffer ; // ,
11 ,
return i s ;

istream
.v >>. ,
, , -
- (..
). , width ( )
?
string vector, ,
.
, (-
) .
read word ( ) , .v :
int main ( )

constexpr int = 128 ;


for ( char s [max] ; read word ( cin , s , max) ; )
_
cout << s << 1 1 i s 11 ;
if ( ! isyalindroe ( s , strlen ( s ) ) ) cout << 11 not 11 ;
cout << 11 palindroe\n11 ;

strlen ( s )
read_word ( ) , cout<<s
, .
string ,
, ,
(. . 1 ).

1 8. 7
. .


.
bool isyalindroe ( const char* firs t , const char* last)
// fi rs t
11 l a s t

while ( first < last) { //


722 1

if ( *firs t ! =*last) return false ;


++firs t ; / /
--last; //

return true ;

,
. ,
, - -
. ,
,
. , .
is_palindrome () , ,
:
int main ( )

const int = 128 ;


for ( char s [max] ; read_word ( cin , s , max) ; ) {
cout << s << 11 is11 ;
if ( ! is_Palindrome { &s [ O ] , &s [ strlen ( s ) -1] ) )
cout << 11 not11 ;
cout << 11 palindrome\n11 ;

is_palindrome ( ) :
bool is_Palindrome (const char* first , const char* last)
// fi rs t
// l a s t

if (first<last) {
if ( *first ! =*last) return false ;
return is_Palindrome (first+l , last-1 ) ;

return true ;

,
; ,
,
, .
1 8.7. : 723

-../
:
, - .
, .

l . ga int,
l , 2, 4, 8. 1 6 ..
2. f ( )
.

int int,
.
3. f ( ) .
la int,
.
ga la.
la.
int
,
, ,
.
,
, .
,
.
, .
4. main ( ) .
f ( ) ga.
, ,
(.. 1 ,
2* 1 , 3*2* 1 , 4*3*2* 1 ..).
f ( ) .

1 . vector<int> gv;
1 , 2, 4, 8, 1 6 ..
2. f ( ) , vector<int>.
3. f ( ) .
724 1

vector<int> lv -
, . r .
qv lv.
lv.
vector<int> lv2;
. ,
.
lv2 .
4. main ( ) ,.
f ( ) r qv.

vector<int> vv
( 1 . 2* 1 , 3*2* 1 , 432 1 ..).
f ( ) r vv .


1 . " !"?
2. ?
3. -
, ?
4. ?
5. ?
6. ,
?
7. ? ?
8. vector ?
9. .
1 0. explici t?
, ?
1 1 . r ?
1 2. ?
1 3 . ?
1 4 . ?
1 5.
?
1 6. , -?
1 7. ?
1 8.7. : 725

explici t


l . char* s trdup ( cons t char* ) , -


,
. .
,
*
2 . char*findx ( const char* s , const char*x) ,
- - s .
. ,
* .
3. int strcmp (const char*s l , const char* s2 ) ,
- .
sl s2,
, - , sl
s2 - .
. ,
* .
4 . , strdup ( ) , findx ( ) strcmp ( )
. -? !
, char* ,
, , (
- - ;
).
-" .
, .
,
-
.
-.
5. string cat_dot ( const string&sl , const string&s2) ,
.
, t_dt ( "" , "" ) . .
6. cat_dot ( ) ,
,
( ).
726 1

7. t_dot ( ) ,
- -
, .
.
. , ,
new,
delete. , . 5 6.
8. 1 8. 7 ,
: . " home " ,
" emoh" ,
, home - .
9. ,
1 7.4. . ,
, .
: ?
, .
- ?
1 0. 1 8. 7 . 2
. ,
: 1 ) ,
: 2)
. .
1 1 . , (skip list) ,
. !
1 2 . "Hu nt the Wumpus" (" "
""). ( ) ,
(Gregory ). -
,
. - .
, :
. ,
. ,
.
.
, . .
.
" " , .
" " , .
" " ,
.
1 .7. : 727

.
. ,
, " 1 2;
1 , 1 3 4: ?"
ml (" 1 3") sl-4-3 ("
1 3, 4 3").
. .
,
, , ,
.
,
,
. ,
(, randint ( ) std_lib_facilities . h) ,

. r1:
.


vector
, .
- ,
. ,
,
.
,

.YU1.U "' . .
- ,,. ("



SL: vector .
.
.
.
. .
.
.
.
vector . .
, :
. ,

.
. ,
,
.
.
730 1 9 ,

1 9. 1 . 1 9. .6.
1 9. 2 . 1 9..7. vector
1 9.2. 1 . 1 9.4.
1 9.2.2. reserve capacity 1 9.4. 1 . :
1 9.2.. resize
1 9.2.4. push_back 1 9.4.2.
1 9.2.5.
1 9.2.6. 1 9.5.
1 9.. 1 9.5 . 1 .

1 9.. 1 .
1 9.5.2. - -
1 9..2. -
1 9.5..
1 9 .... 1 9.5.4. unique_ptr
1 9..4. 1 9.5.5.
1 9..5.
1 9.5.6. RAll vector

1 9 . 1 .
1 8 vector ,
.
vector,

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