Открыть Электронные книги
Категории
Открыть Аудиокниги
Категории
Открыть Журналы
Категории
Открыть Документы
Категории
92++
32.973.26-018.1
24
. .
24 C++. -. - .: -, 2004. - 512 .: .
ISBN 5-94157-358-8
C++,
, -
, -
, , .
,
,
- ,
,
C++. , "" ,
, -
.
, -
, -
.
.3.068+800,92++
32.973.26-018.1
:
.
.
.
ISBN 5-94157-358-S . .. 24
, "-", 2004
9
; 11
12
13
I. C++ 15
1. C++ 17
22
- 27
29
break continue 33
34
34
40
46
49
51
52
57
" " undefined behavior 59
2. 61
62
63
66
68
70
71
: 73
4 ^
" " 77
82
84
85
89
92
95
inline- 99
101
3. 105
- 105
109
116
119
f 120
123
125
127
129
132
134
, 135
140
4. , , 143
- 143
144
147
148
150
151
153
* 155
157
158
string.li 159
161
164
166
168
169
169
173
175
177
main 179
5
5. 183
184
184
189
191
, 195
196
203
204
209
214
215
6. C++ 221
C++ 222
223
225
227
228
231
234
235
238
240
242
() 246
247
249
250
254
257
7. , 259
"" 259
"" '. 262
264
270
272
275
278
284
284
286
289
6
II. -
291
8. ! 293
293
295
300
306
311
312
316
? 319
322
9. 327
327
330
332
"" 335
339
340
"" 343
atttojttr 345
10. 347
347
348
349
, 350
352
353
, 354
357
. 361
361
363
365
368
370
RTT1 dynamic_cast 374
376
, 377
379
380
382
384
7
12. .387
388
'. 391
- 393
393
398
- 401
405
- 407
409
410
14. 443
I 443
2 444
447
4 450
5 451
. ....457
IV. 459
1. Borland 461
Borland C++ 3.1 461
IDE - 462
463
465
- 465
, 466
466
466
467
5
() 467
467
467
Borland C++ 5 470
IDE 470
470
474
474
476
476
Borland C++ Builder 6 479
IDE 479
479
482
, 482
482
483
3. C++ 493
4. ..- 495
499
[49], "
2 768 942 C++".
2 768 943- .
C++?
C++ -
, ,
C++ .
"" , ,
. . ,
,
. ,
"C++ ", -
.
C++ :
C++, Bor-
land C++ Builder Visual C++, -
. , , -
C++.
,
.
-,
, -
. -
C++,
.
,
,
.
10
-, , -
, ,
. -
" ", ""
. /
. ,
, / C++ -
.
, . , -
,
, . , -
! ,
, -
.
-
. , , ,
Windows.
,
. -
, ,
, Windows.
- .
, , . -
, -
C++ . , ,
, . , . 4 -
maiioc "" , . 6
/ stdio.h. He -
.
, ,
, (,
). , , -
, . , -
.
, -
. ""
, . ,
-
, .
, .
.
11
C++ Borland Visual C++.
, , -
, C++. -
( ), (
) . -
, , .
4- . I C++.
. 1 , -
. , , -
, .
. 2 .
, , .
, -
. ,
,
. 3 ,
, . "-
" , .
C++ -
. . 4.
. 5 -
. .
. 6 -
/ C++.
. / . :
, , "-
" .
II -
C++.
. . 8 -
, ,
.
. 9 . -
,
12
. -
.
. 10 ,
.
. 11 -
,
; RTTI.
.
. 12 STL, -
.
III Windows. . 13
Win32 API -
.
. 14 -
C++ Builder.
( IV) -
, -
, ,
.
1, 2 .
Microsoft Visual C++ 6, STL DinkumWare
STLport 4.5.3. -
Visual C++ 7. ,
, -
. -
-
Borland: Borland C++ 3.1, Borland C++ 5,
Borland C++ Builder 6. Borland,
. , ""
, Visual C++ 6
MSDN.
Borland C++ 3.1.
.
Borland C++ 3.1,
"
", .
.
13
-
"-", -
. ,
. -
RSDN, ,
C++.
I
C++
1. C++
2.
3.
4.
5.
6. ++
7.
1
C++
"!"
.
" ", -
,
"Hello, World!".
" ": -
.
Borland C++ 3.1 " ",
1.1.
( <Ctrl>+<F9>).
, , <Alt>+<F5>
, :
. -- -
. // -
. -
, .
, , -
, , .
18 I.
C++ ,
. /* -
*/.
, ,
:
/*
*/
.
, -
, -
.
. "" ,
*/ ,
, .
(~ j
. Borland
, .
/.
C++, , /,
.
.
iostream.h , -
include. C++ -
. C++
, -
/.
# (). ,
. , -
.
, define,
.
( ),
C++, main.
, main
int main ()
.
1. C++ /9
main :
.
int, -
C++ -.
C++ ,
;
main ; .
, main
C++;
, main .
.
, -
. C++ -
.
, -
.
, ,- ( ).
cout "2 * 2 =" 2 * 2 endl;
return 0;
return C++;
, ,
. , ,
,
. .
, -
. ,
1 -1.
,
( ) .
:
cout - ,
, .
C++, , -
, cout -
- ;
:
"2 * 2 =", 2 * 2 endl. -
20 I. C++
. , -
" ".
, ,
.
( , -
\ ), . -
, .
.
: * (-
), . ,
.
endl
.
,
2 -
10 , -
. ,
. -
,
. , 3
2 . -
. (
1.2) -
.
I 1.2.
#includp <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k; //
cout k "* 1 =" k * 1 endl;
cout k "* 2 =" k * 2 endl;
cout "* 3 =" k * 3 endl;
cout k "* 4 =" k * 4 endl;
cout k "* 5 =" k * 5 endl;
cout k "* 6 =" k * 6 endl;
cout k "* "7 =" k * 7 endl;
cout k "* 8 =" k * 8 endl;
cout k "* 9 =" k * 9 endl;
cout k "* 10 =" k * 10 endl;
return 0;
1. C++ 21
. ,
int k;
. -
. -
, -
_. C++ ,
. . -
, .
"" ""
. C++ , -
: ab, , , .
, ,
.
.
int , -
. , int .
, -
. -
, -
. C++ -
, .
, , ,
. -
, .
, C++ -
.
-
.
,
cin k;
k. cin
, .
. ,
, <Enter>.
, cout, cin C++,
- .
, C++
,
.
.
22 ^ ^ C++
.
,
1 9:
, . -
5 <Enter>, :
5 * 1 = 5
5 * 2 = 10
5 * 3 - 15
5 * 4 = 20
5 * 5 = 25
; 6 = 30
5 * 7 = 35
. * 8 = 40
t;
. * 9 = 45
5 * LO = 50
:
k ( 5), -
,
.
(. 1.2) ,
3. ,
, 147,
147. -
. -2. -
. -
, , . -
,
1 9. "" .
"" :
,
. ,
, , . -
-
. :
1 ^ 9 ;
;
1. / C++ 23
, .
C++ :
if () ;
:
if () _1;
else 2;
if else . -
if : ,
. .
,
if .
: , ;
, __2.
, .
, -
. . -
, " "
C++ ,
if . -
. if
:
if ()
{ //
cout k . 1 * 1 <- endl;
=" k
cout k M - * 2 : endl;
2 =" k
cout k * * 3 <: endl;
3=" k
cout k i . 4 <: endl;
4=" k
cout k , 5 =" k
* 5 <: endl;
cout k "* 6 =" k * 6 : endl;
cout k "* 7 =" k * 7 <; endl;
cout k "* 8 =" k * 8 ; endl;
cout k I 9 = k * 9 <: endl;
cout k : 10 =" k * 10 endl;
, ,
. -
, . ,
else.
24 _ I. C++
, . C++ <
<- ( ). ,
C++ : i <^ k <=9.
. , ,
1.3.
............ : .................................................................................................................... .........................................................................................
| 1.3.
^include <iostream.h>
int main{)
{ int k;
COUT: " I 9: ";
cin k; //
if (I <= k <= 9) cout "" encl;
else cout "" endl;
return 0;
. ,
k
"". , 1 <= <== 9 -
. 1 <- ,
9.
C++: . -
1 <= k ! , -
1; , 0. -
, : 0 < 9, 1 < 9.
-
. -
,
. C++ :
(1 <= k) && (k <= 9)
C++:
( < k) & (k < 10)
,
. . -
, 1.4 -
.
1. C++ 25
1.4.
^include <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k; //
if ( < k) && (k < 1 0 ) ) //
{ cout k "* 1 =" k * 1 endl; //
cout k "* 2 =" k * 2 endl;
cout k "* 3 =" k * 3 endl;
cout k "* 4 =" k * 4 endl;
cout k "* 5 =" k -' 5 endl;
cout k "* 6 =" k - 6 endl;
' cout k "* 7 =" k * 7 endl;
cout k "" 8 =" k * endl;
cout k "* 9 =" k * 9 endl;
cout k "* 10 =" k * 10 endl;
}
else cout " !" endl;
return 0;
( 1.5),
.
, , .
, . ,
,
. ( < ) , ""
: < < .
, , :
1 1
( < ) ( < ) "" " .
,
,
:
( > ) { t = ; = ; = t; }
( > ) { t = b; b = ; = ; }
{ > ) 1 t = ; = b; b = t; }
. , , - 10, =- 8, = . -
== 8, = , = ; = 8, = ,
- . ,
, < . ""
C++.
26 I. C++
^include <iostream.h>
int main ( }
{ double a, b, ; //
cout " : ";
cin b ;
double x, ; //
cout " : ";
cin ;
if ( > ) { double t = ; = ; = t; I
if (a > b) { double t = ; - b; b = t; }
if (b > ) { double t = b; b = ; = t; )
if (a > b) { double t = a; a = b; b - t; }
if ( (a < x) S& (b < y) ) cout " !" endl;
else cout " !" endl;
return 0;
, ,
. -
. . -
:
cin ;
, :
cin ;
cin ;
, ,
. <Enter> . -
.
, t
"" . , -
.
-
. C++ , ,
, .
-
. , -
, "
" (. . 2).
1. C++ _ 27
-
C++ - .
, .
, -
. +, -, v , /.
- C++, -
. 1.6.
j 1.6. switch
!....>. ................................................................................................................................ ............................ -.. ................ .., .................................................... .............. ;
, ,
, .
. --
. - :
switch (_)
{ }
char, .
:
: ;
28 /.
, .
C++,
. ,
default. .
C++. - -
:
;
-
;
-
;
,
default.
switch . -
-
. .
- -
break. C++. -
switch. , -
- :
;
, -
, ;
G break,
-.
,
, default, -
. -
break, . .
-.
-
. -
:
if (op == '+') cout + b endl;
else if (op == '-') cout a b endl;
else if (op == '*') cout a * b endl;
else if (op == '/')
1. C++ _ 29
break .
, -
, .
[3, 32, 35],
switch -
.
" ", -
1.4. ,
. : -
, -
; ,
. .
.
: -
. -
, .
,
, . ,
, 573. ! 573
, :
, 86 400 -
. 2 ! -
? -
.
, , -
. (
)
.
. -
1 . -
i. 1 .
. C++ (
) .
:
30 I. ^
i = 0;
( ) i;
, ,
, ... ?
. -
i, 9. i 9,
. : i < .
C++ , -
. C++ ,
, :
while () ;
while C++.
: ,
; , -
. -
, if. -
-
. . C++
:
1=0; //
while (i < 10) //
{ ++1; // 1
cout k "*" i "=" k * i endl;
.
-
: , ,
.
, 1.7.
: 1.7. while
i- -- - .'.'. ;
#include <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k; //
if ({0 < k) && (k < 10)) //
{ int i = 0 ; //
1. C++ _ 31
,
, .
, .
if. i,
. C++
.
,
( < ). , .
i 1 i -
++1. . -
k, ( ) *, i (
), = () k * i. -
. -
, . (i < ) -
, . -
, i
. ( < ) -
( ) .
if. , -
.
, -
. -
:
do ;
while () ;
,
. -
,
,
,
. . -
do. . .while, 1.8.
32 I. C++
tinclude <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k;
if ((0 < k) && (k < 10]] //
{ int 1 = 0 ; //
do ( ++i; // 1
cout k "*" i "=" k * i endl;
1
while (i < 10); //
)
else cout " !" endl;
return 0;
, , -
.. ,
C++
.
, ,
(. 1.7, 1.8)
.
1. -,
.
2. , -
1.
3. .
, -
.
, : .
:
for(_; ; __) ;
_ -
, , -
. _ ,
++i, ,
.
1. C++ 33
:
_.
for;
. ,
. ,
;
_;
.
, ""
, ,
. for 1.9.
[ 1.9. for
ttinclude <iostream.h>
int main()
{ int k;
cout " 1 9: ";
cin k;
if ((0 < k) && (k < 10))
for (int i = 0; i < 10; ++i)
cout k "*" i "=" k * i endl;
else cout " !" endl;
return 0;
.
. :
for.
break continue
,
. :
, . -
. , -
. break. ++
, .
continue. ,
:
2 . 1264
34 I. *C++
int k = 0;
for(;;} //
{ cout " 1 9: ";
cin k;
if ((1 > k) || (k > 9)} //
continue; //
else break; //
continue, -
,
. , -
. if -
( ), break, -
.
-:
while(1) { ... )
. .
,
.
.
1. .
2. .
3. .
: . .
, , -
.
, . . C++.
.
.
" -
". :
. ,
1. ^^ C++ 35
, , : 2.0,
-123.4567, ., . .
, , : 1.23445, .7-23.
, "" (
"") "10 ".
, 1.2341045, -
0.62310~23. , -
.
C++ "" : float, double, long double (
). long
float, double, -
.
.
.
, . -
(Help) Borland C++ 3.1, , -
float 3.410~38 3.410+38. ,
double 1.7~308 1.7+308.
, long double 3.410~4932
1.110+4932. -
Intel.
Borland C++ 3.1 -
float.h include. (,
Visual C++ 6) limits.
, .
:
1.451014. -
, "" -
.
"" .
. float 78
, double 1516 , a long double 1920.
double , -
long double.
.
, . Intel
. . -
, ,
1.10.
36 _ I. ^ C++
I 1.10.
^include <iostreara.h>
int main ()
{ ccut sizeof (float) endl;
cout sizeof(long float) endl;
cout sizeof (double) endl;
cout sizeof (long double) endl;
return 0;
sizeof C++
"" (-). C++ sizeof .
siseofjTHn) (
, ).
4, 8, 8 . ,
float 4 , long float double -
8 long double 10 .
Visual C++ 6 long double double (8) .
-
, double.
, .
3.141592653 double, F ( 0 -
3.141592653F float, L ( I) -
3.141592653L long double. -
. .
, , 1.11.
1.11.
^include <iostream.h>
int main ( )
{ cout sizeof 3.141592653F endl;
cout sizeof 3.141592653 endl;
cout sizeof 3.141592653L endl;
return 0;
s i z e o f : sizeof -
, .
1. C++ 37
4, 9, ,
.
C++ -
: , , , . -
, :
+ //
//
* //
/ //
i
. -
++ ,
, . -
1, i.
++ //
//
++ //
//
.
:
//
//
//
//
//
//
. -
,
. , -
, . , -
,
.
,
"" "".
cout ( < ) endl;
1, 0 , -
.
_35 I. C++
: ,
1.
"", - "". 1
while.
. _
J
C++ bool true false.
-
i .
C++ :
;
:
!, 2, ;
.
, :
float x, , ;
double , , ;
long double v, w;
:
float t = 0.456, = 1-23;
:
long double x = 1, z = 0;
:
const double d = 0.123456;
float const exponenta = 2.718281828;
const C++.
, .
-
.
C++ .
:
float t (0.456), p<le-23);
long double x(l), z(0);
const double d (0.123456);
, .
1, C++ 39
.
(). , , ,
,
. Borland C++ 3.1 -
M_PI.
, Visual C++ 6 -
! .
,
/ # include.
-
1.12.
| 1.12.
^include <math.h> //
^include <iostream.h>
int main ()
{ double = 0.0;
label: cout " : ";
cin ; //
if (r < 0}
{ cout " !" endl;
goto label;
}
double L = 2*M_PI*r; // = 2**
cout " = " L endl;
double S = M__PI*r*r; // = 7i*r^
cout " = " S endl;
double V = 4/3*M_PI*r*r*r; // = 4/3*7i*rJ
cout " = " V endl;
return 0;
,
. -
goto. -
. ,
,
. , , ,1
1
40 _ !. C++
,
. , goto
, .
C++:
short;
"" int;
long.
, -
.
Borland C++ 3.1 (
1.13), , 2, 2 4 -
.
1,13.
LI ......,."... ................... ..... .- ............ .."...,.....
^include <iostream.h>
int main ( )
{ cout sizeof (short) endl;
cout sizeof (int) endl;
cout sizeof (long) endl;
return 0;
Visual C-t-+ 6
Borland C++ Builder 6, 2, 4, 4. , -
, , :
Sizeof (short) < sizeof {int) < sizeof (long)
. (37] ,
-
. , short -
, , 2 , long 4 .
, (signed)
(unsigned). , . -
, -
. "-
", 1.14.
1. KaKjianncarb C++ 41
\ 1,14. ""
^include <iostream.h>
int main()
( cout sizeof(short) endl;
cout sizeof(int) endl;
cout sizeof(long) endl;
cout sizeof(signed short) endl;
cout sizeof(signed int) endl;
cout sizeof(signed long) endl;
cout sizeof(unsigned short) endl;
cout sizeof(unsigned int} endl;
cout sizeof(unsigned long) endl;
return 0;
,
, "" ""
, -
.
)
Borland C++ 3.1
limit.h, include.
: -
, .
, . ,
.
i. -
. , -
9
, , , D, , F , , . d, e, f.
, , -
, . ,
, .
, "-
" int, int,
, . -
, , U ( ) L
( 1): 123 , 123U
int, 123L , 123LU
I.
( I23ui) .
.
, -
,
:
int Jc;
short , , z; . // int
long a = , = 1; // int
unsigned int w = 2, v(10); //
signed long , rr(-5); // signed
unsigned long int t = 10; //
unsigned f; // int
int,
. ""
, -
: short, long, signed unsigned, -
short, long, int , int.
signed unsigned,
signed.
,
, :
const unsigned int BakersDozen = 13; //
unsigned int const AnotherBakersDozen (13) ; //
enum, :
enum { one = 1, two = 2, ten = 1 0 };
:
const int one = 1, two = 2, ten = 10;
, , ,
:
enum { one = 1, two, three, four, five, six, seven, eight, nine, ten };
1 , .
, -
, . . ,
:
enum { zero, one, two, three, four, five, six, seven, eight, nine, ten };
"L C++ 43
:
enum { bit = -1, zero };
1
. , zero
.
enum .
() , :
enum Boolean { false, true };
, -
:
Boolean a;
Boolean b(true);
Boolean d = false;
, -
, . -
Boolean , ,
. ,
"" :
= + d;
int f = b + false;
. , ,
.
""
"", typedef.
typedef int Boolean;
const Boolean false = 0;
const Boolean true = 1;
.
true false
.
(
C++ bool. true false
, Borland C++Builder 6, Visual C++6
. Borland C++ 3.1 .
typedef, ,
:
typedef ____ ___;
44 I. C++
Turbo Pascal,
word real, :
typedef unsigned int word;
typedef double real;
Windows ""
DWORD, BOOL BYTE.
__ -
.
-
, . -
. ,
, : 32/4, /4, 34/4, 35/4.
C++ , -
: ( ),
% (). -
, 1.15.
[ 1.15.
tfinclude <iostream,h>
int main()
( cout 5 % 2 endl;
cout - 5 * 2 endl;
cout 5 % -2 endl;
cout -5 % -2 endl
return 0;
: 1, -1, i, -i.
, ,
. -
. k
, , , -
:
if (k % 2 = 0) cout " !" endl;
, .
, , . -
, , 4
( ), , 100.
100, ,
1. C++ 45
400. , 1600,
2000, 2004, 1996, 1900 2003 . -
, , -,
. ,
( 1.16).
1.16.
frinclude <iostream.h>
int main()
{ unsigned int year = 0; //
begin: cout " : ";
cin year;
if (year == 0) goto end; //
if ((year s 100) != 0}
if ((year % 4) == 0) cout " !" endl;
else cout " !" endl;
else
if ((year % 400) == 0) cout " !" endl;
else cQut " !" endl;
goto begin; //
end:
return 0;
, -
. -, ,
. goto
: begin end . -, -
. if :
if () if () ; else ;
, if else,
C++ , :
if () (if () ; else ; |
, else
if.
, -
:
(( 100) ( 4))
( 400} ,
;
46 VacTbJ.
, -
.
:
if (((year % 100) != 0) && ((year % 4) = OJ || ((year % 400) == 0))
cout " !" endl;
else cout " !" endl;
(&&) ,
(| |).
- : 2 -
. 2000,
1996, 1700, 1997, ,
, . -
.
, C++
. , ,
, , . .
.
( j
, -
Borland C++ 3.1. , -
Windows, . .
C++ .
C++. - -
( ): '', ' + ', ''.
. -
- Borland C++
. , C++ -
. -
\ (backslash). C++ -
'\", ' V " , ' \ \ ' .
.
; -
, '\' <Backspace>, ' \ '
. '\',
:
cout ' \ ' endl;
t C++ 47
endl
1 , ,
. , \ -
,
151 = '\' <Enter>.
1 , , sizeof (int) .
f \ t '
,
, 1.17.
I 1.17.
^include <iostream.h>
int main()
{ cout " " endl
"1 \ t2 \ t3 \ t4 \ t5 \ t6 \ t7 \ t8 \ t9" endl
--------------------------------- endl;
for (int 1 = 1 ; i < 10; i++)
{ cout i "| ";
for (int'j = 1; j < 10; j++) cout j * i '\f;
cout endl;
return 0;
:
.
1 2 3 4 6
11 1 2 3 4 5 6 7 8 9
21 2 4 6 8 10 12 14 16 18
31 3 6 9 12 15 18 21 24 27
41 4 8 12 16 20 24 28 32 36
, 10 15 20 25 30 35 40 45
6 6 12 18 24 30 36 42 48 54
71 1 14 21 28 35 42 49 56 63
8| 9 16 24 32 40 48 56 64 12
9 9 18 27 36 45 54 63 72 81
char. char, -
, 1 .
char (signed), (unsigned).
48 _ 4acTbJ. ^ C++
"-",
, (
).
, ,
. " " .
, ,
,
. , -
.
, , -
1.18.
I 1.18.
# include <iostream.h>
int main ()
{ unsigned char nn = 48; //
cout nn ' ' int(nn) encil;
return 0;
,
.
, ' ' . '
"-".
,
int (nn). , -
( ),
. :
cout nn ' ' (int)nn endl;
, , .
. -
, :
cout int('\t') endl; //
cout int('\n') endl; //
cout int('\a') endl; //
cout int('\b'J endl; // backspace
: 9, , 8, 7.
, -
, -
. , 1.19.
1. C++ 49
\ 1.19.
#include <iostream.h>
int main()
{ for (char i = 48; i < 59; ++i) cout i ' ';
return 0;
0 1 2 3 4 5 6 7 8 9 . ,
, -
48 57.
, -
. .
, , ch . -
ch - ' '
.
,
typedef :
typedef signed char small;
typedef unsigned char byte;
small
byte, , , char.
, -
, .
Borland C++ 3.1, ASCII.
, , ,
, .
IBM PC
.
,
.
, -
.
-
. .
.
. :
=
50 I.
= .
,
. -
. , -
, , -
. ,
.
: ++ , ++; -
.
, -
.
:
_1 = __2 = __3 = . . . = __ =
: -
, _,
. . _1. -
, -
, :
= 5 + ( = 6) ;
, -
11 (5 + ). :
a = (b > 5);
i
. , .
-
( ). 5. -
:
d = ++;
d = ++;
1 -
d.
-: 5 d,
1. , -
:
1. C++ 51
, "" : -
d = (--) ;
5 d, -
1. , -
, () -
-.
,
(, ) . -
:
= + ;
, = ()
. , :
i = i + 2;
: i (, )
2 (5) -
.
.
#=-, -
: +, -, *, /, %. -
:
#=
:
= # ()
,
#, . ,
2 C++ -
:
, . ""
, .
, , .
-
.
:
52 I. C++
[[char, short] -> int] -> unsigned int -> long ->
unsigned long -> float -> double -> long double
, "" "".
char short
int, -
.
, :
float -> int
( ), -
.
. -
:
(}
()
.
. , " ...
" [37, . 172].
C++
:
static_cast<TMn> ()
reinterpret_cast<Trai>()
. ,
static_cast. -
.
, ,
. . -
[37, . 172],
reinterpret_cast " ". -
.
dynamic^cast,
If.
?,
, :
_ ? _1: 2
. ^^^ C++ 53
: _, -
, _1 -
; , -
_2.
. , :
max = ( > ] ? : ;
&& ( ) | |
( ).
, ! ( ). -
, , .
.
0 (false ) 1 (true ).
. -
:
! = 1 !false = true
!1 = 0 'true = false
:
|! 0 = 0 false | | false = f a l s e
0 [| 1-1 false I | true = true
1 || 0 = 1 true | | false = true
1 || 1=1 true | I true = true
:
&& 0 = 0 false && false = false
0 && 1 = 0 false && true = false
1 && 0 = 0 true &5 false = false
1 && 1 = 1 true & true = true
:
, -
. ,
(1 I ] )
i, -
.
( & & )
, .
:
_1 && _2 !_1? (__2?1: 0) : 0 ) ;
D _1 | | _2 (_1?1: (_2?1:0} ) ;
[ (?0:1).
54 I. C++
( C++ -
): ( -
) , -
.
, . . . -
, , -
. , C++
:
if () ;
:
if ( != 0) ;
if ( == 0) ;
if (!) ;
, -
=*= () = (). , -
:
if ( = 0) cout " 0" endl;
else cout " 0" endl;
-
, .
: , ,
. ,
else.
, , -
:
if (0 =- a) cout " 0" endl;
else cout " 0" endl;
,
.
. ,
C++ :
whi1 () ;
:
, .
for, :
for (_1; _2; _3) ;
1. C++ 55
_1
; ,
_. _2
,
. -
.
, -
, -
, .
.
, ,
. , -
( 1.20).
^include <iostream.h>
int main()
( unsigned int counter = 0;
double summa =0.0, number;
while (cout " : ", cin number) //
//
summa += number, ++counter; //
cout "=" summa / counter endl;
return 0;
.
, .
, ,
. , -
. C++
, ! -
() . , ,
,
.
.
, -
<Ctrl>+<Z> ( <Enter>). -
" ", -
0, .
, , , -
.
, .
56 I. C++
for (
1.21.
#include <iostream.h>
int main()
{ unsigned int counter = 0;
for (double summa =0.0, number; //
(cout " : ", cin number); //
summa += number, -t-+counter) ; //
cout "^" summa / counter endl;
return 0;
for -
.
for.
( )
. ,
, , -
while .
.
_1 _2.
,
. -
<Ctrl>+<Z>,
number , for -
! -
_ , . ,
_3, -
_2, .
""
:
= it = , += 1, t) ;
t , .
C++ (. . 1.1), ( -
) . , (. . 2)
" , () -
1. C++ 7
. (. . 3) .
,
. . 1,1
:
, .
1.1. C++
sizeof new delete
*r >*
*/%
. -
" ",
. , -
, . ,
float, , , ,
1037. , :
cout * 1000 endl;
I. C++
, float.
. , -
. -
-
.
.
Borland C++ 3.1, , int -
32 768 +32 767. , , -
:
cout +32767 + 1 endl;
cout -32768 - 1 endl;
-32 168, -
32 767. , ,
1 () -
. 1
() , , -
.
long short.
unsigned int,
0 65 535.
cout 65535U + 1 endl;
. ,
cout Ou - 1 endl;
65 535. , -
, -
.
char ,
"-". " "
-128 127. Borland,
Visual C++ 6 "", , , -
:
char ch = 255;
ch -1.
, Visual C++ 6 :
1. C++ 59
1
warning 4305: 'initializing : truncation from 'const int' to 'char'
1
'': 'const int' 'char
. -
, d
double d = 15/2;
7, 7,5. , 15/2
. -
( ) double.
,
:
double d - 15.0/2;
double d = 15/2.0;
"" , :
double d;
d = 3,141592653;
d , "", -
. ,
double d = 3,141592653;
. :
j - k i, a j + k
5. . -
, -
. Visual C++ 6 :
i = (i = j - k) + (i = j + k) ;
. ,
2.
undefined behaviour .
. ,
:
fa += b) += a;
: ,
, ? -
,
. ,
i = ++i + 1;
, -
. - , , ,
,
, , -
i 2. ,
1=7, i++, i++;
defined behaviour .
i 9. , ""
" " . -
,
, (| |) (&&).
, -
, .
.
2
, , " ". -
,
"". -
, -
. ,
. , ,
. -
-
. , ,
. -
.
Fortran -
: . -
, Pascal, -
.
C++ , .
. -
, ,
, -
.
C++ ,
, , -
. -
, .
:
, ,
62 /. . C++
,
, .
, , ++
, -
, , ,
, .
C++ -
.
C++,
. , ,
(Standart Template Library, STL).
STL ,
.
, ,
. C++ "-
" :
, "" ;
C++, STL;
G , , -
, conio.h;
- (MFC,
ATL, WTL, OWL, VCL),
Windows;
API , ;
, , boost.
, , -
. , , -
.
, , , , -
1 +1. -
, .
, - ,
"". C++ -
, :
( );
, C++.
, , ,
2. 63
, , .
. ,
void.
. C++ -
, -
, .
:
{ )
, ,
. , , -
, C++ -
. , , -
, -
:
double = sin(x) + 2 * cos(x) + 1.53;
,
void. , , -
,
, :
randomize () ;
- , , -
,
.
C++ .
,
, -
, include .
C++ -
, Borland
math.h. ,
()
(. 1.7). ,
ha, hb, he. , -
, ,
- )( - )
64 I. C++
- (++) /2 .
.
: math.h -
:
double sqrt(double x) ;
long double sqrtldong double x) ;
, , , -
.
-
. ,
.
, -
. , -
. (
) ,
\ . -
. , -
2.1.
j )2.1.
^include <iostream.h>
#include <math,h>
int mainf)
{ double a, b, ; //
double ha, hb, he; //
double p = 0 , S = 0 ; //
cout " : ";
cin a ;
= (++)/2; //
3 = sqrt( * (-) * (-) * (-)); //
//
ha - 2/a*S;
cout " =" ha endl;
hb = 2/b*3;
ccut "Hb =" hb endl;
he = 2/c*S;
cout "He =" he endl;
return 0;
,
, , .
-
2. 65
: , , -
. , 4, 5
, 4, ,
, .
1, 1, 1 : .866025. -
, , 2 -
1.885618, 2.828427.
math.h :
, -
; ;
.
.
C++
stdlib.h. ,
, .
, -
, [44], .
"" 1 1000,
.
. ,
. Bor-
land C++ 3.1
randomize, :
void randomize (void) ;
, ,
.
random, :
int random (int num) ;
(~ j
, C++, -
Borland Pascal, Borland
"" . Visual C++ 6 .
[0, num).
, num : -
num1. ,
1 1000, :
int number = random{1000] + 1;
, ,
random :
int number = random( ) + ;
3 1264
66 I. C++
, 2.2.
! 2.2.
^include <iostream-h>
#include <stdlib.h>
int m a i n ( )
{ cout " 1 1000." endl;
cout " !" endl;
cout " 10 " endl;
const int n = 10; //
randomize () ; //
unsigned int number = random(lOOO) + 1; //
unsigned int getnum; //
int i - 0;
while (i < n)
( cout " ?" endl;
cin getnum;
++i; //
if (number < getnum) cout " ! \";
if (number > getnum) cout " ! \";
if (number === getnum)
{ cout " ! ! ! \ n " ;
cout " " i " . \n";
return 0; //
, .
500, :
. , 500. -
250 0500. ,
500, 750, -
5001000. -
"" .
" " , C++
. -
, , -
67
. , -
. , , -
. .
. ,
.
int max3(int a, int b, int c)
{ int t = (a > b) ? a: b;
return (t > c) ? t: c;
1
. -
t, -
. ,
-
.
(
2.3), -
, stdlib.h.
""
| 2.3.
,
.
, :
int = (, 5, );
# include <stdlib.h>
. . max . -
-
. :
double LengthCircle (double r) ( return 2*M_PI*r; }
,
:
double SquareCircle (double r} I return M_PI*r*r; }
.
68 _ I. C++
""
. ,
, -
void .
. ,
.
, , -
. -
, -1 ,
. .
2.4.
:
int main ( }
{ int k, cc;
cout " 1 9: ";
cin k;
if (cc = TableMult fk) )
cout " ! " endl;
return cc;
}
, main . -
: ,
.
. ,
-1, .
. C++
. C++ -
2. 69_
( ) , ().
,
.
, , :
.
:
: extern static. -
extern;
, void. int;
;
;
;
throw ( ).
.
extern , -
. static
. ,
, -
. -
. II.
.
, . .
, . -
, -
. { }:
void f(void) { }
"".
v
J
. -
,
. ,
.
:
float Maxffloat x, float ) //
( return ( < ? : ); }
int main(void)
{ float Cube(float a); //
int cc= 15, k = 2;
JO __ __ I.
//
,
. , -
, ,
.
-
. -
.
, -
:
, C++:
, , -
. , -
, . -
- : -
,
.
C++ -
. ,
.
, C++ .
1 . .
2. .
3. .
, , C++
,
.
: -
? , -
.
2. 71
-
, , -
. , -
(. . " " " "
),
:
_ = . ,
( ), .
,
, ;
,
, -
;
,
.
: sin, cos, ., -
. -
.
cube,
. .
float Cube(float x) //
{ return (x * * ); }
float = 5.5, - 1.23, = Cube{2); //
//
float d = Cube (b); //
float e = Cube(a/b + 2,51); //
float f = Cube(Cube()); //
, . -
f (f ( ) ) . 8, -
: 8 512,
134217728.
, -
,
.
72 /^ C++
, -
1.
cout Cube(b++) endl;
cout b endl;
i
2
, ,
, +-
-
:
= ++;
,
. C++
, .
C++
"" ( ) -
( 2.5)'.
V V * - . :
\ 2.5.
double sqr(double ) //
{ return * ; }
int main(void)
{ double k = 0;
double g - 1, f=2;
g = sqr({f = sqrif), k += f)); // -
//
cout "\n\n f =" f;
cout "\n g =" g;
cout "\n k =" k;
return 0;
g - s q r ( ( f = s q r ( f ) , k += f ) ) ,
. -
C++: ""
. , ,
' , ^include.
! j. 73
, . -
( ) -
. "" , -
: .
. sqr -
f 2, , .
f - s q r ( f ) f 4,
-
k. k += f, ""
sqr, 4. g
16 = 4 x 4 . , :
f = 4
g = 16
k = 4
C++ . -
:
=
, -
. C++ -
:
()
.
( ) .
-- ,
. -
, .
, . -
. -
(. 1.17). , : -
, , -
: ,
60. 2.6.
I 2.6,
void repchfchar ch = '-', int k = 60)
{ for (int i - 0; i < k; ++i) cout ch; }
74 I. _++_
C++ :
repchO; // 60
repch('+'); // 60
repch{'=', 52); // 52
,
. C++ -
.
repch!,54};
. -
:
void repchfchar ch = '-', int k)
, :
repch(54);
, 54- , -
60 .
: 54
' 6 ' . -
, , .
. -
:
void repchfchar ch, int k = 60}
repch(54);
60 .
.
, , ,
2.7.
2.7.
, -
, .
, -
, . ,
, , -
( 2.8).
! 2.8.
double g = I, f;
double sqr (double x) //
{ return x * x; |
double ff (double x = sqr[f}) //
/ /
{ return x; }
int main (void)
{ f = 2;
cout "\n \n f =" f endl;
cout "\n ff(x = sqr(f) =" ff(); //
//
s
cout "\n ff(3) = " ff (3) endl; //
return 0;
,
:
f = 2
ff (x *= s q r ( f ) } = 4
ff(3) = 3
ff f,
4. -
, ff . -
,
, .
(. -
2.5) ( 2.9).
; 2.9.
;,...:. ...... - ..................... , ..................... : ......... ;. .......... :......:. ...- ..... :,...-. ......................... . .............. : .................. .. ....... . ................... .-.-.-.-. .
double g = 1, x;
double" sqr (double x) //
{ return x * x; }
76 I. C++
,
( = 2) sqr.
, , -
.
(-
2.10).
2.10.
double = 0; //
double ff (double x) ; //
double ff(double x = ff ()) //
{return ;} //
int main(void)
f = ;
cout "\n ff(x - ff(y)) =" ff();
cout "\n ff(3) =" ff(3) endl;
return 0;
}
f f,
. ,
. -
f f ,
, . -
:
ff( - ff()) = 6
ff(3) - 3
. ,
, . -
_2. 77
, -
! .
, -
.
" "
-
. .
. -
. ,
. " "
" ". ,
,
" ".
C++ ,
, ,
, .
,
. . .
" "--
.
. C++
. . " "
. -
, . . -
.
, , , -
, -
. ,
. -
:
, . .
. ,
. -
;
. "-
" "" . ,
" "
78 _ I. C++
. . -
1: -
, -
.
:
1. ,
?
2. ,
( )?
3. ,
" " ?
: -
,
.
: ""
. :
int x = 5; //
void f(void)
( int x = 3; //
cout "x = " " endl;
:
= 5
= 3
, .
,
f , -
. -
.
1
, Last In First Out
(UFO) " ".
_ . 79
- ,
, ?
::. , .
int x = 5; //
void f(void)
{ int x = 3; //
cout "x = x " " endl;
cout "x = :: x " " endl;
}
int main(void)
. . .
cout "x =" x endi;
f();
cout "x =" x endl;
. . .
1
:
= 5
= 3
= 5
= 5
,
: , , -
. , ,
, -
, , . , -
? C++ -
, .
. -
:
( ) . -
. (
2.11).
! 2.11. :
int f l i n t x = 3);
// int f(int x = 7); // ,
int main(void)
{ cout f() endl; // 3
80 _ 1^^
int f ( i n t x = 9} ; //
cout f f j endl; // 9
return 0;
int f(int x) //
( return x; }
: -
, , -
, .
: -
, " " -
. -
. :
static
, , -
. -
,
. -
.
.
-
, , . -
:
unsigned int f ()
( static unsigned int count = 0;
// . . .
-t-4-count ;
return count;
:
insigned int n = f () ;
.
, .
(. 1.11). 2.12
.
!_ 81
! 2.12.
;
int main()
i double data = 1, middle;
while (data) //
{ cout " : ";
cin data;
middle = Average(data]; //
cout " : " middle endl;
)
return 0;
J
summa count -
. summa
data, count 1. -
( ).
( ) -
, , -
. , -
.
C++ , , . -
if, :
if (int = 1) cout endl;
:
if (int { 1 ) ) cout endl;
if, ""
true, "" false.
" ". ,
, .
.
I. C++
Borland C++ Builder 6, Visual C++ 6,
.
while. ,
. for -
_1. -
for,
"" , .
,
.
Borland C++ Builder 6, Visual C++ 6, -
.
, ,
,
. . -
, ,
.
. , , -
: , .
void Swapiint a, int b)
{ int t = a; a = b; b = t; }
, ,
. -
, .
int = 5, = 3;
Swap(x, ) ;
cout " =" ";
= " endl;
cout = 5 = . ,
Swap . -
:
void Swap(int a, int b)
{ int t = a; a = b; b = t;
2. 83
, :
=* 3; b = 5
=. 5; = 3
, . -
. -
: -
.
, , -
. (-
) , -
.
. swap (
2.13).
; ...."-v ; ;, ; yi-r.pl - ', ;; :
\ 2.13.
-
:
Swap(x, ) ;
, , -
.
C++ -
.
:. J
-
. Borland
Borland, .
,
. . .
,
.
""
(-
2.14).
84 _ I. C++
! 2.14. -
!.;:, ......................... :ILJ..D. .............................................. . .............. '.:..>,., .......................... ~~ ................................ *....;..: ............................................................ :-....-
double (double &) // -
{ return {--} ; }
int main (void)
( double g - 1, f = 2;
g = s({f = s(f), f ) ) ; //
cout "\n \n f = " f;
cout "\n g = " g;
return 0;
! -
- s .
, , :
f= 3
g = 2
, s , f -
^ . -
++, .
.
, .
f.
"int f(int x); //
int f(int& x) ; //
int f (const int x) ; //
int f (const int& x) ; //
, .
-
. ,
. ,
,
. ,
.
, -
.
-
2. 85
.
, .
, -
:
.
.
, : -
, (
Visual C++ 6
),
, ,
( const). ,
,
:
;
.
-, ,
- - -
.
-
, ,
, . , -
.
.
. , -
-
. -
. , ,
,
.
. , -
.
, .
1 ( -
2.15).
1
Pascal : . . -
Turbo Pascal 6.0, 7.0. .: , , 1993.
86 _ ___ I. C++
\ 2.15.
int a, z; //
int (int )
( z -= ; //
return ( * ) ;
}
void main (void)
{ z = 10;
a = C(l); // ""
cout z ' ' a endl; // 9 1
z = 10;
a = C(z); //
cout z ' ' a endl; // 100
z = 10;
a = ) * C(z); //
cout z ' ' a endl; //
z = 10;
a - C(z) * C(10); //
cout z ' ' a endl; // -10 10000
z,
, ,
. ,
. -
.
, -
. z
, z -
, z .
-
. -
, .
, C++ -
: . -
-
.
,
Visual C++ 6 ( -
2.16) (9 1, , 0 , -10 0 ).
, z
.
2. 87
: 2.16.
^include <iostream,h>
int a, z; //
int CC (int &)
{ z ;
return * ;
. - -
-
. ,
2.17.
| 2.17.
int i; //
double f () // ,
{ double sum = 0;
for (i = 0; i < 3; i-H-J sum += i;
return sum; // 3 = 0 -f 1 + 2
! ,
, : -
C++. ,
9, ,
. ! -
i = . f o , -
, . -
: i,
. -
, f ,
, sum ,
9.
,
. , ,
,
.
-
, ,
. , -
. -
. -
:
int f ( i n t &a, int Sb)
{ return (a++ + b++) ; }
. -
int i = 1, j = 1;
j = f ( j , j);
i = f (i, i);
cout i ' ; ' j ' ; ' endl;
,
return:
return {a++, b++, +b);
i = f(i, i);
6.
, .
,
, -
.
(overloading).
, - .
Help Borland
, .
, -
- :
int abs(int x);
long labs(long x);
double fabs(double x);
long double fabsl(long double x);
, -
, ,
, -
. , ,
.
, -
. -
,
. , -
, ,
void print(char t) { . . . }
int print(char t) ( . . . )
, :
f(b);
? Visual C++ 6
"", 29 (" ").
const
. -
.
, -
. , .
.
void gfint a) {cout "g(int a)" endl;}
void gfconst int a) { cout "gtconst int a)" endl; }
2, 91
, Visual C++ 6 ,
- "", -
. :
error 2084: function 'void cdecl gfint)' already has a body
'void cdecl g(int)'
,
void gfint &) { cout "gfint Sa)" endl; }
void g(const int sa) { cout "g(const int &a)" endl; }
, , -
: ,
const; ,
.
"" :
void g(int Sa) { cout "g(int Sa)" endl; }
void gfconst int a) { cout "g(const int a ) " endl; f
, .
!2);
g(v);
2668 .
:
void f ( i n t a) //
{ cout " f ( a ) " endl; }
void f ( i n t a, int b = 0} //
( cout " f ( a , b - 0 ) " endl; }
.
2668. ,
, . , -
.' Borland
.
92 I.
:
void f(double a) I/
{ cout "f{double a ) " endl; }
, -
.
(, ir.t, long short) -
- . -
,
: -
, -
.
.
-
.
-
.
. , ,
"" .
2 -
( signed char unsigned char), 6
3 . 11 -
. ,
. -
, . , -
.
-
, . , .
, C++
, "" ,
. -
:
template < >
(
2.18), .
2. 5
| 2.18.
, , ,
. -
-
. , ,
, .
, " " ,
. -
, :
long k = 7, d = 12;
swap(k, d);
float p = 62.44f q = 68.3;
swap(p, q) ;
:
void swapflongS , longs ) { long z = ; = ; = z; }
void swap(floats x, floats ) { float z = ; = ; = z; }
,
, . -
. -
swap<long>(k, d);
swap<float>(p, q);
, -
.
.
,
. -
, , -
class, ().
J
typename,
. -
Borland C++3.1, -
Borland C++ 5, Borland C++ Builder Q .
94 !. C++
C++ -
, .
)
Borland C++ 3-1 , . . .
, -
(. 2.6) ,
2.19.
.
i 2.19.
-
. -
:
repch<10>('+');
- . ,
.
template <int n = '10>
void repchfchar ch = '-')
{ for (int i = 0; i < n; ++i) cout ch; }
C++ . , -
, ( 2.19), -
(. 2.6). -
:
repch<10>('=');
repch('=');
repch(' + ', 30) ;
repch<5>('+');
( ^)
Visual C++
. ,
. Borland C++ Builder 6 .
,
. , -
STL.
2. 95
C++ -
. . -
C++ ctype.h,
. ,
. . 2.1.
2.1. ctype.h
, is, -,
- ,
"" "". -
, true
false, . "", , , -
"" i. Borland C++ 3.1 -
, . , -
2 . Visual C++ 6 -
2 4. ,
1, . tolower toupper.
-
, .
96 I. C++
,
. , ,
.
. ,
-.
Borland conio.h, -
. -
Visual C++ 6.
, :
int getch(void);
, -
. ,
. -
, <Esc>,
27. , -
unsigned char.
2.20.
[ 2.20.
tinclude <iostream.h>
^include <conio.h>
int main f )
{ const int Esc = 27; // ASCII- ESC
unsigned char ch = 0; // char
while (ch != Esc}
{ ch = getchO; // char
cout ch '='
int(ch) endl; //
}
return 0;
,
128 159. "",
240. :
"" "" 160 175 (
32), "" "" 224 239.
"" "" 48 : 176223 -
.
, , "",
241.
2. 97
Visual C++ 6 Borland C++ Builder 6
.
, , -
, ctype.h.
isalpha, isalnum, isgraph, islower, isupper, tolower, toupper. -
isupper 2.21.
1 2.21. isupper
;.'. ; '........ , , .'.;
l, . -
, -
.
,
if ( { 6 4 < ch) && (ch < 9 1 ) ) return 1;
.
, -
.
""
. , "" -
. islower -
2.22.
) 2.22, islower
-
.
int isralphafint ch)
( return (isrlower(ch) I I isrupper(ch)); )
. 12fi4
98 _ ^ C++
, isainuni isgraph .
- . -
toiower 2.23.
; ............................. ............... ;" ...... ' ...... :".' ...... ' .............................................. ' ..................... *- ...... .' ............................ '"""' ..... " ................................................................ ]
! 23. toiower :
! ....................... ;. ............ ___ .......... , ................................. , ........................... .... ................................... ; ...... ....;..: ...... ::. ................ :. ................ ;: ..... .: ....... ......
,
. , , -
,
. toupper -
2.24.
; .......................... ; ...... ; ............................. ' ....... ". .............. ; .............. : .................................. " ..... ........... " .......................................... ; .............................. -{
I 2.24. toupper
, :
. ,
Windows, , -
. , -
-
Windows, DOS! -
.
2. 99_
inline-
. -
.
"" C++, ""
. C++ -
. -
, . -
.
( J
, , Bin
Borland C++ Builder 6 cpp32.exe.
.
#inciude (. . 1).
, ,
ttdefine. :
ftdefine
:
^define and &6
#def ine or 1 |
ttdefine not !
.
float. h limit.h, , .
, float double
Visual C++ 6 float. h :
ttdefine DBL_MAX 1 . 7976931348623158e+3Q8 /* max value */
ttdefine DBL_MIN 2 . 2250738585072Q14e-308 /* min positive value */
#define FLT_MAX 3 . 402S23466e+38F /* max value */
ttdefine FLT MIN 1 . 175494351e-38F /* rain positive value */
limit. h , -
:
tdefine INT_MIN (-214748364T - 1) /* minimum (signed) int value */
ttdefine INT_MAX 2147483647 /* maximum (signed) int value */
#define UINT_MAX Oxffffffff /* maximum unsigned int value */
Windows. : -
^include
100 I. C++
, #define, -
.
, ,
-. ,
C++ const, -
. . -
const
^define.
, #define, , , -
, . -
.
(. 2.3). , -
, , -
-
. , , -
. ,
.
, .
, ,
#define max3(a, b, ) max(max(a, b ) , c )
int x = max3(c + d, 5, C u b e ( n ) ) ;
, :
int = max (max( + d, 5 ) , C u b e ( n ) ) ;
, include -
, .
,
randomizef) random . stdlib.li, , -
, :
#define RAND_MAX Ox7FFFU //
tdefine random(num) {int) ( ( (long) rand () * (num)) / (RAND_MAX -f 1))
ftdefine randomize()srandf(unsigned) time (NULL))
To max mi
#define max(a, b) ( ( ( a ) > (b)) ? (a) :
^define min(a, b) ( ( ( a ) < ( b ) ) ? (a) :
2^ 101
"
". ,
.
. , , -
.
tfdefine max(, ) > ? :
int d = max (a += 1, b += 2) ;
:
int d - += 1 > += 2 ? += 1 : += 2;
, . . >
+=. i > , -
+=, ,
" ".
, -
, C++ -. -
inline. max :
inline int max(int a, int b) { return (a > b ? a: b); )
,
.
inline ,
-
, . ,
, -
,
.
, :
;
"". , ;
switch goto;
,
, .
"" inline,
,
.
, -
102 I C++
. -
, # (). -
:
tfdefine DIAG(v) cout #v '=' v endl
.
tt 55.4.
DIAG(tt);
:
tt = 55.4;
, , -
-
.
,
. ,
LINE , FILE , DATE .
2.25.
2.25. -
ttdefine Print(arg) \
cout #arg ';' endl; \
cout __DATE '; ' \
cout << FILE__ ';' endl \ __LINE__ endl; \
arg //
"" -
. , . .
( ).
, -
.
. :
DATE___ , __FILE -
, LIME - print ( -
2.26).
2.26. Print
#include <iostream.h>
//
//
2. _ 103
, , -
, -
;
int = 3; // arg
May 25 2003; // _ DATE__
PREPROC.CPP; // _ FILE _
11 // __LINE _
3 // main
.
(. . 11).
assert, -
. assert. h. . 1
(. 1.3). if
assert ( 2.27).
:,! ............... ......... "-;" ......... : ................................... " ........................................................... ' ........................................................... ' ..................................................... ;
I 2.27. assert
^include <assert.h>
^include <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k; //
assert ( (0 < k) && (k < 10)); //
cout k "* 1 =" k * 1 endl;
cout k "* 2 -" k * 2 endl;
cout k "* 3 =" k * 3 endl;
cout k "* 4 =" k * 4 endl;
104 I. C++
k ,
.
: Assertion failed. -
:
,
. ( < k} && (k < );
d , "".
cassert, file cassert.cpp;
, .
line 6.
Borland C++ 5, Borland C++ Builder 6, Visual C++ 6, Win-
dows, .
, ,
print. , , , . . -
:
.
. , -
^include <assert.h>
.
^define KDEBUG
ftinclude <assert.h>
, -
assert .
3
, ',
"", , , -
. , -
, , . -
. -,
: , , -
, , , , . .
(
). -, -
.
.
. -
.
C++ -
. -
, C++. -
.
-
-. , Pascal -
. C++ ( )
.
, , -
, ,
(2003). , -
706 I. C++
, . : -
1, 2 . . ,
. , 12 ,
: 31 , -
28 . . . :
,
7. , ,
( )
(-
). .
. ,
2003 . -
12 . ,
.
. -
switch:
switch () {
case 1: = 0; break;
case 2: = 31; break;
case 3: = 31 + 28; break/-
: -
, 12.
-
. ,
, . , -
, . ,
. , -
, :
[];
,
.
12 , :
int month[12];
-
. , C++ -
3. 107
, ,
.
, , . -
:
int month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
-
:
int monthf] - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
.
, ( -
),
. , -
:
int v[10] = (0);
, . .
, . ,
int v[] = { 0 } ;
, .
, -
, , -
sizeof . -
24 12
:
cout sizeof(month) sizeof (month) / sizeof(int) endl;
, -
. C++ (
) .
,
-- . ,
,
. , month[0] ,
month[1] . . (month[11]).
,
. -
for. ,
month:
for (int i = 0; i < 12; --i) cout month[i] endl;
108 _ I. C++
, -
:
for (int i = 0; i < 12; ++i) cin month [i];
,
:
for (int i - ; i < 12; ++i) month [i] = random(lOO);
:
int v[10] ;
v = { 1, 2, 3, 4, 5 );
, ""
. -
, for.
20 . -
:
for (int i = 0; i < 20; ++i) a [ i ] - b [ i ] ; .
, -
3.1.
| 3.1.
#include <iostream.h>
int main ()
{ unsigned int day, m;
int month [12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
cout " : ";
cin m; //
cout " : ";
cin day; //
for (int i = 0, s - 0; i < m - 1; ++i)
s += month [i]; //
+= day; //
int weekDay = ( (s + 2) % 7) ? (s + 2} % 1 : 7; //
//
cout weekDay endl;
return 0;
,
. -
. _ 109
weekDay? , 1 .
7 , 7.
:
. , -
.
, -
:
( . . );
, ;
;
.
, .
,
. , , , -
.
:
double Surrma (double array [], int n)
C++ ( ), -
, , " ", -
.
. , ,
, .sizeof, ,
month. -
:
double Summatdouble a r r a y [ ] )
( unsigned long n = sizeof (array) / sizeof (double) ;
double s = Q ;
for (int i = 0; i < n; ++i) s += array [ i] ;
return s;
main :
int main ( )
{ double v[10] { X, 2, 3, 4, 5, 6 };
1
, .
110 I. C++
!
:
, , -
, . -
, ,
.
cout, -
.
double Sumna(double arrayM)
{ cout ": " sizeof(array) endl;
unsigned long n = sizeof (array) / sizeof(double);
double s = 0;
for (int i = 0; i < n; ++i) += array[i];
return s;
int main()
{ double v[10] = { 1, 2, 3, 4, 5, 6 };
cout ": " sizeof(v) endl;
cout Summa(v) endl;
return 0;
}
, , 80 (sizeof (double) * ).
4. , -
: 4 / sizeof (double) ,
, .
:' -
. , .
, ,
. . 2
. ,
, .
3.2.
; 3.2.
, ,
.
, -
: "" , -
. -
, .
, , v[5]
, "" , 5- -
. C++ , -
.
int main ()
{ double v[10] = { 1, 2, 3, 4, 5, 6 };
cout Summa (v, 10) endl; //
cout Suirimafv + 2, 8) endl; // 8
cout Summa (v + 5, 5) endl; // 5
return 0;
J
-
, :
int ma in ()
{ int i = 3;
const int ten = 10;
double v[ten] = { 1, 2, 3, 4, 5, 6 } ;
cout Summa (v + i, ten i) endl; // v + i
/ / -
return 0;
1
, ,
.
C++ ,
. ,
, , -
:
cout Surma (v, 10000) endl; //
, , -
" ".
.
, -
. ( 3.3) -
112 _ \. _ ^
. , -
-1.
I: ...........
. 3.3.
.................................................... _ .......... . .......................... , ......... .............................. ; ..
, -
. (. 3.2),
"" -
, :
double pi = 3.141592653;
int k = FindNurnber (pi, v + 3, n 3) ;
- ,
:
. , -
, 3,4.
| 3.4. -
- ,
. , -
. ,
.
.
- .
"" ,
. .
.
typedef .
:
typedef [] ;
3. 113
. -
. -
. ,
:
typedef double DoubleArray[10];
DoubleArray a;
Suim-a :
double Surnma (DoubleArray &a) // 3.4
-
3.4. -
-
.
DoubleArray Summa(DoubleArray Sa)
"".
:
double Summa(DoubleArray a)
"" C++. -
. , -
" ". -
, .
, ,
.
, ,
, . . Surama:
double Summa(DoubleArray &a) (//, . .};
double Summa(DoubleArray a) (//. . .);
. -
" " " -
", . . .
cout Summa(v) endl; //
cout Summa(v + 2 ) endl; //
//
cout Summa(v + 5) endl; // 5
//
: ,
. ,
/. ^ C++
. . . -
, , -
, .
,
. , -
, 3.5,
.
; 3.5.
Borland C++ 3.1,
typename class.
, . ,
, .
. -
, . const
, . -
: int double.
int main ()
( int i;
int v[10] = {0};
int w[10] - { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (i = 0; i < 10; ++i) cout v[i] ' '; //
cout endl;
Copyfw, v, 10); // w -> v
for (i = 0; i < 10; ++i) cout v[i] ' ';
cout endl;
double vl[10] -= {0};
double wl[10] = { 1.1, 2.1, 3.1, 4, 5, 6, 7, 8, 9, 10.1 };
for (i = 0; i < 10; --i) cout vl[i] ' ';
cout endl;
Copytwl, vl, 10); // wl -> vl
for (i = 0; i < 10; ++i) cout vl[i] ' ';
cout endl;
return 0;
3. 115
:
0 0 0 0 0 0 0 0 0 0 // v int
1 2 3 4 5 6 8 9 1 0 // v < - w
0000000000 // vl double
1.1 2.1 3.1 4 5 7 8 9 10.1 // vl <- wl
, const,
.
, -
, & -
- .
, -
, ,
2 ( 3.6).
j 3.6. 2
, -
.
"
".
, , .
, , , -
: " ".
void Divide (double array [], int n, double m)
{ for {int i = 0; i < n; ++i) array[i] /= m; }
"" . , -
double w[10] = { 1.1, 2.1, 3.1, 4, 5, 6, 7, 8, 9, 10.1 };
Divide (w, 10, 2};
Divide (w, 10, 3) ;
Divide (w, 10, 7.543);
, -
. ? :
Divide (w, 10, w[5] ) ;
, , . -
, -
116 _[. C++
; ,
. 1.
w Divide, ,
,
1. -
. " " . -
, i,
!
, . -
, .
, ,
, .
-
.
(. . 7),
. -
: -
, . -
,
. -
,
/ ,
[9, 14, 39, 41, 45],
, .
. -
, . ,
, , .
-
.
. ,
:
int BinarySearch(int a [ ] , int begin, int end, int v)
begin end -
, a v . (
3.7) , ,
-1, .
3. 117
. 3.7.
, -
, const. -
-
. , :
begin = 0;
end = begin + (n 1);
,
, -
. -
.
, -
, . .
-
-.
, , -
. -
, " ",
-
.
,
. .
- "". -
3.8.
[ 3,8.
min, k- -
, .
double wl[10] = { 9, 8, 7, , 5, 4, 3, 2, 1, 10.1 };
for (i = 0; i < 10; ++i) cout wl[i] ' ';
cout endl;
SortArray(wl, 10);
for (i = 0; i < 10; ++i) cout wl[i] ' ';
cout endl;
:
9 8 7 6 5 4 3 2 1 10.1 / /
1 2 3 4 5 6 7 8 9 10.1 / /
. ,
, "" .
, -
.
1 -
. , -
: -
, , I,
. ,
"" .
-
. , , -1,
, .
- 3.9.
! 3.9, -
return 0;
, . -
int w[10] - ( 10, 2, 3, S, 8, 6, 5, 7, 4, 1};
for (i = 0; i < 10; ++i) cout w[ij ' ';
-
cout endl;
SortArray(w, 0, 10);
for (i = 0; i < 10; ++i) cout w[i] ' ';
cout endl;
:
10 2 3 9 8 6 5 7 4 1 //
1 2 3 4 5 6 7 8 9 10 //
.
.
C++ ,
. -
. -
, -
. typedef:
typedef float array [10]; // array - 10 float
array m [ 1 0 ] ; // 10 float [10]
-
:
int a[10] [10];
, :
int [3] [3] = { { 1, 2, 3 }, { 2, 3 } };
3- ,
3 . [0] { 1 , 2 , 3 ), A[i]
{ 2, , }, .
120 I. C++
:
[ 0 ] , [1], [ 2 ] . ,
. . 3.1 , .
] AM] A [2]
0^ 0 |
|0][0] [0][1] [0][2] |1][0] [1][1] [1][2] [2][0] A[2]|1J A [2] [2]
. 3.1. [ 3 ] [3]
A[i] [j], , .
"
:
int [ 3 ] [ 3 ] - { 1, 2, 3, 2, 3 };
. , -
, , :
int [ ] [ 3 ] = { { 1, 2, 3 }, { 2, 3 } };
,
(. 3.2), 3 .
API A[1]
1 ~2 ~]2 [0
[0][0] [0][1] [0][2]
. 3.2. [ ] [3]
[ 0 ] , [ 1 ]
, .
- ,
. 4.
, (. 3.1). -
. . -
weekoay , -
:
cout [>] encil;
3. _ 121
, C++ .
,
.
[17], -
. C++ string
C++.
. string
,
Borland C++ 3.1.
, char. -
, 7 ,
char.
:
char s [ ] = "";
,
, :
char s [ ] = { ' 1 , ' ' , ' ' , ' ' , ' ' , ' ' , ' 1 , ' ' , ' ' , ' ' , ' ' };
, , "" ,
char ( ) ""
.
7 , -
.
,
:
char Days [] [11] = { "", "", "", "",
"", "", ""
1;
, -
.
3.10.
! 3.10.
tinclude <iostream.h>
int main ( )
{ unsigned int day, rn;
int month [12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
122 I. C++
, -
-, -
. - ,
. ,
:
^include <iostream.h>
int main ( )
( cout sizeof ("") endl;
cout sizeof ("") endl;
return 0;
12 8 ,
- 1 , -
. , ""
"" 12 , ,
. - ""
"" , -
, "".
3. 123
Visual C++ 6 ,
Borland "" , .
, ,
, 12.
char Days[][12] = {"", "", "", "",
"", "", ""
. , -
. -
, -
.
( ) , -
.
( 3.11),
.
\ 3.11.
, . -
, s -
. "" , -
, . L ,
1 -
L - 1.
.
( ) -
( ). ( -
3.12), ,
.
, .
124 !.
3.12, atoi
.
n = n * 10 4- (number[i] - ' ' } ;
. (number - ' 1 ) , ,
. . , -
. , , number 5472.
n , n
5. :
n = 5 * 10 + 4 = 54;
:
n = 54 * 10 + 7 = 547;
, , -
:
n = 547 * 10 + 24 = 5472;
sign . -
:
char s [ ] = "-5427";
int a -= a t o i ( s ) ;
int b = a t o i { " 3 4 5 6 " ) ;
, .
,
atoi, atoi atof, stdlib.h.
.
, - ,
- . -
. -
3. 125
. -
, (. -
3.4).
, -
. Length
(. 3.11). ( -
3.13) , , -1,
.
, , -
.
I 3.13.
:
char s[80] = (0}; // ""
int = CopyStr(" ", );
:
char ss[80] = ""; // ""
char dd[] = " ";
int = CopyStr(dd, ss) ;
""
. ,
, :
for (i = 0; i < 80; ++i) cout (int) s [ i ] ' ';
cout endl;
for (i - 0; i < 80; ++i) cout (int} ss[i] ' ';
ccut endl;
, . . , ,
.
, / . -
(. ) ,
:
cout Days[weekDay] endl;
126 I. C++
C++ , -
-.
, :
char s[] = "";
COut s endl;
,
char s [ ] = { ' 1 , ' ' , ' 1 , ' ' , '', ' 1 , ' 1 , ' ' , ' ' , ' ' , '' };
. ,
sizeof. -
-:
cout s i z e o f ( s ) endl;
,
.
^include <iostream.h>
int main()
{ char b = 'a';
char s[] = { 'a', 'b1, 'c'f 'd', 'e1, 'f };
int d = 10101;
cout sizeof (s), endl;
cout s endl;
1
Borland C++ 3.1 :
6
abcdefwa
6 s. ,
"" wa. Visual C++ 6, -
"":
6
abcdef![!!|8_
, -
.
, -
, . -
""
Length (. 3.11).
sizeof, -
:
3. 127
, cout , -
-.
- "-
" . , , ,
:
cin s;
, , -
:
char s [80];
. :
^include <iostreara.h>
int main()
{ char sd[80];
cin sd;
cout sd endl;
return 0;
}
" " -
<Enter>. . -
"".
.
for (int i = 0; i < 80; ++i) cout sd[i];
, "".
,
. , . 6, -
/ C++.
( ) , . -
, : , -
. C++ :
struct Date { unsigned int year, month, day; };
128 I. C++
, 3.14.
j 3.14.
enum Month
{ jan = 1, feb, mar, apr, may, jun, j u l , aug, sep, oct, nov, dec };
struct Date
.
unsigned int year;
Month month; // .
unsigned int day;
Month Visual C++ 6
"" ,
: fatal error ciooi: INTERNAL COMPILER ERROR, -
. , Month
,
oct dec. Month,
(Get oct_),
"" . -
, .
. , " ",
Date, , .
Date , :
Date today; //
Date dl = { 2003, feb, 22 }; //
Date d = { 2000, jan }; //
Date d2 = dl; //
Date d3(d2); //
. , -
, .
C++ -
, .
:
Date z = { z.year = 2004, z.month - feb, z.day - 28 };
,
.
3. 129
,
struct Old { int a; float b; char ch; };
Old z = { z.b = 2.5, z.ch-'O 1 };
2 ( 2.5 0.5), -
48 ( '0'), ch .
, -
, :
struct ( unsigned int year, month, day; ) dl = { 2C03, feb, 22 );
-
Date, , -
.
"-
" . ,
di, di.year, di.month,
di.day. , ,
:
dl - today;
, :
cout " ( ):";
cin di.year dl.month di.day;
:
cout di.year '-' dl.month '-' di.day;
C++ , -
,
. -
, . -
. -
3.15.
5 . ! 264
130 t. C++
{ p.a = 6;
p.b - 7; //
return p; //
}
int main(void) //
{ Old = { 1, 2 }, x; //
x = g(y); //
= g fy) ; //
old ,
old
. x = g(y) . =
. = 7; , -
.
,
.
-
, . .
.
3.16.
! 3,16.
int main(void)
{ Old = { 1, 2 }, x;
x = g(yX; //
}
- g f y ) ,
.
const
, ( )
. ,
3.17,
.
3. 131
3117.
, -
. -
:
Old g(01d p ( 1, 2 })
- .
-
, :
Old g(Old p = t)
Old g(01d p ( t ) )
, .
. ,
. ,
.
,
, (
3.18).
3.18. -
struct Old //
{ int a; float b; };
Old = { 1, 2 }, x; //
Old q(01d p); //
Old g(01d p = g(y)} //
132 I.
( - - 6;
. = 7; //
return ; //
void main(void)
{ = g(); //
cout " =" . ";" y.b endl;
cout "x =" x.a ";" x.b endl;
= g(y); //
cout " =" . ";" y.b endl;
}
-
.
( ).
:
, ,
. , -
:
struct Point { double x, ; };
" ", -
. .
"" :
template <class T>
struct Pair { first; T second; };
-, -
3.19.
3. 133
; 3.19. -
,
.
,
, . . main
. -
, . -
, , ,
.
:
Pair <double, double> a; //
Pair <double, double> t = { 1, 2 }; //
Pair <double, double> r = t; //
Pair <double, double> d{t); //
Pair <int, double> p = ( 1.1, 2 };
Visual C++ 6 -
.
,
:
Pair <double, double> t = { 1, 2 };
Pair <int, double> k = t;
, , .
,
, , Date.
:
Pair <Date, double> rr;
typedef.
typedef Pair <double r double> ddPair;
typedef Pair <int, double> idPair;
-
. '
Windows, -
.
134 I. C++
:
, -
-
. , 10 -
. :
struct Number {double number[10]; Date date; );
,
:
Number N = { { 1.1, 2 . 2 , 3.3, 4 . 4 , 5.5, 6 . 6 , 7.7, 8.8, 9.9, 0},
{ 2003, feb, 2 } };
100 ,
:
Number NN[100];
, , .
-
. , :
NN[0].numberfO] - 1.2;
NN[0].numberfl] = 3.4;
// . . .
NN[0].number[9] = 5.7;
NN[0].date.year = 2003;
NN[0].date.month = feb;
NN[0].date.day = 22;
,
.
:
-
struct Array { double a[10]; };
Array a = { (1, 2, 3, 4, 5, 6, 7, 8, 9,0 } }; //
//
Array b = ;
Array ;
= ;
-
:
Array = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; //
3. 135
-
, .
, , -
,
. ,
-
. :
struct Person { char fio[30]; Date BirthDay; };
:
Person * { "", ( 1955, 12, 21 } };
:
Person w [ 2 ] = { { "", {1955, 12, 21 } },
( "", { 1975, 10, 12 } }
};
, ,
.
person :
Person mans[100];
BithDay mans
:
raans[5].BirthDay.year = 2003;
mans[5].BirthDay.month = feb;
mans[5].BirthDay.day = 22;
,
-:
mans[5].fio = ""; //
.
, . .
.
:
string.h, string,
,
Person .
,
.
. -
.
136 /. C++
. -
(. 3.2) (
3.20).
'!" "!">!!' ,-.... ............... .-.-, ,; |
; 3.20.
; ,_.. , , ..*.., ." -* "...."* ,...,.,.. ,._*^; . ,.,:,.,.,,, .,";.......^~.~, ...L,*.-!
N Number
:
double a = Average(N.number, 10);
,
, " "
. , ,
. ,
. -,
- -
.
- , . . Java.
,
, .
3.21.
| 3.21.
struct Number
{ double number [10];
Date date;
double Average (void) { return Summa (number, 10) / 10; }
Number, -
. ,
:
cout N. Average () endl;
,
. .
, -
, .
inline-,
3. 137
, .
- , ,
, .
::.
double Number :: Average(void) 1 return Summa(number, 10) / 10; }
: -
, . , -
, . ,
,
.
, , ,
. -
, -
, ' .
enum, , , -
. -
3.22.
3.22.
struct Number
{ enum { k = 10 };
double number[k];
Date date;
1;
double Number :: Average(void) { return Summa (number, k) / k; }
( ) -
,
static int k;
int Number :: k = 10;
.
:
static const int k 10;
- :
const int Number :: k;
138 I. C++
, Visual C++ 6, Borland C++ 5
.
Borland C++ Builder 6.
, .
: Number
10 . , -
, ,
10 , 12,
7. -
. .
-
, 3.23.
.
"" , 3.24,
:
3.24. ""
.'. : ', :.. .-. ;,...
template <int n>
struct Number
{ double number[n];
Date date;
double Average(void);
);
template < int n >
double Number <n> :: Average(void) ( return Summa (number, n) / n; }
, template ,
.
, , .
:
3. 139
Number<12>N = { { 1.1, 2,2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 0 },
{ 2003, feb_, 2 } };
cout N.Average() endl; // 12
Number<10>M = { { 1.1, -2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 0 },
{ 2003, _, 2 } };
cout M.Average endl; // 10
12 (-
2 ) 4.125.
10 4.5. , -
,
, " -
".
,
. "-
" , 3.25.
: 3.25.
!;..,..; : .-. ; .-... .'. :....,.,'. ;.-. .:,.;
,
:
Nuniber<12>N = { { 1.1, 2.2, 3.3, 4 . 4 , 5.5, 6.6, 7 . 7 , 8.8, 9.9, 0 },
( 2003, feb_, 2 } );
cout N.Average() endl; // 12
NumberOM = { { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 0 },
{ 2003, feb_, 2 } };
cout M.Averaged endl; // 10
, . . .
, ,
.
, ,
.
. , Date
.
140 I. C++
- -
.
Visual C++6. ^
3.26.
: '. ' ' """"
I 3.26.
^include <iostream.h>
int main ()
( struct AAA { char ch; double b; };
struct BBB { double b; char ch; };
struct CCC { int b; char ch; };
struct DDD { double b; char ch;
void f(void) { cout "Hello!" endl; }
:
16
16
DDD 16
, , -
:
-
, .
16, . . double
8. 8 , . . int
4 ;
.
.
Release
Minimize Size. Borland C++ Builder , Release
3. 14_1_
, , -
:
Bor-
land C++ 3.1 , char,
, ;
char 2 ;
.
.
-
, sizeof (), -
ttpragma.
,
(linker). ,
p a c k ( i ) .
( J
Borland C++ 3.1 " ". -
: ttpragma option --.
.
,
^pragma p a c k ( l )
^include, :
. 9
9
5
DDD 9
i
, , .
4
C++ -
. ,
. ,
. -
, , C++.
. [48],
" C++" - . -
, -
. -
. , -
.
,
-
. " -
", .
" ":
"" -- "". -
Java .
-
. 3 ,
(. 3.2). , -
, , -
:
Surama(A, A +' 10)
SuircnalA + 3, + 3)
144 _ I, C++
10- . ,
, 5- , [ 3 ] [ 7 ] . , -
. ,
: \1 .
, -
. - -
:
, , -.
,
.
. -
,
4.1.
: ............... "- .......... ""-* .................... ....... " .............................. > ......... -.' ................................... *...'. ......... ,...,. ......... , ...... ._, ........ ..-~. ----- -..* ......... * ....... . . ............ ..^
\ 4.1.
, . -
.
:
sum += *begin; //
, *begin ,
sum. , .
, -
. , -
.
, .
Intel . .
, ( 1). -
4. 145
. -
, -
. ,
, -
. , -
.
, -
, . ,
C++ , -
.
, .
, . -
, ,
. :
int *p; // int
. -
int ; // int
,
.
,
, & ():
int a;
= 3; //
int *pa;
pa = &; //
- -
. . 4.1.
&
, *
. 4.1.
C++ (and),
,
. , -
, :
int sra = ;
146 ^.^^ C++
- , , ,
, .
.
" " ,
. -
, .
- -
:
int *p;
-
, * . -
, -
. -
- begin (. 4.1).
- , -
, , -
. , & ( ) * (-
) . -
,
4.2.
| 4.2.
ttinclude <iostream.h>
int main()
{ int a = 5, b = 0; / / 1
int *pa; pa = &a; // 2
cout "a =" a endl; // 3
cout "*pa =" *pa ; endl; // 4
cout "pa =" pa endl; // 4 . 1
b - *pa - 3; // 5
cout "b =" b endl; //
if fa == *pa) *pa = b; / / 7
cout "a =" a endl; // 8
cout "*pa =" *pa endl; // 9
b = 6; pa = fib; // 10
cout "b = " b endl; // 11
cout "*pa =" *pa endl; // 12
cout "a =" a endl; // 13
return 0;
4. 147
:
= 5 // 3
* = 5 // 4
pa = Cx0066FDE8 // 4.1
b = 2 //
= 2 // 8
* =2 // 9
b = 6 // 11
* == // 12
= 2 // 13
1 2 .
. 4 -
, * . -
, * ,' 4.1, -
. 5
.
* - 3
: ( )
, . -
if 8 9
*: * .
. 11-13
.
, " ",
. , C++
(
):
*.
, ;
void*;
.
. -
. -
148 I. ^ C++
, -
.
.
,
,
.
-
.
:
*
, -
, -
(. 4.1). , -
, -
. , C++, -
. , ,
:
int* pa, pb;
, .
, " " - - -
, .
:
int = 3; //
int * = &; //
int *h{&a); // h
int *r = h; //
.
h, . *h, *pa * -
: , -
.
, "" -
, .
, "". -
, , -
. -
4. 149
, -
. , , -
:
double * = 0;
:
if ( ! p > . . .
"".
NULL, -
.
, -,
:
'
int 1 ;
.
const int ci = 5;
const int *pi = &ci;
pi
. - -
:
int *const pi = Sci; // ci -
!
, .
. -
, ,
:
D *
, ;
const * const *
, ;
* const -
, ;
const const *
const * const , .
, :
inn **p;
. "- -..."
.
150 I. C++
;
void *;
C++ void,
. (. . 1), C++
: -
:
()
()
,
:
int a = ;
int *pa = 5;
void *vpa = (void *)5a; // int* void*
void *vp = {void *)pa; // int* void*
:
-
int a = 6;
int *pa;
void *vpa = (void *)&a; // int* void*
pa - (int *)vpa; // void* int*
-
:
int a = 6;
double *dp - (double *)&;
int *ip - (int *)dp;
, (. . 1), C++ -
:
static_cast< i ran>()
reinterpret__cast<TMn> ()
, void * :
int = 6;
int *pa = &;
void *pointer = static_cast<void *>(sa);
pointer = reinterpret cast<void *>(pa);
4.^ 151
-
static_cast :
int = 6;
int *pa = &;
double *ddp = static_cast<double *>(); //
int *ip = static_cast<int *>(dp); //
double *dp = reinterpret_cast<double * > ( & a ) ; //
4.3.
int main()
{ cout sizeof(void *) endl;
cout sizeof(char *) endl;
cout sizeof(unsigned char *) endi;
cout sizeof(sort *) endl;
cout sizeof(unsigned short *) endl;
cout sizeof(int*) endi;
cout sizeof(unsigned int *) endl;
cout sizeof(long *) endl;
cout sizeof(unsigned long *) endl;
cout << sizeofffloat *) endl;
cout sizeof(double *) endl;
cout sizeof(long double *} endl;
return 0;
}
C++ -
. ,
, -. -
152 I, C++
: -
. :
int a[10];
int *p;
.
- -
:
, , -
+ i
1- . , i --
, . -
, i sizeof (). 1- -
, :
*( + i)
, *
+.
p[i] a [ i ] . * + i
: [0] + i. , a[i], * ( + i) -
, .
, -
, *( + i), a [ i j .
, - , -
(
). C++ -
. , -
*{ + i) *{i + ) -
, , 1[]. -
i 2.
coat 3[] endl;
cout (i + 1)[] endl;
ccut i[ + 1] endl;
cout * (i + + 1) endl;
[3].
, ++
++ "" :
sizeof () .
4. __ __ _ 153
, , 5- . -
// 2
// 3
, , :
i 5-
"" ;
2 5- -
;
"" -
6- .
,
.
. . -
. -
:
int a [10] ;
int *pl = &[1] ;
int *p2 = &[8] ;
cout 2 - pi endl;
7 [1]
[8].
, ,
.
C++ , -
. swap, -
. - -
:
.
void Swap(int *, int *b) //
{ int t = *; * = *b; *b = t; } //
, -
- . -
.
154 I. _ C++
-
(
), :
Swap(&x, fiy) ;
, , -
,
, &
( -
).
, -
. :
G - ;
- .
Swap -
4.4.
-:
-
4.4.
'.
-
','
f
int *&vi : vl
int. -
. swap :
int i - 10;
int j = 20;
int *pi = si;
.
int *pj = Sj; //
Swap (pi, pj>;
swap -
4.5.
:
;'"'"'""'"""""-'-' ""'" :"':"" ; '- -""'"'.' ' '"'-' '.'""' ."' "" ' " I
: 4.5.
!
"" :
Swap(&pi, s p j ) ;
4. (^ 155
-
.
void , -
.
void * :
void f(void *p)
, :
void *f(void *p)
,
.
.
-
.
. : -
, . -
-
void *. , ,
. . .
switch,
.
4.6.
| 4.6.
"" ,
-
switch. . -
, -, -
. , -
. case
-
"" .
, ,
stdlib.h.
, 4.7.
4.7.
int main(void)
{ int m = -123;
long ram = -321;
unsigned long mu = 1234567890;
float d - -123.4567e-9;
double dd = -123.45678901234;
char ch[50];
NumberToASCII(Int, &m, ch);
cout ch endl;
NumberToASCII(Long, &mm, ch);
cout ch endl;
NuriiberToASCII (UnsignedLong, smu, ch) ;
cout ch endl;
NumberToASCII(Float, &d, ch) ;
cout ch endl;
NumberToASCII{Double, &dd, ch);
cout ch endl;
return 0;
4 157
.
(- ):
int NumberToASCIKint n r char buffer[]);
int NumberToASCII(long n, char buffer[]);
int NumberToASCII(unsigned long n, char buffer[]);
int NumberToASCII(float n, char buffer []);
int NumberToASCII(double n, char buffer[j);
:
int NumberToASCII(int n, char buffer[])
{ buffer = _itoa(k, buffer, 10); }
, ,
.
, -
,
-
.
, -
, . ,
:
unsigned int Length(const char *s)
{ int L = 0; while (s[L++]); return (L -1); }
-
-. -
,
, :
void Print(char *s = " ")
{ cout endl; }
-. . [37] -
, :
const char* error_message(int i)
{ return "String of return"; }
158 t. C++
const, . i
, -
-.
, ,
-.
. 2 , , ,
. -
Borland C++3.1. -
. , -
Windows,
, Windows.
, Windows ASCII, -
. , -
Windows- ASCII. -
WinAPI charToOem.
4.8.
i 4.8,
i : ,. .-.,.....,. '.., .,.,!
BOOL CharToOem(
LPCTSTR IpszSrc //
LPSTR IpszDst //
:
IpszSrc ;
IpszDst , .
,
, Windows, -
-
. , , -
, .
. -
, windows.h. -
, , -
4.9.
: v;--V- " ">" " -" '" !'"' '. ' ' f";.""v :
! 4.9.
,
:
char * Rusfchar *in, char *out)
-
:
char s[100] = " !";
cout Rus(s, s) endl;
-:
char s[100];
cout ' ! ! Kellc!", s) endl;
string.h
:
, , -
-
.
string.h. C++.
BOB-
(char *). , , -
, :
size_t strlen(const char *s);
sizej: typedef unsigned
int.
. 4.1.
, -
, ASCII-. -
.
strlwr strupr, -,
. -
, ,
. strlwr
4.10.
I 4.10. strlwr
int L = strlen ( s ) ;
for (int i = 0; i < L; ++i)
for (int j = 0; j < nA; ++j}
if (s[i]=RusB[j]J { s[i] = RusM[j]; break; }
strlwr(s);
return s;
strRupr strRlwr :
if (s[i] == RusM[j]} ( s[i] = R u s B t j l ; break; }
.
: RUSB RUSM -
.
4.1.
char *strcat{char *dst, src dst.
const char *src)
strlen(src + dst)
char*strncat (char*dst, const char*src, -
size_t n) src dst.
strlen {dst! + .
char *strchr (const char *s, int ) s
4.1 ()
Person (. . 3).
strcpy:
strcpy(mans[5].fio, "");
Borland . -
Visual C++ 6 .,
, . ,
cout Rus(strRlwr("!"), ss) endl;
,
char ss[100] = "!";
cout Rus(strRlur(ss), ss) endl;
. , - -
-, , -
(char *). ,
:
char *sl = " ";
char s2[] = " ";
, ,
:
si , 4 ; 2
, 16 (15 -
);
6 1264
162 I, C++
si
; 2 ,
, 2[1] = ' ( ;
si = s2; s2 = si.
, -, -
. " ".
Borland .
, C++ -
,
cout ("abcdef" + 2 ) [ 0 ] endl;
cout 2["abcdef"] endl;
cout "abcdef" + 2 endl;
"". , -
-, .
. 3
.
: .
char *Days[] = {"", "", "", "",
"", "", ""
};
, (-
).
, -
.
:
char s[3][10] = {"", "", ""};
char * s [ l G ] = {"", "", ""};
. 4.2 4.3.
, , -
, , -
.
, 4.11, -
, -
.
. -
, .
4. 163
char s[3][10]
s[0]
9(2]
10
. 4.2.
char 413]
. 4.3.
4.11.
- ,
, :
.
.
, 4.12.
I 4.12.
,
( ) .
164 - I. C++
, ,
C++ , -
. :
,
, ,
4 . : -
.
.
. , " "
, -
, . -
new, -
delete.
int *p;
= new int;
* = 10;
delete ;
= 0;
, , , -
, .
:
int * = new i n t ( 1 0 2 4 ) ;
, . .
. , Date
(. 3.14) :
Date *p = new Date;
(. . 8).
"" .
:
(*)-year - 2004;
4. _ 165
, . . "" -
.
->:
-> month = 12;
, ,
"" .
new , , -
, . ,
.
delete ( !).
, ,
.
.
new[], , , -
deietef].
.
(~ j
, -
. , Borland "" ,
char *s = new char [sin (x) ].
, .
, -
4.13. string. h, -
, .
,
. , -
.
| 4.13.
, ,
.
166 I. C++
, . . ,
.
, -
new. , ,
, . -
-
4.14.
4.14.
int f f f i n t m) ( return m; } //
int* f (int n, int *a)
( // -
return a;
,
, .
, ,
, , .
, -
, , C++
, . -
, ,
. -
, .
, R. , -
r [ i ] [ j ] :
, R
:
4. ^}
i < n; i++)
j < n; j + + )
for (int k - 0; k < n; k-t-+). s += a[i] [k] * fa[k] [jj;
: , 5, V, 17
. . , -
, . . -
, -
.
n x :
float *r = new float [n] [n] ;
, . .
. -
:
, -
. , "" -
. C++ :
int n; //
cout : ;
cin ;
float **m = new float * [n] ; //
for (int i = 0; i < n; ++i) m[i] = new float [nj; //
.
, -
. -
, :
,
for (i = 0 ; i < n; i++) delete a[i];
delete []a;
, .
4.15.
; 4.1 5.
int main(void) //
{ int i, j , n;
cout " , , R ";
cin n;
//
float **a = new float*[n]; //
168 I. C++
, -
, i (). -
.
-
. .
. -
, . . .
, -
, :
void PrintMatrix(int m[] [], int n)
void PrintMatrix(int *m[], int n)
4. -/69
void PrintMatrix(int **m, int n)
:
for (int i = 0; i < n - 1; i++)
{ for (int j = i + 1; j < n; j+-t-) cout m [ i ] [ j ]; cout endl; }
.
. -
, STL
.
. ,
4.16,
.
.
.
4.16.
, -
.
= g(fiy) ;
, .
,
. , -
, -
I. C++
. ,
. , -
, .
.
, . , -
. -
, . Node -
:
struct Node { Car car__; Node *next; };
, , Node
Mode *next .
, , "-
".
Node,
Node *. -
. , -
, Mode, -
:
Node *Head, *Tail; //
Node *Cars; //
,
.
: ,
, . . , -
, . .
Head Tail. -
. 4.4.
Head
. 4.4.
:
Cars = new Node; //
Cars -> next = 0; // 2
Head = Tail = Cars; //
2 -> next.
4. 171
:
Cars - new Node; //
*
Cars -> next = 0; //
Tail -> next = Cars; // 3,
Tail = Cars; // 4,
4 , -
"" .
:
Cars = Head; //
Head = Head -> next; //
delete Cars; //
, .
.
.
.
. -
. ,
:
Cars = new Node; //
Cars -> next = Head; //
Head = Cars; //
, -
, .
, , -
. ,
. (node)
, :
.
, . , -
. .
. (. 4.5) ,
.
,
, .
. 4.6. list -
STL.
. ,
, . 4.7.
172 I. C++
. -
, -
. () set () -
STL
.
Head
ik
. 4.5.
. 4.6.
Root
. 4.7.
, -
, ,
(. 4.8).
.
.
:
4. 173
template <class T>
struct Node
{ info; //
first *Node;
second *Node;
};
(
), , .
.
Left () Right ().
. , ,
"" ( ). -
" ", -- " ". "" ,
( ).
. 4.8.
\{
:
void *info;
. [5, 9, 14, 16, 34, 39, 40, 41, 45, 47]
,
, , -
.
, -
. , -
" " , -
, . :
, ;
"", .
174 I. C++
"".
, -
f "",
: * = 1. -
* = ,
. ,
, * !
, " " , , -
. ,
:
, , "".
" " .
,
4.18.
xi, -
. , -
, . ,
. ,
, .
, - -
,
:
void X l ( i n t *p) // -
{ = new int[1000]; } //
void_mainfvoid)
{ int *pp = 0; X I ( p p ) ; } //
, -
4.18.
XI. , .
-, -
int **,
int *&p.
,
.
2 (. 3.6).
, -
,
. ,
.
""
.
C++ .
"" ( -
, ) - . -
, - ,
, . , -
, ,
Summa (. 4.1). , , ForEach.
, .
"" 4.19.
-
176 I. C++
4.19. ForEach
!. ; : -.. - - -I.'. :;...-:.1.*.-, : '
, ,
. .
: , -
.
:
int f2(int a) //
{ return (* = 2); }
double f3(double a} // +2
{ return ( += 2 } ; }
int main(void)
{ int a[10] = { 1, 2, 3, 4, 5, 16, 7, 8, 9, 10 );
ForEach(a, a + 10, f 2 ) ;
for (int i - 0; i < 10; ++i) cout a [ i ] ' ';
cout endl;
double b[10] = f 10, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
ForEachfb, b + 10, f3) ;
for (int i = 0; i < 10; ++i) cout b [ i ] ' ';
cout endl;
.
:
2 4 6 8 10 32 14 16 18 20
12 14 15 16 17 18 19 20 21 22
, -
.
STL .
- , , -
-.
. -
. : .
, -, -
. , , -
, .
4. 177
, , , -
. -
, , -
.
:
;
;
,
, .
-
. , , -
, (,
Excel). C++ -
,
. union - .
union:
union DD { int a; double b; };
, union ,
-. . 3 , -
(. 3.26). ,
4.20, struct
union.
! 4.20.
ftinclu.de <iostream.h>
int main()
{ union { char ch; double b; };
union BBB { double b; char ch; };
union CCC { int b; char ch; };
union ODD { short b; char ch;
void f(void) { cout "Hello!" endl; }
} /"
cout "AAA" sizeof(AAA) endl;
cout "BBB" sizeof(BBB) endl;
cout "CCC" sizeof(CCC) endl;
cout "ODD" sizeof(ODD) endl;
return 0;
178 I. C++
:
. 8
8
4
DDD 2
, , ,
. , ,
, -
.
DD
. 4.9.
union DD
int a;
double b;
. 4.9. DD
( 4.21)
.
! 4.21.
struct CELL
{ char attrib; //
union //
{ char text[MAX + 1]; //
double value; //
struct //
f double fvalue; //
char formula[MAX + 1]; // -
} f;
} v;
, - . -, -
, -
, , ,
,
. ,
:
4. 779
CELL A;
Strcpy(A.v.text, " ");
, - -
, -
,
,
"" . ,
:
Strcpy(A.v.f.formula, "sin(al) * 0.12");
A.v.f.fvalue = Calculated;
main
Windows -
.
. , -
. C++ -
. -
, C++.
, C++,
main. -
, .
main
, argc
argv :
int main(int argc, char * a r g v [ ] }
-
:
int main(int argc, char **argv)
, ,
argc (argument count ) argv
(argument vector ) C++
. argc -
. 1, . . -
(, ) .
argv ,
.
> echo
.
argv , . 4.10.
180 I. C++
argv *argv[]
h 0
|
0
. 4.10.
>
C++ , argv[argc] .
, -
. Windows (
MSDOS), ( -
). , -
, "". -
,
, atof .
( 4.22),
.
.
1 4.22. main
itjtf,,:....(Ub ........... .'. ........ > ........ ---- .......... - ...................... ( .............. -'- ............ ii.i ....................... <'; ............ .- ............. -. ............ -
^include <iostream.h>
int main(int argc, char *argv[])
{ if (argc ! = 2) //
{ cout " !" endl
return 1;
}
cout "!" argv[l] endl;
// if (argv [argc] 0) cout " !" endl;
return 0;
VW.EXE,
,, ,
> VW " , ?"
:
! , ?
'
4. 181
> VW
:
!
argc argv
(99), C++,
. -
,
. Borland C++ Builder 6 ,
:
int main()
int mainfint argc)
int mainfint argc, char * argv[])
int mainfint argc, char * argv[], char * env[])
Visual C++ 6. -
. , ,
path. , -
env, environment.
main
int. , ,
, return. -
. -
( ).
.
main -
, :
void main(void) { / / . . . }
, , -
. , [45], "",
,
int.
5
199S C++ .
" " -
. -
"" ,
C++ . "" -
,
C++, -
. -
. 1994 () -
( Meng Lee)
, STL
(Standart Template Library ). -
C++.
, -
- , , -
, .
, -
, , -
, . -
, . -
""
. ""
, (,
Visual C++ 6). , , , -
, "" STL, -
( ) .
184 I.
,
bool, true false C++
. -
, . .
(&&), (| |) (<).
,
, , , ,
,
. sizeof(bool) 1. ,
(. . " -
" . 1) 1 -
. :
false, true. , -
-
.
booiaipha, :
cout booiaipha (a > 1) endl;
true false -
.
(" j
Borland C++ 3.1 Borland C++ 5 " " ,
.
, -
C++
string. " string "
[37, . 645], -
: -
; , .
, , ,
, . -
string,
, .
[]. ,
.
string :
;
J. 185
. , . . string ,
, .
-
, 5.1.
5.1.
^include <iostream>
^include <string>
using namespace std;
int main(void)
{ string sO; // 00
string si = "11111111111111111111" // 01
string s3 ("98765432109876543210"); // 02
char cs4[] = "01234567890123456789"; // 03
string s2 (si);
cout "s2 =" s2 endl; // 04
string s4(cs4);
cout "s4 =" s4 endl; // 05
string s5(cs4, 15);
cout "s5 =" s5 endl; // 06
string s6(cs4, 15, 5);
cout "36 =" s6 endl; // 07
string s61(cs4, C54 + 4);
cout "s61 -" s61 endl; // 08
//string s7(s3, 15);
cout s7 endl; // 09
string s8 (s3, 15, 5};
cout "58 =" s8 endl; // 10
string s7(s3.begin(), s3.end());
cout "s7 =" s7 endl; // 11
string s71 (s3.begin(), s3.begin() + 6);
cout "s71 =" s71 endl; // 12
string s72(s3.begin(), 15);
cout "s72 =" s72 endl; // 13
string s73is3.end() 7, s3.end());
cout "s73 =" s73 endl;
string s74(s3.begin() + 6, s3.end() 7);
cout "s74=" s74 endl;
string s9(20, '-');
cout "s9 =" s9 endl; // 14
si = s3;
cout "si =" si endl; // 15
s2 += si;
186 _ ^ C++
,
"" . so, si, s2, "-
" . 34, s5, , ! -
, -
. s4 ,
15 , 5 , 15-. -
-
. , -
-.
09 Visual C++ 6,
Borland C++ 5 Borland C++ Builder 6
. [37, . 652] .
Visual C++ 6 (-
, STLport 4.5.3} .
s8 ( )
( 07): 5 , 15-. , 9
.
5. ^ 187
,
sv, s7i, s72, s73, s?4
. -
, .
( -
""),
. : begin(} end{).
, 7
s3, s7i 6
, si2 -
15- s3.
09, .
73 7 , s74
7 , 7-, . . ?4 "3210987".
, 23, 24, 25 ,
-
(. . "- "
. 4). ,
. -
, -
C++, ( -
).
15-22
. ,
. -
string assign, -
, . -
,
:
A.assign(B);
A.assign (, 15, 5);
A.assign(.begin(), .end());
A.assign(C);
A.assign(C + 10);
A.assign(100, '+');
assign, .
, 26 , , 27
. .
28 ,
, "" .
.
188 \._ ^ C++
"" , .
-, ^include
h. C++. -
" " , -
. string.h,
.
, " " - C++. -
, C++
"". , C++ string.h
cstring. ctype.h cctype, a
math.h cmath, . .
-
. "" string -
h. " "
"", .
-, :
using namespace std;
Visual C++ 6 ,
.
. ,
- . -
" std". -
-
. , -
,
.
,
-
std: :
std::cout
std: -.string
.
, - , using
, :
using std::cout;
using std::string;
.
5. 189
, , -
:
string , -
. string .
string 5.2.
5.2. string
,
. , -
, const -
. -
: string
string, .
- .
5.3.
[ 5.3. -
1.1.,'. ,..; j _.., :... , '. _;.;;..';
-
(. . " " , 4). -
,
(. . " " . 2).
- -
string, (. -
4.9) string. " -
" charTooem string. -
, - ""
. , -
, string c_str(),
. -
5.4.
\ 5.4. string
i: ;. .-....".... .?: : :..:::.:: i J ;.,: i
char * Rus(const string sin, char out[])
{ if (CharToOem(in.c_str(), out)) return out; else return 0; }
,
:
string ' ! ! Hello!");
cout Rusfss, s) endl;
. ,
. , -
. -
Rus.h -
ttinciude .
include. Rus.h
5.5.
: 5.5. Rus.h
^include <windows.h>
ttinclude <string>
using namespace std;
char as[100]; //
char * Rus(const char in[], char out[]J
{ if (CharToOemi.in, out)) return out; else return 0; 1
char * Rus(const string Sin, char out )
{ if (CharToOsmfin.c str(), out)) return out; else return 0; }
5. 191
windows.h
string. ,
. , -
,
:
^include <Rus.h>
,
, . -
.
, :
. -
, . . string -
. -
1 999 999 999 -
int.
(_
, Visual C++ 6 -
int64.
long.
: 5.6. toNumeral
//
string toNumeral(unsigned long Number, bool Thousands)
{ string units[] =
{ "", "", "", "", "",
"", "", " ", ""
192 I. C++
string tens[] =
{ "", "", "", "", "",
"", "", "", ""
1;
string hundreds[] =
{ "", "", "", "", "",
"", "", "", ""
,.
string secondten[] =
{ "", "", "", "", ",
"", "", "", ""
I:
string Women[2] = "", "" };
string result(""); -
typedef unsigned char byte
byte digits[3] = { 0 }; //
unsigned long n = Number;
digits[0] = n % 10; //
digits[1] = (n / 10} % 10; //
digits [2] = n / 100; //
if (digits[2] > 0) result += (hundreds[digits[2] - 1] + " ");
if (digits[1] > 0}
{ if ((digits [1] === 1) && (digits [0] != 0))
{ result += (secondten[digits [0] 1] -f " "); return result,
else result += (tens[digits[1] - 1] + " ");
if (digits[0] > 0)
{ if (((digits[0] > 2} && (digits[l] != 1) f [Thousands) }
result += (units [digits [0] - 1] " ") ;
else result += (Women[digits[0] - 1] + "
}
return result;
: -
9 .
: digits[2], -
digits[1] digits[0]. ,
-. --
. , -
.
(): 1, .
, secondten
. 1, - -
5. ^^ 793
tens.
1 2 women -
.
, -
. (
5.7).
5.7. toNumerai
int main ( }
{ unsigned long Number [] =
{ 1, 2, 9, 10, 11, 19, 20, 21, 22, 45, 100,
101, 102, 109, 110, 118, 150, 870, 912, 999
Number
.
. -
RUS (. 4.9, 5.4). -
. ,
. toNumerai string,
( ) c_str(). -
,
.
( 5.8),
. ,
toNumerai, "" -
.
I 5.8.
7 . 1264
194 I. C++
, . ,
, :
1, .
.
, -
5. 195
switch,
.
: 5.7 -
, , ,
charToOem:
CharToOem(Numeral(Number[i]) ,c str ( ) , sj;
,
, -
. . ,
,
"". , , ""
. , (. . 3)
. -
60 .
,
,
.
, -
-
, . string -
, .
. -
, , -
, .
: .
,
. , -
. , -
(vector) (list), (queue)
(priority^queue), (deque) (stack).
.
(. 5.1). ,
,
. ,
.
(set) -
(multiset ),
() (multimap ).
196 I.
Borland C++ Builder 6 STL (STLport 4.5.3) -
, slist hashjnap.
5,1.
push front + +
pop front + + +
push back + + . + + +
pop back + + + +
insert + + +
erase
[ ], at
. -
, -
, . -
, -
.
. w,
,
.
, :
cout [""] endl;
"" , w . C++ -
,
.
(. 1.20
1.21). . 2 (. 2.13)
, -
, .
, . .
. "" , -
.
5.
, , -
.
-
. ( )
. -
, :
^include <_>
using namespace std;
vector. , -
( -
5.9).
| 5.9.
vector <double> u; // 1
vector <int> v(10); // 2
vector <int> x(15, -1); // 3
vector <int> t(v.begin(), v.end()); // 4
int a[10] - { 1, 2, 3, 4, 5 };
vector <int> w(a, a + 10); // 5
int n;
cout "Input n: ";
cin n;
vector <int> w(n, 1); // 6
tt = w; // 1 tt = w
for( i - 0; i < tt.sized; ++i) cout tt[i] ' '; // 8
cout endl;
t.assign{20, -1);
, vector ,
(. . " ". 3),
.
: , .
1 "" ,
double.
push_back. , "" -
empty , true,
.
2 10- int,
.
15- ,
-1.
198 I. C++
4 , -
,
.
, string.
5 -
. ,
.
,
, , -
. , -
.
, -
. ,
. ,
-
. -
.
1. .
2. -, , .
3. .
4. .
assign .
.,
,
. ( 7) tt -
w. ,
.
, ,
.
, "
". .
: at, -
. , v[ij
v . a t ( i ) . s i z e ( ) ,
.
5.9
assign,
. ,
.
5. 799
. :
,
.
. 5.10.
j 5.10. , 1
^include <vector>
using namespace std;
double Average(const vector <double> &v)
{ double s = 0.0;
for (int i = 0, count = 0; i < v.sizeO; ++i)
s += v[i], ++count; /7
return s I count;
Average . ,
,
, -
.
, : -,
, ;
-, , . . -
. -
. -
, ,
.
:
^include <iostream>
tfinclude <Rua.h> , //
int main )
f vector <double> u; double el;
while (cout Rus(" : ", ss), cin el)
u.push_back(el); // ""
cout Average(u) endl;
return 0;
i
.
- -
. , -
"", -
:
200 _ I, C++
,
, -
accumulate. :
double Average (const vector <double> &v)
( if (Iv.emptyO) return
(accumulate (v.beginO , v.end(), 0.0) / v.siaeO);
return 0;
,
, . -
:
((include <numeric>
: vector, list deque,
( [25]) ,
, vector list
deque, ^include.
5.11.
:'- ; ;
I 5.11. deque
^include <iostream>
tinclude <deque>
^include <numeric>
using namespace std;
tinclude <Rus.h> //
double Average(const deque <double> sv)
{ if (!v.empty())
return (accumulate (v.begin (), v,end(), 0.0) / v.sizeO);
else return 0;
]
int main()
{ deque <double> u; double el;
char out[100]; //
while (cout Rus(" : ", out), cin el)
u.push_back(el); // ""
cout Average!u) endl;
return 0,-
5. ^ 20?
, -
. , ,
, -
, -
. , ,
,
. , string.
, typedef, -
:
typedef vector <string> Question;
Question GetTest(const Question &Test, int n)
:
;
;
i -= 0;
(i < +1)
;
,
[];
++i;
m -
. ,
. , set , -
.
. -
. ,
, -
-. -
. 5.12.
\ 5.12.
int i = 0;
while (i < n)
{ int k = randQ % Test.size(); //
set <int> :: iterator ik = s.find(k); //
if (ik == s.end()) //
{ s. insert (k) ; //
ConcreteTest.pu5h_back(Test [k] ) ; //
++i; .
i
i
return ConcreteTest ; // -
, :
^include <set>
-
find, , , -.
, end . -
, .
, -
, :
Question w;
w.push_back( "") ;
\.__]("") ;
w . push_back ( "" ) ;
w.push_back ("") ,-
w.push_back("Maji") ;
w.push_back("raoHb") ;
w . push_back ( " " ) ;
w.push__back("aBrycT") ;
w.push_back("ceHTH6pb"} ;
w.push_back( "") ;
w,push_back{ "") ;
w.pu5h__back(" ") ;
:
Question tt = GetTest(w, 5) ;
for (int mm = 0; mm < t t . s i z e O ; +4) cout Rus {tt [ton] , as) endl;
12 , :
5. 203
, , , . .
. ,
,
.
. , ,
, :
vector <void *> v;
,
-, ;
vector <int> *m = new vector <int>;
, ,
, , :
typedef vector <double> row;
row *m = new row(10, 2.1);
10
2.1. :
cout (*m)[i] endl;
:
cout m -> a t ( i ) endl;
.
, , ->:
row *mm = new row{10, 2); //
mm -> push_back(l); // 1
cout mm -> size () endl; // 1
cout mm -> capacity(} endl; // 2
vector <double> :: iterator k;
for (k = mm -> begin(); k < mm -> end(); ++k) cout setw{4) *k;
cout endl;
i (
11), 2
204 _ /. ++_
(. . 3) , -
-
, ,
.
STL . -
( ) -
, .
, , ,
(
, - . -
4.1), .
-,
( ) ,
, .
( ^
-
.
. "" "-
".
, -
. ,
, -
, . . .
, , -
- list
double,
. (
5.13), -
,
| 5.13.
^include <iostream>
Itinclude <algorithm>
^include <list>
^include <cstdlib>
^include <ctirae>
5. 205
:
77 59 24 23 3 6 97 45 10 96
96 10 45 97 3 23 24 59 77
L. -
, , -
. mu, -
. ,
5.12, srand,
time ( ctime).
1 1970 ,
IBM PC. rand.
srand rand CStdlib.
4 L,
: ( 5) ( ).
"" -
, .
.
begin ( } . -
rbegin().
rend{).
-
. : -
, -
(. 5.1).
206 I. C++
()
begin() > > end()
i
i i
i i
.
[first, last)
, last -
-
last - 1. [first, last) first
last, -
. : -
.
, ,
:
(
cout *il '; cout *ril ' ';
. 4 ,
. ,
, , : ,
, . -
-. , -
.
. -,
.
, const (,
). -, STL 5
: (input), (output), (forward), -
(bidirectional) (random access). -
, -
.
,
. -
(i j ):
5. 207
1 =
i = j
i != j
, , -
. 5.2.
5.2.
= *i
*i -
= *i, *i =
= *i, *i = x, i, i
x = *i, *i = x, i, i , , list
i + n, i n, i += n, i -= n,
i < j, I > j, i <= j, i > j
, ,
: distance, ,
advance, i += n.
distance :
n difference_type, -
last - first. , ,
unsigned int.
(reverse) . ,
, , .
:
back_insert_iterator back_inserter
( );
front_insert_iterator front_
inserter ( );
insert_iterator inserter (
).
208 ____^ I. C++
:
template <class Container>
back__insert_iterator<Container> back_inserter(Containers x)
.
:
template <class Container, class Iterator>
insert_iterator<Container> inserter(Containers x, Iterator i);
, , .
. ,
, .
,
. -
:
vector<int> v(a, +10);
list<int> L;
copy (v. begin () , v.endO, inserter{L, L.begin()));
L , -
:
(V.begin(), V . e n d ( ) , front_inserter(L));
, -
/, . -
Borland C++ Builder 6 -
:
ostream_iterator<int, char> out(cout, " "};
copy(vl.begin(),vl.end{},out);
out,
.
-
, .
cout , .
, -
:
fill n(ostream iterator<int,char>(cout," "), 3, 5);
5. 209
.
fill_n(ostream_iterator<char,char>(cout, ' - ' ) , 30, ' - ' ) ;
"" 60 .
Borland C++ Builder 6 ( -
5.14), . -
, -
.
| 5.14.
^include <iterator> //
^include <numeric>
ttinclude <deque>
^include <iostream>
using namespace std;
int main()
( int arr[4] = { 3, 4, 1, 3 };
int total = 0;
deque <int> d(arr + 0, arr + 4);
//
copy (d.beginO, d.end(} 1,
ostream__iterator <int, char> (cout, " + "));
cout *(d.end() - 1) " = "
accumulate (d.beginO , d.end(}, total) endl;
return 0;
}
(
) ( -
).
:
;
end();
, ,
.
:
#include <iterator>
:
G ,
;
210 I. C++
, , -
, ;
;
, , ,
"" .
,
. , - sort,
, sort. -
: -, -
, -
, ; -,
.
, -
(. 5.3).
5.3.
find
find_first_of -
find end
find if , -
search
search n n-
count -
count if -
,
equal
lower__bound -
upper_bound -
5. 211
5.3 ()
binary_search -
equal range -
,
sort
partial_sort
merge
.
. -
, 5.15.
I 5.15.
^include <iostream>
#include <algorithm>
#include <list>
^include <cstdlib>
^include <ctime>
using namespace std;
^include <Rus.h>
int main()
( double elem; int i; const int nn = 20; double mu[nn];
//
time_t t; srand ((unsigned) time(St));
for (i - 0; i < nn; ++i) mu[i] = (rand() % 1000);
list <double> u(mu, mu + nn); //
list <ciouble> : : iterator iu; //
//
for (iu = u.beginO; iu != u.end{); ++iu) cout *iu ' ';
cout endl;
elem = *min_element(u.begin(), u.end(j); // 1
cout elem endl;
cout *max_element(u.begin(), u.end()) endl; // 2
cout *min__elernent (mu, mu + nn) endl; // 3
return 0;
i
212 /. C++
mu, , -
. 1 2 -
. -
, -
, 3.
, . . .
, .
-
, :
. 5.15 :
*min__element (u.begin ( ) , u . e n d ( ) } = 400;
*max_element (mu, mu + rm) = 200;
400,
200.
, . ,
C++ -- -
. , . 7.
,
. , -
,
. ,
, ,
( 5.16). ,
.
5.16.
bool i s P r i m e f i n t number) //
{ number = abs(number); //
// 0 1
if (number == 0 | | number == I) return false;
int divisor; // ,
for (divisor = number / 2; number%divisor != 0; divisor);
return divisor == 1; // 1,
}
int main()
{ list<int> L; //
list<int> :: iterator pL; //
// 20 40
for (int i = 20; i <= 4 0 ; +4-i) L.push b a c k ( i ) ;
5. _ 273
, ,
20 40. -
:
cout setw(3) *pL;
,
L 3 .
, :
#include <iomanip>
,
.
31 ""
, 31. :
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
:
20 21 22 23 24 25 26 27 28 29 30 40 39 38 37 36 35 34 33 32 31
, 5.13,
. -
reverse,
.
214 I. C++
- isPrime, .
= find_if(L.begin() , L . e n d { ) , isPrime);
. -
23. , -
L.endo . -
find_if .
find_if -
, , -
, ,
() . , . . -
, :
* f i n d _ i f ( L . b e g i n ( ) , L . e n d ( ) , isPrime} = 0;
.
count_if.
cout count_if (L.begin ( ) , L . e n d O , isPrime) endl;
,
.
STL.
, . -
, -
.
, sort -
, .
.
. ,
( -
5.17).
1 5.17,
sort(rnu, mu + 10); //
sort(nu, nu + 10); //
double ru[20];
merge(mu, mu + 10, nu, nu + 10, ru); // 4
for (i = 0; i < 20; -h+i) cout setw(3) ru[i]; cout endl;
, , .
( 2 )
( 4). -
:
56 5 83 12 33 39 63 37 91 83 //
27 0 0 83 97 0 16 0 37 67 // nu
5 12 16 27 33 37 37 39 56 67 68 83 83 83 91 97 // ru
sort , , -
(), -
. -
. ,
, , " ",
, .
! 5,17 ru. -
mu nu.
.
, unique:
unique(ru, ru-+ 20);
for (i =0; i < 20; ++i) cout setw(3) ru[i]; cout endl;
,
:
6 23 34 36 43 58 59 73 73 78 80 83 89 90 91
23 34 36 43 56 59 73 78 80 83 89 90 91 80 83 89 90 91
.
ru , -
unique. -
:
vector<double> V(ru, ru + 20);
unique(V.begin{), V.end());
for (i = 0; i < V.sized; ++i) cout setw(3) V[i]; cout endl;
216 I. C++
, ,
, .
remove -
: "" remove, remove__copy, -
remove_if remove_copy__if.
, unique, -
. -
erase, . -
v:
vector<double> V(ru, ru + 20};
remove(ru, ru + 20, 0) ;
for (i = 0; i < 20; ++i) cout setw(3) ru[i]; cout endl;
vector<double> :: iterator pos = remove_if{V.begin(), V.endl), isPrime);
for (i = 0; i < V.size{); --i) cout setw(3) V[i]; cout endl;
V.erase (pos, V.endO ) ;
for (i = 0; i < V.size!); ++i) cout setw(3) V[i]; cout endl;
ru :
1 14 15 26 33 37 38 47 61 62 65 68 69 85
remove :
7 14 15 26 33 37 38 47 61 62 65 68 69 85 61 62 65 68 69 85
, , ,
"" .
remove . v remove_if
:
14 15 26 33 38 62 65 68 69 85 65 68 69 85
. erase -
, :
14 15 26 33 38 62 65 68 69 85
5.4. ,
,
copy_backward ,
5. 217
5.4 ()
fill
f ill_n
generate
generate_n
remove ""
remove_if "" ,
replace
replace_if ,
transform -
unique ""
unique_copy ""
reverse
reverse_copy
,
, -
( ) . ,
. -
:
STL *, -
. -
"" . -
. 5.5
, .
5.5.
plus<T> +
minus<T> -
, .
218 I. C++
5.5 ()
multiplies<T> x *
divide s<T> x /
modulus <T> x %
negate<T> - X
equal__to<T> ==
not_equal_to<T> !=
greater<T> >
less<T> <
great er_equal<T> >=
less equal <T> <=
logical and &&
logical or X | I
logical not !
. -
:
. -
:
bool compare(int x, int ) { return x > ; }
v :
. sort (V.begin () , V . e n d f ) , compare) ;
, . .
greater<T>.
:
sort(V.begin f ) , V . e n d ( ) , greater<int>());
, -
:
^include <functional>
. -
accumulate -
(. 5.11). -
rnasa5L 219
, , , ,
:
vector<double> v; // -
double = accumulate (v.begin () , v.endO, 1.0, multiplies<double> {) ) ;
,
. , -
, STL ,
generate. ,
:
double f ( ) { return ( r a n d f ) % 100); }
:
double mu[10], nu[10]; time_j: t; srand((unsigned) time(St));
generate(mu, mu + 10, f) ;
transform,
, -
. ,
: ,
, ,
. -
:
trans-form(mu, mu + 10, mu, negate<double>());
fill(, + 10, 1); //
transform(mu, mu + 10, nu, nu, plus<double>());
mu (
transform), ,
( transform), -
. transform . -
, nu 2:
double f2(double a) { return *2; }
transform(nu, nu + 10, nu, 2);
, -
(- !) .
, -
:
double ff(double a, double b) { return a*2-b;}
fill(nu, nu + 10, 1); //
transformfmu, mu + 10, nu, nu, ff);
220 /. C++
mu :
30 91 94 92 66 54 56 56 31 95
:
59 181 187 163 131 107 111 111 61 189
: -
, . -
- !
6
C++
,
. , ,
. -
.
, .
/,
. / ,
. , /
,
. ,
, -
.
/ -
- -. ,
, ,
, UNIX. -
, / -
. stdio ( -
/) C++. -,
/-
. , -
. -
^ . ,
.
C++ -
/ iostream. ,
/ C++. stdio
222 I. C++
, iostream
.
,
, .
" " .
. ,
[29] / Visual C++ 6,
. .
[37, . 705]: " , , -
". -
[37] Borland C++ Builder 6
Visual C++ 6 STLport 4.5.3.
C++
/ C++ , / .
,
: , .
.
, , . -
string. -
. -
. <iostream> / -
:
cin ; ;
cout ; ;
clog ; ;
-=- ; .
cout "" ,
clog .
, cout:
" !" endl;
cleg " I" endl;
"" "-
" . - ,
cout , clog, - -
"" cout,
clog.
-
, . . [37,
6. C++ 223
. 671] ; ", -
, ...
". , , -
. -
cin -
(, int double ,
). -
cout.
(
, 3.12).
. ,
31 12.78.
:
int ;
double b;
cin b;
:
31<enter>
12.78<enter>
:
31<npo6en>12.?8<enter>
, <Enter>.
:
cn a;
cin b;
,
, <Enter> ,
, .
-
, , :
31rl2.78<enter>
31 , . -
, -
, . ,
. -
224 /^ C++
- , ,
. -
, , . [37, . 681],
( 6.1).
6.1.
,
( 6.2).
I 6.2.
copy : -
,
. eof ""
"end of file" . , -
, dd. ,
<Ctrl>+<Z>, <Enter>. -
, 5 ,
:
1<>2<>3<>4<>5<><^1>+<2><^>
, <Enter>.
,
- ,
. :
<><><>
:
int a;
char ch;
double b; -1
6. C++ 225
3112.78
:
cin ch ;
( . . ), -
ch.
<Enter>. -
:
char ch;
for (int i = 0; i < 10; ++i) cin ch;
<Enter> , ,
. 10- -
-, <Enter>
10 . ,
:
for (int i = 0; i < 10; +->-i) { cin >> ch; cout ch; }
: -
-, -
, ( <>)
, .
(
, )
, . , ,
, -
, , ,
( )
.
, .
/ . 6.1.
/ :
-
/;
_
3 .
.
,
.
. 3 , -
.
8 . 1204
I, C++
. 6.1. /
string. -
, -
.
.
/ -: read, get
getiine. read . -
, , , -
. - get. -
. 1 ,
.
char s[100] - { 0 } ;
, .
:
cin.get(s, 1001;
, , <Enter>,
' \ ' , -
6. C++ 227
. -
( ), '\'
. ignore. ,
s :
cin.get(s, 100);
cin.ignore{);
' \ ,
, * ; ' .
cin.get(s, 100, ' ; ' ) ;
cin.ignore();
,
<Enter>. , get . -
getiine get,
- ,
ignore . -
getiine :
cin.getiine(sr 100);
cin.getiine(s, 100, ';');
,
. ,
.
string getiine, -
:
string s;
getline(cin, s); //
, , . . -
cin, , , ,
. , ,
.
,
, -
. "" . -
( )
: ,
, .
228 I. C++
. ,
. -
.
! ,
.
, .
, -
,
. -
:
;
;
(/);
;
.
.
,
" " . -
- , .
,
.
""
.
,
, . , .
- . -
, -
.
Windows, ,
C++. , -
.
C++ -.
,
6. C++ 229
- . string .
:
>, <, :, ", |;
,
;
, .
file, -
, FILE, File.
, , -
. , -
"". ( ) .
. , -
. Windows
MAX PATH.
Borland C++ 3.1 ( )
. 8 . MSDOS
Windows 3.1. Windows (95, 98, 2000, .)
8 .
A Z ( a z) .
.
-. Windows
\ ( backslash). C++
\\. , -
/.
. "." () ".." ()
.
.
C++ ,
API
Windows. API Windows,
Microsoft ,
API.
Windows XP, Windows NT, -
API Windows NT. -
, GetcurrentDirectory (
6.3).
230 I. C++
( 6.3.
DWORD GetCurrentDirectory (
DWORD nBufferLength //
LPTSTR IpBuffer //
:
nBufferLength , -
;
IpBuffer , .
,
-, IpBuffer. -
. , .
, (
6.4). -
,
Rus.h (. 5.4).
; >'- ; ;
6.4.
^include <iostream>
^include <Rus.h>
int main ( )
( char pp[MAX_PATH] ; long cc - MAX_PATH;
DWORD t = GetCurrentDirectory (cc, pp) ;
cout Rus (pp, pp} endl;
return 0 ;
, windows. h
C++ (_)
(DWORD), Windows.
,
SetcurrentDirectory, -
6.5.
| 6.5. SetCurrentDirectory
BOOL SetcurrentDirectory(
LPCTSTR IpPathName //
6. C++ 231
ippathName
.
( 6.6),
,
, .
) 6.6.
tinclude <io5tream>
tinclude <Rus.h>
int mainO
{ char pp[MAX_PATH], tt [MAX_PATH] ;
long cc - MAX_PATH;
DWORD t = GetCurrentDirectory (cc, pp) ; //
cout Rusfpp, tt) endl; //
SetCurrentDirectory ("a: ") ; //
t = GetCurrentDirectory {cc, tt) ; //
cout Rusftt, tt) endl;
SetCurrentDirectory (pp) ; //
t = GetCurrentDirectory (cc, pp) ; //
cout Rus(pp, pp) endl;
" ",
.
,
tt. ,
SetCurrentDirectory .
, .
,
: ,
C++. .
, "" ,
, 10 :
" C++?"
" C++?"
" "
" "
232 I. C++
" "
" "
" "
" "
" C++?"
" "
question.txt, -
tests :.
. , -
. -
, , ( 6.7).
6.7.
^include <fstream> // 1
ffinclude <iostream>
^include <string>
using namespace std;
int main()
( ofstream tof'c:\\tests\\question.txt"); //
if (!to.is_open()) // 2
{ cout "Error! Not openfile. "endl; return 1; }
string s;
while(getline(cin, s) ) // <Ctrl>+<Z>
{ to 5 endl; } / / 1
to.closed ; // 3
return 0;
, 1
fstream.
, . :
ofstream to(":\\tests\\question.txt");
to, -
. ,
. 2
is_open , .
, .
, . . .
, .
, . -
6. C++ 233
. ,
:
:/tests/question.txt
( ),
. .
(, , ),
, "". ,
:
ofstream to; .
to.open(":\\tests\\question.txt");
, -
-
. ,
, , -
. -
.
: -
(, ), (-
, ). -
. , ,
.
. -
, get line". ,
, <Ctrl>+<Z>.
, -
cout:
to s endl;
endl ,
.
. , -
, 6.8.
6.8.
^include <fstream>
#include <iostream>
^include <string>
using namespace std;
int m a i n ( )
234 _ ___ _ I. C++
1 from,
, (. 6.7)
. , . .
.
: ,
, :
SetCurrentDirectory (": /tests/"} ;
ifstream from ("question. txt") ;
2 . -
-
(. 6.7), ,
, . -
, <Ctrt>+<Z>.
, - .
-
. / C++ ,
[37, . 683]:
bool good () const; //
bool eof () const; //
bool fail () const; //
bool bad{) const; //
ios_base :
typedef int iostate;
enum io_state ( goodbit = 0x00,
badbit = 0x01,
eofbit = 0x02,
failbit = 0x04
J;
, ,
, . . -
10.
6. C++ 235
good, : -
-
/.
. eof
.
<Ctrl>+<Z>. question.txt -
. getiine, ,
. , -
:
while { ! f r o m . e o f ( ) ) //
{ getiine (from, s) ; cout s encil; }
fail bad .
, .
bad fail: -
bad, fail; .
bad
fail, , .
bad, .
, , (). -
fail
clear ( ) :
if (stream, f a i l ( ) ) stream, clear ( ) ;
.
ios_base
iostate _M_iostate;
-
. :
iostate rdstate();
:
void setstate(iostate flag);
. -
(. . "" ) -
5 , .
:
236 /. C++
"1", "2", "5", "8 , "10"
"1", "2", "5", "3 , "7"
"al", "_bbb", "_5_", "8_d", "abcdefghlO"
"1.0", "lei", "1000000L", "10e-5f","-le-1"
"for", "const", "if", "switch", "cout"
"for", "main", "cout", "function", "procedure"
"cin", "cstream", "cout", "clog", "cerr"
"A", "L", "u", "F", "f"
"1", "3", "2", "4", "5"
"return", "goto", "continue", "break", "case"
: 4, 4, 4, 3, 5, 2, 2, 1, 2, 4.
.
,
.
. , , ,
.
. -
, . -
, $ @,
C++. -
getiine ( , string) -
. -
. , -
, , :
C++?
1@2@5@8@10@4
string. ,
:
, -
1 5. , . . -
. ,
( 6.9).
6.9.
^include <fstream>
ttinclude <iostream>
using namespace std;
^include <Rus.h>
hool isRight(char digit)
g. C++ 237
if ( !isRight(s[s.size(} -1]))
{ cout Rus (" !", ss) ;
goto variants;
};
to s endl;
1
to. close () ;
return 0;
,
question.txt.
( ) -
. , -
variants, .
tests question.txt -
:
C++?
C++?
1@2@5@3@7@4
al@_bbb@_5_@ 8_d@ abcde f ghl 0 @ 4
l.O@leieiOOOOQOL@10e-5f@-le-l@3
for@const@if @switch@cout@5
235 _ I. C++
for@main@cout@function@procedure@2
cin@c5tream@cout@clogScerr@2
C++?
1@3@2@4@5@2
return@goto@continue@break@case@4
,
, -
.
: I .
,
: 5, 2;
5 , 3; 7
8 , 5 ,
. ,
:
; ;
=
< 5 ""
= 5 = 6 ""
= 7 = 8 ""
= 9 = 10 ""
,
. -
,
:
struct Questions
{ string question; //
string answer [5]; //
char right; //
STL, :
vector <Questions> Q;
6. C++ 239
, . . .
,
. answer
.
( 6.10), -
. ,
,
.
6.10.
void ReadQuestion(vector <Questicns> &Q)
{ Questions trnp;
char s [ 5 ] [ 3 0 ] ; //
ifstream f r o m ( " c : / t e s t s / q u e s t i o n . t x t " ] ;
while ( ! f r o m . e o f { ) )
{ getline(from, tmp.question); //
for (int i - 0; i < 5; ++i)
{ from.getline(s[i], 30, ' @ ' ) ; //
tmp.answer[i].assignfs[i]); //
}
from.getline(tmp.right, 2, r\n'); // A
Q.push_back(tmp); //
)
I
,
/, "" .
:
. . from ,
;
G
' \ ' (" "), getline.
right
Questions char right [2], . . ;
-
, . . -
.
)
-*
, / Visual C++ 6.
, . .
/ , -
"".
240 . I. C++
( -
6.11).
! .11, !
;.;, ,
int main ()
{ char ss[100}; //
vector <Questions> v;
ReadQuestion(v); //
unsigned int i = 0, summa = 0;
int j; char a[2];
while (i < v.size ()}
{ cout i + 1 ". " v[i].question endl;
for (j = 0; j < 5; ++j)
cout ' ' j+1 "}" v[i].answer[j] endl;
cout Rus (" :__", ss) ; cin. get line (a, 2, ' \n ') ;
if {a[0] == v[ij.right[0]) ++summa;
,
. ,
.
,
cout, -
.
.
,
. . -
, ,
, .
,
.
6. C++ 241
1. ?
2. ?
3. ?
.
.
entii, 5.23
setw.
, ,
boolalpha. .
/
ios_base :
typedef int fratflags;
enum{
left = 0x0001, //
right - 0x0002, //
internal = 0x0004, // ,
dec = 0x0008, //
hex = 0x0010, //
oct = 0x0020, //
fixed = 0x0040, // dddd.dd
scientific = 0x0080, //
boolalpha = 0x0100, // true false
showbase = 0x0200, // oct hex
showpoint = 0x0400, //
showpos = 0x0800, // +
skipws - OxlOOO, // -
unitbuf = 0x2000, //
uppercase = 0x4000 // X
, :
adjustfield - left I right | internal //
basefield = dec [ hex | oct //
f l o a t f i e l d = scientific I fixed //
/.
ios_base :
fmtflags M fratflags; //
-
.
-:
242 10 C++
, :
streamsize precision{) const;
streamsize precision(streamsize newprecision);
:
streamsize width(} const;
streamsize width(streamsize newwidth);
, iomanip :
resetiosflags (ios_base: : fmtf lags mask) ;
setiosflags(ics_base::fmtflags flag);
setprecision(int n ) ;
setwtint n ) ;
basic_ios :
char_type f i l l { ) const;
char_type fill(char_type f i l l ) ;
- ( ).
. , -
. , -
-
, , 10 15. (. -
5.12). ,
.
, test.ini. -
: ,
.
() .
,
/ -
.
, , . , , -
! C++ _ 243
, "". ()
, -
,
. -
,
.
, .
-
. -
. -
. -
,
, . -
.
""
-. , ( -
)
:
1. -.
2. .
3. . .
4. -.
5. . -
. -
.
6. . .
7. . .
8. . .
,
:
-
, , -
, -
. . -. , ,
, -
:
244 _ /. C++
const int n = 5;
string mm[n]= { "0. ",
"1. ",
"2 . ",
"3. ",
"4. ",
, "", -
. ""
. C++
,
cout.
. ,
. :
25 \:
void ClearScreen (void)
{ cout "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\" flush; }
flush -
. (
6.12). - ,
.
. 6.2.
! 6.1 2, -
int Menu(vector<string>mm)
( ClearScreen () ; //
int k = -1; char ss[100]; string blank{29,' ');
for {int i = 0; i < ran. size () ; ++i)
cout Rus (blank + mm[i] , ss) endl;
cout endl endl flush;
cout Rus (blank + " :_", ss) ;
cin k;
return k;
1
-
29 . ,
6.13. -
.
6. C++ 245
. 6.2.
6.13. -
int main()
{ const int n = 5;
string mm[n]= { "0. ",
"i. ",
"2. ",
"3. ",
"4. ",
return 0;
J
, . , -
.
. -
, , :
void setOptions { cout "setOptions ( ) ; " endl; }
.
()
(. 6.9) "-
" .
question.txt, , . -
, , -
. . - -
: , -
, /
.
, C++
, . -
.
, ,
. ,
question.txt -
(. 6.9) -
:
of stream to (": /tests/question. txt", ios_base: :app) ;
.
ios__base /,
. -
, . .
[37, . 705] "" -
. ,
C++ Builder 6 ios_base -
( 6.14).
6. C++ 247
| 6.14.
"", , -
, :
in .
if stream;
out . -
ofstream;
. -
ofstream, fstream;
trunc . , -
; -
ofstream;
ate .
, , -
;
binary .
. , -
. , ios.h Visual C++ -
iostream.h Borland C++ 5 :
CJ nocreate -- , ,
;
ncrepiace , ; -
, .
. -
binary.
.
, , -
:
248 I. C++
D , -
"new line" ( ), /
C++ * \ ' ;
( atoi . 3.12);
.
, , .
, , /
.
,
. , , ,
sizeof(int) .
, -
. , 1 1 ,
55 555 5 . -
( ). ,
, 1000
. , -
.
: fstream, ifstream, ofstream -
/ . -
, -
. read/write char *. -
,
. , long:
from , to .
6.15.
' 6.15. /
long a;
from.read((char *)&a, sizeof(long)>;
to.write((char *)&a, sizeof(long));
C++:
to.write(reinterpret_cast<char *> (&), sizeof(long));
from.read(reinterpret_cast<char *>(&a), sizeof(long));
. -
, Date :
to.write(reinterpret_cast<char *> (&), sizeof(Date));
from.read(reinterpret cast<char *>{&a), sizeof(Date));
6. C++ 249
1000
, - ,
.
, .
6.16.
! 6.16. 1000
U...;.; :' ;<.-. ; .- ,
int main ()
( of stream toBinf "c: /tests/number. bin", ios :: binary) ;
time_t t; srandf (unsigned) time(St) ) ;
for (int i = 0; i < 1000; ++i)
{ int t = rand f ) ;
toBin, write (reinterpret_cast<char *> (&t) , sizeof (int) ) ;
I
toBin. closet) ;
of stream toTxt {"c: /tests /number. txt") ;
srand{ (unsigned) time (&t) ) ;
for (i - 0; i < 1000; ++i) tcTxt randf) endl;
toTxt. close () ;
return 0;
"" ,
, , number.bin
4000 ( * 4). number.txt
.
, , Notepad. -
.
/ . , -
,
. number.bin, , , -
, .
k- k * sizeof ( ) .
/
, . ()
. / -
, -
.
istream -:
pos type t e l l g f ) ;
250 I. _++
ostream, :
pos_type t e l l p f ) ;
,
seekg{pos_type)H seekp(pos_type) ,
. ( ):
s e e k ( o f f s , pos)
pos ,
ios_base :
static const seekdir beg, //
cur, //
end; //
, ' offs.
,
, , . -
, : -
, sizeof ( ) -
.
-
. , -
, ,
.
[41, 45].
,
, . -
:
1
1
, '
:
A
ch - const;
-
. , , 0x55, -
010101, ,
10101010.
6. C++ 251
,
6.17. -
.
6.17.
-
:
char Code(char ch)
{ char L = ch & OxfO; char R = ch & OxOf; return ( (L 4}|(R 4)); }
char Decode{char ch)
{ char L = ch & OxfO; char R = ch & OxOf; return ((L 4) j (R 4)); )
,
. .
.
. ,
, . . .
, -
. /
read/write get/put. -
. . ""
. ,
, . .
.
#. -
. , .
, -
, -
(
), . ,
6.18.
6.18.
, -
.
: ? -
(. 6.8). -
-:
char Code(char ch) { return ch; }
, .
"" . ,
,
. ,
. .
.
6.19.
16.19.
ch = from. get () ; //
ch = Decode (ch) ; //
tmp. right = ch; //
Q.push^back (trap) ; //
from. get (ch) ; //
: -
, ?
eofbit:
, -
. , eofbit -
- .
,
, .
, -
, . . .
,
, .
, get,
. ,
:
254 I. C++
while (from.get(ch)) //
{ s = ""; s += Decode(ch); //
while (from.get(ch)) //
( ch = Decode(ch); if (ch == '#') break; s += ch; };
tmp.question = s;
for tint i = 0; i < 5; ++i)
{ 3 - "";
while (from, get(ch)) //
{ ch = Decode[ch); if {ch == '@') break; s += ch; )
tmp.answer[i] = s;.
}
from, get (ch) ; // ^
tmp.right = Decode(ch);
Q.push_back(tmp);
}
, .
.
, -
6.20. :
. -
,
. ,
1015 . -
, , 10 .
; 6.20.
writelnteger (f , n) ;
}
int getOptions (void)
j ifstream f ("c: /tests/test . ini", ios_base: :binary) ;
int n = readlnteger (f } ; return n;
setoptions -
-, getOptions .
. -
10 , , . -
-
.
.
-
( 6.21).
6.21.
( 6.22).
6.22.
void testing!)
{ char 55 [100]; vector <Questions> v;
ReadQuestion(v); //
int k = getOptions () ; /'/
vector <Questions> w; //
w = GetTest(v, k); //
unsigned int i = 0, summa = 0; int j; char a;
while(i < v.sizef))
{ cout i + 1 "." v[i].question endl;
256 I. C++
for (j = 0; j < 5,
cout ' ' j + 1 ")" v[i] .answer [j] endl;
cout Rus ( " :_", ) ;
cin a;
if (a == v [ i ] . right) ++suirms; ++i;
}
W h a t f k , surratia) ; //
getch(); // ,
, (. 5.12).
. typedef -
, :
typedef vector<Qaestions> Question;
, . -
: ,
, -
. . ,
,
"" (# @).
6.23.
6.23,
void lookQuestions ( )
{ vector <Questions> v; ReadQuestion (v) ;
ClearScreen ( ) ; //
vector<Questions>: : iterator i;
for(i = v. begin {}; i < v.endO; ++i)
{ cout i -> question endl endl endl; // 2
cout Rus (" <0-, 1->? ", as);
int a = 0; cin a; if (a) v.erase(i);
}
ofstream to("c: /tests/question. bin", ios_base: : binary) ;
for (int m = 0; m < v.size(); ++m)
f spring s = v [m] .question+ ' # ' ; writeString (to, s) ;
for (int k = 0; k < 5; f+k)
{ s = v[m] .answer [k] + '@'; writeString (to, s) ; }
s = v[m] . right; writeString (to, s) ;
I
to. close ( )
.
6. C++ 257
[371, , -
. , -
/ . ,
, . -
/ :
istringstream; //
ostringstream; //
stringstream; //
sstream.
, -
. , -
6.24, .
j 6.24.
/
, " C++".
93. 1264
7
C++ ,
. :
, , , .
C++
: "" , -
, . ,
(, Basic), C++ -
.
""
, , -
. C++ .
(. . 8).
, C++ , -
. -
. , .
, , :
, , ? , -
. -
:
int& fffint k)
{ int d; . . . return d; }
,
. Visual C++ 6 "" -
, :
260 I. C++
, -
, .
( 7.1), , -
, .
[ 7.1. ,
int n = 2; //
int& f f ( i n t k} // ,
( return k; } // ,
int f ( i n t Si) // -
{ return i; }
int main(void)
{ ccut f ( f f ( n ) ) endl; // -
int g = 5;
cout f ( g ) endl;
return 0;
"" 6684064, , , -
. , , 5.
? , , :
inns ff (int& k ) ;
G .
,
, -
. -
:
ints f f t i n t k )
{ static int a; . . . return d; }
\ 7.2. ""
, :
double k = max(a r b) ;
,
, :
double = 5, b = 6, = 7;
max(, b} = 10;
, .
, :
if ( > ) = 10; else b - 10.
, :
max(max(a, b ) , ) = 10;
max (max (a, b) = 10, ) = 0;
( ) . -
,
, 7, ( ) .
. ,
.
, -
. "" ,
, 7.3.
7.3. ""
, -
.
:
int [] = { 10, 20, 40, 5, , 1, 8, 9, 50, 12 };
int m = Mmin(10, x ) ;
262 I. C++
m 5
Mntin. , -
. :
Mmin(10, ) = 0;
, 5, .
""
. 5 , , ,
.
. . ,
, -
7.4.
I 7.4. ""
:
double = 5, b = 6;
*(&, sb} = 10;
. -
, . Mmin
:
int* Mrain(int n, int d[])
{ int im = 0;
f o r t i n t i = 1; i < n; i++) im = d[im] < d [ i j ? im : i;
return ud[im];
I
:
int [] = { 10, 20, 40, 5, 6 1, 8, 9, 50, 60 };
int rn = *Mmin(10, x j ;
*iin(10, x) = 0;
, ,
: -
, , ?
Visual C++ 6
7. 263
, (
7.5).
7.5. ""
; ,'....". .' : .'..;,.-.....' '. ;
, -. -
& &. :
cour *max(a, b) endl;
*max(a, b) - 20;
max (, -
, -
.
, -,
Visual C++ 6 :
error C2440: 'return' : cannot convert from 'double *' to 'double &'
'return': 'double *' 'double &'
:
cout max(sa, Sb) endl;
max(Sa, &b) = 10;
, C++ "-
". , , const
. 7.6.
7.6. ""
:
error C2166: 1-value specifies const object
1-value
264 I. C++
, , const, -
, ,
, .
double * const rr.ax (double *, double * y j
( return (*x > *y? x: y ) ; )
. -
- double.
*max{&a, &b) = 10;
, ,
.
C++ -
. , -
. , -
, -
.
-
:
int f ( . -
. -
,
, , , , .
-
.
-
. . -
, . -
, . - -
, , -
, , , "" .
, ,
, -
. :
int f |int k . . . )
, ,,
.
7. 265
, -
: ,
, -
.
-- C++
. , -
, , ,
, -
- .
,
. :
;
"" ;
.
, -
.
, -
. ,
,
. , -
-
, .
, -
: , , -
.
.
:
;
,
.
-
"" , -
: .
. 7.7.
1 7.7. ( )
double f(double n, . . . ) //
{ double *p = &; //
double sum = 0, count = 0;
while <*p) //
266 /. C++
{ sum += (*); //
++; //
count++; //
:
double = f (1.0, 2.0, 3.0, 4 . 0 , 0 . 0 ) ;
2.5. -
,
f d , 2, , 0) (
), ( -
) .
, -
, , . -
, .
. :
double ffint n, ...) //
{ int *p = &; // - ""
double sum = 0, count = n;
for (;; (double*)--) // int* -> double*
sum += (*p);
return ((sum)?sum/count:0);
:
double f(int n, ...) //
{ double *p = (double *)&n; //
double sum = 0, count = n;
for (;n;p++) //
sum += (*p) ;
return ((sura) ?sum/count:0) /
}
,
, . -
double-, sizeofunt;
"" .
8 (sizeof (double)),
. , ""
, double *.
?. _ 267
,
. -
7.8.
| 7,8. ()
double f ( i n t n, . . . ) //
{ int *p = &;
++; / / ( "" double)
double *pp = (double *)p; //
double sum = 0, count = n;
for (;n ;pp++} // 8
sum += (*pp) ;
return ( ( sum) ?sum/count : 0) ;
++
double. 8 -
:
double * = (double *}p;
++
sizeof (double) 8 , .
.
-
. -
,
. , -
. -
( ):
double f (double- &n, . . . )
:
double m[] = { 1.0, 2 . 0 , 3 . 0 , 4 . 0 , 0 . 0 f;
cout f ( m [ 0 ] , m [ l ] , m [ 2 ] , m [ 3 ] , m [ 4 ] ) endl;
2 . 5.
-+
. -
,
- "". , -
, (. 7.8) ( -
-) * double, -
. "" ,
268 I. C++
"" .
, ,
, -
Intel 4 . ""
union. , ,
7.9.
I 7.9. - ()
(
- )
. -
"" ,
7.10, "" -
.
7,10. - ( )
:
f ( & a , ub, 0 )
: -*- -
, . -
, .
, -
char.
:
(char) , (char *) -
;
Q char * , -
.
,
,
. :
char *t(char *sl, ...)
,
, . -
, -
0. 7.11.
I 7.11. { )
while (*cp)
{ strcatfs, *cp); // ( )
++; //
return s;
:
char * = f ( s l , s2, s3, 0);
si, s2, s3 ,
char *. -
:
char *sd = f("First", "Two", "Three", 0 ) ;
, - () -
, (). , -
char , -
.
-
, stdarg.h. -
,
, -
. NULL. ,
, -
:
void va_start (va_list prra, );
va arg(va_list prm, );
void va_end(va_list prm);
typedef va_iist.
va_start va^iist ,
va_arg ,
va_end .
.
1. -
,
va_iist, va_list Last?;
.
2. (
)
7. 271
7.12.
()
,
7.13.
7.13. ( )
do.. .while, . .
.
, .
C++ ,
-
. , ,
.
!.
,
:
! = 1 * 2 * 3 * ... *
-
, , for.
7.14.
! 7. 14.
() ,
:
0! = !
V >0 ! = ( -1)!
()
, :
/ -1
2) = i
V > 2/() = F(n - 1) + f ( - 2)
, .
,
. , , -
[9, 30, 39],
. -
7, 273
( ) -
. , -
! ( 7.15).
7.15.
long Fact f i n t k)
{ if (k = 0! return 1;
return ( k * Fact(k 1 ) ) ; //
,
, 7.16.
7.16.
long Fibo(int k)
{ if ({k = 2) I | (k = 1)} return 1;
return (Fibo(k - 1) + Fibo(k 2}); //
,
. , ""
, . ,
"" .
.
, -
, .
, .
.
J L W
. ,
,
. [9] [30, 39]. :
void Quicksort(A, I, n)
{ // 1 < k <
// k-ro
Quicksort(, 1, k-1); //
Quicksort , k+1, n) ; //'
1
"" (
, . .),
274 I. C++
[9. 34| . -
,
. ,
. ,
7.17, -
(. 3.11).
I 7.17.
-
, :
cout Len("1234567390");
.
"" ,
,
:
G .
, ;
, , -
, ;
,
if -
.
:
unsigned int Length (char *s)
if (*s) return 1 + Len(s + 1) ;
else return 0;
1
:
;
;
.
, -
.
^. __ 275
, , -
. -
:
while ( )
i //
/ /
.
f.
,
, .
7.18.
.......................................................................................;;
! 7.18.
, ReadFiie
. -
, f .
, ,
.
Rec
s
Rec. ,
: , -
. , -
276 I. C++
. -
" ..." -- -
.
, :
void PriestAndDog(void;
( cout " , ,";
cout " ', ,";
cout " , :";
PriestAndDog();
I
.
( -
). ,
, -
. if
, . -
if , .
-
. ,
Fact(k ),
.
k . -
, -
.
(-
).
1. ( -
): void Reclvoid) { S; if () Rec ( ) ; }.
2. ( -
): void Rec [void) ( if () R e c ( ) ; S; }.
3. ( ),
( ): void Rec (void)
{ 31; if () Rec ( ) ; S2; }.
void Rec (void)
, .
" " " "
7. 277
"-
".
.
, . ,
,
( 7.19).
7.19.
. .......... ..:. ..:!>.....-.;'..;;;:: Z... ..*. . ,. ;
long Fact(int k)
{ if (k > 0) return (k * Fact(k - 1)); //
else return 1;
. -
"" :
void ReadFile(ifstream &f)
{ char s[100]; //
if (If.eofO) //
ReadFile(f); //
getline{f, s); //
cout s; //
. ,
.
,
. , :
void ReadFile(ifstream &f)
{ char s[100]; //
getlinetf, s); //
if (If.eofO) //
ReadFile(f); //
coat s; //
}
, , . -
. , -
.
,
.
278 I. C++
, ,
"" . , -
-
. ( , ) -
,
, . -
. -
, , .
. -
. -
. ,
ReadFile
. , -
.
.
. -
, -
: ,
, .
,
, . -
, , (
).
:
bool Palindrom{string s)
{ int i - 0, j = s.length(I - 1;
for (;i < j ; i++, j ) if ( s [ i ] != s [ j ] } return false;
return true;
.
.
, . -
, ,
:
j s ;
s .
7. __ _________ __ __ 279
.
:
bool Palindromfstring s)
{ if (s - = 1) return true
else if ( == )
return Palindromfs )
else return false;
, -
7.20.
I 7.20. PaJ-indrom
, , :
cout PalindromfnoTon") endl;
:
string s = "";
;
cout Palindrom(s) endl;
.
, , : ,
, ,
,, . -
. . -,
,
. . -,
. -
. -
.
"".
, s [ 0 ] - s[end] ( ""). -
true, return
:
Palindrora(s.substr(I, end-1))
280 I. C++
end. s[0] == s[end] ( ""), -
-
"". true if.
,
.
(true,
l).
. -
:
1. .
2. "", .
3. "",
.
4. "",
.
3 4 . -
"" C++
( 7.21).
i 7.21.
;....'.'. ; -...' -.. !
, 1. -
.
, .
. :
int d[] = { 1, 2, 3, 4, 5, , 7, 8, 9, 10, 12, 13 };
int k = BinarySearch(d, 0, 12, 7);
k i, . -
,
7.
1 () ,
4 .
, -
- " ":
,
.
. , -
. -
.
-
:
for (int m = [ 0 ] , i = 1; i < n ; i++) m = ( ( a u ] < m) ? a [i] :m) ;
,
. 1
.
, , -
. , -
, , -
. ,
( -
7.22).
| 7.22.
, -
( ), (
). min -
, , ,
, .
. -
, -
. ,
-
282 _ I _++_
. ,
, :
void ppfints tt, int 1, int )
{ cout endl;
for (int i = 0; i < tt; i++) cout ".";
cout "min(" 1 ',' r ')';
}
"" min :
int min (int [], int left, int right)
{ static int tt = 0; //
pp(++tt, left, right) ; //
if (left == right) return a [left]; // a[right]
int m - (left + right) /2; //
int x = min {a, left, m) ; //
pp( tt, left, right); //
int = min (a, m+1, right); //
pp (--tt, left, right) ; //
if (x < y) return x; else return y;
}
"" , 8
in (, , 7) :
. min {0, 7)
. . min(0, 3]
. . . min (, 1)
. . . . min (, )
. . . min (, 1)
. . . . rrtinfl, 1 )
. . . min (0, 1)
. . min (0, 3)
. . . min (2, 3)
. . . . m i n (2, 2 )
. . . min (2, 3)
. . . . min(3, 3 )
. . . min (2, 3)
. . rtiinlO, 3)
. m i n ( 0 , 7)
. . min ( 4 , 7 )
. . . m i n ( 4 , 5)
. . . . min ( 4 , 4 )
. . . min ( 4 , 5)
. . . . mints, 5 )
. . . m i n { 4 , 5)
\7^ 283
. . min(4, 7)
, . . m i n t , 1)
. . . . min(6, 6 )
. . . m i n t , 7)
. . . . min(7, 7 )
. . . m i n t 6, 7)
. . min(4, 7)
. m i n ( 0 , 7) //
& -
, -
.
. -
. "
" "" -
: ( -
) , 1 . -
7.23.
....... rirwi
17.23. .......
" .........
, (. 7.22), -
, . -
, ,
. , -
"" :
. min(0, 9)
,1 9}
. . mind, ,
. . . min(2, 9)
. . . . min(3, 9 )
..... min(4, 9)
...... min(5, 9}
....... m i n t 6, 9}
........ min(7, 9)
......... min(8, 9)
.......... min(9, 9)
......... min(8f 9}
. minC7, 9)
284 I. C++
min(6, 9)
min(5, 9)
m i n ( 4 , 9)
. . . . min(3, 9 )
. . . m i n ( 2 , 9)
. . m i n f l , 9)
. m i n ( 0 r 9)
, -
. -
.
.
.
! -
:
else return a [ b e g i n ] ;
. , -
, -
( ).
, ,
: , .
( ),
.
, -
.
, . 4.
:
struct Item { int info; Item* next; };
:
typedef Item* link;
, :
link Head; //
7. 285
, -
( 7.24). -
- .
7.24.
, ""
.
.
-
. ,
, , . -
7.25.
7.25.
, ,
. , , -
.
-
!
ReadFile,
,
. -
7.26.
\:
I 7.26.
286 _ I. C++
, -
. , " " - -
. -
-
,
.
. ""
.
7.27.
;:; ..... ' ............... ' ........................... ............................. - ......................................................................................................................................................... '"" ----- :
; 7. 27,
, , -
. -
" ". -
:
struct Node {
string 'name; //
Node *Left; // " "
Node *Right; // " "
, , ,
. ,
, .
"" [9, 14, 16, 39, 41, 45]
"" . -
: Root, " " Left, " " Right,
.
1. Root, Left, Right. [9] preOrder.
2. Left, Root, Right. inOrder.
-
. . -
. -
7.28.
! 7.28.
*". /..-
- -
typedef Node* link;
void preOrder (link p, void visit (link p) )
{ if (p ! = NULL} //
{ visit (p) ; //
preOrder (p -> Left); //
preOrder (p -> Right); //
visit , -
inOrcier -
.
,
,
.
. -
,
.
,
,
, -
288 I.
, -
[16]. :
. ,
. , -
NULL. -
: ( -
), .
7.29.
[, 7.29.
,
. , -
, 7.30.
7.30.
unsigned long -
.
( 7.31).
I 7.31.
-
.
. .
, -
. :
?
-
?
-
. , -
, 10!.
:
long Fact(int k = 10)
{ if {k > 0) return k * Fact(k-l); else return 1; }
:
cout Fact(6) endl;
cout FactO endl;
720, 10!,
3628800. ,
.
. ,
, , :
double Fact(double k);
double Fact(double k = Fact(4))
{ if (k > 0) return k * Fact(k-l); else return 1; }
:
,
, .
F(F(x)).
(4!)! = 241;
. 1264
290 I. C++
double, 24!
, unsigned long.
, ,
Pentium 32? 1754!. 104930,
long double;
double, -
. -
.
, -
"" (. 2-5)
.
, , .
,
- ( 7.32).
| 7.32. -
int n = 5;
Factfn} 120, n 5.
? :
double Fact(unsigned ints k)
( if (k = 0) return 1;
return (k * Fact (k)); //
}
Fact(k-i) Fact(--k). -
.
n 1. -
, .
Fact (n) n .
.
II
-
8.
9.
10.
11.
12.
'
8
-
. - , -
, .
,
, .
"",
,
.
C++ ,
. ,
C++ -
, , " ". -
, , -.
, . -,
,
. -
. ,
. -, -
,
, .
C++,
: enum struct. , ,
,
.
, .
294 //. -
, , , -
:
1
= b + ;
: -
= add(b, ) ;
. C++
. ,
/ , " "
. , , ,
iostream .
C++ .
, -
. .
, (, ** )
, ,
(, "). , -
:
. () ;
. * ;
?: ;
:: ;
sizeof ;
# () ;
G ## .
, -
. ,
double, . -
:
double operator A (double a, int b)
Visual C++ 6
2803:
'operator ' must have at least one formal parameter of class type
'operator A l , ,
, ,
, -
erium.
8. 295
=, [ ] , -
-> . -
.
, .
"" .
- :
operators( );
.
. ,
. -
, -
, -
, .
, , :
operators( 1, _2);
, . -
-
:
_1@_2;
D operator@ (_1, _2).
:
operater@();
:
;
-
operators ().
enum struct. .
Borland C++3.1
.
Visual C++ 6, Borland C++ Builder 6 Borland C++ 5.
enum
:
enum Decade -
{ zero, one, two, three, four, five, six, seven, eight, nine };
296 . -
.
,
10.
. , -
. 8.1, -
10.
8.1. 10
+ zero one two three four five six seven eight nine
zero zero one two three four five six seven eight nine
one one two three four five six seven eight nine zero
two two three four five six seven eight nine zero one
three three four five six seven eight nine zero one two
(our four five six seven eight nine zero one two three
five five six seven eight nine zero one two three four
six six seven eight nine zero one two three four five
seven seven eight nine zero one two three four five six
eight eight nine zero one two three four five six seven
nine nine zero one two three four five six seven eight
, - -
:
Decade operator*{const Decade &a, const Decade &b)
, ,
.
" "
Decade operator-*- (const Decade &a, const Decade &b)
{ return Decade((a + b} % 10); }
Visual C++ 6 -
. , +
,
-. ""
( 8.1).
I 8.1. , 1
Decade operator+(const Decade sa, const Decade &b)
{ Decade t = Decade{(int)a+(int)b); return Decade(c % 10); }
8. 297
t -
. -
+ , -
int , -
Decade. return ,
Visual C++ 6 :
error C2440:'return': cannot convert from 'int1 to 'enum Decade1
'return1: 'int' 'enum Decade1
, . ,
C++ .
Decade , ;
= seven;
= six;
cout A + endl;
(7 + 6) % 10, . ,
, ,
. , -
:
Decade A = seven, = six;
.
cout operator+(A, ) endl;
-
.
.
:
+=, "" -
. . ,
: -
, , ,
. ,
( -). , -
, Decade.
8.2.
\ ....... ' ........... "": ......... " ................... : ................... '"-"''"" ....................... - ..................... - .................. 7~ ....... ............................. " ..... " .............................. \
I 8.2. ()
"" : +
.
:
cout ( += ) endl;
, +=
,
.
+= , -
. , :
decade A = one, = one, D = one;
cout ( += += D) endl; // - two, A = three
cout (( += } += D) endl; //
+= -
, !
. :
cout operator-*-=(A, operator+=(B, D) ) endl;
8.3.
+= -
8.4.
| 8.4. , 2
, .
Decade int ( 8.5).
8. 299
( }
,
Visual C++ 6 Decade, a
int ! Visual C++ 6 -
, , operator (, 1 ) .
, , Visual C++ 6, . . Borland, Visual C++ 7
, . -
, , -
"" .
,
. -
++. (. . I),
. -
( 8.6).
:-- .............. ............................................................. ........................................................ ' .................................................... ............. |
| 8.6.
........ .', .................. ... ............. , ................. i.;...'.\, ....... ,*......*,. ...... ..,.. .......... . ............................... ,...,..,!...,. ......................... '. ..... ,... ..... ,.,.,; *'--i->i
:
Decade operator++{ Decade &a)
{ Decade t = ; //
= Decade ( ( a += one) % 10) ; //
return Decade (t) ; //
, -
, . , -
,
.
,
, .
-, ,
8.7.
300 II. -
\ 8.7.
! ~_ .".* ~ ;...... .._ ;; ...;;;.
-
:
int main (void)
{ Decade A = seven, = six;
cout +H-A endl; // eight
cout A++ endl; // eight
cout A endl; // nine
return 0;
i
, , -
, , C++ -
.
,
Decade.
, .
- 8.8.
| 8.8. Decade
out s [a] ;
return out;
,
Decade.
, . -
. -
8. 301
,
. [37] " ".
.
,
. -
, unsigned long.
struct Rational { unsigned long num., denum; };
Rational
, .
() -
. -
8.9.
,
> .
+=. , -
:
[/) + (c/d) = (ad + cb)/(bd)
.
. -
, -
. 8.10.
| 8.10.
a.denum = a.denum*b.denum; //
reduce() ; //
return ;
Decade,
,
. .
""
( 8. ).
: ? ,...--..-.-....,. ...
I 8.11. ""
(-
8.12).
| 8.12. /
//
ostreamfi. operator (ostreamS t, Rational a)
{ return ( t ' ( ' a . num ' / ' a . denum ' ) ' ) ; }
//
istreams operator (istream& t, Rationals a)
{ t a. num; t a.denum; reduce(a); return t; }
,
.
:
void main (void)
{ Rational A, B, ; //
A. num = 1;
A.denum = 2;
. num = 1 ;
B. denum = 3;
= + ; //
cout endl; //
( 7 / 6 ) . , -
. , C++
- .
8. 303
, ,
:
void main (void)
{ Rational d[5], ;
cout " 5 : num denum <enter>" er.dl;
for {int i - 0; i < 5; i++) cin d[i]; //
for (i = 0; i < 5; i++) coat d[i] endl //
= d[0] + d[l] + d[2] + d[3] + d[4J; //
cout endl;
-
.
, . -
<Enter>:
1 i
2 2
3 3
4 4
5 5
-
, .
:
(1/D
(1/D
(1/D
(1/1)
(1/1)
(5/1)
,
. . -
( 8.13) -
.
8.13.
unsigned long ( 8.14). .
$:: ": ;''. ;
;
: " -. 8.14.
. . i- -i Rational - -unsigned
"S" long
,:
,.,..,.,,.....
, "" . -
:
int main {void}
{ Rational A, B, ; //
cout " : <Enter>";
cin A;
cout " : <Enter>";
cin B;
cout A * 3 endl;
cout 5 * endl;
ULONG d = 8;
cout * d endl;
cout .d * A endl;
return 0;
\
= (1/2) = (1/4) :
8. ; 305
-
. ,
.
Decade -
.
+= ,
( 8.15).
.15. Rational
int
, ,
+= 2. 2 += -
C++, . -
(-
8.16).
8.16.
,
, :
cout ++ endl;
cout ++ endl;
cout A endl;
( 2 / 3 ) , :
(5/3)
(5/3)
(8/3)
, , > (-
). 8.17. , -
, . "", -
.
.
30g II. -
\ 8.17. >
;
!...., , "... , .1...... ......... ...... , .
. Person. -
,
string:
struct Person { string fio; Date BirthDay; };
, .
Person = {"", ( 1955, 12, 21 ) };
Visual C++ 6 -
. , ,
. C++ -
, -
. , ,
. .
, -
. , -
:
.
Person, -
fio .
:
Person{char s [ ] )
:
Person(char *s)
( 8.18),
| ' ...;, ..; , .,... ..,
: 8.18.
struct Person
( string fio;
Date BirthDay;
Person(char *s)
8. 307
, , ,
.
Person :
Person (" !");
:
Person = " !";
:
Person = Person(" !");
() , -
.
, -
.
" ": ,
Person t;
. , ,
.
, - -
:
;
, ;
.
, C++.
, C++ " "
. -
, .
1. . , -
, .
2. . -
. Person
c h a r t ] .
3. . -
.
- .
, , .
, -
BirthDay. -
8.19.
8. 309
. , -
:
Person(const char *s, unsigned int y, Month m, unsigned int d)
{ fio = s; BirthDay.year = y; BirthDay.month = m; BirthDay.day = d; }
, -
:
Date. , ,
Month , . .
. -
, typedef.
, -
. . ,
.
Date ( 8.20).
. 8.20. Date
:
1.
, .
2.
. ,
Month . ,
310 II. -
.
C++ .
,
"" ,
. "" :
Date::Date(const , const Month m, const UI d)
{ year = y; month = m; day = d; }
Date::Date(const UI y, const UI mr const UI d)
{ if ( (0 < m) &S (m < 13)) { year = y; month = Month(m) ; day = d; } }
,
. :
, -
; , .
:
Date A; //
Date = (2001, Date::feb_, 22}; /I
Date (2002, Date::feb_, 22); //
Date G(20Q3, 2, 22}; // 4-
Date D(B};. //
Date F = ; //
Date H = Date(2005, Date::feb_, 22); //
Date E = Date(2007r 7, 29); // 4-
Month , -
.
Person:
Person U(" ", Date{1986, Date::jul_, 2 9 ) ) ;
,
Date, .
-
. C++
*. Rational (
8.21).
! 8.21. Rational
' He , -
.
8. 311
8.22.
8.22.
struct Rational
{ ULONG num,, denum;
Rational(} :num(0) , denum(l) ( }
Rational(ULONG n) :num(n), denum(1){ }
Rational(ULONG n, ULONG d) : num(n) , denumi(d != 0)?d:denum) { }
Rational (const Rational &r) : r.umfr.num) , denumfr.denum) ( }
, . , -
(. 8.22)
, . -
, ;
Rational(ULONG n, ULONG d):num(n), denum(init{d)) { }
, , :
ULONG init (ULONG ) { return ( != 0) ?y:denurr.) ) ; }
,
, -
-
.
. , , -
.
-.
-
- ( 8.23).
312 II. -
\ 8.23.
.
.
,
.
8.24.
: 8.24.
void main(void)
{ Old x = g(new Old(l, 2.0)}; } //
,
. -
. Person:
Person B = Person(" !");
:
const char * > Person
(, 8.13 8.14), ,
,
8. - 313
. -
. -
, (
8.25).
| 8.25.
struct Rational
{ ULONG num, denum;
Rational (ULONG n - 0, ULONG d = 1)
{ if (d != 0) { num = n; denum = d; } }
. ,
. -
:
long -> Rational
:
Rational = 3;
-
,
. -
== . , -
bool, :
bool opera tor== (Rational a. Rational b)
{ return (a.num*b.denum == b. num* a. denum) ; }
-
.
, =
-
.
-
:
Rational > long
C++
:
operator long () { return ( num/ denum) ; }
314 _ II. -^^
-. -
Rational :
int main (void)
{ Rational mm = 11; // inr. -> long > Rational
cout ram endl; // (11/1)
long i = mm; // Rational -> long
cout i endl; // 11
return 0;
, i ,
Rational.
long. -
.
:
Rational f (Rational a)
{ return a; }
long f f (Rational a)
{ return a; } i
:
Rational zz - f (13) ;
long k = f f (zz) ;
13
Rational , zz /1).
-
, k 13.
, . -
Rational -> long, , -
.
long -> Rational C++ -
explicit.
:
explicit Rational (ULONG n = , ULONG d = 1)
, . -
:
Rational ram = Rational (11) ;
Rational zz = f (Rational (13) );
Rational, .
8, _ 315
double
. , -
, .
double, 8.26.
I ......... ; ............................................................... "- ................ "" ......... ; ....... - .............................................................................. - ............ ; ...... - ............................
j 8.26. - double
struct Double
{ double t;
Double (double a = 0.0): t(a) { }
operator doubled { return t; )
double. , ,
:
double operator^ (const Double &a, int b)
( double t = a; // double <- Double
if fb = 0) return 1; // int -> double
else if (t == 0} return 0; // int -> double
else if !b == 1)- return a; // Double -> double
else if {b < G) { t = 1/t; b =- b; } //
double p = 1 ;
for (int i = 1; ! (i > b) ; i++) p *= t; //
return p; '
, :
int main (void)
{ Double d = 0;
cout double (d"-2) endl;
cout double(dnO) endl;
d = 2;
cout double (d^O) endl;
cout double{dAl) endl;
cout double (dn-2) endl;
cout double (d"3) endl;
return 0,-
. -, -
d -
: int -> double double -> Double.
316 11^ -
-, ,
, .
-, double -
,
Double.
, ""
. , -
Java, --
.
Rational -
. ,
"" . -
. +=,
.
, ""
, , . . .
gcd reduce .
reduce "" , a gcd (
8.27).
! 8.27. Rational
void Rational::reduce 0
{ ULOHG t = ( (num > denum) ? gcd (rium, denum) :gcd (denum, num) ) ;
num /=t; denum /=t,-
.
}
ULONG gcd(ULONG x, ULONG y)
{if (y = 0) return x; else return gcd(y, x%y); }
reduce , . . -
. gcd
,
.
+=.
+= (. 8.10) Rational. ,
;
Rational^ operator+=(Rational &a, const Rational &b)
,
:
8. 317
, , . -
, .
- -
. - "-
", . , -
:
_.__ ()
_ -
. t
Rational, . -
- :
Rational t = 2;
t.operator+=(3)
t . ,
-
.operator.
, .
- , this!
,
-
: *, ->.
, +=
. :
Rationale operator+=(const Rational sb)
.
. , . .
. , .
, (. 8.10)
+= ( 8.28).
: 8.28. -
. reduce -
:
this -> reduce();
(*this), this, . . , -
( "" 7.5).
. +=, C++, -
. -
,
:
Rational t, z.;
int it, iz;
t = 2, z = 3; t -1-= z +*= 2; // t = (1, 1 ) , z = (5, 1)
it = 2 , iz = 3; it += iz += 2; //
t - 2, z = 3; t+=(z += 2 } ; // t = ( 7 , 1), z = < 5 , 1)
it = 2 , iz = 3; it+=(iz += 2 ) ; //
t = 2, z = 3; (t += z}+=2; II t - (7, 1 ) , z = (3, 1)
it - 2, iz = 3; (it += iz)+=2; //' it = 7, iz = 3
, "" , "",
.
,
,
. = +=.
8.29.
! 8.29.
Rationale Rational::operator=(Rational b)
( rium = b.num; denum = b.denum; ' return *this; }
:
// t = 7, z = 3
// it = 7, iz = 3
. -
. C++ . ,
, , -
. , ,
, . . [37] -
Date (. 8.20) , , -
1 ( 8.30).
8. 319
; 8.30. ,
Dates AddYear (int n) { year += n; return *this; }
Dates AddMonth (int n) { month = Month(month + n); return *this;}
Dates AddDay (int nj ( day += n; return *this;}
, . -
Date d ( 2 2 , 02, 2003};
:
cout d . A d d Y e a r ( l ) . A d d M o n t h ( l ) . A d d D a y ( l ) endl;
:
23 3 2004
Rational. -
. , -
. :
Rational Rational::operator++()
{ return (*this += 1); }
, ,
. , -
:
Rational Rational::operator++(int)
{ Rational t = *this; -this += 1; return t; }
, -
"" . -
, , :
int main(void)
{ Rational z = 2;
cout ++z endl; // (3/1)
cout z++ endl; // (3/1)
cout z endl; // (4/1)
-
-= .
?
""
-.
. 8.31.
320 It. -
8.31. -
.,.* ,*..,. .. - , ' ;,'."...,,. .,...., 1 -,.,
Rational operator+(int b)
{ Rational t = *this; return (t += b); }
Rational t = 2;
cout t + 3 endl; // {5, 1)
cout 3 + t endl; // 5
, ,
! -
:
cout t,operator+(3) endl;
cout 3.operator+(t) endl;
, Visual C++ 6
. , -
3 Rational. -
,
long. , -
.
Rational, :
cout Rational(3+tJ endl; // (5, 1)
:
cout Rational(3)+t endl; // (5, 1)
, -
.
, "" Decade (. 8.5),
Rational, . C++
.
, , , -
Rational ( , -
) -
:
Rational t = 2, z = 3;
cout t + z endl; // (5, 1)
cout z + t endl; // (5, 1)
. -
-.
8. 321
(. 8.12). -
: . -
, , , -
. ,
( 8.32).
8.32.
. -
R: ,
R.operator (cout) ;
:
Rcout;
, -
! , . -
, "" -
-.
, . -
/ -
.
, -
8.33.
8.33. Rational
1! . 1264
II. -
,
. , -
C++ .
:
class _
{ };
5. 323
,
. :
C++ "" ? ,
8.32 struct class
:
int main()
( Rational = 1, = 2; cout endl; return 0; }
. -
2248 :
' R a t i o n a l : : R a t i o n a l ' : cannot access private member declared in class
, , -
" " Rational. ! ,
.
. "" ,
, -
. ,
. -
,
.
public , private .
Rational,
. , -
, Rational.
. -
-
. "" -
, . . " "
Rational. \
,
public, Rational
( 8.34).
8.34. Rational
struct Rational
{ Rational(ULONG n = 0, ULONG d = 1):num(n), denum(d) { reduce(}; }
Rationals operator+=(Rational &b);
Rational^ operator=(Rational b);
Rational operator++();
Rational operator-- tint);
324 II. -
Rational 8.35.
I 8. 35. Rational
class Rational
{ public :
Rational (ULONG n - 0, ULONG d = l ) : n u n ( n ) , denum (d) { r e d u c e d ; }
Rational^ operator+= (Rational &b) ;
Rational*; cperator= (Rational b) ;
Rational cperator++ ( ) ;
Rational operator++ (int) ;
operator l c n g ( ) { return num/denum; }
private:
ULONG num, denum;
ULONG gcd (ULONG x, ULONG y)
{ if {y == 0) return x; return gcd(y, x%y) ; }
void reduce ( ) ;
, public private,
, , .
. , , -
, . -
.
,
, . -
. :
int main{)
{ Rational = 1, = 2; cour A + endl; return 0; }
, -
- .
: .
,
. . , , -
8. 325
, ,
"". "" - - ,
. -
"", 8.36.
friend.
8.36. Rational, 2
. , ,
. " " [37].
, .
9
-
-
. , STL
C++.
. "
" .
.
; , -
.
.
, Pascal
. C++ " "
. -
,
,
,
.
, -
, ()
.
, -
.
328 II. -
array. , ,
. -
:
array (const int left, const int right);
,
. ; -
array. -.
array, -
. STL -
, :
. -
-.
. , -
, 0, 1 . ,
67 .
.
[], , .
[]
, .
.
, -
. -
. , -
, ,
.
, . .
, . , STL,
- assign,
, ,
(, )
.
, ,
.
, : -
, ,
.
-
. ,
: ,
,
.
9. 329
, -
. -
, .
- ( ) ,
! . , :
-.; II
,
, . ,
- -
, .
. -
(,
) , .
double. 9.1.
9.1.
class array
{ public:
typedsf unsigned int UINT;
array(UINT size = 10, double k = 0.0);
arraytint 1, int r, double k = 0.0);
array(const array &a) ;
arrayfconst array sa, int 1, int r) ;
arraytconst double * const begin, const double * const end);
-array () ;
doubles operator[](int index);
arrays operator=(const array &a);
arrays assign(const array Sa, int left, int right);
arrays assignfconst double *begin, const double *end);
arrays operator+=(const double sa);
arrays operator-=(const double Sa) ;
arrays operator*^[const double Sa) ;
arrays operator/^(const double sa);
int Left()const; //
int Right()const; //
QINT size()const; //
doubles max value();
doubles min_jvalue () ;
double summa () ;
double product!); //
void operator-{); //
double* find(double a); //
friend array operator+(const array&a, const array sb);
330 . -
typedef ,
UINT unsigned int.
min, max ,
. find_yaiue
, , .
.
, data. -
, .
, -
.
. .
, -
- Destroy (),
delete[].
deleted: . C++
, -
. , .
, ,
~ (). ,
.
. ,
, delete. -
.
-
. 9.2.
| 9.2.
,
.
, inline-
. : -
(. 8.36)? -
, " ".
,
, . .
[37], " ".
, . .
delete . -
, - -
. , , -
.
-
array :
double [] = { 1, 2, 3, 4, 5, , 7, 8, 9 };
array A; // "" double [10]
array B(a, a + (sizeof (a) /sizeof (double) }); //
array D(a +3, + 7); //
array C(-2, +5); //
array F(-5, +5, 2.2); //
array G(B); //
array X(F,0,5); // array
array Z(20); //
array W(10u, -1.0); //
332 _ II. -
( 10), ( -
, -).
.
"" .
-
.
, .
(, ).
,
, . 10. -
,
. -
9.3.
! 9.3.
S ...................... .. ............... .. .......................................... . .................................... ....................................... .... ............. i.i --- i.;,_., .i,.; ..............................
, , ,
-
. , -
"" ( -
).
-
.
,
. "" ,
, . , . -
data
. = . data
A. d a t a ! :
B.size_array, -
, . ,
= ( ) ,
9. 333
(
9.4) .
- 9.4.
|35J:
-,
swap , -
. 9.5.
9.5.
. -
:
, .
assign (
9.6).
| 9.6. assign
Swap(temp);
return *this;
}
arrays array::assign(const double *begin, const double *end)
{ array temp(begin, end);
Swap(temp);
return *this;
i
, -
.
, .
9.7. .
i 9.7. - array
- "". -
, -
. ,
9.8, :
, .
| 9.8. ""
: -
, . . -
9. 335
BOB. , -
9.9.
.,
, , z
, -
.
STL
9,10.
9.10.
,
, -
: , ?
. 11.
""
Matrix, -
. -
. -
(. . 4).
336 II -
. -
, ,
array ,
.
( 9.11).
.11, Matrix
class Matrix
{ public:
Matrix (int n = l r int m = 1, double k = 0.0); //
-Matrix (); //
//
private:
double **data; //
int n; int ; //
};
//
Matrix: :Matrix (int n, int m, double k):n(n), m(m)
{ int i, j;
data = new double* [n];
for (i = 0; i < n; -t-t-i) data ] = new double [m] ;
for (i = 0; i < n; ++i>
for (j =0; j < n; ++ j } data[i] [ j ] = k; //
}
//
Matrix : : ~Matrix ( )
( for (int i = 0; i < n; ++i} delete [] data[ij;
delete [] data;
:
1. . -
array. array
Matrix.
2. ,
: m [ i , j]
.
"": array -
"" Matrix:
friend class array;
9. 337
"" , , -
. Matrix -
, friend.
; ) .
:
doubles operator!)(int i r int j) { return data[i][j]; )
double operator!)(int i, int j) const { return data[i][j]; }
array -
.
J
, , -
.
Matrix A(10, 12];
:
A ( i , j) = 12;
double a - ( 0 , 0 ) ;
-
[49]. :
struct Index { int , ; Index(int x, int ):(), !){} };
:
doubles operator[](Index i) ( return data[i.x][i.]; }
,
.
:
A[Index(0, 1) ] = 123;
, -
.
-
C++, - -
A I I H J ] . ,
array. 9.12
Matrix .
338 II. -
class Matrix
( public:
Matrix(int n = 1, int m = 1, double k = 0 . 0 ) ;
-Matrix();
arrays operator[](int i) { return data[i]; }
private:
array *data;
int n;
!
, .
Matrix ( ) -
, array. ,
"" [ ], -
"" :
int main ()
( Matrix A(10, 10);
for {int i = 0; i < 10; ++i) A [ i ] [ i ] = 1;
for (i - 0; i < 10; ++i)
{ for (int j = 0; j < 10; ++j) cout A [ i ] [ j ] ' '; cout endl; }
A [ 0 ] [ 1 ] = 123;
for ;i - 0; i < 10; ++i) cout A[0][i] ' '; cout endi;
return 0;
1
, ( !).
:
array ; // "" double[10]
Matrix M(1G, 10);
- ,
:
[9. . _ 339
// i-
// j-
array.
array
Matrix, array .
: ^include. -
array Array. cpp
, Matrix. -
Matrix :
^include; "Array. cpp"
. "" "-
" .
,
, -
. , -
. :
struct tt ( string fio; unsigned long tel; };
:
tt[100000] ;
. ,
. STL -
, -
. -
, . -
, .
, :
tt& operator [] (const string &s)
( for (int i = 0; i < 100000; ++i)
if ( t t [ i ] . f i o == s) return tt[i];
-
- . , -
, -
, , -
340 II. -
,
.
array, , -
, .
,
(. . 4), , -
. -
, .
] ,
.
. , -
, ,
, ,
, .
, . -
, ()
. , -
.
,
. ,
. , -
, .
:
struct Node
( item; Node *next; Node *prev; };
Node, :
Nodeiconst & ) : i t e m ( { ) ) {}
.
STL (STL ) -
"" , -
. ,
, .
9. 341
"", -
"" . "" -
. .
, -
Node. , , -
Deque. Node -
Deque, "" Node.
, 9.13.
I 9.13.
{ if ( ! isErnpty ( ) ) return Tail -> prev -> item; else return dummy; }
void push_front(const &a) ;
void push_back(const & ) ;
void pop_front() ;
void pop_back();
void Print();
:
Deque dummy,
: front
back, . , -
, ;
-
Deque.
;
"" .
( ^
Deque Borland C++ Builder 6 Visual C++ 7.
Visual C++ 6, Node -
.
.
, .
, . -
, :
teraplate<class T>
Deque<T>: : -Deque ()
( Node *delete_Node = Head;
for (Node *p = Head; p != T a i l ; )
{ p = p -> next; delete deleteJSTode; delete_Node = p; }
delete delete_Node;
}
"" ( -
9.14).
| 9.14. ""
template<class T>
void Deque<T>:.:push_front (const T &a)
1 Node * = new Node { ) ;
9. _ 343
Deque. -
"" ,
.
""
, : "" ""
(. . 4). [1],
, -
: .
, : -
. -
. ,
.
, ,
"" , . -
:
;
;
.
"-
" '( , smart pointers).
"" , , .
: -
.
9.15.
344 II. -
1
" " '
! 9.15. ""
return ^pointer;
T* operator-;-() const
return pointer;
private:
T -pointer;
, ""
. explicit, -
"-
".
, ,
.
, -
"" , :
SmartPtr<double> ip(C);
, 9.16,
.
I 9.16. ""
template<class T>
SmartPtr<T>::-SmartPtr()
{ if (!pointer) delete pointer; }
-
. ,
-,
9. _ __ _ 345
. *
, ->
( 9.17).
| 9. 17.
"" -
, "
:
if ( ! i p ) ...
. 9.18.
| 9.18.
. -
" " .
, -
(destructive copy), -
STL.
auto_ptr
STL "" ,
auto_ptr. [24] . , ,
, -
( 9.19).
: ....... "" .................... V, ......... -"" ..... * ....... ,.,. .............................. - .......... ..,.,-.,... .................. ..... .^
\ 9. 19. auto_ptr
{ pointer = rhs.pointer; //
rhs.pointer = 0; //
}
template <class T>
auto_ptr<T>& autojptr<T>: :operator= (auto_jptr<T> srhs).
{ if (this != &rhs)
( delete pointer; //
pointer = rhs.pointer; //
rhs.pointer = 0; //
}
return *this;
1
auto_ptr<T>::~auto_ptr()
i delete pointer; }
, -
. ,
. ,
. , -
"" -
.
.
[35|,
:
void f {)
( auto__ptr<T> p_t ( new T ) ; }
"", . .
auto_ptr, .
. ,
. -
, -
. auto_ptr
.
:
auto_ptr<T> f{)
{ return auto_ptr<T> pt ( new T); }
, "" ,
, : . -
-
.
10
,
, .
,
. , ,
. , . . 9
'
, -
-. , -
. -
-. -
- , .
, , :
,
- -. , -
, , -
.
,
. -
:
. , -
, -
- , -
, .
C++ , .
.
: throw (), try (), catch ()
348 II. -
. :
, ,
; ,
, "" .
.
, . . -
. - -
. ,
, , . -
, ,
. ,
, - ,
.
- , -
nporpaNfMHCTOM. , . . -
,
. - throw,
:
throw __;
"" __ -
, . :
throw ": !"; // 1
throw 13; / 7 2
throw s [ i ] ; /7 3 .
- , -
. ,
.
, :
throw 2*i f c value;
, ,
string s [ 2 3 4 ] ;
- -
. -
, -
.
--
, :
10. . 349
class ZeroArgument)};
ZeroArgument ex;
if (x > 0) double = l o g ( x ) ;
else throw ex;
, - -
:
throw new ZeroArgument{);
, , . . -
"".
( ^
- .
-
, -
.
try, -
"-" catch,
. try
,
:
try ( /* */ }
try
catch. "-" :
catch (__) { /* * / }
_ , -
, :
( ) // 1
{} // 2
(...) // 3
, -
, , ,
. -
"-" : , -
. -
. ,
350 . - ^
.
_ ( )
( ),
.
try ,
catch ,
catch. try -
, "-
". , -
. , catch -
, .
terminate.
catch -
throw .,
t r y . . .catch. ,
, -
. try...catch ,
try, catch:
try { // ,
try ( //
>
catchf...){ }
}
catch { //
try { //
}
catch{...){ }
-
. -
. , -
, , 1754. ,
, 1754, .
. -
-
10.1.
10. 351
10.1.
double f ( i n t k)
(try //
{ if (k > 1754) throw " > 1754!";
if (k < 0) throw " < '!";
if (k == 0) return 1;
else return k * f(k 1);
}
catch(const char *s) //
( cout ss endl; }
, .
cout f(5555) endl;
:
> 1754 !
.
Visual C++ 6 "-
" , "-
" I.#INF. , -
throw
. ? -
, -
. "-" , -
.
, -
, .
catch , .
. ""
main .
,
, . -
: , -
.
10.2.
! 10.2.
double f l ( i n t k)
{ if (k > 1754) throw " > 1754!";
352 //. -
int m a i n ( )
{ try 1 cout f (55555) endl; }
catch (const char 's)
{ cout ss endl; }
return 0;
}
,
.
-
. -
. ,
, -
. "-"
. , -
, -
.
-
. -
, .
( 10.3).
! 10.3.
struct ErrorTriangle
{ unsigned int a, b, c;
const char *message;
ErrorTriangle(unsigned int x, unsigned int y, unsigned int z,
const char *s): a(x), b(y), c[z), message(s) (}
return sqrt ( * ( - ) * ( - ] * ( - z) ) ;
}
else throw new ErrorTriangle(x, , z, "He !");
.
, -
. , -
. -
. , throw
-, . -
catch . -
10.4.
10.4. catch
int ma in ()
{ try { cout triangle(1, 1, 1) endl; )
catch {ErrorTriangle *e) //
{ cout e -> message ' ';
cout e -> a ';' e -> b ';' e -> endl;
return 0;
I
.
, -
.
, , :
void f l ( v o i d ) throw(const char *s, string;;
void f 2 ( v o i d ) throw IZeroDevide);
- ,
- zeroDevide.
:
void f I ( v o i d ) t h r o w ( ) ;
, .
,
.
12 . 1264
354 //. -
,
, -
. ,
, -
. ,
.
,
unexpected ( ) , terminate , , , -
abort!). .
, -
:
void F(void) //
unexpected ( ) :
set_unexpected(my_uriexpected);
terminated :
set_terminate (my__terminated) ;
-
.
uncaugirc_exfception(), -
true, , -
. -
.
,
, -
.
(. . 9) .
( array). -
-
, -
, .
, -
. :
class bad_Index {}; //
class bad_Range 1); //
class bad_Size {};
,
10. 355
, ,
oducthinner.
array, , -
, . -
10.5.
10.5. array
, , "" -
-, -
- array. ,
assign!) , . . -
.
bad_index, , :
double [] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
array (, + (sizeof(a) / sizeof (double) } ) ;
array G(B) ;
double aa;
int index = G.sizeO;
try { aa = product_inner (B, G) ;
mm: cout G [index] endl; // 1,
1
catch (array : :bad_Index *e)
{ cout "Index out of range!" endl;
index = 0;
goto mm;
1
array. ,
10. 357^
. 1 -
bad_index ( try -
), ,
0, try
. -
, , catch.
( }
Borland C++Builder 6
try, .
, , -
-
. -
, . -
, . -
.
, , -
(, ) .
.
, :
( catch ), -
;
( )
catch, -
;
, , -
,
" " (stack unwinding).
C++ , -
. , -
, . . ,
. -
: -
. C++, ,
. --
.
, 11, -
.
358 II. -
array
: bad_Range, bad_lndex bad_Size. -
.
array. -
. , ;
array_exception, /:
class array_exception {}; //
class bad__Range: public array_exceptioii { } ; // -
class bad__Index: public array_exception { } ; // -
class bad_Size : public array_exception { } ; // -
,
10.6.
10.6.
class exception { / / . . . };
class logic_error : public exception { / / . . . };
class domain_error : public logic_error ( / / . . . };
class invalid_argument : public logic_error { / / . . . };
class length_error : public logic_error { / / . . . } ;
class out_of_range : public logic__error { / / . . . | ;
class runtime_error : public exception { / / . . . } ;
class range_error : public runtime_error { / / . . . };
class overflow_error : public runtime_error { II...}
class underflow_error : public runtirr,e__error { / / . . . } ;
class bad_cast : public exception { //... };
class bad__alloc : public exception { / / . . . };
class bad_tipeid : public exception { II... } ;
class bad_exception : public exception ( II... } ;
class i s s _ b a s e : : f a i l u r e : public exception { II... };
.
. -
, exception. -
:
#include <stdexcept>
, try "--
" catch.
catch : ( try)
, .
catch .
10. 359
:
bad_aiicc, new
;
bad_cast bad_typeid, RTTI
(Run-Time Type Information! . . //);
runtime_error, - -
.
- what ,
Borland C++ Builder 6. 10.7.
! 10.7. what
^include <stciexcept>
ttinclude <iostream>
^include <string>
using namespace std;
void f ( ! f throw runtime_error("a runtime e r r o r " ) ; }
int main (}
{ string s;
try { s.replaceddd, 1, 1, ' c ' ) ; }
catch (const exceptions, e)
{ cout "Got an exception: " e.whatO endl; }
try { f () ; }
catch {const exceptions e)
{ cout "Got an exception: " e.whatO endl; }
return 0;
\
:
Got an exception: basic__string
Got an exception: a runtime error
-
replace ,
runtime_error f.
catch;
, -
- -
. -
. 11.
11
: -
. , -
, .
C++ , -
.
. C++
.
: , ,
.
- () -
(). - "" (
), - (
). C++ , -
.
.
i
.
, . -
, :
, .
,
.
:
class __: [_] _5__
{ }
362 II. -
- ( ) (
-) . -
-. -
, . -
.
: public ()
private (). - protected
(). ,
( ), ,
. -
. , -
: , -
(public).
, (private)
.
, , , .
" "
protected.
: , -
, .
,
- .
. 11.1 -
.
11.1.
-
struct class
11.1 ()
struct class
public private private private
protected private private private
private private
, ,
, . .
public, -
. , -
protected , private
.
. -
.
"":
- "" -, .
. [23] , "
- C++ -
: " ".
, Derived ()
Base (), ,
Derived Base. ! -
, , .
- ,
. Windows,
, .
, , ,
Derived. - ,
11.1, .
11.1- ""
class Clock { . . . ) ; //
class Alarm: public Clock {...}; //
void settime{Clock &c, time t); //
364 . -
, -
.
-
: -
, .
Base 11.2.
11.2.
class Base
( public:
Base () :a(-l) {)
void Print{} { cout a ';'; }
private: int a;
>'
class D01: Base
{ public:
void Print(){ cout b endl; }
void set(float b) { this -> b = b; }
private: float b;
};
class D02: Base
{ public:
void Print() { Base::Print(); cout a endl; }
void set(int a) { this -> a = a; }
private: int a;
I;
-
print , .
: DOI D02. DOI float
print ( ) . , set .
- D02 : -
, -
. ,
11. 365
- .
- -
, ::.
-
. D02 P r i n t ; ) ,
. -
void Print()
{ Print{); cout endl; }
. -
Print!) D02 :
, . Print DOI
DOI.
, , -
, .
.
,
. 11.2.
-
.
: , -
,
.
-, . -
, 11.3.
; 11.3.
;..'.:... ,.... ;; ._ .-. . .;...,u.i.^u.i
class Point2D
{ public:
// Point2D(): x(O.O), y{0.0){) // A
Point2D{double x, double y): x(x), y(y) {}
Point2D(const Point2D fit): x(t.x), y(t.y) (}
void Print() const { cout '<' x ',' >'
double getx() const { return x; }
double getyO const { return y; }
private:
double x, y;
366 It. -
f
class Point3D: public Point2D
{ public:
Point3D(double x, double y, double z) : Point2D(x, y) , z ( z ) U
double g e t z ( ) const ( return z; }
private:
double z;
i,
" " Point2D,
:
. " "
Pointso , -
-
. -
-:
Point3D(double x, double , double z) : z(z) { Point2D(x, ); }
, -
-. , . . ,
. (protected)
- ,
:
Point3D(double x, double y, double z):z(z)
( this -> x = x; this ~> = ; }
, , -
. , -
Point2D ;
Point3D b;
. -
( ), -
,
:
Point2D a; //
Point3D b; // -
. -
, -
. :
Point2D b(l, 2) ;
Point3D d(3, 4, 5) ;
Jf. 367
//
//
,
. -
"": ,
.
, .
"", ,
-
. , -
:
d.operator=(b);
, -
, ,
. ,
.
,
getxo g e t y O , ( ):
Point3D d ( l , 2, 3) ;
cout d . g e t x t ) ' , ' d . g e t y O ' , ' d.getz {) endl;
:
1 2, 3
, -
. -, , , -
;
. -, -
, -
. -, -
:
, . ( )
. ,
11.4.
: -"" " '"_ ' """
11.4.
class
{ public: Base () { cout "Base " endl; }
~Base() 1 cout "-Basel)" endl; )
368 !!. -
:
Base ()
Derived()
-Derived()
-Base()
, C++
" ". ,
, , (. 11.1).
,
:
class Clock { . . . ) ; //
class Alarm: private Clock { . . . } ; //
void settime(Clock &c, time t ) ; //
Clock ; // -
Alarm A; // -
settime(, t ) ; //
settime(A, t}; // -
, Alarm
Clock. , .
,
settime(reinterpret_cast<Clock &> () , t); //
, , -
.
static__cast "" ,
Alarm Clock "".
11. 369
D , ,
D . -
" "
. , -
, , , , .
:
class Date { . . . } ; //
class Address { . . . } ; //
class Person {
public: ...
private:
string name; //
Date birthday; //
Address address; //
iJ /.
, Person
Date, Addres string. He -
(. 9.11). ""
"" "".
,
, , , "
"? . 9 -
. C++,
STL . -
"" , . .
-
. [23],
,
. :
0 , ;
- -
, .
, , ? -
, 11.5.
1 11.5.
class GenegicStack
{ protected: // " "
GenericStack(} ;
-GenericStack() ;
370 //. -
- (GenericStack)
-,
. -, -
. ,
:
, -
. [23], "
, ,
".
.
C++ .
( ),
, -
, .
. ,
, 11.6,
Base print - Derived,
.
11. 371
11.6.
class Base
{ public:
Base(int x): a(x) {}
void Print()const { cout a ';'; }
private:
int a;
:
Base o b ( l ) , *pb; //
Derived(2, 2 . 0 ) , *pd; //
, :
,
, . , ,
,
.
"" ,
.
, -
:
pb = new Base{2); // ->
pb -> Print(); // Base::Print()
pb - new Derived(3, 3.0); // ->
pb -> Print(); // Base::Print()
, ,
(). , -
: .
Shape () Ellipse () Rectangle
(). , -
, ,
.
372 II. -
, -
:
((Derived *)pb) -> Print();
v
)
static__cast , . . "",
.
, . . -
,
.
-
-.
11.7.
! 11.7.
class Base
{ enum Object_type { , D }; //
public:
Base () : type () {} //
//...
private: II...
Object_type type; //
};
class Derived: public Base
( public:
Derived()( type = D; } //
:
void Print (const Base *po)
{ switch (po -> type)
{ case E a s e : : D : //
case Base::B: //
break;
11. 373
break -
, . . .
. -
,
. -
, .
--
,
.
-
, C++ -
-. -
, virtual,
.
print , 11.6:
virtual void Print() const //
{ cout a '; '; }
virtual void Printf) const //
{ Base::Print (); cout b ';'; }
virtual, -
.
pb = new (2); // ->
pb -> Print ; // : :Print (}
pb = new Derived{3, 3.0); // ->
pb -> Print(); // Derived::Print()
, : -
, -
.
: :
\
delete pb;
, -
: -
.
-
:
virtual -Base(){};
, -
.
374 II. -
.
1. .
2. - -
.
3. ; , .
4. , -
( -
) (
virtual ) -
.
5. .
6. ( ) -
.
, -
C++. [1] , C++
:
(compile-time polymorphism ~
),
;
(run-time polymorphism -
), .
, , -
.
RTTI dynamic_cast
,
: , .
-
, , -
, . C++ , .
-
RTTI (Run-Time Type Information -
).
'
dynamic_cast,
.
1
"" J37],
11. 375
, "", . -
(
)
static_cast reinterpret_cast. ,
. -
:
dynamic_cast<TMri*> ()
.
.
,
- - ,
.
( ), . , -
.
. [37], dynamic_cast<T*>(p) -
: ", -
, ?" dynamic_cast<T&> (r),
. , * , : ",
, ." , -
, -
. "" .
C++, -
, typeido.
, <typeinfo>. -
type_info,
. -
typeido ,
Borland C++
Builder 6. -
11.8
^include <iostream>
^include <typeinfoiop>
tinclude <conio.h> // getchf)
using std::cout;
using std::endl; -
class A //
{ virtual void f(void){},; } ;
class B: public A // -
{ virtual void f(void)O; };
376 II. -
void main () {
char ;
float X;
// type_info::operator^()
if (typeid(C) = = typeid(X))
cout " X ." endl;
else cout " X ." endl;
// : "double" < "int"
cout " " typeid(A).name(] ":"
(typeid(A).before(typeid{A)) ? true : false) endl;
A a, *pA;
b, *pB;
pA - a;
cout typeid!*pA).name(); //
pB = Sb;
cout typeid(*pB) .name () ; //
pA = Sb;
cout typeid("pA) .name () ,- //
getcn (}; //
: -
.
, . -
, , ,
. . -
.
,
. -
.
, ,
. : -
. -
.
, . (-
60 60 , -
). -
. , -
, ?
.
11, 377
-
. C++ -
. ,
.
. -
( ,
) -
().
-,
.
-
linker ().
,
. s i n ( x ) ,
,
, .
.
,
.
. , -
f. -
,
. -
,
, , , .
,
(
extern).
, , -
.
static. , C++ -
. -, -
static .
,
, . , static
private ( ). ,
, - ml.cpp 2.
378 //. - ^
( , , ,
):
// ml.cpp
void f1(void) {...); //
static void f2(void) //
// m2.cpp
fit); //
f2(); //
static -
, . , -
, (
static) ( ,
). ,
fi 2.
, ml.cpp:
// ml.cpp
void fl(void) {...}; //
static void f 2(void)(...}; //
fl(>; //
// m2.cpp
static void f1(void) {...}; //
fl(); //
f2(); //
// m3.cpp
static void f2(void) {...}; //
fit); //
f2(); //
, : static
,
main. "": Java
main,
. ,
" " :
main.
extern , -
. :
// ml.cpp
int = 1; //
void f(void); //
int main()
11. 379
( f(); // 1
= 2
f () ; // 2
return 0;
// 2.
extern int x; //
void f(void) //
{ cout endl; }
ml.cpp ,
f, 2..
(extern) -
( ). , -
extern , . .
extern . , static
.
. 2,
(). -
"".
. ,
, .
. , if
, .
. , ,
.
. ;
.
. , -
.
. , -
.
. C++ ,
.
,
.
380 II. -
static ,
. C++ -
, . -
C++ ( , -
, ). -
std.
.
-
, .
:
namespace
{ // }
namespace .
, , ,
, . . . [37, . 211],
.
-
, -
. , , /, -
.
( ) -
:
_::
. ,
, , ;
std::ccut
:
std::endl
, -
using:
using __::;
using!
using namespace _;
using , namespace, .
-
11. 381
.
, -
( )
. ,
.
, -
. . ,
, . ,
C++ ( ):
namespace DCS = Departament_of_Computer_Science;
i
.
,
"" .
std. ,
. [37, . 228], ""
:
// ml.cpp
namespace A {
void fl(void) {...};
int x;
I
// m2.cpp
namespace A (
void f2(void) {...};
int y;
class A{};
. ,
:
namespace A {
void fl(void) {...};
int x;
void f2(void) {...};
int ;
class A{};
}
: . .
, ,
.
382 _ !1. -
.
, -
, , , :
void A: : f l (void) {...};
.
:
namespace Outer { //
int i;
namespace Inner { //
void f() ( i++; } // Outer: :i
int i;
void g() { i++; } // Inner::!;
// namespace
namespace BI = BORLAHD_3OFTWARE_CORPORATION; //
// ,
namespace NBI =
BORLAND_SOFTWARE_CORPORATION: :NESTEDJ30RLAND_SOETWARE_CORPORATION;
,
.
,
, .
, -
: : . , -
-
, . .
. , -
, Windows (,
windef.h):
: :max (5, 6)
11. __ _ _ 383
static C++
() . -
, . .
. -
Borland C++ Borland 6 , -
Visual C++ 6.
1 1.9.
I 11.9.
// ml.cpp
ttinclude <iostream>
extern void func (void) ; //
namespace f //
float pi ^ 3.1415926F; // pi
)
int main ( ) f
// float pi = 0.1; // 1:
std::cout "pi = " pi std::endl;
func ( } ;
return 0;
I
/ / ml . cpp
ttinclude <iostream>
namespace ( // namespace
float pi = 10.0001F; // pi
void f(void) { //
std::cout "First func() called; pi = " pistd: : endl;
:
pi = 3.14159
First func () called; pi - 10.0001
Second funci) called; pi = 10.0001
ml.cpp , -
pi. main 1 , -
pi . ,
384 II. -
-
( pi ).
, -
.
2. ,
pi, .
2. f t ; . -
f u n c ( ) , -
f < ) . ml.cpp -
, .
ml.cpp f f ) , pi
2..
,
. , "-
", -
. (-
, ), --
/ .
ttindude. ,
#include "_"
.
C++, . . -
. .
:
^include <_>
include. -
, -
.
, -
,
. :
"" .
^define .
:
11. 385
// myfile.h
Hfndef MYFILE // "" ?
#define MYFILE //
//
#endif
,
^include "myfile.h"
MYFILE ("") .
. , "" -
. -
. ,
math.h Borland C++ 5 :
/* math.h
Definitions for the math floating point package.
*/
/*
* C/C++ Run Time Library - Version 8.0
*
* Copyright (c) 1987, 1997 by Borland International
* All Rights Reserved.
*
*/
/* $Revision: 8.10 S */
flifndef _MATH_H
ttdefine _MATH_H
, "" ___.
Visual C++ 6 :
/***
*math.h definitions and declarations for math library
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* This file contains constant definitions and external subroutine
* declarations for the math subroutine library.
* [ANSI/System V]
*
* [Public]
13 . 1264
386 . -
ttifndef _INC_MATH
#define INC MATH
"" _INC_MATH.
-
/ , , , ,
, , , -
, .
.
12
.
. -
C++ , .
,
. .
.
STL.
, C++ -
[38]. -
C++ . ,
10 , ,
, . [37]
. ,
C++: , -
- .
.
,
" ", -
" ".
. , -
.
-
,
385 It. -
. , -
[10] .
- .
.
. 4 ,
(. 4.18), -
(. . 7). , -
, -.
- , . -
C++
- . -
, -
:
(* )( );
int (*fp) < i n t ) ;
fp , int -
int. ,
int *fp(int);
-
int, int.
; .
. :
void fl(void){ }; //
void f2(void){ }; //
void f*pf)(void); //
pf = fl; // fl
pf = f2; // f2
, .
:
12. 359
void f I ( v o i d ) { }; //
void (*pf)(void) = f l ; //
:
(* )( );
:
( );
, pf :
(*pf) ;
:
pf ( ) ;
, , -
. -
, 12 (Visual C++ 6).
I 12.1.
void f ( i n t k)
f static void (*g)(int k) = f; //
if (k > 0) //
{ cout "call " k endl;
g(k-l); //
cout "return" k endl;
.
}
,
. f ( 3 )
:
call 3
call 2
call 1
return 1
return 2
return 3
( ) -
.
,
C++ .
390 II. -
, typedef, .
. :
float (*p[5])(float);
(Visual C++ 6), 12.2.
j.._:,........,,..,......
| 12.2.
, :
0.841471
0.540302
f
-
, typedef. -
typedef
, , :
typedef >(*<_>)(< >);
,
typedef float (*PF)(float);
PF, :
PF pi; //
PF 2[5]; // - float (float)
. -
typedef , int,
:
int(*function)(int *)
12. 391
, . typedef -
:
typedef int ('function)(int *};
function t (int);
_
typedef "
":
int (*t ( i n t ) ) (int*};
( ) function
typedef ,
. tt, -
i n t ( * f ) (int, int*)
(int*, int). , :
int (*tt(int*, i n t ) ) ( i n t , int*)
Visual C++ 6 .
, typedef,
. , ,
typedef, " " -
. ,
typedef . -
:
void f(void)
{ typedef int (*t)(int); } //
int main(void)
{ t a; } // t
, . . , -
. stdlib.h -
, :
, , . -
:
void qsort(void *base, //
size t num, //
size_t width, //
int (*compare)(const void *eleml, const void *elem2));
392 _ II. -
, ,
. -
void *. void C++ ,
.
( ),
.
1, -1 . . [37]
,
. :
struct User
{ char *name; //
int dept; //
}
User V[100] ;
-
:
int crr.pS (const void* m, const void* n)
{ return strcmp (static_cast<const User*> (m) ->name,
static_cast<const User*> (n) ->name) ;
:
int cmpD (const void* m, const void* n)
{ return (static_cast<const User*> (m) ->dept //
static_cast<const User*> (n) ->dept) ;
"" -1, , 1, , -
, . , , ,
: ( (User * ) m ) .
:
qsort ( (void *) V, 100, sizeof (CJser) , cmpS) ;
:
qsort ( (void *) V, 100, sizeof (User) , cmpD) ;
,
. . , -
- -
, C++ -
, .
12. 393
-
PF , -
f ( ) = [, ], :
double R(PF f, double a, double b, double epsilon)
, epsilon .
= - 1.5+ 1.2 [0, 2]
10~5 :
float f(float x) //
( return (** - 1.5* +1.2); }
int main(void)
( double result = R(f, 0.0, 2.0, 0.000001); }
- -
. , , :
double R(PF f - cos, double a = 0.0, double = 2.0,
double epsilon = 10e-5)
:
double = R ( ) ;
-
:
double R ( P F & f , double a, double b, double epsilon); //
double R ( P F *f, double a, double b, double epsilon); //
f (),
:
(*f}();
, -
, :
double R(const PF &f, double a, double b, double epsilon);
,
C++.
.
I. -
?
394 II. -
2. ?
3. " ", -
?
:
. .
.
, . -
.
, :
void f(double m[], int n)
-
. ,
, : NULL
. -
. , -
. , "-
" , ""
, . ,
"" . ,
Intel 4 ,
. , -
, Intel . -
-
. , 12.3,
Visual C++ 6, Borland C++ 5.
: ' " ' " ;
! 12.3.
{ double V [ 1 0 j ;
for (int i = 0; i < 10; ++i) V [ i ] = i;
for (i = 0 ; i < 10; ++i) cout V [ i ] ' ';
cout endl;
F ( V , 10, f l , f2, 0 ) ; // ""
for fi = 0; i < 10; --i) cout V [ i ] ' ';
return 0;
: -
1, .
;
0 1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18 20
F
:
double ** = (double * * ) & f
, 4 . ,
& , . .
, ;
HULL , , *
;
"" , -
. pf *p = (pf *)pp
-- ""
"" ;
"" "-
" (*) (, ) . , . . ""
-
;
"" , ""
.
, -
.
C++:
double ** = reinterpret_cast<double **>(sf);
pf *p = reinterpret_cast<pf *>(pp);
, -
,
396 _ II. -
. , , -
:
. ,
, -
.
.
, -
-.
va_. . .
:
enum typeFunc { typel = 1, type2 = 2, end = 0 };
, , :
void fl (double b[], int k, int n) //
( for (int i - 0; i < k; i++) b[i] += n; }
void f 2 (double b[], int k) //
{ for (int i = 0; i < k; i++) b[i] *= 2; }
,
. , -
,
. --
, .
12.4.
[ 12,4.
case type2: // 2
12. _ 397
( pf2 = va arg(p, p f 2 ) ; //
(, ) ; //
break;
va_end(p) ; // ""
}
,
"" . ,
:
(pfl (va_arg(p, pf 1) ) ) (a, n, 3);
:
typeFunc tp[3] = { typel, type2, end } ; //
:
ff(V, 10, tp, f l , f2); //
NULL , . . -
.
int main (void)
( double V[10]; //
for (int i - 0; i < 10; ++i)
V[i] = i; //
for (i =0; i < 10; ++i)
cout V[i] ' '; //
cout endl;
typeFunc tp[3] = ( typel, type2, end } ; //
ff(V, 10, tp, "fl, f2); //
for (i = 0; i < 10; ++i)
cout V[i] ' '; // -
cout endl;
return 0;
"
:
0 1 2 3 4 5 6 7 8 9
6 8 10 12 14 16 18 20 22 24
-
,
.
395 II. -
.
-,
typedef , -
, . -
, " ":
" ". [35]
: , ,
" " .
-
. "-
" [47J.
: , , -
.
.
-
, . -
, , .
. .
, ,
, . -
: "-
" .
, -
, .
, -
" ", , -
(. . 1). . 12.1.
4 : "", " ", "" "".
"". -
, " ". -
, -
, -
"". ""
, - .
( , )
: -
.
-. 12.5 (
^include namespace). -
12. 399
, " "
.
. 12.1. ,
12.5.
int main(void)
( string s = "il23456789"; //
enum States //
{ Start,
NextLetter, //
Error, //
Finish
1;
States x = Start; //
int i = 0;
while((x != Finish) && (x != Error)) //
{ switch(x) //
{ case Start:
if (isLetter(s[i]JJ{ i++; x = NextLetter; }
else x = Error;
break;
case NextLetter:
if (isNextLetter(s[ij)) { i++; x - NextLetter; }
else x = Finish;
break;
case Error:
cout s " !" end!;
break;
400 _ //. -
cout s endl;
return 0;
isLetter isNextLetter , -
.
( ), -
, .
,
" ", .
-, typedef, -
: " " -
:
typedef function (*function)
C++ "" -
:
typedef void !*fp)(const char ch);
typedef fp (*States)(const char ch) ;
. typedef,
.
- -. -
return -
. , -
12.6.
I 12.6.
:
return - "" -
, -
" ";
"" x - (states)x(s[i++]),
;
, , NULL
Finish Error. Finish
, . ,
"" (, , -
) , ,
.
-
. , ,
"" . ,
switch ( , )
. -
( )
-, .
(. 12.6) .
. -
. , ,
. -
, " "
.
-
, -
C++. , -
. ,
402 II. -
. 5,
-, .
.
- ,
( ) . , -
, . -
( ) .
-
, . ,
( )
. - -
.
(. 5.16) - -
( - 12.7).
I 12.7. -
; .'.... ;
struct isPrime (
bool operator() (int number) const
{ number = abs(number);
if (number == 0 I | number == 1) return false;
int divisor;
for (divisor = number/2; number%divisor != 0; divisor);
return divisor == 1;
}
- :
list<int> L;
list<int>::iterator pos; //
// 20 40
for (int i - 20; i <= 40; ++i) L.push_back(i);
pos = find_if (L.beginO, L.endO, isPrimeO); //
if (pos != L . e n d O ) cout *pos " " endl;
else cout " " endl;
//
cout count__if (L.beginO, L . e n d O , isPrimeO) endl;
-
. - -
.
-
, -
12. _ 403
.
,
.
,
. -
, -
(. 11.5).
[1, 34, 49].
,
.
, , -
.
, , -
"- -
".
" " ,
. -
, , .
-
(, , -
).
Person (. . " " . II).
class Person (
public : . . .
private :
string name; //
Date date; //
Address address; //
int dep; //
int children; //
bool sex; //
:
vector<Person> V(100) ;
-,
, , . -
, -: ,
, . -
Person, .
- ,
. lessthan ( 12.8).
404 II. -
12.8. lessthan
(equaisto ,
greaterthen )
.
, -
, STL
, -
.
- :
template <class T>
vector <Person> filter{vector <Person> &v, lessthan<T>(x));
.
, -
. , -
, - -
. ,
: ,
, , .
12.9.
12.9. -
- :
template <class , class Cornp >
vector<Person> filter (const vector<Person> &w, Compares<T, Comp>(x))
:
vector<Person> {10);
bb = filter(bb, Compares<int>(3));
:
bb - filter{, Compares<Date, lessthen<Date> >(Date(22, 02, 1953)));
- , -
. STL : unary_function
binary_function. " ",
. -
. 12.10
.
I 12.10. -
#include <functional>
template <class Arg, class Result>
struct unary_function {
typeclef Arg argument_type;
typedef Result result__type;
};
template <class Argl, class Arg2, class Result>
struct binary_function {
typedef Argl first__argument_type;
typedef Arg2 second argument type;
i
typedef Result result_type;
};
template <class _Tp>
struct equal_to : public binary_function<_Tp,_Tp, bool>
{ bool operator()(const _Tp& x, const _Tp& y! const f return x == y; }
1;
template <class _Tp>
struct plus : public binary_function<_Tp,_Tp,_Tp>
{ _Tp operator () (const _Tp& x, const _Tp& y) const { return x + y; }
406 _ . -
, -
: , . -
,
,
( ). -
, . -
Borland C++ Builder ,
12.11 .
; 12.11. -
template<class Arg>
class factorial : public unary_function<Arg, Arg>
{ public:
Arg operator () (const Arg6 arg)
{ Arg a = 1;
for (Arg i = 2; i <= arg; i++) a *= i;
return a;
-, . -
, , :
int init[7] - { 1, 2, 3, 4, "5, , 7 };
deque<int> d(init, init + 7) ; // -
vector<int> v(7); // -
transform(d.begint) , d.endt ) , v. begin (} , factorial<int> f) ) ;
transform .
-
-: (). -
12, 407
, -
.
-, , -
. ,
. ,
100 - v -
(. 12.10) find_if. -
:
bind2nd(equal_to<int>, 100)
.
- , -
!.
,
. : -
.
:
! ( < 100)
:
notl(bind2nd(les5<int>, 100))
r.ot2.
-
, - -
-~ [10]
.
- (. . 9), -
, . ""
[10], 23 . -
, -
-
. -
-
.
, singleton (""). "-
" , -
408 //. -
,
. . -
, -
. Windows
. .
"" ,
. , -
, ,
. -
.
. ""
12,12.
; """' : ' - ,"". ; -' :"<
| 12.12. singleton
class Singleton
{ public:
static Singleton * Instance(); //
private:
Singleton(); //
Singleton(const Singleton &sg) ;
Singletons operator^(const Singleton &sg) ; //
-Singleton ); //
static Singleton * instance^;
};
Singleton * Singleton::instance_= 0; //
Singleton * Singleton::Instance!)
{ if (instance^ == 0) instance_ = new Sigleton();
return instance ;
-. -
singleton
, -:
Singleton *p = Instance ;
- -
.
Adapter,
. -
.
12. ^ _ 409
, -
, ,
. -
. . 9 -.
-
: .
, .
-
. -
, ,
. C++ :
class Adapter: public Queue, private Deque
, ( -
) "" .
" ", .
, :
virtual void f (void) = 0;
,
, . -
,
.
, -
, " "
. " ", -
.
. -
,
. ,
shape (),
-.
window (), -
.
.
,
"" . -
[4], -
Windows.
410 II. -
, -
, .
, .
- 12.13.
| 12.13.
1994 , ,
(Erwin Unruh)
. , -
, . -
-
, -
.
:
, 2 . ,
, ,
:
double pow2(const int n)
{ double n2 = 1;
for (int i = 1; i < n+1; ++i) n2 *= 2;
return n2;
12. 411
:
cout pow2(0) endl;
cout pow2(10) endl;
. ,
, -
.
, .
,
,
, - , . -
.
, -
.
: -
, .
"" , ,
, "" . -
, "-
"
, .
,
. , -
, .
:
. -
. ,
, ,
.
, - -
. : -
. C++ . , -
- (
).
-
. :
2= 1
2" = 2 2" - 1
, -
. , -
, , " ".
12.14.
412 -
\ 12.14.
tfifndef POW2_HPP
#define POW2_HPP
// 2 N
ternplate<int N>
class Pow2 {
public: enuiti { result = 2 * Pow2<N-l>: : result };
};
//
templateo
class Pow2<0> {
public: enum { result = _ 1 };
};
ttendif // POW2_HPP
, -
. , :
^include <iostream>
#include "pow2.hpp" //
int main ()
{ std::cout "Pow2<10>:iresult = " Pow2<10>::result '\n'; }
. -
. ,
. , , ,
.
, C++
. ,
. -
, -
, 231
.
(. . 1).
(
). , ,
:
template<int n>
struct Factorial {
static int const value = n * Factorial<n-l>::value;
i
1;
12. 413_
ternplateo
struct Factorial<0> {
static int const result value = 1;
Borland C++ Builder 6, . .
, , -
.
, -
, , " ( -
12.15).
12.15. "
"" () :
cout "Pow<3, 0>::result = " Pcw<3, 0>::result '\n';
cout "Pow<5, 3>::result = " Pow<5, 3>::result '\n';
:
.
:
template <typenaine T>
inline product (int dim, T* , * )
{ result = 0;
for (int i = 0; i < dim; ++i)
result += a[i] * b[i];
return result;
}
,
, :
4t4 II. -
int main()
( double a [ 3 ] = { 1, 2, 3 };
double b [ 3 ] - { 5, 6, 7 };
cout "product(3, a, b) = " product(3, a, b) endl;
cout '"product (3, a, a) = " product(3, a, a) endl;
}
,
. ,
:
a[0]*b[0] + a[l]*b[l] + [2]*[2]
12.16.
I 12.16.
//
template <int DIM, typename T>
class Product {
public:
static result (* , * b) {
return *a * *b + Product<DIM~l, T>::result(a+1, b+1);
//
template <typename >
class DotProduct<l, T> {
public:
static result (* , * b) ( return *a * *b; }
};
// "" Product
template <int DIM, typename T>
inline product (* , * b)
{ return Product<DIM, T>::result(, ; }
, -
. -"" -
, . -
-"" :
cout "product<3>(a, b) = " product<3>(a, b) endl;
cout "product<3>(a, a) = " product<3>(a, a) endl;
,
.
III
Windows
13. Windows
14.
13
Windows
, -
. -
.
CharToOem, . "-
" Microsoft (-.)
Application Programming Interface. -
" ", ,
, . -
API. API Windows
WinAPJ Win32 API. 32 ,
, 32- 16--
. , Win64
[33, 43].
WinAPI 2000 ,
, . ""
.
, -
Windows. .
,
. , -
, , , -
. -
Microsoft , -
,
Windows.
143. 1264
418 lit Windows
"" include , -
,
win. ,
, . Microsoft
typedef #define. -
. Windows
C++. , -
Pascal, .
C++,
, .
IBM PC, -
Windows.
. , , windef.h -
: 1
, . -
Windows : ,
. :
() ,
(). , sz ,
(string terminated by zero); i
(integer); (pointer) .
. ,
:
typedef FLOAT *PFLOAT;
typedef ULONG *PULONG;
typedef USHORT *PUSHORT;
typedef UCHAR *PUCHAR;
typedef char *PSZ;
13. Windows . 419
, L
( . 4.8).
, ,
L long "". Win32 API -
"".
win -
:
typedef UINT WPARAM;
typedef LONG LPARAM;
typedef LONG LRESULT;
.
, , API,
. -
, ttdefine.
,
, . (-
) , ,
,
. -
. 13.1.
13.1.
(char); (const)
by (byte)
i
, ( )
, , "" "" (count)
b f (bcol); "f" "" (flag)
w (word)
I (long)
(unsigned)
dw (dword)
fn
420 III. Windows
13.1 ()
s
sz ,
h (handle)
(pointer)
, "". ,
IpSzFileName , 6, ,
; fdwAccess , -
, ( -).
h,
handle (, ). "-
". [431 ( .
descriptor). , -
, "". ""
, -
, .
, , "".
, Windows "".
Windows.
Unicode
, ,
, .
, Microsoft ASCII.
-
char. C++ (
Intel 1 ).
, Windows, -
, . . Windows
.
, -
. -
, -
Unicode. Intel Unicode
2 . wcharj;, -
"" .
13. Windows 421
Borland C++ Builder 6 , wchar_t
, int. , "-
" , .
, "" , -
L:
wchar_t *ws = " !";
wcs,
str. :
char *strcat(char *dest, const char *src);
wchar_t *wcscat(wchar_t *dest, const wchar_t *src);
, , .
,
cout ws endl;
, ( ).
Windows WinAPI.
WinAPI ( char
CHAR) "" ( WCHAR,
wtypes.h). Windows "" Unicode.
Microsoft WinAPI
TCHAR ( tchar.h),
, , , -
, . -
.
,
. -
. , -
, , .
,
.
1. TCHAR, LPTSTR,
LPCT3TR.
2. ( -
):
(" ");
_(" ");
TEXT(" ").
422 ///. Windows
3.
sizeof(TCHAR).
4. <windows.h> <tchar.h>.
-
.
5. <windows.h>
: ^define UNICODE #define _UNICODE.
-
:
1. .
2. .
3. , .
4. .
, , -
13.1.
13.1. Unicode
//
PrintMsg(hStdOut, CharUpper(_T (" : \") ) ) ;
//
TCHAR s t r l [ 3 ] ; _ i t o t ( k , strl, 10); s t r l [ 2 ] = 0;
for (int i = 0; i <= 10; i++l //
{ TCHAR str2[3]; _itot(i, str2, 10); s t r 2 [ 2 ] = 0;
int d = k * i; //
TCHAR s t r 3 [ 3 ] ; _itot(d, str3, 10); s t r 3 [ 2 ] = 0;
TCHAR *st = new TCHAR [25]; //
for (int j = 0; j < 25; ++ j ) //
at[i] - _ T ( " \ 0 " ) ;
//
st = wcscatfst, str2);
st = wcscatfst, _T("*"));
st = wcscat{st, strl);
st = wcscatfst, T("="));
st = wcscat(st, str3) ;
st = wcscatfst, _T("\n"));
PrintMsgfhStdOut, st) ; //
)
getch ( ) ; //
return 0;
-"", -
. Windows CONINS
CONOUTS . -
. ,
Windows, .
"" -
, ,
,
.
, , -
"" .
,
.
, , , -
. , -
.
, -
13.2.
424 III. Windows
\ 13.2.
Printstring -
, v a _ . . .
writeConcoie, -
"" , , -
Win API, -
.
5*7=35
6*7=42
7*7=49
8*7=56
9*7=63
. 13.1.
13. Windows 425
.
. 13.1.
,
.
Borland C++ Builder 6 MS SDK Help Files
Microsoft MSDN, .
Windows ,
"" .
, ,
, . -
" " . -
" ", -
WinAPI. , -
. "" , ""
.
Windows.
""
""
: ,
. .
-
, .
main, . -
winMain, -
.
1. .
2. .
3. .
4. .
( ) , "
.
13.3. ,
.
426 III. Windows
13.3.
return msg.wParam;
:
, -
: -
"" .
:
Regciass (
). ( 13.4) -
, , ,
.
,
,
.
13. Windows 427
13.4.
.
we. style = CS_HREDRAW I CS_VREDRAW;
,
- (CS_HREDRAW) (CS_VREDRAW)
. ,
.
, -
. WndProc.
, ( ).
.
. ,
, . .
.
( ).
, , -
. - , ( -
ico) ( cur) -
. ,
.
428 III. Windows
-
.
, , -
API RegisterCiass. -
, ,
.
. "" ,
.
, ,
(WS_OVERLAPPEDWINDOWIWS_VISIBLE), -
( ).
. -
, showwindow.
, .
Updatewindow. -
"" .
,
, ,
. -
13.5.
! 13.5. ( )
return 0;
//
return DefWindowProc (hwnd, msg, wParam, IPararn)
, --,
. -
. WM.
MSG, -
.
.
, (. 13.3) -
Windows
, , -
. " -
, -
Windows , -
.
MessageBox{)
. -
, .
, .
, .
. 13.2.
;
! |
WM_DE5TROY
^)
^
i OK ;j
. 13.2.
, ,
, ,
. 13.3.
, -
. (
Windows), , -
.
430 III. Windows
. 13.3. ""
, Windows, -
: , , -
, , , -
. ,
, . . ,
. -, , -
.
, (-
13.1),
, . -
10 . Windows
,
. , -
, , -
.
:
1 10 -
. ,
.
,
.
;
( ,
) . -
;
13. Windows 431
-
;
-
.
. 13.4.
1 3
xZ 7
4 9 _
5
9
. 13.4.
-
, -
, -: -
WinAPI.
Windows, ,
.
Windows .
""
, .
,
, ( ).
Windows ,
.
,
.
"" .
: ?.
-
.
. -
: no-,
432 III. Windows
. :
.
no-
.
-
, .
.
ftdefine:
#dsfine IDM_MULT_1 1001
tfdefine IDM_MULT_2 1002
#define IDM_EXIT 1003
ttdefine IDM_ABOUT 1004
,
.
"": "" -
, "" :
static HMENU hMenu, hFirstMenu, hAbout;
, (, "")
,
.
WM_CREATE,
(. .5) , 13.6.
| 13.6.
hMenu = CreateMenu(); //
hFirstMenu = CreatePopupMenu(}; //
AppendMenu(hFirstMenu, MF^STRING, IDM_MOLT_1, " no-");
AppendMenu (hFirstMenu, MF_3TRINGr IDM__MULT_2, " ") ;
AppendMenu (hFirstMenu, MF__SEPARATOR, 0, NULL); // -
AppendMenu(hFirstMenu, MF_STRING, IDM_EXIT, "");
//
AppendMenu (hMenu, MF__POPUP, (UINT) hFirstMenu, "");
hAbout = CreatePopupMenu(); //
AppendMenu(hAbout, MF_STRING, IDM_ABOUT, " ");
//
AppendMenu(hMenu, MF_POPUP, (UINT)hAbout, "?");
SetMenulhwnd, hMenu); //
DrawMenuBar(hwnd); //
13. Windows 433
, , "" -
, Windows.
, -
WM_CREATE . ,
,
Createwindow.
. -
"". . -
, , -
. ( ) -
, .
13-7.
1 13.7.
struct OneButton {
long style; //
char 'text; //
,
GroupFlags. -:
ttdefine ID_ALL 4010
, ,
40004009. , -
-"":
static HWND hGroupFlags;
static HWND hflags[NN];
434 III Windows
, -
:
. . -
:
HOC hdc; // -
TEXTMETRIC tm; //
hdc = GetDC(hwnd); // ""
//
SelectObject(hdc, GetStockObject(SYSTEM_FONT)};
GetTextMetrics(hdc, stm); //
cxChar = tm.traAveCharWidth; //
cyChar = tm.tmHeight; //
ex = tm.tmAveCharWidth;
//
= tm.tmHeight + tm.tmExternalLeading;
//
SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
ReleaseDC(hwnd, hdc); //
, ,
( 13.8). ,
.
; 13.8.
; '....: '. ', .' : ': .:::: ."...!
//
hGxoupFlags = CreateWindow)
"button", GroupFlags.text,
WS_CHILDIWS_VISIBLEIGroupFlags.style,
cxChar, cyChar,
21 * cxChar, 7 * cyChar + 5,
hwnd, 0, hlnst, NULL);
//
cxChar = tm.tmAveCharWidth + 5;
cyChar = tm.tmHeight * 2;
for (i = 0; i < NN/2; i++)
hflags[i] = CreateWindow(
"button", flags[i].text,
WS_CHILD|WS_VISIBLE|flags[i].style,
cxChar, cyChar + tm.tmHeight*!,
60, cyChar/2f
hwnd, () (4000 + i), hlnst, NULL);
13. Windows 435
II
cxChar = tra.tmAveGharWidth + 85;
cyChar = tm.tmHeight * 2;
for (i = 5; i < NN; i++)
hflagsfi] =? CreateWindow(
"button", flags[i].text,
WS_CHILD|WS_VISIBLE|flags[i].style,
cxChar, cyChar + tm.tmHeight*(i - 5),
60, cyChar/2,
hwnd,(HMENU)(4000 + i), hlnst, NULL);
//
static HWND hLabel;
cxChar = tm.tmAveCharWidth + 160;
cyChar = tm.tmHeight;
hLabel = CreateWindow(
.
"static",. " ",
WS_CHILD|WS_VISIBLE,
cxChar, cyChar,
160, cyChar + tm.tmHeight/2,
hwnd,0,hlnst,NULL);
//
static HWND hButton;
cxChar = tm.tmAveCharWidth + 160;
cyChar = tm.tmHeight*6 -f 5;
hButton = CreateWindow(
"button", button.text,
WS_CHILD[WS_VISIBLE|button.style,
cxChar, cyChar,
160, 2 * tm.tmHeight,
hwnd,(HMENU)ID BUTTON, hlnst, NULL);
, .
"" -
"" .
( static). 1 -
- ID_BUTTON:
tdefine ID_BUTTQN 2000
: -
, - -"":
const char *szList[10] - "", "", "",
"", "", "",
"", " ", "", ""
436 III. Windows
-
:
cxChar = trrutmAveCharWidth + 160;
cyChar 2 * tin. tmHeight;
hListBox = CreateWindow(
"listbox", NULL,
WS_CHILD!WS_VISIBLSIWS_VSCROLL|LBS_NGTIFY|WS_BORDER,
cxChar, cyChar,
160, 5 * tm.tmHeight,
hwnd,(HMENU)ID_LISTBOX,hInst,NULL);
for {i = C; i < 10; ++i] //
SendMessage(hListBox, LB_ADDSTRIKG, 0, (LPARAM) szList[i]);
:
SendMessage(hListBox, LB_SETCURSEL,0,0);
WM^CREATE
:
return 0;
(. 13.5) -
wM__coiAMD. , -
. ""
-,
-. -
wParam.
.
13.9.
13.9.
WM_COMMAND:
switch(LOWORD(wParam)i
{ case IDM_EXIT: //
SendMessage(hwnd, WM_CLOSE, 0, OL) ;
return 0;
case IDM_MULT_1: // no-
SendMessage(hflags[10], BM SETCHECK, FALSE, OL) ;
13. Windows 437
return 0;
case ID__ALL: // ""
case IDM_MULT_2: //
// ?
if (!SendMessage(hflags[10], BM_GETCHECK, 0, OL))
{ SendMessage(hflags[10], BM_SETCKECK, TRUE, OL) ;
for (i = 0; i < 10; ++i) .//
{ SendMessage(hflags[i], BK_SETCHECK, TRUE, OL) ;
EnableWindow(hflags[i], FALSE); //
m2[i] = 1 + 1 ;
I
else
{ SendMessage(hflags[10], BM_SETCHECK, FALSE, OL!;
for {i = 0; i < 10; ++i) //
{ SendMessage(hflags[i], BM_SETCHECK, FALSE, OL) ;
EnableWindow(hflags[i TRUE); //
m2[i] = result[i] = 0, //
I
return 0;
case IDM_ABOUT: // " "
if(!IsWindow(hAbout)) //
hAbout = CreateWindowt
szClassAbout, " ",
WS_POPUPWIOW|WS_VISIBLE|WS_CAPTION,
CW__USEDEFAULT, CW_USEDEFAULT,
strlen(" ") * ex * 1.5
hwnd, 0, hlnst, MULL);
return 0;
case ID_BUTTON: //
for (i = 0; i < 10; ++i)
{ r e s u l t [ i ] = ml * m 2 [ i ] if(result[i]) All = true; }
if (All) //
{ if(!IsWindow(hMulti)) //
hMulti = CreateWindow(szClassMulti,
" ",
WS POPUPWINDOWIWS VISIBLEIWS CAPTION,
438 III. Windows
CW_USEDEFAULT,CW_USEDEFAULT,
strlenf" ") * ex * 1.5, * 12,
hwnd, 0, hlnst, NULL);
}
else
MessageBox(
hwnd, " He !",
"",
MB__OK|MB_ICONERROR) ;
return 0;
case 4000:case 4001:case 4002: //
case 4Q03:case 4004:case 4005:
case 4006:case 4007:case 4008:case 4009:
i = LOWORD{wPararn) - 4000; m2[i] = i + 1;
if(!SendMessage(hflags[i], BM_GETCHECK, 0, OL))
SendMessage{hflags[i], BM_SETCHECK, TRUE, OL) ;
else
SendMessage(hflags[i], BM_3ETCHECK, FALSE, OL) ;
return 0;
case ID_LISTBGX: //
if(HIWORD(wParam) == LBN_SELCHANGE)
{ ml - SendMessage(hListBox, LB_GETCURSEL, 0, 0) + 1; }
return 0;
J
, , -
, , -
, "". -
SendMessage, "",
- , -
. , :
SendMessage(hflags[10], BM_GETCHECK, O f OL)
:
SendMessage(hflags[ij, BM_SETCHECK, TRUE, OL) ;
EnableWindow{hflags[i], FALSE);
ID_ABOUT -
.
(popupwindow) . -
, . -
. "", -
:
13. Windows 439
.
WM_PAINT.
. , -
,
WinAPI, - Graphics Device Interface (GDI) -
. 13.10
.
i 13.10.
BeginPaint ,
GetciientRect ,
, ,
SetBkColor.
DrawText,
, .
' \ ' . -
.
EndPaint ( ) .
. 13.5.
440 III. Windows
. 13.5.
, ,
. ,
. ,
13.11, WM_PAINT.
13.11.
.
-
73. Windows 441
, / stdio.h.
"" .
, .
,
(. 13.6), (. 13.7).
[= F< \
1 X 7= 7
i
3x8 --*
2 7= 5 8
7 = 21 7 8 = 56
4 7-28 8 8 = 4
5 7 = 35 8 = 80
7 = 42
7 7 = 49
8 7 = 56
9 7 = 63
7 = 70
. 13.6. . 13.7.
,
, .
, -
Windows. WinAPI?
, WinAPI
. ,
. -
Windows,
, -
. , -
. , -
-
.
( ) -
:
we. hbrBackground = (HBRUSH) (COLOR_BTNFACE - 1) ;
, -
. "" , , .
.
" Windows 95"
"HelloWin", " ".
44g III. Windows
-, , -
. -,
. , "" "": -
, , .
-
.
"".
-, , "" - -
- , MFC VCL -
.
"",
WmA.Pl
14
. Windows
WinAPI -
. "" .
, Borland
, -
. Pascal Delphi, a
C++ C++ Biulder. -
- VCL (Visual Compo-
nent Library), , ,
. -
. -
,
.
1
C++ Builder. ( -
)
. .
(<Ctrl>+<F9>) (<F9>) -
, "" ,
"" Windows: , -
.
WinAPI , -
. , ,
VCL ,
.
444 III. Windows
2
( )
.
(. 14.1).
Standard j Additional {. Win32 1 Susteml DatnAceea! DataCortiolH dbExwess] DataSnaol BDE 1-lLL
A 35
I l* ^j i* :~
. 14.1.
GroupBox ( -
, ) Button ()
Standard.
11 checkBox
(),
( ).
.
,
<Shift>. -
.
,
. 14.2.
. &i . -inui
.- GioupBoxV " : GroupBon2 "
:;
CheckBoxl CheckBox2 ' iCornboBuKl C
. . ~-5 ;
' CheckBox3 Check8ox4
-', CheckBon5 CheckBonG
1 CheckBox? CheckBoxB
! OieckBox9 CheckBoxlO
CheckBonll Bultonl
. 14.2.
. -
(Design Mode) Object Inspector,
, 14.3.
.
14. 445
Properties
AJigi albione
AlphaBlend .false
Alpha8tendVak255
S Anchors
AuioScrotl
AutoS'i
BiDiMode
BorderStyle
j Boide Width
j Caption
-
. , ,
, . -
. , -
, , . -
, .
.
.
, caption (. )
. .
:
Font , ;
Align ;
TabOrder
<>;
Tabstop <>;
Hint , ;
G Enabled / ;
visible / ;
G Left, Top, width, Height -
.
, ,
(Name). , -
. , -
checkboxi , -
TCheckBOK* checkboxi.
446 III. Windows
Tag ( ).
,
, .
, -
. ,
Checked, true, , false,
.
ComboBox items. ,
. .
.
, Text itemindex,
() .
, .
, .
, . 14.4.
"*""""
|-1 '""'1 - "'
1
! 1 2 (;,; ( -
> '.
" 4
5* 6
7 8
,
| : '.
. 14.4.
. TMainMenu
,
. 14.5.
. -
:
?, .
:
-
;
;
.
14. 447
AIM-F4
. 14.5.
, & caption
"". ,
, <>+<>.
" " (
shortcut). Radioitem
().
(. . 14.5) : -
, . . -
: , -
-
, .
, /
/. -
, : -
. ,
.
Events
Object Inspector, . 14.6.
onclick,
. , -
Object Inspector
.
- (. 14.7).
448 III. Windows
^-- ; ^i
:
|cb_all - _^J
Properties Events j
I Action JTJjt.
I OnClick cb_alOck
[ QnContextPopi
!
i OnDtsgDrap
i DnDtagOver
\ OnEndDQck
DnE ndDfag
: DnEnter
. ,; OnEnit
. OnKeyDown .
' OnKeyPress ^J
All shown
,
.
Main.cpp
. 14.7.
, ,
.
N2->Checked = !(cb_all->Checked);
H3->Checked = cb all->Checked;
14. 449
N2 , N3 .
onclick ,
,
cb_aii->checked.
, cb_aii, -
, checkBox
. " ", 14.1.
14.1. / (1)
if (cb_all->Checked! !
cbl->Checked = true;
cbl->Enabled = false;
cb2->Chec:<ed = true;
cb2->Enabled = false;
cb3->Checked = true;
cb3->Enabled = false;
cb4->Checked = true;
cb4->Enabled = f a l s e ;
cb5->Checked = true;
cb5->Enabled = false;
cb6->Checked = true;
cb6->Enabled = false;
cb7->Checked = true;
cb7->Enabled = false;
cb8->Checked = true;
cb8->Enabled = false;
cb9->Checked = true;
cb9->Enabled = false;
cblO->Checked = true;
cblO->Enabled = false;
}
else {
cbl->Enabled = true;
cb2->Enab!ed = true;
cb3->Enablea = true;
cb4->Enabled = true;
cb5->Enabled = true;
cb6->Enabled = true;
cb7->Enabled - true;
cb8->Enabled - true;
cb9->Enabled - true;
cblO->Enabled = true;
I
450 _ III. Windows
, 14.2.
'!. ..................... ................... ..... - ............. .......... - ........................................................................ ....... ...................... ...... . .......... ......... ; ............. ..............
I 14.2. / ( 2)
if (cb_all->Checked) {
for (int i 1; i < 11; i++) {
TCheckBox* cb_control =
(TCheckBox*) freMain->FindCornponent ("cb" + InrToStr (i) ) ;
cb_control->Checked = true;
cbcontrol->Enabled = false;
else {
for (int i = 1; i < 11; i++) {
TCheckBox* cb_control =
(TCheckBox"] FindComponent ("cb" + IntToStr ( i ] ) ;
cbcontrol->Enabled = true;
FindComponent ( )
. , -
TComponent* ( ). -
, , cbx, -
1 10.
,
-:
cb_all->Checked = false;
cb_all->Checked = true;
.
,
. 14.8.
4
. File { New j
New Form. , ""
Label . -
, . 14.9.
14. 451
' .*tt*l
'- 17 ::
5- . - 1> '
17 '-I- -P ;'.
17
. 14.8.
; ; ;
. -
! ','.-"-'.
" ".
', - -
"" .
Copyright - !iic,2t)Q3
01
. 14.9.
, h, cpp
( ) dfm (-). h- -
. ? -
:
fmAbout->ShowModal();
:
, .
closed.
.
5
: Grid
(, ) . . 14.10.
452 III. Windows
. 14.10.
, -
. h- .
.
.
, -
.
, Projects | Options
( . 14.11).
Forms.
,
14.3.
14.3.
int , k = 0;
TfmResult* resfrrn = new TfmResult (this) ;
= ComboBoxl->lternIndex+l;
for (int 1 = 1 ; i < 11; i++){
TCheckBox* cb_control = (TCheckBox*)FindComponent("cb" + IntToStr(i));
if (cb_control->Checked){
resfrm~>AddResultLine(i, y);
14. 453
if (k > 0) resfrm->Show();
else MessageBoxf this->Handle, "", "", M3- OK | MB ICONEXCLAMATICN);
Mainform: ffmMdn
Cancel Help
. 14.11.
, , , .
-
. -
, ,
ftddResuitLine(i, ) -
.
, ,
, WinAPl MessageBox.
, , -
. , , , -
,
. 14.12.
AddResuitLine. C++ Builder ,
. -
. 14.13.
454 III. Windows
f
'- 17
3' 7 21
4" 7 29
5" 7 35
L. 7 42
,.- 7 43
3" 7 56
7" '
' 63"
10 -7 70
. 14.12.
[Add Method
AddtoDa^l^TlrnRe^ll jj
;~ Diiectives
. abstract , const (3]
i virtual (2) [7 iastcaStil
-
; Mgssage Handle;: i
[___; ._ . :
Implementation details ~
j " Callinherited * Injine P i;i,.'i :rh;irft '
; File: Epr^FWL^^buierl^FleslJlt.cpp
, , :
StringGrid->RowCount = StringGrid->RowCount+l;
StringGrid->Cells[0][StringGrid->RowCount-l] =
IntToStr(x) + " * " + IntToStr(y);
StringGrid->Cells[l][StringGrid->RowCount-l] = IntToStr(x*y);
.
: onFormShow .
:
StringGrid->Cells[0][0] = "";
StringGrid->Cells[l][0] = "";
StringGrid->FixedRows = 1;
,
! , , -
. , , ,
, .
C++ -- -
.
( ) - ,
. "" -
, . , -
, , .
4 , , -
, , .
,
, .
-
. [4] "" [8].
"" " " -
[10].
[7, 9, 14, 39, 40, 41, 45].
[91, Pascal,
[39,40]. , [14] -
.
Visual C++ 6, [19, 26, 28, 46], "", -
, [19].
[3, 32, 36] . -
. Windows -
[43], -
[33] [50].
455
. [37, 38], [23, 24, 25], -
"C++ In-Depth" [1, 15, 20, 35],
.
[49]. STL
[2], [42] . -
"" . STL.
, , C++. , C++
(
).
.
, , ,
. -
!
IV
1, Borland
2. Microsoft Visual C++ 6
3. C++
4.
1
Borland
-
, , -
, , , ,
. -
(Integrated Development Environment IDE). -
, , -
, .
-, -
, , ,
. -
: Borland C++ ,
Borland C++ 5 Borland C++ Builder 6. ,
. . .
" ".
(
). ,
:
Bin ;
Include ;
Lib (
obj) ( lib) , -
;
Examples ;
Doc
;
TVision - -
OWL VCL.
IDE
Bin
,
( ).
Windows, -
. .1.
. -
:
? ;
File (, , . .);
d Edit (Clipboard);
Search ;
Run ;
Compile ;
Debug ;
Project ( );
Options ;
Window ;
Help .
<F10>.
" " <11>+<>.
, :
File | Quit;
<Alt>+<F> <Q>;
<A3t>4-<X>.
( -
)
.
, :
File | New;
<Alt>+<F> <N>.
,
(. . 1.1).
. :
File | Save;
<Alt>+<F> <S>;
<F2>.
, -
( Bin )
464 IV.
nonamexx.cpp ( .).
. 1.2.
<Enter>.
,
File | Save As, .
. 1.2.
1
Fl Help== renter directory patn-.ana rile-natie mask
. 1.3.
1. Borland 465
:
File | Open;
<Alt>+<F> <>;
<F3>.
,
. 1.3.
( ) .
.
.
, , -
,
. Project , -
, , .
:
1. Project j Open project. ,
(. . 1.3), -
.
. 1.4.
466 IV.
2. (. 1.4) ,
( <Insert>, Project | Add Item).
<DeIete>, Project | Add Item.
Compile | Build all
Compile | Make :
,
.
,
Compile:
Compile <Alt>+<F9> ;
Make <F9> ;
Build all .
Make Build all -
.
Run (
<Ctrl>+<F9>). -
. -
( Window ] Output), ( -
<Alt>+*<F5>).
( Run | Arguments).
-
. -
. ,
, ,
, .
( -
<Ctrl>+<F2>, Run | Program reset).
, -
.
1. Borland 467
.
, , -
<Ctrl>+<F4> (
Run | Goto cursor).
()
, -
.
:
G "" , ( <F7>,
Run | Trace into).
"" ,
( <F8>, Run | Step over).
, -
. ,
.
:
Inspecting ( <Alt>+<F4>, -
Debug | Inspect) (. 1.5);
Evaluate and Modify ( <Ctrl>+
+<F4>, Debug | Evaluate and Modify) (. 1.6);
Watch ( <Ctrl>+<F7>,
Debug | Watch) (. 1.7).
Borland ( , , Microsoft)
-
.
<F1>. "" ,
468 IV.
" " -
. Help -
. :
Contents ,
(. 1.8);
^include <iostreani.h>
int mainQ
unsigned Int year = 0;
begin: 'h** J hspecti ng year,' f*3?I ]
cout " : ";
if (CO<year)&SCyear<121))
{ int digit = year&lQ;
int one = year / 10 % 10;
if Cone != 1)
eise IT ( == 2)|Kdigit ==
cout year ! "end! ;
else cout " !"11;
else cout year" flerlu<<endlj
else goto begin;
return 0;
Ip F1Q Menu
. 1.5. Inspecting
^include <iostream,h>
int main(}
unsigned
begin:
cout "3
if C(0<ye
{ int dig
int one
if (one
else i
cout
else
}
else
else goto b^gin;
return 0;
. 1:7. Watch
^include <iostrean
int main
{ H
unsigned i,
begin: IMenus and Hot Keys
cout "
if ((Ckyea
{ int digi
int one Assembler (built-in) . Functions
if (one Borland ** Language Header Files
{if (dig Coraiand Line (BC) lOStream cla
else if "Cowmanc! Line CBCC) Uti"Htieb
cout > : Container Classes
else > -> "Error Messages -
. 1.8.
Index ( <Shift>+
G Topic search (
<Ctii>+<Fl>), -
C++ : -
470 IV.
"
";
D Previous topic .
Help :
Editor Commands
;
Borland C++ Language C++ -
, ;
Command Line () ()
;
Functions , ;
Header Files ,
include;
Error Messages .
Borland C++ 5
,
, C++.
Windows, -
-
.
IDE
Win-
dows, . -
. 1.9.
, Borland C++ 3.1
, .
File | Exit.
" " Windows <Alt>+<F4>.
Borland C++ 5 . -
: .
File I New I Text Edit.
1. Borland 471
(. ), -
. ,
.
, ,
getcho conio.h.
,
""
( ). conio.h , -
Borland, Visual C++ 6.
. :
File | Save File | Save As;
Of <Alt>+<F> <S>;
1 <Cirl>+<S>.
(. 1.11), -
nonamexx.cpp (
).
<Enter>.
,
File Save As, .
472 IV.
File Edit Search View Project Script Tool Debug Options WJndow
, .1.10.
, ici] projOOOS.cpp
JP |J3prolQ10I,tpp
% _
r
: 14 jj j
: |D*+ souice (",cpp;".c) ^~3
. 1.11.
1. Borland 473
:
File | Open;
<Alt>+<F> <>.
,
. 1.12.
Drives
, 1.12.
, , -
" " (<F9>, <Alt>+<F9>, <Ctrl>+<F9>), -
Borland C++ 3.I.
, . 1.13.
. 1.13.
/ , -
, . 1.14.
. 1.14.
,
. 1.15.
474 IV.
. 1.15.
, .
"" ,
,
. , File 9 ,
. ,
File I Open (. . 1.12)
.
-
, . -
File | New ( Project.
,
. 1.16.
( Project Path and Name) no .
Target Name.
:
Target Type Application [.exe];
Platform Win32;
Target Model Console.
Static,
Libraries .
1. Borland 475
m
: OK
i **
X Cancel 1
1
pTojoms" \u. Browse
Ad ancedl
Class Libiary MFC, j f
Dynamic Library [.dll] Kelp |
. :
Static Library [for ,] [.lib]
Static Library (for. dll]i I'b] QWL
Import Library [.lib] _
! . . -, ' Muftithcead
Target Expert
. 1.16.
. 1.17.
,
. 7.
:
1.
(delete node).
2. (
. 1.18).
.cpp t.cppl
. 1.18.
476 IV.
3. ;
(. . ),
.
-
. -
, "" --
, .
.
.
Debug.
" ", -
Borland C++ 3.1, - , -
Windows.
, Help. -
, :
Contents , ,
;
Keyword search ( <F1>) .
<F1>
;
Windows API , Windows
.
Windows ,
. 1.19. -
.
-
C++ (. 1.20).
. ,
, -
, . 1.21.
,
. 1.22.
(, vector), -
2 , 3 -
, .
1. Borland 477
; I
Environment Options
Project Options
Project Manager
!
ClassExperl
TargelEnpeit
Debugger
Browser
Hew to...
. j
. 1.19.
] I
..
2. Q ""
getebrk _
gelchar
gelche
Li'.-l " i: .; .
GelCilpboardToken
GetCommard
gelcutdir
gelcwd
geldate
geldfree
Qelolsk
geldla
getenv
get! at
get! aid
g elf lime
I
liJ
. 1.20.
478 IV.
,
,
.
^ ,
[ ]
. .
- ,
"".
(* [
, <"" >
<
>
. 1.21.
! |
1,.
1 vector!
2, .
"". -
; 1. ., , ,
. 1.22.
1. Borland 479
Borland C++Builder 6
, , -
2002 . -
, Borland, ,
Visual C++ 6. ,
STLpoit (
3). -
, -
, Windows API.
.
IDE
Win-
dows, . -
,
(. . 14). -
, File | Close All.
File I New I Other.
.:: l-ton t
. 1.23. { )
480 IV.
, ,
Borland C++ Builder 6.
. 1.23.
Console Wizard New -
. , . 1.24.
Console Wizard
Source Type
; UseVCL
" Use CLX
" Multi Threaded
] sole Application
01
Console Application
C++. -
, . 1.25.
.
Unill.cpp
Pioiectl - Classes
1: 1 Modified Inser!
. 1.25.
1. Borland 481
Save Uiiit! As
1
: 3 CcwpaHi-
L:.;..........
: [C++Builde'unit [*) !
j
. 1.26.
Sara Pfojecll As
iRualpha.bpr
(strrus.bpr
!cassert,bpr Istruconst.bpr
I Cop/File, bpr ! Structure, bpr
Istrusize.bpr
! functions, bpr jTernpFunc.bpr
I Functors, bpr Slypeids.bpr
! increment, bpr Surnolfunc.bpr
I Inherited, bpr ! undefined, bpr
. 1.27.
163. 1264
!
482 IV.
. :
File | Save File | Save As;
<Alt>+<F> <S>;
<Ctrl>+<S>,
(. 1.26), -
unitl.cpp.
. ,
( File | Save Project As) -
( File | Close All) -
(
. 1.27).
, .
( bpr),
.
"" , -
File | Reopen. , -
,
File | Open Project ( <Ctrl>+<Fll>).
, (. . 1.27),
.
,
/ ,
" " (<F9>, <Alt>+<F9>, <Ctrl>+<F9>), -
Borland C++ 3.1. -
, . 1.28.
. 1.28.
Borland, .
Run.
1. Borland 483
Borland C++ Builder 6 , ,
, -.
Windows, -
. -
24 ,
, . 1.29.
> - ; X
dSMS 5 Hefc Files
Runtime Library Reference
C++Builder Command-line Tools
C++Build et Language Guide
CodeGuard User's Guide
Creating Custom Components
'ij Customizing Help
Developing COM-based Applications
; Developing Database Applications
Developing Distributed Applications
Error Messages and Warnings
Image Editor Help
Integrated Translation Environment
InterBase Express Reference
Help, Intranet ActtveX Online Hdp
Object Pascal Reference
: 222003., 5:26 Programming with C++Builder
Quick Reports Help
SQL Builder Help
Standa(d C++ Library Help
TeeCnart Help
Using C++6uilder
Visual Cwnponent Library Reference
What's New in C++Buiider
WinSght Users Guide
. 1.29.
, -
(. . 1.19). C++
C++.
2
Microsoft Visual C++ 6
1998
98- . -
.
IDE
Windows, -
. -
. 2.1.
File | Exit Windows -
<Alt>+<F4>.
Visual C++ 6 . ,
, .
, ,
, -
.
. -
-
.
. -
.
486 IV.
. 2.1.
^laltorms:
Win32 Dynamic-Link Library g___
j Win32 Static Libraiy
1. File | New.
2. (. 2.2) Projects
Win32 Console Application.
3. Project name, First.
Workspace ,
. 2.3.
1
Workspace
1
Workspace ' f i r s t : 1 p t o j e c t ( s
! first files
dassView | J) FiteView I
. 2.3.
(
) ,
Debug 5 .
, , .
dsw. -
, .
488 IV.
.
1. File | New (. . 2.1).
2. (. . 2.2) Files
C++ Source File.
3. File names, First.
(. 2.4).
ClassView _g FileView
. 2.4.
, Build
MiniBar, . 2.5.
,
. 2.6. , Borland
, (Press any
key to continue).
2. Microsoft Visual C++ 6 489
Run <Ctrl+FS>
Build Breakpoint
<F7> <F9>
Compile Go <F5>
<Ctrl+F7>
Stop build
<Ctrl+Break>
. 2.5. Build MiniBar
. 2.6.
,
( File | Close Workspace).
,
File | Resent Workspace . -
, , -
File | Open Workspace.
(. 2.7) dsw.
, -
.
.
490 IV.
Open Workspace
: | Q
Debug
: Workspaces (dsw;.mdp|
. 2.7.
Visual C++ 6
(Win32 Debug), (Win32 Release). -
. -
().
Build | Set
Active Configurations -
. ,
Project | Settings.
Debug, -
Debug, . 2.8.
. 2.8. Debug
Visual C++ 6 -
: ,
,
.
2. Microsoft Visual C++6 491
Debug | Go ( '
). . 2.9.
. 2.9.
, Stop Debugging ( <Shift>+
3
C++
http://www.research.att.com/~bs/homepage.html C++,
C++, .
D http://anubis.dkuug.dk/jtcl/sc22/wg21/ C++.
, , -
.
http://msdn.microsoft.com -
Microsoft. , .
http://rsdn.ru .
-
, ,
.
, RSDN.
. ,
.
http://clubpro.spb.ru/ --
C++, rsdn .
www.cuj.com C++ "C++ users
journal", , -
, .
http://www.gotw.ca/gotw/index.htm Guru Of The Week, -
. -
, .
[35].
http://www.aristeia.com/right_frames.html - ,
C++.
494 IV.
http://staff.develop.com/slip/ -- ,
C++.
http://www.firststeps.ru/ " ", -
.
http://www.cppwmeste.r2.ru/ "
".
-
:
http://www.codetools.com/
http://www.codeproject.com/
http://www.codenet.ru/
http://www.codeguru.com/
http://www.doc.ic. . uk/lab/cplus/c++. rules/
http://www.helloworld.ru/
http://www.emanual.ru
http://www.sourcesl.ru
http://www.rusdoc.ru
[25]
STL, -
:
http://www.sgi.com/tech/stl/ - STL;
http://stlport.org/ , ,
, Inprise Borland C++ Bor-
land 6;
http://www.boost.org/ "" , -
, -
. .
http://gcc.gnu.org -
C++.
http://www.digitalmars.com/ --
.
- ,
, .
,
. ,
Google.
4
,
.
.
11. . . C++. . - .: ,
1996.-272.
12. . Windows 95/NT / .
. .: -, 1997. 640 ., .
13. . ., X. . C++.
- UML /
. . - .: , 2002. 1152 .
14. . ., . .
C++. , 3- / . . .: ,
2003. - 848 ., .
15. ., My . . C++.
C++ In-Depth, . 2 / . . .: , 2002. 384 ., .
16. . ., . / . . -
.: , 2001. 381 ., .
17. ., . / . .
3- ., . .: , 2001. 352 ., .
18. . . Borland C++ 5 / . . .: -,
2000.-976., .
19. ., ., . Microsoft
Visual C++ 6.0 / . . .: ; .:
, 2001. 864 ., .
20. . . C++. C++ In-
Depth, . 1 / . . .: , 2002. 256 ., .
21. . Borland C++ 5. / . . .: , 3997.
560 ., .
22. . . C++. . .: - - -
, 1999. - 400 .
23. . C++. 50
. . /
. . .: , 2000. 240 ., .
24. . C++. 35 -
.
/ . . .: , 2000. 304 ., .
25. . STL. -
/ . . .: , 2002. 224 ., .
26. . ., . . Visual C++ MFC / . .
.: -, 2000. - 1040 ., .
27. . . STL. .: , 2003. -
262 ., .
4. ^ 497
45. ., . . . -
, .
/ . . .: , 2001. -
736.
46. . Visual C++ 6: .: , 1999. 576 .,
.
47. . ., . ., . . -
. , 2002, 8.
48. . C++ / . . .: -,
2000. 416 ., .
49. . C++: . .: , 1999.-
320 ., .
50. . Windows. .: ,
2002. - 1072 ., .
51. Programming languages C++. International Standart ISO/IEC 14882:1998(E).
27
28
: 28
207, 408 310
195, 408 312
: :
152 181
149 115
152 267
148 412
145. 147 :
145, 148, 154, 177 195
265 195
v 149 195
263 195
377
67
212
265
274
224 35, 47
209, 216 122, 226
210 370
210 33
272, 398 67
210, 214 ctype.h 95, 97
212 math.h 65
61, 176, 195, 210, 334, stdarg.h 270
401 stdio221
195 stdlib.h65, 100, 156
210 STL62, 214
215 STLport 222
: string.li 135, 159, 188
382 / 221, 241
381 . . 500
500
(.): 125
62 22. 56
183 34, 55
128 47
61 19
62, 176, 183, 196, 340 125
26, 67 :
349 281
349, 351 367
/ 225 310
225 307
395
372
332
: 371
21, 55 371
221 389
224 405
225 276
127, 225 275-277, 290, 296, 365
55, 225 261
223 7476, 401
d
/: 212
251 298
423 372
251 20, 24, 34, 36, 49. 50-52,
257 54-56, 59,60, 63,71,73, 124
: 154
203 107
203 94, 106, 113
199, 239 24, 43, 53
198 27
199 57, 59
202 43
417, 418, 419 411
323
409 411
:
253
212
201
444
281
259, 260
262
398
: 205
173 171
77 162
171 ,
: 344
252 331
221 367, 409
501
373 :
330, 342 "" 37
333 "" 37
367 19
: 37, 95
203 38
171 21
167 74, 76
: 73
pragma 141 124
using 188
126
:
267, 268 21, 62, 70
171, 195 432
136 429
265, 270 :
203 358
323 357
187, 195 403
380 358
129, 178 21, 50
165, 264, 408 18
362 19
184 229
203 378
196 100
184, 195 , 166
249 69
- 356
181
21
381 146
377 128
228
19, 62
445
:
126 116
351 116
158 :
188 197
179
428 311
191 203
264 164
- 296 107
249 119
19, 21, 23, 30, 128
36, 38, 43, 63, 101, 184, 314, 323, 347, - 311
373 . . 502
502
(.): 347
186 365
80 324, 370
185, 190 354, 358
388 324
68, 77, 87, 204, 361, 40] 427
: 387
37, 50, 56 374
37, 50
361
21
361
409
362, 363
439
327, 329 363
417 365
430 408
374 184
375 403
353 :
348 452
348 :
357, 359 49, 158, 420
184, 187, 195 420
224 :
206 106
207, 208 107
208 109
209 17
206 18
206 :
206
450
206
444
209
445
206
205 377
- 204 224
206 398
> 208 398
207 34, 71
205 "" 37, 43
"" 37
28
41
228 41
. 229 36, 59
231 152
229 39
190 38
229 Windows 230
380 138
322 47
409 412
362, 363, 366 122, 124
503
431
431
432
: 431
224, 236 446
240 431
100,411 431, 442
NULL 149 387, 4 1 1
101 410
103 138
504
: 348, 349
RTTI 359 406
376 406
347, 354 366
350 317, 319, 321
93 215, 402, 403
50 :
, 201 38, 42
151 177
: 21, 26
362 161
362 377
377 148
403 390
:
438
433
409 433
357, 361, 365 451
363, 368 428, 439
363 425, 433
361 19-21, 23, 28, 30, 32, 50, 51,
409 55, 57, 58, 60
363, 368 typedef43, 112
36! 20, 26, 225
60, 88 19, 31, 67
19, 23, 34, 46, 225
278, 281 28, 33, 34
152 348
39, 45
357
27, 28, 244, 396, 429,
436
379, 380, 381, 391 349
77 39, 45
260 34
382 52
228 23, 26, 30, 31, 32
164 23, 25, 28, 46, 54 '
82 30, 31, 32, 33, 54, 55, 106
387 29, 32, 54
: 31
349 D
30
332 32
287 " 30, 31,56
177, 268 20, 34, 59
: delete 164
337 new 164
366 sizeof 36
193, 307 37, 51
343 56, 317
505
157 364
301 57
315 278
337 290
330 :
354 249
339 249
299 :
307 111
90
211
300
<>
402
261
305
89, 361
303
294 374
- 316 374
301 374
361 374
94 366
392 :
110, 113 204, 212
: 175
70, 85 :
70, 82, 84, 297 195
70 195
: 195
154 195
154 195
227 195
21, 24, 26-28, 30, 31, 34, :
38, 44, 50, 51, 54, 56, 59, 60, 64, 71, 423
221, 2*22, 232
77, 78, 87, 379
221, 222
164, 170, 174, 327
222
21, 22, 29, 31
249
21
31, 38 227
426 223
26, 67, 77, 78, 84, 278, 364 228
82 234
86 222
81 222, 257
21 228
> 48 222
181 :
80, 281 407
29, 32 404
21, 58 :
145 368
146 344
: / 223, 248
250 394
6 250
267
507
401
314
150, 248, 401 239
297 :
51, 148
184
374
166
314 126
50
178
375 278
375
207
375
229
52
:
52, 150
141
18, 99
246
18, 99
/
100
241
100
272
99
288
356
283, 284
188, 418, 419, 429
284
234
283, 284
395
272
<> 450
407 276
411
253
276, 278, 284
:
273
179, 421
273
179, 425
274
309, 323, 330
274
:
198
367
129
236 :
274 445
377 446
383 171
382 164
383 420, 447
380 421
188 227
380 227
188, 191, 380 242
76 226
295 251
80 223
79 251
295 421
157 223, 229
- 295 227
- 175 201
508
19, 21, 222 C++ 43, 60, 70, 83, 88, 94, 150,
: 159, 179, 180, 183
347 18, 183, 195,
348, 350, 354 405
357 186
347 / 221
249
447 204
447
: 405
. 104 191
92, 115, 332, 348 240, 244
436 222
111 222
111 123
: 123
117, 329 105, 127
218 129
117 129
: 169
235 128
235 129
235 130
235 131
: 128
413 140
413 169
: 171
17! 129
365 128
353 132
171 :
171, 284 280
171 23, 25, 29, 238
264 / 225
340 201
172 273, 279
269 273
89 256
72, 394
270
72
72
233
93
233
265, 267
19,21, 36, 52, 56
343
70, 265
174
266
259
184
260
19, 63, 67,
175
288
174, 332
21, 46
509
171
:
286
227
173.
248, 249
173
228
171
242
143
228
152
229
147 229
203 228
148
247
148 221, 228
153 228, 231, 232
510
: 175
235 64
241 251
, 453 136
: 425
452 > 334
444 378
443 63
276, 287 136
: 63
295, 317 353
295 301
: 333
299 123, 135, 162
299 285
388, 402, 404 402
217 63
218 425, 427, 429, 432, 439
217 69
407 428
18,62,409, 414 244
API 417, 428 387
API Windows 229 70
inline 331 79, 89
main 19, 68, 179 190, 240
377 85, 88
277 101
96 155, 391, 396
373, 374 229
214, 262 438
259 95, 214, 402
158, 262, 264 124, ISO, 313
251 243
[68 63, 69
251 251
63, 67 426
157 115
67 116, 272, 273, 274, 278,
289
352
272 264, 265, 395
65 354 -
18, 378,425,428 335
377 302
116 392
350 353
246, 252 218
19, 67, 69 62, 71, 354
246 414
67 136, 143
131 67, 69
511
SoftUne? Microsoft
, .. -
,
- -
Borland
.
SoftLine-direct
www.jo fj [in.
SoftLine
. .rosyitdint
, SoftUne?
COMPAQ.
(Microsoft, CA/Ptatinum, Rational, SilverRun,
Quest).
Si (Centura, Merant, Microsoft).
? (PlanisWare, PlanView, Microsoft).
( Allaire, Borland, IBM, Microsoft, -
Allround Automation, ComponentOne, Crystal Decisions, Janus,
Sitraka, Stingray).
9 {Compaq, Fuji, Intel, MainSoft, Sun, Sybase, Tenberry).
IB (NuMega, Intuitive Systems, Segue).
(InstallShield, Wise Solutions).
(Remedy, RoyalBlue, CA, Network
Associates).
IS (Adobe, Allen Communications, cl>ck2learn.com,
eHelp, Macromedia, Quest, Ulead). sitraka "
SoftLine aire>
a SoftLine, -
, , -
GA
(
), -
RAD - . - InstallShield
- ,
, -
Microsoft Dei/eloper Network, Sun Developer Essentials -
- - SoftUne Support Subscription, -
- .
SoftLine .
i SYBASE