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

C C++

: ..

,

. ..
.

C C++.

C++
C#
Java
C
C
C++
Windows- Visual C#
C# 2.0

0.
.
1.
C
C++.
,
2.

Borland C++, ver 3.1,
Borland C++ Builder.
,
3.

.
,
4.

.
, ,

5.
C

C++.

6.

.
,
7.

.

8.

.

9.

, .

10.

.
,
11.


. ,
.
12.
Borland C++ 3.1

Borland C++ 3.1. ,

13.
. C++
.
,

14.
.
.

15.

,
.

16.

, ,
,
.

-
http://www.INTUIT.ru

C C++

0. : . : PDA

,

.
.. . , C, C++
2- "
",
Turbo Pascal 7.0, Delphi 6.
Borland C++ (ver 3.1) Borland C++ Builder (ver 5.0).


.

.. ,
.

.


C++
,
.

Bell Labs , C
(.
BCPL, . B).
, ,
Unix PDP-7,
1969 . ,
GE-645, ,
Multics. . (,
Multics) ,
. GE-645 75$,
.
, . ,

PDP-7 . .
. Unix , ..

Multics. 1970 .
Unix PDP-11.
,
.
C,
. 1973 . .
Unix C (
).
1974 Unix C
.
Unix,
, .
Unix, 90%
, C,
Unix.
C 1983
Bell Labs .
C++ ( C
).
, ,
-,
.

C, C++, Borland, ,
. ,

, ,
. Turbo Pascal,

Turbo C.
. .

C++.
Visual C++
Microsoft.
.
Borland Borland C++ ( 3.1, 4, 5),
Borland C++ Builder. ,
.
Intel.

Symantec.
1. : C: PDA
C++.
,
C++,
a b,
.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15

#include <iostream.h>
#include <conio.h>
int main(void)
{
int a,b,max;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
if(a>b) max=a;
else max=b;
cout << "max="<<max;
getch();
return 0;
}

// a
//
// b
// b
// a>b max=a
// max=b
//
//
//

, ,
.
. 01 02 (include
) (h header
) .
. ,

.
(cin >>) (cout <<),
iostream.h, - (getch),
conio.h.
- ,
, . : io input/output
(/), stream (), con (console , ..
).
03 main.
C, C++.
, (
main). int ( integer ) ,
main .
, main,
"", .
, main,
.
void ( ), , ,
main .
( ) ( 04 15).

, // ,
.
05 a, b max,
( int).
06 ,
, (a=).
. 07
, Enter.
, , a.
08 09 b.
10 (if ) a b.
, .. a ,
max ,
. (else ) max
b.
12 (max=)
( max).

getch ( 13)
, - (getch
get character, ).
14
(return )
.
. ,
.
,
main. ,
- , .
(
begin end).
2. : : PDA
Borland C++, ver 3.1,
Borland C++ Builder.
,

. ,
,
.
,
. ,
.
Borland .
(IDE Integrated Development
Environment), Turbo Pascal.
,
.

,
.


.
, ,

.
, .

, ,
, - .

.
, ,
.

.

.
.

( ).
, .. /
, ,
,
. (
, . link) ( ,
. load).
, .
(
. debug).

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

2.1. Borland C++, ver 3.1


Borland C++ (ver. 3.1)
Borland 1992 MS- DOS.
, ,
, MS-DOS.
640 ( ,
), 64 (
, ), int -32768 32767.
16- .
,
.

BC 3.1 Windows,
. BC 3.1
Windows, ,
. 2.1.


. 2.1. Borland C 3.1
,
:

File ;
Edit ;
Search ;
Run ;
Compile ;
Debug ;
Project ;
Options ;
Window
;
Help .

,
.
File (. 2.2) (
New),
( Save Save as), (
Open). (Quit)
.
9


. 2.2. File
File File,
Alt+F.

.
.

. . 2.2 New,
" ".
,
,
NONAMEnn.CPP, .. "" ( nn
, ).

. ,
, , ,
.
.
Edit.

10

Run
(. 2.3). Run
, .
,
,
.
,
. ,
,
, .
, ,
.
.
,
.

.
,
( , , ..),
.
.
Program reset.


. 2.3. Run
Go to cursor
, .
.

11

.
F7 F8
.
,
. F7 ,
. F8
.

Help (. 2.4).


. 2.4. Help
. ,
, .

F1.
Help Contents () Index (
, ).
Contents (.2.5)
:

How to Use Help ;


Menus and Hot Keys "" ( );
Editor Commands ;
Assembler (built-in) ;
Borland C++ Language C++ Borland;
12

Command Line ;
Container Classes -;
Error Messages ;
Functions ;
Header Files ;
IOStream Classes /;
Utilities .

. 2.5. BC 3.1
Index (.
2.6),
.

. 2.6.

2.2. Borland C++ Builder


13

Borland C++ Builder (ver. 5.0) 2000 .


Windows,
. 2.7.
File, Edit, Search, Project, Help.
BC 3.1 ,
. , ,
Compile, Debug, Options.
" ".

BCB C++
Windows.
Windows
MS-DOS. , DOS,
Alt+Enter.
,
Windows,
, 32- .
, int 4 ,
231-1.


. 2.7. Borland C++ Builder (ver. 5.0)
Windows
,
,
, , ,
, ,
.

14

BCB
New File (. 2.8)
Console Wizard OK.

. 2.8.
(. 2.9)
(C C++) .
OK (.
2.10). ,
Unit1.cpp ( Unit2.cpp, Unit3.cpp, ),
Project1.bpr (Project2.bpr, Project3.bpr, ...).

15

. 2.9. Console Wizard



(. . 2.10), vcl.h,
hdrstop,
( ),
main.
(argc)
(argv[]). int,

(return 0).


. 2.10.
, ,
#include <vcl.h>.

. ,
File Save Project As.
Run Run.
3. : : PDA
.
,

.
16

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

3.1.
-
(
), (
)
.

3.1.1.
IBM PC
.
.

. :

25%
.

3.1.2.
IBM ,
-128 127, . C, C++
char.
.
17

C unsigned char.
[0, 255].

3.1.3.
,
IBM PC, .
-32768 +32767,
- 0 65535.
BC 3.1
int ( ) unsigned int (
).
,
.
1, +32768
-32768.
. ,
. Borland C++ Builder

short unsigned short.

3.1.4.
IBM PC .
-2147483648 +2147483647,
- 0 4294967295.
C, C++
long ( long int) unsigned long.
C++ Builder int long .

3.1.5.
, IBM PC
, -263 263-1,

. , , Turbo Pascal
comp,
.
int64.

int128.

3.2.

18

,
m p. x m*2p.
, , , ""
( - ).

,
.
,

. 32- .
,
[0, 2*109]. , 32 1 , 8
23 - .
(2-1)
(2-128), .. 2-129, 10-39.
(1-2-23)*2+127,
10+38. , 9
,
77 .
. 32- 10
, 23-
7-8 .

(4 ) (8 ) .
10-38
10+38 7-8 . 8-
- 10-308 10+308,
15-16. IBM PC
, (
) 10-4932 10+4932 19-20 .
Borland C++
float ( , 4 ), double
( , 8 ) long double (
, 10 ).
IBM PC
- . , ,
,
Intel.

19

, (0.5 m <
1) (1 m < 2) ..
,
.

3.3.
C, C++
. ,
,
. - ,
+ -, , ,
. :
x=-25;
y=y+2.4;

z=2.1e-6;

// -25
//
//

,
:
#define Nmax 100
const double eps=1e-6;
..................
int a[Nmax]
..................
for(i=0; i<Nmax; i++)
....................
if(abs(z)<eps)...


,
.
,
.

, , ,
,
.
(3.1415)
(314.159265e-02, 314.159265E-02) ,
.


20

("") ("") ,
.
C , :

0x5,0X5 - ( - 0x 0X);
05 - ( - );
5H,5h - ( - h H sHort)
5U,5u - ( - u U, Unsigned);
5HU,5hu,5Hu,5hU - ;
5L,5l - ( - l L, Long);
5LU,5lu,5Lu,5lU - ;
5f,5F - ( - f F, Fixed);
5LF,5FL,5fl,5lf,5Lf,5lF,5Fl,5fL - .

BCB
:

i8 - (, 127i8)
i16 - ;
i32 - ;
i64 - ;
ui64 - .

3.4.
, ,
.
Pascal C, C++
(),
:
int main(void)
{ int i,j;
...........
s=0;
for(int k=0; k<10; k++)
........................

i j ,
k . C++
. ,
( i j ),

.
( ).
, (
21

k for),
. . ,
k, ,
. ,
C++ BC 3.1 .
BC 3.1
, .. k
. Borland C++ Builder
.
,
- .
, . 3.1 (BC 3.1) 3.2
(BCB). .

char, signed
char
unsigned char
short int, short
unsigned short
int, signed
unsigned int,
unsigned
long, long int
unsigned long
float
double
long double

char, __int8
unsigned char

3.1.

1 -128
127
1
2

8
10

0
-32768

255
32767

65535

-32768

32767

65535

-2147483648

2147483647

4294967265

3.4*e-38

3.4*e38

1.7e-308
3.4e-4932

1.7e308
1.1e4932

1
1

3.2.

-128
0

127
255
22

short int, short,


__int16
unsigned short
int, signed, __int32
unsigned int,
unsigned
long, long int
unsigned long
int64, __int64
__uint64
float
double
long double

-32768

32767

2
4
4

0
-2147483648
0

65535
2147483647
4294967265

4
4
8
8
4
8
10

-2147483648
0
-4611686018427387904
0
3.4*e-38
1.7e-308
3.4e-4932

2147483647
4294967265
4611686018427387903
9223372036854775807
3.4*e38
1.7e308
1.1e4932


():
int x=18,y=-5;
float a=5F;

Pascal
C, C++ .

3.5.

3.5.1.

,
(stdin).
,
.
:
cin >> d;
...........
cin >> x1 >> x2 >> x3;

d.

, x1,
x2, - x3.

, , , .
Enter. ,
23

, , ,
.
,
.
,
cin.

, .
.

iostream.h:
#include <iostream.h>
int main()
{ int i;
float f;
double d;
..........
cin >> i >> f >> d;

char unsigned char


, ..
, .
.

3.5.2.

, scanf,
:
#include <stdio.h>
int main()
{ int i;
float f;
double d;
..........
scanf("%d %f %lf",&i,&f,&d);

(stdin),
. Enter.
scanf ,
,
, , ,

24

.
.
i ( - &i),
int,
%d. ,
(d - decimal, ).
f float
%f. ,
, .
d double %lf (l - long).
, ,
scanf, ,
. ,

.
. :
scanf("%d %*l %lf",&i,&d);


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

:
%[*][][{l|h|L}]{d|i|u|o|x|X|f|e|E|g|G}

,
. ,
.
% ,
.

25

*
. , , ,
,
. l, h L
(l, L - long; h - short).
. 3.3.
3.3.

d

i

u

o

x,X

f

e,E

g,G


char. ,
scanf - 2 .
char.
,
. -
char .
BC 3.1 short int
- %d, %i, %u, %o, %x
%X. long
- %ld, %li, %lu, %lo, %lx %lX.
BCB
h - %hd, %hi, %hu, %ho, %hx
%hX. %d %ld .
,
- 0x 0X
, 0 .
%d
,
. %i (i - integer)
, .
%o, %x %X
, .

26

float
- %f, %e, %E, %g
%G. ,
. double
l - %lf, %le, %lE, %lg %lG.
long double %Lf, %Le,
%LE, %Lg %LG.

3.6.

3.6.1.

(stdout), ,
printf. :
#include <stdio.h>
int main()
{ int i;
float f;
double d;
..........
printf("%d %f %lf", i+1, f, f*d);

scanf
, ,
, ,
.

,
:
%[][][.][{l|h|L}]{d|i|u|o|x|X|f|e|E|g|G}

,
. , ,
.
.
,
. ,
,
, .
[-,
+,,0,#]. ""
( ).

27

"" (
). "" ,
"+" . ""
, .
# ,
.
, - 0x
0X. f, e, E
( , , 0,
). g G
.
. 3.4 3.5, ,
I=555 R=5.5
.

( ,
). , -5
%8.4d ,
, -0005.

. ,
. , float
3.14159265, %10.8f
3.14159274. , ..
float 7-8 .
3.4.
%6d %6o
%-+#0 +555
01053
%-+#
+555
01053
%-+0
+555
01053
%-+
+555
01053
%-#0
555
01053
%-#
555
01053
%-0
555
1053
%555
1053
%+#0
+00555
001053
%+#
+555
01053
%+0
+00555
001053

%8x
0x22b
0x22b
22b
22b
0x22b
0x22b
22b
22b
0x00022b
0x22b
0000022b

28

%+
%#0
%#
%0
%

+555
000555
555
000555
555

1053
22b
001053
0x00022b
01053
0x22b
001053
0000022b
1053
22b
3.5.
%10.2e %10.2f
%-+#0 +5.50e+00
+5.50
%-+#
+5.50e+00
+5.50
%-+0
+5.50e+00
+5.50
%-+
+5.50e+00
+5.50
%-#0
5.50e+00
5.50
%-#
5.50e+00
5.50
%-0
5.50e+00
5.50
%5.50e+00
5.50
%+#0
+005.50e+00
+0000005.50
%+#
+5.50e+00
+5.50
%+0
+005.50e+00
+0000005.50
%+
+5.50e+00
+5.50
%#0
005.50e+00
0000005.50
%#
5.50e+00
5.50
%0
005.50e+00
0000005.50
%
5.50e+00
5.50
%0x %0X ,
[a,b,c,d,e,f],
- [A,B,C,D,E,F].
( char)
- %o, %0x, %0X, %i, %d, %ho, %hx,
%hX, %hi, %hd.
%u. ,
, .
127 %u
. -5,
65531 (
).

- ( %f %lf)
( %e %E). %g

29

%G ,
.
,
"e".
.
. ,
, . ,
:
x1=127; x2=-350;
printf("x1=%d x2=%d",x1,x2);

:
x1=127 x2=-350


,
"\n":
printf("\nx1=%d x2=%d",x1,x2);


. C, C++ "\"
( Escape), . 3.6.
3.6.

\a
( 0x07,
\\
\
BEL)
\b
\'
'
( 0x08, Backspace)
\f
\"
"
( 0x0C, FF)
\n

\?
?
( 0x0A, LF)
\r

\0xxx

( 0x0D, CR)
xxx
\t

\xhh

( 0x09, HT)
hh
\v
( \xHH

0x0B, VT)
HH

30

3.6.2.

(stdout)
:
#include <iostream.h>
int main()
{ int i;
float f;
double d;
..........
cout << i+1 << f << f*d;

.
.

'\n',
endl:
cout << i+1 << f << f*d << endl;

:
cout << "x1="<< x1 << "x2=" << x2 << '\n';


. ,
. ,
C++ - .

- (hex -
, setw - , setprecision ..)
(cout.fill(:), cout.width(...)).
,
.
4
.

3.7.
1.
#include <iostream.h>

31

#include <stdio.h>
#include <conio.h>
int main()
{ char ch1 = 69;
char ch2 = -128;
unsigned char uch1 = 70;
unsigned char uch2 = 129;
__int8 i8_1 = 71;
__int8 i8_2 = -127;
printf("\nch1=%d ch2=%d",ch1,ch2);
printf("\nuch1=%u uch2=%u",uch1,uch2);
printf("\ni8_1=%i i8_2=%i\n",i8_1,i8_2);
cout << "ch1=" << ch1 << endl;
cout << "ch1=" << (int)ch1 << endl;
cout << "ch2=" << ch2 << endl;
cout << "ch2=" << (int)ch2 << endl;
cout << "i8_1=" << i8_1 << endl;
cout << "i8_1=" << (int)i8_1 << endl;
cout << "i8_2=" << i8_2 << endl;
cout << "i8_2=" << (int)i8_2 << endl;
getch();
return 0;
}
//=== ===
ch1=69 ch2=-128
uch1=70 uch2=129
i8_1=71 i8_2=-127
ch1=E
ch1=69
ch2=
ch2=-128
i8_1=G
i8_1=71
i8_2=
i8_2=-127

1 ,
.

( (int)) .
,
.
2.
#include <stdio.h>
#include <conio.h>
int main()
{
char ch1 = -5, ch2 = -127;
unsigned char uch1 = 25, uch2 = 255;
printf("With sign :\n");

32

printf("-5=%3d(10) -127=%3d(10)",ch1,ch2);
printf("\nOhne sign :\n");
printf("25=%#3o(8) %#3x(16) %3d(10)",uch1,uch1,uch1);
printf("\n255=%#3o(8) %#3x(16) %3d(10)",uch2,uch2,uch2);
getch();
return 0;
}
//=== ===
With sign :
-5= -5(10) -127=-127(10)
Ohne sign :
25=031(8) 0x19(16) 25(10)
255=0377(8) 0xff(16) 255(10)

2
, (
).
3.
#include <stdio.h>
#include <conio.h>
int main()
{
float f1=3.14159265, f2=20000000;
double d1=3.14159265, d2=20E+125;
printf("\nf1=%10.8f f2=%f", f1,f2);
printf("\nf1=%e f2=%e", f1,f2);
printf("\nf1=%g f2=%g", f1,f2);
printf("\nd1=%12.9f d2=%g", d1,d2);
printf("\nd1=%12.9e d2=%G", d1,d2);
getch();
return 0;
}
//=== ===
f1=3.14159274 f2=20000000.000000
f1=3.141593e+00 f2=2.000000e+07
f1=3.14159 f2=2e+07
d1= 3.141592650 d2=2e+126
d1=3.141592650e+00 d2=2E+126

4.
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int main()
{ int i=15, j=6, k;
float v=1.23456, x=3.149;
cout << hex;
cout << "i=" << i << " j=" << j << " x=" << x << " v=" << v;
cout.fill('*');

33

cout << "\ni=" << setw(6) << i << " j=" << j << endl;
cout << setprecision(3) << x << " v=" << v << endl;
cout.width(10);
cout << x << ' ' << j << endl;
for(k=0; k<8; k++)
cout << setprecision(k) << v << endl;
getch();
return 0;
}
//=== ===
i=f j=6 x=3.149 v=1.23456
i=*****f j=6
3.15 v=1.23
******3.15 6
1.23456
1
1.2
1.23
1.235
1.2346
1.23456
1.23456

3.8.
( ) C, C++
- (x+y),
(z-5), (x1*x3) (y/w). Pascal
. , 5/2=2.
C, C++ %, ,
5%2=1.
(
- <<), ( - >>)
:
y=x<<3; //
z=y>>5; //

-
.
:
#include <stdio.h>
#include <conio.h>
int main()
{
int x=5, y=-5;
unsigned z=0xFFFFFFFB;
printf("x=%x y=%x z=%x",x,y,z);
printf("\nx<<2=%x x>>2=%x",x<<2,x>>2);
printf("\ny<<2=%x y>>2=%x",y<<2,y>>2);

34

printf("\nz<<2=%x z>>2=%x",z<<2,z>>2);
getch();
return 0;

}
//=== ===
x=5 y=fffffffb z=fffffffb
x<<2=14 x>>2=1
y<<2=ffffffec y>>2=fffffffe
z<<2=ffffffec z>>2=3ffffffe

, n
2n 2n .
.
.

- ( '|'),
( '&'), (
'^') ( '~').
x=5
( 00...0101) y=7 ( 00...0111).
#include <stdio.h>
#include <conio.h>
int main()
{
int x=5, y=7;
printf("x=%x y=%x",x,y);
printf("\nx|y=%x x&y=%x",x|y,x&y);
printf("\nx^y=%x ~x=%x",x^y,~x);
getch();
return 0;
}
//=== ===
x=5 y=7
x|y=7 x&y=5
x^y=2 ~x=fffffffa


.
math.h stdlib.h.
.
abs(x) . atoi(s)
atol(s) , .

( atoi int, atol - long)
. itoa
ltoa. - int long.
( ),
. ,
35

2 36, ,
.
, (
-), -
. random(N)
0 N-1.
,
. , random(1000) 1000 .
, . ,
, ,
, . ,

.
randomize(),
, .

. ,
0 35 ( 51).
0 35
k- (k - ).
, ,
.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void mixer(char *b)
{ int j;
char i,tmp;
randomize();
for(j=0; j<10000; j++)
{ i=random(36);
tmp=b[0]; b[0]=b[i]; b[i]=tmp;
}
}
int main()
{ char j,k,a[36];
printf("\nPlaying-cards before:\n");
for(k=0; k<36; k++)
{ a[k]=k; printf("%4d",a[k]); }
for(j=0; j<5; j++)
{ mixer(a);
printf("\nPlaying-cards after %d:\n",j);
for(k=0; k<36; k++)
printf("%4d",a[k]);
}
getch();

36

return 0;
}
//=== ===
Playing-cards before:
0
1
2
3
4
5
6
18
19 20 21 22 23 24 25
Playing-cards after 0:
9
0 13 32 18 30 14
6
23 21
8 31 17 29
2
Playing-cards after 1:
28
9 16
1
6 19
3
14
17
8 34 25 11 12 13
Playing-cards after 2:
33 28
5
0 14 23 32
3
11 34 10
2 26 35 16
Playing-cards after 3:
4 33 30
9
3 17
1
32
26 10
7 13 15 20
5
Playing-cards after 4:
18
4 19 28 32 11
0
1
15
7 24 16 27 21 30

10

11

12

13

14

15

16

26

27

28

29

30

31

32

33

34

35

24

34

28

26

35

16

27

15

22

33

12

19

25

10

20

29

10

33

24

15

20

32

22

30

27

31

35

23

18

21

12

29

27

21

30

31

19

22

25

18

20

17

13

24

35

24

18

12

22

19

25

23

31

21

11

16

28

14

29

34

20

29

35

31

34

23

17

25

13

14

26

33

12

10

17

11

26

15

27

22

3.9.

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

:

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

37

,

. , ,
5/2+1.0 .
float, double, long
double. ,
1.0,
(.. double).
3.000000, .
,
.
, , .. 2,
2.5. double,
3.0.

math.h.
. 3.7.


double acos(double x)
double asin(double x)
double atan(double x)
double atan2(double x, double
y)
double atof(const char *s)
double ceil(double x)
double cos(double x)
double cosh(double x)
double exp(double x)
double fabs(double x)
double floor(double x)
double log(double x)
double log10(double x)
max(a,b)

3.7.

arccos x
arcsin x
arctg x
arctg (y/x)

s
""
cos x
ch x
ex
|x|
""
ln x
lg x
(a,b),

min(a,b)
(a,b) ,

double pow(double x, double y) xy
double pow10(int p)
10p
double sin(double x)
sin x

38

double sinh(double x)
double sqrt(double x)
double tan(double x)
double tanh(double x)
double hypot((double x, double
y)
double poly(double x, int n,
double *a)
double ldexp(double x, int n)

sh x
x
tg x
th x
x2+y2

x*2n

max min, ,
, ,
stdlib.h.
double double
long double.
. 3.7 l - (fabs, fabsl), (acos, acosl), ...:
ceil floor.
, x.
, x.
, , ,
double.
ceil(0.1) =1.0
ceil(0.5) =1.0
ceil(0.9) =1.0
ceil(-0.9)=0.0
ceil(-0.5)=0.0
ceil(-0.1)=0.0

floor(0.1) = 0.0
floor(0.5) = 0.0
floor(0.9) = 0.0
floor(-0.9)=-1.0
floor(-0.5)=-1.0
floor(-0.1)=-1.0

floor(x+0.5).
,
, floor(-0.5+0.5)=0. , , C, C++
round ,
:
int round(double x)
{ int res;
res=(x<0)? x-0.5 : x+0.5;
return res;
}

,
10k, ,
10k.

39

1.
.
2.
, . , ,
.
math.h ,
(. . 3.8).

M_PI
M_PI_2
M_PI_4
M_2_PI

3.8.

M_E
e=2.718...
M_LOG2E log2e
/2
/4
2/

M_1_SQRTPI 1/
M_2_SQRTPI 2/

M_LOG10E log e
M_LN2

ln 2

M_LN10

ln 10

M_SQRT2 2=1.414...
M_SQRT_2 2/2

4. : : PDA
.
, ,

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

(code page).
IBM,
7-
40

IBM/360.
128 ,


IBM .
, ,
IBM.
256 ,
. , .
866.

, .
(, ), ..
.

8- ASCII (American Standard Code for Information
Interchange ) 16-
Unicode.
65536 , .

Unicode.
BC 3.1 BCB
ASCII.
, MS-DOS
866, Windows
98/NT/2000/XP 1251.
IBM.
( 0x00 0x1F),
(, , ..) ,
.
- - ,
Windows, BCB
. BC 3.1, ..

.

4.1.
( )
,

.

41

866-
:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
gotoxy(37,1);
printf("ASCII");
for (i=32; i<=52; i++)
{ gotoxy(1,i-29);
for (j=i; j<=255; j+=21)
printf("%c %3d ",j,j);
}
getch();
}

. 4.1.

. 4.1. ASCII (code page 866)


ASCII 32 (0x20),
"". 128
(0x80), '' .
''
.
. ''
.
1251 (..
Windows) MS-DOS.

42


BCB.

.
ASCII (. . 4.1) ,
:
'F', '%', '$', '', '5', '+', '"', 'q', '', ' '

.
"" .

" ":
'\x27'
'\x5C'

( "", 39)
( \, 92)

,
( ).
,
ASCII. C, C++ Escape-,
(. . 3.6). :
'\'' ""
'\\' " "


" ":
'\47'
'\134'

( "", 39)
( \, 92)

,
( )
[0, 377].
char
unsigned char.
:
char ch1='',ch2='\x9F',ch3='\237',ch4=0x9F,ch5=0237,ch6=159;

DOS, Windows.
Windows
ASCII.
43

4.2.
C .
C ,
,
.
Escape- '\0',
'\0x0'.
C++ string.
.
""
:
const char c1[]="ABCDEFGH";
char str1[]="1234";
char letter[]="a";
char symbol='a';


. c1 9 ,
str1 5 . , letter
2 , symbol 1 .
,
:
c1[3] c1 (.. 'D')
str1[0] str1 (.. '1')

""
:
str1[3]='L';


:
str1[4]=5; //
str1[5]=6; // -

4.3.

4.3.1.
scanf
(%c) (%s) :

44

#include <stdio.h>
void main()
{ char ch1,ch2;
char str1[10];
scanf("%c %c",&ch1,&ch2);
scanf("%s",str1);
....................

, ch1 ch2
(&ch1, &ch2),
str1 . ,
. str1 &str1[0]
.
ch1 ch2
. -,
, Enter.
-, , ch1,
Enter.
.
, "" str1,
9 ( )
. ,
. ,
,
.
,
%
:
char q[20];
............
scanf("%20c",q); // 20 q

%s q
,
Enter.
scanf ,
, ,
, . ,
0 9 %[0-9],
, %[^0-9]. ,
,
, ""
.

45

#include <stdio.h>
#include <conio.h>
void main()
{ char str[10];
int j;
printf("Enter 123ABC\n");
scanf("%[0-9]",str);
printf("str=%s\n",str);
for(j=0; j<10; j++)
printf("%3x",str[j]);
printf("\nEnter DEF123\n");
scanf("%[^0-9]",str);
printf("str=%s\n",str);
for(j=0; j<10; j++)
printf("%3x",str[j]);
getch();
}
//=== ===
Enter 123ABC
str=123
31 32 33 0 0 1 0 0 1 0
Enter DEF123
str=ABC
DEF
41 42 43 a 44 45 46 0 1 0

'123',
str .
'ABC' Enter
( 0xa).
'DEF' .. ,
str.
, 0xa
.
%s .
, str[4], , str
, " ".
main, .
,
, scanf
fflush(stdin).
fflush, str
'DEF'.

4.3.2.
""
:

46

#include <iostream.h>
void main()
{ char ch1,ch2;
char str1[10];
cin >> ch1 >> ch2;
cin >> str1;
....................


, .

4.3.3.
getch() getche()
( Enter)
.
5.
#include <stdio.h>
#include <conio.h>
void main()
{ char ch[4]={'1','2','3'};
ch[1]=getch();
printf("ch[0]=%c ch[1]=%c ch[2]=%c",ch[0],ch[1],ch[2]);
getch();
return;
}
//=== ===
ch[0]=1 ch[2]=5 ch[3]=3

getch ( get character ) , .. , .



() ,
.
- .
getche ,
-.
. ,
.
. .
, ,
.. getch/getche
ASCII. , ""
Esc ( 27), Enter ( 13), Ctrl+Z (
26 ). ,
, ASCII. ,
47

, F1, F2, ...,


, Insert Delete .
, 0,
scan- ( , ).
getch/getche
, scan- .
,
getch/getche (
, 0).
gets ( get string )
, :
#include <stdio.h>
#include <conio.h>
void main()
{ char str[80];
gets(str);
printf("\nstr=%s",str);
getch();
}

stdin
k , :
cin.getline(str,k);

k-1 , ..
. , ,
k-1 , .
.
, k-1 , . ,
, ,
:
cin.getline(str,k,'Q');

'Q', .
cscanf,
.
scanf, . .
, (stdin) (stdout)
(, ).
,
() , .

4.4.
48

,
, , Windows (
1251), 866- .

. 1251
, 192 ( '') 255
( ''). '' '' 164 184 .
:

, [192, 239] 64,


[128, 174];
, [240, 255] 16,
[224, 239];
240 241 .

#include
#include
#include
#include

<stdio.h>
<iostream.h>
<conio.h>
<string.h>

char *to_866(unsigned char *s)


{ static unsigned char str[80];
int j=0;
while (s[j]!='\0')
{ str[j]=s[j];
if(s[j]>=192 && s[j]<=239) str[j]-=64;
if(s[j]>=240 && s[j]<=255) str[j]-=16;
if(s[j]==164) str[j]=240;
if(s[j]==184) str[j]=241;
j++;
}
str[j]='\0';
return str;
}
void main()
{ char s[]="";
cout << s << endl;
cout <<to_866(s) << endl;
getch();
}
//=== ===

,
.

49

4.4.1.
printf
%c, %s.
Windows
, to_866.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch1='F';
unsigned char ch2='5';
char ch3[]="ABCD";
printf("%c %c %s",ch1,ch2,ch3);
getch();
}
//=== ===
F 5 ABCD

4.4.2.

Windows.
#include <iostream.h>
#include <conio.h>
void main()
{
char ch1='F';
unsigned char ch2='5';
char ch3[]="ABCD";
cout<<ch1<<' '<<ch2<<' '<<ch3;
getch();
}
//=== ===
F 5 ABCD

4.4.3.
puts(str),
str stdout,
cprintf.
printf, .
,
.

4.5.

50

4.5.1.
.
,
ASCII,
. ,
, ctype.h.

.
0,
,
. . 4.1.
4.1.


isalnum(ch) ch
isalpha(ch) ch
isascii(ch) ch ASCII
iscntrl(ch) ch (ch<0x20)
isdigit(ch) ch
isgraph(ch) ch (0x21 ch 0x7E)
islower(ch) ch
isprint(ch) ch (0x20 ch 0x7E)
ispunct(ch) ch - (iscntrl || isspace)
isspace(ch) ch (0x20, 0x09,0x0A,0x0D)
isupper(ch) ch
isxdigit(ch) ch
type.h ch.
:

toascii(ch) , 7 ch;
tolower(ch) , ch
;
toupper(ch) , ch
.

4.5.2.
, ,

. string.h
str ( string).
51

,
:

S, S1,S2 ( , );
CS const char * (..
);
ch , int;
k .

strlen(CS)
strdup(CS)
strcpy(S1,CS2)
strncpy(S1,CS2,k)
stpcpy(S1,CS2)
strset(S,ch)
strnset(S,ch,k)

strcat(S1,CS2)

strncat(S1,CS2,k)

strlwr(S)
strupr(S)

strrev(S)
strtol(CS,ptr,r)

4.2.


S

, CS
char*
CS2 S1, S1
k CS2 S1,
S1
CS2 S1, S1
S ch,
S1
k ch S,
S1

CS2 S1,
S1 ( S1
)
k CS2 S1,
S1

S ,

S ,


S

, CS
r,
long. ptr
, .
52

.
strtoul(CS,ptr,r)

strtod(CS,ptr)

double.

strcmp(CS1,CS2)
0, CS1=CS2, 0,
CS1>CS2, 0, CS1<CS2
strncmp(CS1,CS2,k) k CS1 CS2
stricmp(CS1,CS2)


strcmpi(CS1,CS2)
,
strnicmp(CS1,CS2,k) k

strncmpi(CS1,CS2,k) ,

strchr(CS,ch)
CS
ch. , ""
CS, ,
null (.. 0)
strrchr(CS,ch)
CS.

strstr(CS1,CS2)
CS2 CS1.
,
.
null

strpbrk(CS1,CS2)
CS1 , CS2.
null.
strspn(CS1,CS2)
CS1,
CS2 ( )
strcspn(CS1,CS2)
CS1,
CS2
strtok(S1,CS2)
S1 , CS2
, . 4.2,
.
strtol strtoul,
,
r=0. .
'0', ,
7, .

53

'0x' '0X',
, , r=16.
strtok ,
-.
S1
. S1
- .
.
strtok
. ,
.
, S1.
:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{ char *ptr;
ptr=strtok("FEB.14,2006",".,-/");
while(ptr!=NULL)
{ printf("ptr=%s\n",ptr);
ptr=strtok(NULL, ".,-/");
}
getch();
}
//=== ===
ptr=FEB
ptr=14
ptr=2006

4.6.
MS-DOS
( Windows ),
( 25, 80,
).
,
. ,
( ).
, , ,
.
C,

.

54

clrscr(),
clear () screen ().

gotoxy(x,y). x (0<=x<80) y
(0<=y<25) , .
window(x1,y1,x2,y2)
, .
,
, 79- 24- .
,
- ( , ,
,
). (x1,y1)
, (x2,y2)
.
,
.

. , ,
, 2 .
ASCII ,
(. 4.2)

. 4.2.
(R),
(G) (B).
( J=1).
( M=1).
,
cprintf, textcolor(fc).
[0, 15]. cprintf
textbackground(bc).
,

55

textattr.
:
(bc<<4)+fc
128+(bc<<4)+fc

//
//,

:
#include <conio.h>
void main()
{ int j;
textbackground(0);
clrscr();
for(j=0; j<24; j++)
{ gotoxy(2*j+1,j+1);
textcolor(128+j);
textbackground(j+2);
cprintf("Color palette");
}
getch();

5. : C:
PDA

C++.

5.1.
C, C++ ,
Pascal .
,
.
,
main.
, :
trv namef(type1 par1,type2 par2,...)

trv ;
namef ;
par1 , type1;
par2 , type2;

trv
void. ,
, , void.
56

() (.. )
, :

(
);
(
*);
( &).

1. ,
.
double mid(double x,double y)
{ return (x+y)/2.0; }

2. .
.
void swap(int *x,int *y)
{ int tmp;
tmp=*x; *x=*y; *y=tmp;
}

3. .
.
void swap(float &x,float &y)
{ float tmp;
tmp=x; x=y; y=tmp;
}

, ,

.
.
. ,
.
*.
(
, ),
. -,
, .
,
.
.
4. .
57

double mid(double x, double y);


void swap1(double *x,double *y);
void swap2(double &x,double &y);
void main()
{ double a=1.5, b=-2.5,c;
c=mid(a,b);
swap1(&a,&b);
swap2(b,c);
.............
}
double mid(double x, double y)
{ return (x+y)/2.0; }
void swap1(double *x,double *y)
{ double tmp;
tmp=*x; *x=*y; *y=tmp;
}
void swap2(double &x,double &y)
{ double tmp;
tmp=x; x=y; y=tmp;
}

,
. ,
, .
:
double mid(double, double);

,
, . C, C++
. , ,

, . ,
,
:
int intersect(double&,double&,double&,double&,double&,double&);

, ,
. ,
:
int intersect(double &x1, double &y1, double &x2, double &y2,
double &x, double &y);

5.2.
( ), ,
, .
, .
58

, ,
. ,
.

.
,
.
- ,
, .

. ,
, .
,
.
#include <iostream.h>
#include <conio.h>
int x=20; //
void main()
{ int x=40; //
cout << "Local x=" << x << endl;
cout << "Global x=" << ::x << endl;
getch();
}


, .

,
. ,

. ,
static. ,
, .

, .
,
.
:
[static] tv namev [=value]

tv ;
namev ;

59

value .

,
BC 3.1 BCB
.
.
( ) .
,
( ,
).

static. ,
.
, .
. , ,
extern ( . external ):
double qq(int n,double r)
{ extern float eps;
...................

, ,
.

:
const [tc] namec=value;

tc ( tc=int);
namec ;
value .

:
const Nmax=100;
const double eps=1e-6;


:
#define Nmax 100
#define eps 1e-6

60

, (,
) ,
Nmax, 100, eps 1e-6.
,
.

5.3.
,
.
:
namev = expression;

namev :
expression ,
namev.

namev ,
. namev
expression .
. ,

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

, :
v1=v2=v3=exp;

,
,
. :
i=5;
i=a[i]=4;
j=6;
a[j]=j=2;

61


.
, ..
.
C, C++
. ,
1,
:
x++;
++x;
x--;
--x;

//
//
//
//

x=x+1;
x=x+1;
x=x-1;
x=x-1;


.
( )
. :
y=a[i++];
z=b[++j];

y a[i] ,
i 1. j
1, b[j]
.

, -60:
v = (a > b)? e1 : e2;

. ,
, , v
e1. v
e2.

5.4.

:
COD A1,A2

COD ;
62

A1,A2 / .

A1 A2
A1 (A1=A1 A2). C, C++
:
v

= exp;

// v = v

exp;

:
+,-,*,/,%,<<,>>,&,|,^

:
x += 2;
z /= 1.5;

// x=x+2;
// z=z/1.5;

,
. :
x=(y=sin(a+b),z=cos(a-b),max(y,z));

, ..
sin(a+b), y,
z. ,
x, max(y,z).

5.5.

,
:
if() {S1; S2;...} [else {Q1;Q2;...}]

, ,
S1, S2, ....
Q1,Q2,.... else
.
S1,S2,....

:
if(a>b) cout<<a; else cout<<b;
if(x>=0) y=sqrt(x);

63

, C, C++
:
==
!= >
>= < <=

"" (||), "" (&&), "" (!). ,
x [a,b] :
if(a<=x && x<=b) ...


:
if(a)...

C .
,
, .
:
if(a != 0)...

5.6.

, :
m5: printf("\nx=%f",x);


goto m5.
. goto .
,
. . goto ,
.
goto
, ,
.
,
. (-)
10 5 goto.
5 goto.

64

5.1. -.
#include <stdio.h>
#include <conio.h>
void main()
{ int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(a>b) goto m1;
if(b>c) goto m2;
m3: printf("max=%d",c);
goto m5;
m1: if(c>a) goto m3;
m4: printf("max=%d",a);
goto m5;
m2: printf("max=%d",b);
m5: getch();
}

5.2.
#include <stdio.h>
#include <conio.h>
void main()
{ int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(b>a) a=b;
if(c>a) a=c;
printf("max=%d",a);
getch();
}

, goto

:
int i,j;
for(i=0; i<n; i++) {...
for(j=0; j<20; j++) {...
if(_) goto mm;
}
}
mm:

, , goto:
int i,j,k=1;
for(i=0; i<n && k; i++) {...
for(j=0; j<20 && k; j++) {...
if(_) {k=0; break;}
}
}

goto, :

65

,
;
, ;
;
.

5.7.

, . ,

(if...) (goto)
.

, .
for:
for(S1; C; S2) { Q1; Q2;...}

S1 ,
Q1, Q2, ..., ;
C , .
C , ;
S2 ,
.
.

for
,
(
):
s=0;
for(j=0; j<10; j++)
s=s+a[j];

,
. j
0 10, s
a[0].
j 1 .
, 10,
.

66


, C++
for:
for(int j=0; j<10; j++)

()
, :
s=0;
for(j=9; j>=0; j--)
s=s+a[j];

a , .
:
count=20;
for(s=0; count; count--)
s += a[count];


count, "".
(.. ""), .
, for
1, for C, C++
.
,
,
. ,
:
s=0;
for(j=0; j<10; j+=2)
s=s+a[j];

S1, ,
, .
, (
, ).
s
:
for(s=0,j=0; j<10; ++j)
s=s+a[j];

67


S2, .
n-
:
for(x=1,y=1,i=2; i<n; z=x+y, x=y, y=z, i++);


for:
for(;;){Q1;Q2;...;}

for,
. DOS - ,
. (,
,
), break goto.
Windows (
) , .
while (
. ):
while() {Q1; Q2;...}

, .
, ( Q1, Q2,...) .
. while
:
s=0; i=0;
while(i<10)
{ s=s+a[i]; i++; }

, ,
for, ,
for.
while . ,
x (
) :
yn+1=0.5*(yn +x/yn)

:
y=x; //
while(fabs(x-y*y)>1e-6)

68

y=0.5*(y+x/y);

do ( . )
while:
y=x; //
do
y=0.5*(y+x/y);
while(fabs(x-y*y)>1e-6)

do while
, , , .
..
.
(
)
.
.
,
:
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{for(d=0,k=0; k<n; k++)
d += a[i][k]*b[k][j];
c[i][j]=d;
}

5.8.
break
( . ) continue ( . ).
break
, . ,
, a ,
:
for(s=0,j=0; j<n; j++)
{ if(a[j]<0) break;
s += a[j];
}

break ,
, ,
, . ,
,
. , ,
69

, . ,
,
.
continue ,
. , ,
:
for(s=0.0,j=0,k=0; j<n; j++)
{ if(a[j]<=0) continue;
s += a[j];
k++; //
}
if(k>0) s=s/k;

5.9. ()
if,
:
, .
if . ,
:
cin >>x>>y; //
cin >> ch;
//
if(ch=='+') {z=x+y; goto m;}
if(ch=='-') {z=x-y; goto m;}
if(ch=='*') {z=x*y; goto m;}
if(ch=='/' && y!=0) {z=x/y; goto m;}
cout << " "<<endl;
z=0;
m:

cout << "Result="<<z<< endl;


switch:
cin >>x>>y; //
cin >> ch;
//
switch(ch)
{
case '+': z=x+y; break;
case '-': z=x-y; break;
case '*': z=x*y; break;
case '/': if(y!=0) {z=x/y; break;}
default : cout << " "<<endl; z=0;
}
cout << "Result="<<z<< endl;

switch ( . )
,
70

.
. case ( .
) ,
. , ,
.
, . ,
break, ,
.
,
,
case, .
, default (
. ). ,

. ,
default .
,

, .. .
break -
, ,
(
"case c:" ).

5.10.
, ,
:
namef(a1,a2,...);

namef ;
a1, a2, ...

,

. ,

. ,
void *,
. ()

71


&. . :
char str[80];
int x;
........
scanf("%d %s",&x,str);

,
. , &str[5] str+5:
scanf("%c",&str[5]);
scanf("%c",str+5);

,
. :
void swap(int &a,int &b)
{ int tmp=a;
a=b; b=tmp;
}
..........
swap(x,y);

, , .

:
y=a*fin1(x,-2)+b;

fin1(x,-2),
.
.
scanf. ,
( , ),
, .
.
C
:
k=scanf("%d %s",&x,str);

5.11.
. ,
,
. ,
, ,
72

. ,

.
C, C++
.
"/*"
, "*/".
. , ,
, ,
:
y=sin(/*x+*/0.25)*q;

,
.
"//"
.

.
6. : : PDA
.
,
,
.
,
() . ,
, ,
"" .

,
(, -20).


( - 1) .

, .. (
, ).

6.1.

73

C, C++ .
(
).
, "".
, "" (
).
void. , ,
(
).
.
p1
p1, p2, ..., "" type1,
:
type1 *p1;
type1 *p1,*p2,...;


"".
- ,
.
:
int
int
int
int

x=2,y;
*p1=&x; // x
*p2(&x); // x
*p3=p1; //


:
p1=&y;

// p1 y

p1 a ,
p1 a[0]:
int
int
int
int

a[10];
*p1=a;
*p2=&a[0];
*p3=(int *)&a;

//p1 a
//p2 a a
//p3 a a

p1 "" x,
x :
int x=5,y;
int *p1=&x;
..........
y=*p1;

// p1 x
// y 5

74

*p1=2;

// x 2

p2 "" q,
:
int q[20];
int *p2=q;
...........
y=*(p2+5);
x=p2[3];
*(p2+1)=7;

// y=q[5]
// x=q[3]
// q[1]=7

C "*".
:
int x,y;
int *p1=&y;
int **p2=&p1;
x=**p2; // , x=*(*p2)=*(p1)=y

:
void *pu;


.
. :
#include <iostream.h>
#include <conio.h>
void main()
{ int x=5;
void *p=&x;
int *p1;
p1=(int*)p; // p int*
cout<<"x="<<*p1<<endl;
getch();
}

pf double f(double x)
:
double (*pf)(double x);

,
.
double, double:
#include <iostream.h>
#include <conio.h>

75

#include <math.h>
void main()
{ double (*pf)(double x);
//
double x=0.2;
pf=sin;
//
cout<<"sin(0.2)="<< pf(x) <<endl;
//
cout<<"sin(0.2)="<<(*pf)(x)<<endl;
//
getch();
}

, ,
, .
.

6.2.
,
. ,
, , ""
"".
,
.
, . ,

.
, 1
, (,
, ):
int q[6]={1,2,3,4,5,6};
int *p = &q[2];
cout << *p++
<<endl;
cout << (*p)++ <<endl;
cout << *(p++) <<endl;

// 3, p=&q[3]
// 4, p=&q[4]
// 5, p=&q[5]

p1 p2,
, (p2-p1)
.

6.3.
, .
,
.
,
:

76

int x;
int &rx=x;

//

rx x, .. x=5 rx=5
. rx
.
Borland C++ Builder
,
:
TColor old_pc,&pc=TForm1->Image1->Canvas->Pen->Color;
............
//
old_pc=pc;
// old_pc=TForm1->Image1->Canvas->Pen->Color;
//
pc=clRed;
// TForm1->Image1->Canvas->Pen->Color=clRed;
............
//
pc=old_pc;
// TForm1->Image1->Canvas->Pen->Color=old_pc;


. ,
(
)
( - ).
7. : : PDA
.

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

. ,
, ( ).

, ,
. ,

.
.

77

IBM-

. , ..
.
,
( ).
"" . ,
,
.
PUSH ( . ).
POP ( . pop up )
.
, , ,
.
,
, , .
LIFO (Last Input First Output, ..
). ,
.
,
. ,
,
. BCB

(fastcall).
, ,
,
. :
int main(int argc, char* argv[])

int main(int argc, char **argv)

, nameprog.exe
:
>nameprog par1 par2 par3

argc 4 (
). main
,
:

78

argv[0]
argv[1] argv[2] argv[3]
"nameprog" "par1" "par2" "par3"
,
, .
, :
>pkzip a r arch.zip qq1.doc qq2.doc

, ,
, . ,
clrscr() , .
,
:
double mid(double x,double y)

, , return
( . ), :
double mid(double x, double y)
{ return (x+y)/2.; }

,
void.
return . return
:
void print_v(int *a,int n)
{ int j;
printf("\n");
for(j=0; j<n; j++)
printf("%8d",a[j]);
printf("\n");
}


void, , int.

7.1. -
-,
. , mid,
,
:
double mid(double x, double y)
{ return (x+y)/2.; }

79

, - ,
():
w1 = mid(x*cos(fi)+y*sin(fi), x*sin(fi)-y*cos(fi));

,
mid x y
( -
). ,
.
.
math.h
.

7.2. -
, *. , swap1,
,
, .. :
void swap1(int *x,int *y)
{ int tmp=*x;
*x=*y; *y=tmp;
}

// -

-
-, :
#define pint int*
...............
void swap1(pint x,pint y)
{ int tmp=*x;
*x=*y; *y=tmp;
}

//


:
typedef int* pint;

, swap1 :
void swap1(int x,int y)
{ int tmp=x;
x=y; y=tmp;
}

80


( ) x y.
,
.
, ,
,
, . ,
, ,
.
, ,
, ..
&:
int x=2,y=3;
swap1(&x,&y);

:
void swap(int **v1, int **v2)
{ int *tmp=*v1; *v1=*v2; *v2=tmp; }
:
int i=10, j=20;
int *pi=&i, *pj=&j;
swap(&pi,&pj);

pi pj "" , ..
(*pi=20, *pj=10), i j
.

. ,
:
int sum_v(int *a,int n)
{ int j,s=0;
for(j=0; j<n; j++)
s += a[j];
return s;
}

:
int q[20];
..........
k1=sum_v(q,20);
k2=sum_v(q,10);
k3=sum_v(&q[5],3);
k4=sum_v(q+5,3);

//
//
//
//


10
q[5]+q[6]+q[7]
q[5]+q[6]+q[7]

81

,
(.. q &q[0] ).

7.3. -
-,
&. , swap2,
,
, .. :
void swap2(int &x,int &y)
{ int tmp=x;
x=y; y=tmp;
}

// -

-
, #define typedef:
#define rint int&
//
typedef int& rint;
...................
void swap2(rint x,rint y)

, -, - .
, ,
.
- . -,
. -,
(..
):
int x=2,y=3;
swap2(x,y);

, :
void swap3(int *&v1,int *&v2)
{ int *t=v2; v2=v1; v1=t; }

:
int i=10, j=20;
int *pi=&i, *pj=&j;
swap(pi,pj);

pi pj "" (*pi=20,
*pj=10), i j .
82

7.4. -

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

7.5.
C++ ,
:
double mid1(double x=0.5, double y=0.5)
{ return (x+y)/2.; }

()
:
z=mid1(0.75); // 0.625=(0.75+0.5)/2.
p=mid1();
// 0.5=(0.5+0.5)/2.

,
, . ,
:
double mid2(double x=0.5,double y)

:
double mid3(double x,double y=0.5)

.
. -,

. -,
,

83

.
,
BC 3.1, .
:
#include <iostream.h>
#include <conio.h>
double mid(double x=1.,double y=1.);
void main()
{ //double mid(double x,double y);
double x=0.4,y=0.2,z;
z=mid(x,y); cout<<"z="<<z<<endl;
z=mid(x);
cout<<"z="<<z<<endl;
z=mid();
cout<<"z="<<z<<endl;
getch();
}
double mid(double x,double y)
{ return (x+y)/2.; }

7.6.
printf scanf
.
, ,
, , , ?
,
, double.
,
(, ) n,
,
.
,
:
double mid_var(int n,...)

,
.
.
, , mid_var
, .. .
. int
n ( , , n
):

84

int *ip=&n;

ip 1, ..
, dp double:
ip++; //
double *dp=(double *)ip;
//

,
, . :
double mid_var(int n,...)
{ int *ip=&n+1;
double *dp=(double *)ip;
double s=0.;
for(int j=0; j<n; j++)
s += dp[j]; // s += *(dp+j); s += *(dp++);
return s/n;
}

,
, .
:
double mid_var(double a1,...)
{ double *dp=&a1;
double s=0;
int c=0;
while(*dp != 0)
{ s += *(dp++); c++; }
return s/c;
}

,
.
*dp, "" **dp.
s +=
(**dp);
stdarg.h (, )
, :
va_list p;
//
va_start(p,p1); //
//
va_arg(p,); //
va_end(p);
//


mid_var:

85

double mid_var(int n,...)


//
{ va_list p;
double s=0,c=0;
va_start(p,n);
while(n--)
// , n != 0
{ s += va_arg(p,double); c++; }
va_end(p);
return s/c;
}

a1,
:
double mid_var(double a1,...)
{ va_list p;
double s=0,c=0,u=a1;
va_start(p,a1);
do {s += u; c++; }
while(u=va_arg(p,double));
// , u != 0
va_end(p);
return s/c;
}

, ,
.

7.7. ,
, , ,
.
,
.
.

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

86

, ,
. , ,
. "" ,
,
:

static;
""
extern.

, static,
.

7.8.
, ,
return. , ,
return.

7.9.

. :
n! = n*(n-1)!
yn+1=0.5*(yn+x/yn)

//
//


, . ()
. ,
, ,
. ,
.

:
long fact(int n)
{ if (n<2) return 1;
return (n*fact(n-1));
}

, n-
: Fn=Fn-2+Fn-1
int Fib(int n)
{ if(n<3) return 1;
return Fib(n-2)+Fib(n-1);

87


. ,
,
. Fib
.
, ,
, .
:
long fact(int n)
{ long f=1;
for(int j=2; j<=n; j++) f=f*j;
return f;
}
//------------------------------int Fib(int n)
{ int j,f,f1=1,f2=1;
if(n<3) return 1;
for(j=3; j<=n; j++)
{ f=f1+f2; f1=f2; f2=f; }
return f;
}

. ,

. :
1. (n1,n2)=(n2,n1)
2. (0,n2)=n2
3. (n1,n2)=(n2,n3), n3=n1(mod n2)
//
int nod(int n1,int n2)
{ if(n1==0) return n2;
return nod(n2%n1,n1);
}

:
int nod(int n1,int n2)
{ int t;
m: if(n2<n1) {t=n1; n1=n2; n2=t; }
if(n1==0) return n2;
n2=n1%n2;
goto m;
}

7.10.

88


, .

-, ,
, .
.
pf f(x),
double, :
double (*pf)(double x);

,
, .
,
, :
double int_rect(double a, double b, double (*f)(double x))
{ int i, n=100;
double s=0,h=(b-a)/n;
for(i=0; i<=n; i++) s += f(a+i*h);
return s*h;
}

int_rect .

. , ,
math.h:
cout << int_rect(0,M_PI,sin) << endl; //= 1.99984
cout << int_rect(0,M_PI,cos) << endl; //=-4.18544e-17


y=f(x), , [x1, x2] .
. xmid=(x1+x2)/2
f, f(x1), f(x2).
, .
..

, .
#include <iostream.h>
#include <conio.h>
#include <math.h>
double y(double x)
// f(x)=x2-4
{ return x*x-4; }
double root(double x1,double x2,double eps,double(*f)(double x))

89

{ double f12,f1,f2,xmid;
f1=f(x1); f2=f(x2);
if(f1*f2>0)
{ cerr<<"Error: sign(f1)=sign(f2)"; getch(); exit(0); }
while(x2-x1 > eps)
{ xmid=(x1+x2)/2.;
f12=f(xmid);
if(fabs(f12) < eps)
return xmid;
if(f12*f1>0) { x1=xmid; f1=f12; }
else {x2=xmid; f2=f12; }
}
return (x1+x2)/2.;
}void main()
{ cout<<root(0,10,1e-4,y);
getch();
}
//=== ===
2.00001

7.11. ""

lvalue rvalue. , ,
, (lvalue = left value)
(rvalue = right value).
, , ,
.
. .
"".
,
:
double& max(double &x, double &y)
{ return (x>y)? x : y; }

:
double r=max(a,b);

"":
double a=5,b=6;
max(a,b)=10;
// b=10;

, max :
double* max(double *x, double *y)
{ return (*x>*y)?*x:*y; }

90

.........................
double a=5,b=6;
*max(&a,&b)=10;
// b=10;

, :
int& Mmax(int a[],int n)
{ int im=0;
//
for(int j=1;j<n;j++) im=(a[im>a[j])? im : j;
return a[im];
}

, :
int* Mmax(int a[],int n)
{ int im=0;
//
for(int j=1;j<n;j++) im=(a[im>a[j])? im : j;
return &a[im];
}

, ,
const:
const double& max(double &x, double &y)
{ return (x>y)? x : y); }

8. : : PDA
.

(.. ),
.
,

.
( ),
.
()
. C, C++
0, xy[2]
xy. double
xy[0] 0x02000000,
xy[2]
0x02000000+2*8. ,
,
, .
(xy[j]
xyj) ,
91

, . ,
xy, 20 , :

for(s=0,j=0; j<=19; j++) s=s+xy[j];

() w[i][j],
i , j ,
wi,j. C
w[0][0], w[0][1], w[0]
[2],..., w[0][n], w[1][0], w[1][1],.... w[i][j]
:
address(w[0][0])+(i*n+j)*size_w

address(w[0][0]) w
size_w w.

, i*n+j, n ,
w[i][j] w
. , ,

:
a[6]
b[1][2]

*(a+6)
*(*(b+1)+2)

C.
a ,
.. , *a, a[0].
b
, .. , **b, b[0][0].
b+1 "" ,
b.
, ..
.
, ,
.

8.1. .

:
92

#define Nmax 50
char a1[20],a2[2][80];
int b1[25],b2[Nmax];

,
.
,
.
, ..

:
char a[7]="";
char b[7]={'','','','','','',0x0};
char c[]="";
float d[10]={1.,2.,3.,4.};
int q[2][3]={{1,2,3},
{4,5,6}};

a, b c.

. 6,
.
b ,
. c
, , .

.
.
d .
,
. . d
, 0 (,
, ). d
- , , d[4],
"",
.
,
(
q).

:
char spisok[][20]={"","-",""};

93

3*20=60 .
3
:
char *sp[]={"","-",""};

3*4=12 sp
27 , .. .
.
,
.

8.2.

.
8.1.
.
:
void invert(int *a,int n)
{ for(int j=0,tmp; j<n/2; j++)
{ tmp=a[j]; a[j]=a[n-j-1]; a[n-j-1]=tmp; }
}


:
#include <stdio.h>
#include <conio.h>
#define N 20
void invert(int *a,int n);
void main()
{ int j,a[N];
printf("Before reverse:\n");
for(j=0; j<N; j++)
{ a[j]=j+1; printf("%3d",a[j]); }
invert(a,N);
printf("\nAfter reverse:\n");
for(j=0; j<N; j++) printf("%3d",a[j]);
getch();
}
//=== ===
Before reverse:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
After reverse:
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

94

8.2.
.
. " ".
, 3 ,
.
invert:
void invert1(int *a, int k, int n)
{//k
//n
int j,tmp;
for(j=k; j<k+n/2; j++)
{ tmp=a[j];
a[j]=a[2*k+n-j-1];
a[2*k+n-j-1]=tmp; }
}


:
#include <stdio.h>
#include <conio.h>
#define N 20
#define M 15
void invert1(int *a, int k, int n);
void main()
{ int j,a[N];
printf("Before reverse:\n");
for(j=0; j<N; j++)
{ a[j]=j+1; printf("%3d",a[j]); }
invert1(a,0,M);
printf("\nAfter reverse head:\n");
for(j=0; j<N; j++) printf("%3d",a[j]);
invert1(a,M,N-M);
printf("\nAfter reverse tail:\n");
for(j=0; j<N; j++) printf("%3d",a[j]);
invert1(a,0,N);
printf("\nAfter reverse all:\n");
for(j=0; j<N; j++) printf("%3d",a[j]);
getch();
}
//=== ===
Before reverse:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
After reverse head:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 16 17 18
After reverse tail:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 20 19 18
After reverse all:
16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13

19 20
19 20
17 16
14 15

8.3. .
:

95

*c ;
n ;
m ;
w (w 9);
row, col ,
, .

printa,
, :
void printa(int row,int col,int w,int *c,int n, int m)
{ int j,k;
char f[4]="%0d";
//
f[1] += w;
// %wd
for(j=0; j<n; j++)
for(k=0; k<m; k++)
{ gotoxy(col+k*w,row+j); // c[j][k]
printf(f,c[k+j*m]);
}
}

. -,
, w ,
printf %wd. w ,
printa.
. f,
( f[1]) 0 w.
printf,
, . -,
, c[j][k]
, printa (k+j*m). ,
, c[j][k]
gotoxy.
printa :
#include <stdio.h>
#include <conio.h>
void main()
{ int a[3][4]={{1,2,3,4},
{10,20,30,40},
{100,200,300,400}};
int b[4][4]={{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
printa(5,5,4,(int *)a,3,4);
printa(5,40,5,(int *)b,4,4);
getch();
}

96

. 8.1. :
,
, printa
(int *). 1
c , .

. 8.1.
,
printf,
*:
printf("%*d",w,c[k+j*m]);

w ,
* "%*d".
8.4. "" .
000000
999999. "" ,
.
"
.
6
, 0 9,
:
#include <iostream.h>
#include <conio.h>
void main()
{ int n=0;
for(int j1=0; j1<10; j1++)
for(int j2=0; j2<10; j2++)
for(int j3=0; j3<10; j3++)
for(int j4=0; j4<10; j4++)
for(int j5=0; j5<10; j5++)
for(int j6=0; j6<10; j6++)
if(j1+j2+j3==j4+j5+j6) n++;
cout << n;

97

getch();

( 2
1 ).
1000 .
[0,27]. , ,
s0 ( , 0), s1 (
, 1), ... . ""
(s0)2+(s1)2+ .... :
#include <iostream.h>
#include <conio.h>
void main()
{ int n=0,k,s[28];
for(k=0; k<28; k++) s[k]=0;
for(int j1=0; j1<10; j1++)
for(int j2=0; j2<10; j2++)
for(int j3=0; j3<10; j3++)
s[j1+j2+j3]++;
for(k=0; k<28; k++)
n += s[k]*s[k];
cout << n;
getch();
}

, "" 28+1000+28,
1000000
.
8.5. . ,
,
. ,
" ", 2
( ) .
- ,
(A,B,C,D,E,F,G,H),
(1,2,3,4,5,6,7,8).
, ( C) 0 7.

.
8 (
).
1.
, , .
2.
, ,
. ,
, -1.

98

a 88 .
-1 ( , ).

0 ( 0 ).
, 8 ,
newlevel ,
a, k
xod. ,
k
k+1. xod=1,
.
, .. ,
xod 0. newlevel try_
( , try ),
, (p,q),
(k+1).
newlevel try_
:
void try_(int p, int q)
{ if(p>=0 && p<8 && q>=0 && q<8 && a[p][q]<0)
{ a[p][q]=k+1; xod=1; }
}
//---------------------------------void newlevel()
{ char di[8]={-2,-2,-1,-1, 1,1, 2,2};
char dj[8]={-1, 1,-2, 2,-2,2,-1,1};
xod=0;
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
if(a[i][j]==k)
for(int r=0; r<8; r++)
try_(i+di[r],j+dj[r]);
k++;
}
//----------------------------------

di dj.
1 2, 8
. r
(i,j).
, newlevel,
:
#include <stdio.h>
#include <conio.h>
char a[8][8],i,j,k=0,xod;
void main()

99

{ for(i=0;i<8;i++)
for(j=0; j<8;j++) a[i][j]=-1;
printf("begin position=");
scanf("%d %d",&i,&j);
a[i][j]=0;
do newlevel()
while (xod==1);
for(i=0;i<8;i++)
{ for(j=0; j<8; j++) printf("%3d",a[i][j]);
printf("\n");
}
getch();
}

(1,1) 8.2.

. 8.2.
8.6. .

. ,
, ,
1.
#include <stdio.h>
#include <conio.h>
void sort(int *a,int n)
{ int tmp;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n; j++)
if(a[j]<a[i])
{tmp=a[i]; a[i]=a[j]; a[j]=tmp; }
}
int difference(int *a,int n)
{ int i,m=1;
sort(a,n);
//
for(i=0; i<n-1; i++)
if(a[i]!=a[i+1]) m++;
return m;
}
void main()
{ int a0[5]={0,0,0,0,0};
int a1[5]={1,1,1,1,1};

100

int a2[5]={0,1,1,1,1};
int a3[5]={0,0,1,1,2};
int a4[5]={0,1,2,3,4};
int a5[5]={1,2,3,4,5};
printf("\na0:%d",difference(a0,5));
printf("\na1:%d",difference(a1,5));
printf("\na2:%d",difference(a2,5));
printf("\na3:%d",difference(a3,5));
printf("\na4:%d",difference(a4,5));
printf("\na5:%d",difference(a5,5));
getch();

}
//=== ===
a0:1
a1:1
a2:2
a3:3
a4:5
a5:5

2. ,
. , k0 1,
k0 0.
a[i] c a[j].
a[i] 0.
k0.
difference:
int difference(int *a,int n)
{ int i,j,k0=0,m=0;
for(i=0; i<n; i++)
//
if(a[i]==0) { k0=1; break; }
//
for(i=0; i<n-1; i++) //
{ if(a[i]==0) continue;
//
for(j=i+1; j<n; j++)
//
if(a[i]==a[j])
{ a[i]=0; break; }
//
}
for(i=0;i<n;i++)
//
if(a[i]!=0) m++;
return m+k0;
}

3.
.
,
.
,
.
0.
, ,
.

101

0,
1.
.
. -,
(, ).
. 32767 ,
4096 .
calloc.
,
:
b=calloc(4096,1);

//

, N b
( byte)
( bit) . 8
, :
char mask[8]={128,64,32,16,8,4,2,1};

, N,
:
byte=N / 8;
bit =N % 8;

difference :
int difference(int *a,int n)
{ int bit,byte,i,m=0;
char mask[8]={128,64,32,16,8,4,2,1};
char *b=(char *)calloc(4096,1);
//
for(i=0; i<n; i++)
{ byte = a[i] / 8;
bit = a[i] % 8;
if((b[byte]& mask[bit])==0)
//
{ m++; b[byte] |= mask[bit]; }//
}
free(b);
//
return m;
}


alloc.h, calloc.

8.3.

102

8.3.1.
C, C++
. , ,
.
8.7. .
#include <stdio.h>
#include <math.h>
// sqrt
#include <conio.h>
double norm(double *a, int n)
{ double s=0;
for(int i=0; i<n; i++) s += a[i]*a[i];
return sqrt(s);
}
void main()
{ double v1[5]={1.,2.,3.,4.,5.};
printf("norm=%f",norm(v1,5));
getch();
}
//=== ===
norm=7.416198

norm , ""

(, &v1[2]),
.
8.8. .
#include <iostream.h>
#include <math.h>
// sqrt
#include <conio.h>
void norm_vec(double *a, int n)
{ double s=0;
for(int i=0; i<n; i++) s += a[i]*a[i];
s= sqrt(s);
for(int i=0; i<n; i++) a[i] /= s;
}
void main()
{ double v1[5]={1.,2.,3.,4.,5.};
norm_vec(v1,5);
for(int i=0;i<5;i++)
cout << v1[i]<< " ";
getch();
}
//=== ===
0.13484 0.26968 0.40452 0.53936 0.6742

8.9. .

103

#include <stdio.h>
#include <conio.h>
double scal_prod(double *a, double *b,int n)
{ double s=0;
for(int i=0; i<n; i++) s += a[i]*b[i];
return s;
}
void main()
{ double v1[5]={1.,2.,3.,4.,5.};
printf("scal_prod=%f",scal_prod(v1,v1,5));
getch();
}
//=== ===
scal_prod=55.000000

8.10. .
#include <stdio.h>
#include <conio.h>
void sum_vec(double *a,double *b,double *c,int n)
{ for(int i=0; i<n; i++) c[i]=a[i]+b[i]; }
void main()
{ double v1[5]={1.,2.,3.,4.,5.};
double v2[5]={1.,0.,1.,0.,1.};
double v3[5];
sum_vec(v1,v2,v3,5);
for(int i=0;i<5;i++)
printf("%3.0f",v3[i]);
getch();
}
//=== ===
2 2 4 4 6

8.3.2.
. -,

, . -,
( ,
).
, .
8.11. .
#include <stdio.h>
#include <math.h>
#include <conio.h>
void eye(int *a, int n)
{ int i,j;
for(i=0; i<n; i++)
for(j=0; j<n;j++)
{ if(i==j) a[i*n+j]=1;
else a[i*n+j]=0;

104

}
}
void main()
{ int i,j,v[5][5];
eye((int*)v,5);
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
printf("%3d",v[i][j]);
printf("\n");
}
getch();
}
//===

, eye v
(int *). ""
, .
8.12. .
n2 -,
:
#include <stdio.h>
#include <math.h>
#include <conio.h>
void add_mat(int *a,int *b,int *c,int n)
{ int i;
for(i=0; i<n*n; i++)
c[i]=a[i]+b[i];
}
void main()
{ int i,j,v3[3][3];
int v1[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int v2[3][3]={{0,0,1},{0,0,2},{0,0,3}};
add_mat((int*)v1,(int*)v2,(int*)v3,3);
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
printf("%3d",v3[i][j]);
printf("\n");
}
getch();
}
//=== ===

105

8.13.
.
#include <stdio.h>
#include <conio.h>
void mult_mat(int *a,int *b,int *c,int n)
{ int i,j,k,s;
for(i=0; i<n; i++)
for(j=0; j<n;j++)
{ s=0;
for(k=0;k<n;k++)
s += a[i*n+k]*b[k*n+j];
//s=s+ai,k*bk,j
c[i*n+j]=s;
//ci,j=s
}
}
void main()
{ int i,j,v3[2][2];
int v1[2][2]={{1,2},{3,4}};
int v2[2][2]={{5,6},{7,8}};
mult_mat((int*)v1,(int*)v2,(int*)v3,2);
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
printf("%4d",v3[i][j]);
printf("\n");
}
getch();
}
//=== ===

8.14.
.
#include <stdio.h>
#include <conio.h>
void transp(int *p[],int n)
{ int tmp,i,j;
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
{ tmp=p[i][j]; p[i][j]=p[j][i]; p[j][i]=tmp; }
}
void main()
{ int v[4][4]={{ 1, 2, 3, 4},
{ 5, 6, 7, 8},

106

{ 9,10,11,12},
{13,14,15,16}};
//
int *p[4]={(int *)&v[0],(int *)&v[1],(int *)&v[2],(int *)&v[3]};
transp(p,4);
for(int i=0; i<4; i++)
{ for(int j=0;j<4;j++)
printf("%3d",v[i][j]);
printf("\n");
}
getch();
}
//=== ===

p v
:
int *p[4]={(int *)v,(int *)(v+1),(int *)(v+2),(int *)(v+3)};
int *p[4]={v[0],v[1],v[2],v[3]};
int *p[4]={*v,*(v+1),*(v+2),*(v+3)};

, p[0] "" v[0][0]. p[0]


[1]=p[0]+1 "" v[0][1], p[0][2] v[0][2] ..
transp :
void transp(int **p,int n)

8.4.
: a, n
(, , ..). ,
q.
( j),
(a[j]=q).

8.4.1.

:

S1: j=1;

107

S2: q=a[j]. ,
;
S3: j 1;
S4: j<n+1.
, S2. , q
a .

. " "
.
. (
) a , q.
j<n+1 . ,
n+1. .
a[n+1].
.
LOOP,
(j--) j 0.

. (q=a[1]).
n .
n/2 .
ssearch, ,
:
int ssearch(int q, int *a, int n)
{ register int j;
for(i=0; j<n;j++)
if(q==a[j]) return j;
return -1;
}

, ,
j (
,
register int j).

8.4.2.
,
, , .
q<a[1] q>a[n] ,
.
( ).
q .

108

. 2,
, .
q ,
.
bsearch, ,
:
int bsearch(int q, int *a, int n)
{ register int left=0,right=n-1,mid;
if(q<a[0] || q>a[n-1]) return -1;
for(;left<=right;)
{ mid=(left+right)/2;
if(q<a[mid]) right=mid-1;
else if(q>a[mid]) left=mid+1;
else return mid;
}
return -1;
}

, ,
log2n. 1000
500 ,
10 .

- ,
, . .

8.5. .

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

.

8.5.1.
,
( "") .
,
109

.
.. ,
,
, .
bubble,
:
void bubble(int *x, int n)
{ register int i,j;
int tmp;
for(i=1;i<n;i++)
for(j=n-1;j>=i; j--)
if(x[j-1]>x[j])
{ tmp=x[j-1]; x[j-1]=x[j]; x[j]=tmp; }
}

bubble1.
q,
- :
void bubble1(int *x, int n)
{ register int i,j;
int tmp,q;
m: q=0;
for(i=1;i<n-1;i++)
if(x[i]>x[i+1])
{ tmp=x[i]; x[i]=x[i+1]; x[i+1]=tmp; q=1;}
if(q) goto m;
}

,
. ,
. (
) n*(n-1)/2,
3*n*(n-1)/2.
3*n*(n-1)/4.

8.5.2.
:
. ,
.. select, ,
:
void select(int *x, int n)
{ register int i,j,k;
int q,tmp;
for(i=0; i<n-1;i++)
{ q=0; k=i; tmp=x[i];

110

for(j=i+1; j<n; j++)


{ if(x[j]<tmp)
{ k=j; tmp=x[j]; q=1; }
}
if(q) { x[k]=x[i]; x[i]=tmp; }

n*(n-1)/2;
:
o 3*(n-1)
o n2/4+3*(n-1)
o n*(log n +0.577216)

8.5.3.
: .
.
. ,
..
. insert,
, :
void insert(int *x, int n)
{ register int i,j;
int tmp;
for(i=1;i<n;i++)
{ tmp=x[i];
for(j=i-1;j>=0 && tmp<x[j]; j--)
x[j+1]=x[j];
x[j+1]=tmp;
}
}

:
. , 2*(n-1)
. ,
n*(n+1)/2.

8.5.4.
1959 IBM D.L. Shell
. ,
3 , , ,
.
: 9 5 3 2 1.

111

n1.2.
, n2.
void shell(int *x, int n)
{ register int i,j,gap,k;
int xx;
char a[5]={9,5,3,2,1};
for(k=0;k<5;k++)
{ gap=a[k];
for(i=gap;i<n;i++)
{ xx=x[i];
for(j=i-gap; xx<x[j] && j>=0; j=j-gap)
x[j+gap]=x[j];
x[j+gap]=xx;
}
}
}

8.5.5.
C.A.R. Hoare 1962
, quicksort.
.
. ,
, , .
.
.
. ,
, n*log2n.
, .
quick,
, qs:
void quick(int *x, int n)
{ qs(x,0,n-1); }
//---------------------------------void qs(int *x,int left,int right)
{ register int i,j;
int xx,tmp;
i=left; j=right;
xx=x[(left+right)/2];
do { while(x[i]<xx && i<right)i++;
while(xx<x[j] && j>left) j--;
if(i<=j)
{ tmp=x[i]; x[i]=x[j];
x[j]=tmp; i++; j--;
}
}
while(i<=j);
if(left<j) qs(x,left,j);
if(i<right)qs(x,i,right);
}

112

,
, . MAX
20.
100000 (BCB ).
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#define MAX 20
void bubble(int *x,int n);
void select(int *x,int n);
void insert(int *x,int n);
void shell(int *x,int n);
void quick(int *x,int n);
void qs(int *x,int left,int right);
void main()
{ int num[MAX],i;
int t1,t2;
/*
cout << "Before sort:\n";
for(i=0; i<MAX; i++)
{ num[i]=random(MAX);
cout << num[i] << " ";
}
cout << endl;
*/
t1=GetTickCount();
// bubble(num,MAX);
// select(num,MAX);
// insert(num,MAX);
// shell(num,MAX);
quick(num,MAX);
t2=GetTickCount();
cout << t2-t1;
/*
cout << "After sort:" << endl;
for(i=0; i<MAX; i++)
cout << num[i] << " ";
cout << endl;
*/
cout << "end";
getch();
}
//

8.1
100000 Pentium 4 ( 2 ).
(
).
8.1.

bubble
20312
113

insert
select
shell
quick

5266
10843
1406
16

8.6.

()
.
a b, , , ka kb
. -

. ,
.
merge goto,
.
#include <stdio.h>
#include <conio.h>
void merge(int *a,int ka,int *b, int kb, int *c)
{ int ja=0,jb=0,jc;
for(jc=0; jc<ka+kb; jc++)
{ if(ja==ka) goto mb;
if(jb==kb) goto ma;
if(a[ja]<b[jb]) goto ma;
mb:
c[jc]=b[jb]; jb++; continue;
ma:
c[jc]=a[ja]; ja++;
}
}
#define na 3
#define nb 4
void main()
{ int j,a[na]={0,2,4},b[nb]={1,3,5,7};
int c[na+nb];
for(j=0; j<na; j++) printf("%4d",a[j]);
printf("\n");
for(j=0; j<nb; j++) printf("%4d",b[j]);
printf("\n");
merge(a,na,b,nb,c);
for(j=0; j<na+nb; j++) printf("%4d",c[j]);
getch();
}
//=== ===
0
2
4
1
3
5
7
0
1
2
3
4
5
7

8.7. .

114

,
, .
,

. ,
, ,
.
q type_q.

malloc:
q=(type_q *)malloc(n_byte);

, malloc
void. malloc
. , int 16-
(, BC 3.1 MS-DOS)
2 , 32- BCB 4 .

calloc:
q=(type_q *)calloc(n_el,sizeof(type_q));


(n_el) sizeof(type_q).
alloc.h stdlib.h.
- ,
(q==NULL).
malloc calloc ,
:
q=(type_q)realloc(q,new_len);

, q
. ,
. new_len=0,
.
, q ,
free:
free(q);

115

q,
(
Windows ):
q=NULL;

// q=0;

:
#include <alloc.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{ int j,*p,s;
char *str="abcdefghijk",*s1;
p=(int *)malloc(4000);
// ""
for(s=0,j=0; j<1000; j++) s += p[j];
printf("s=%d",s);
// - ""
for(j=0; j<1000; j++) p[j]=j; //
printf("\np[500]=%d",p[500]); //
free(p);
//
p=(int *)calloc(1000,sizeof(int)); //
for(s=0,j=0; j<1000; j++) s += p[j];
printf("\ns=%d",s);
// -
free(p);
//
s1=(char *)calloc(20,1);
//
strcpy(s1,str);
//
printf("\ns1=%s",s1);
//
s1=(char*)realloc(s1,8);
//
s1[5]=0x0;
//
printf("\ns1=%s",s1);
//
getch();
}
//=== ===
s=-2138551277
p[500]=500
s=0
s1=abcdefghijk
s1=abcde

C++
:
q = new type_q;
q = new type_q[n_el];

delete q;
delete []q;

//
// n_el
// -
// -


:
int v=new int(5);

// v=5

116

, new ,
.
9. : : PDA
, .

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

9.1.
,
.
, .
.

,
.

. , "" ,
" " , ""
'' '' ..

9.1.1.
, ""
. , ,
, :
struct book {
char title[40];
char authors[30];
char publishing_house[15];
int year;
int pages;
float price;
}

//
//
//
//
//
//

117

book .

book:
struct book b1,b2,b3;

// book

C++ struct :
book b1,b2,b3;

,
. , ..
.
, ,
, :
struct book {
char title[40];
char authors[30];
char publishing_house[15];
int year;
int pages;
float price;
} b1,b2,b3;

, :
struct {
char title[40];
char authors[30];
char publishing_house[15];
int year;
int pages;
float price;
} b1,b2,b3;

-
,
.
,
:
strcpy(b1.title,"C++: "
strcpy(b1.authors,".");
cout << b1.title;
b1.year=2004;

118

, ,
"->":
struct book *p=&b1;
//
strcpy(p->title,"C++: "
strcpy(p->authors,".");
cout << p->title;
p->year=2004;

:
struct book c[20];

//


:
strcpy([2].title," C++ "
strcpy(c[2].authors,"..");
cout << c[2].title;
c[2].year=1996;
struct book *p=;
strcpy(p[2]->title,"C++: "
strcpy(p[2]->authors,".");
cout << p[2]->title;
p[2]->year=2004;


:
struct book a={"Programming Languages", "Jean E.Sammet",
"Prentice-Hall", 1969,785,49.99};

, ,
:
b1=a; // a b1

,
strcpy:
strcpy(b1.authors,a.authors);

// ""

,
.

. ,

. ,

119

.

, , .
:
struct qq{ int a:10; int b:14; } xx, *px;
xx.a=127;
px=&xx;
px->b=9;


-:


:
struct { int a:10;
int :6;
int b:14; } yy;

9.1.2.
,
.
9.1.
#include <iostream.h>
#include <conio.h>
struct book {
char title[40];
char authors[30];
char publishing_house[15];
int year;
int pages;
float price;
};
void show_book(book b)
{ cout << "Title: "<< b.title << endl;
cout << "Authors: "<< b.authors << endl;
cout << "Publishing house: "<< b. publishing_house << endl;
cout << "Year: "<< b.year << endl;
cout << "Pages: " << b.pages << endl;

120

cout << "Price: " << b.price << endl;


}
void main()
{ book a={"Programming Languages", "Jean E.Sammet",
"Prentice-Hall", 1969,785,49.99};
show_book(a);
getch();
}
//=== ===

,
.
9.2. .
#include <iostream.h>
#include <conio.h>
struct book {
char title[40];
char authors[30];
char publishing_house[15];
int year;
int pages;
float price;
};
void input_book(book *b)
//
{ cout << "Title: ";
cin >> b->title;
cout << "Authors: ";
cin >> b->authors;
cout << "Publishing house: ";
cin >> b->publishing_house;
cout << "Year: ";
cin >> b->year;
cout << "Pages: ";
cin >> b->pages;
cout << "Price: ";
cin >> b->price;
}
void main()
{ book a,*pa=&a;
input_book(pa);
getch();
}

9.3.

121

#include <iostream.h>
#include <conio.h>
struct book {
char title[40];
char authors[30];
char publishing_house[15];
int year;
int pages;
float price;
};
void input_book(book &b)
//
{ cout << "Title: ";
cin >> b.title;
cout << "Authors: ";
cin >> b.authors;
cout << "Publishing house: ";
cin >> b.publishing_house;
cout << "Year: ";
cin >> b.year;
cout << "Pages: ";
cin >> b.pages;
cout << "Price: ";
cin >> b.price;
}
void main()
{ book a;
input_book(a);
getch();
}

,
, , .
,
. .
9.4.
struct Point {int x; int y:};
struct Line {Point P1; Point P2;}
Line ab;
ab.P1.x=5;
ab.P1.y=5;
ab.P2.x=25;
ab.P2.y=30;

, ,
,
.

9.1.3.,
,
. , ,
122


.
9.5. g1, g2 g3
#include <iostream.h>
#include <conio.h>
struct ss {int a; float b; };
ss g1(ss v)
// -
{ v.a=7; v.b=8; return v; }
ss g2(ss &v)
// -
{ v.a=v.b+7; v.b=v.a+8; return v; }
ss g3(const ss &v)
// -
{ ss q;
q.a=v.b+7; q.b=v.a+8; return q;
}
void main()
{ ss x1,y1={1,2};
ss x2,y2={3,4};
ss x3,y3={5,6};
x1=g1(y1);
cout << "x1=" << x1.a << '; ' << x1.b << endl;
cout << "y1=" << y1.a << '; ' << y1.b << endl;
y1=g1(y1);
cout << "x1=" << x1.a << '; ' << x1.b << endl;
cout << "y1=" << y1.a << '; ' << y1.b << endl;
x2=g2(y2);
cout << "x2=" << x2.a << '; ' << x2.b << endl;
cout << "y2=" << y2.a << '; ' << y2.b << endl;
x3=g3(y3);
cout << "x3=" << x3.a << '; ' << x3.b << endl;
cout << "y3=" << y3.a << '; ' << y3.b << endl;
y3=g3(y3);
cout << "x3=" << x3.a << '; ' << x3.b << endl;
cout << "y3=" << y3.a << '; ' << y3.b << endl;
getch();
}
//=== ===
x1=7; 8
//x1 -
y1=1; 2
//y1 -
x1=7; 8
//x1 -
y1=7; 8
//y1 -
x2=11; 19
//x2 -
y2=11; 19
//y2 -
x3=13; 13
//x3 -
y3=5; 6
//y3 -
x3=13; 13
//x3 -
y3=13; 13
//y3 -

9.1.4. C++
C++ .
C ,
.
123

, , ..
.
.

9.2.
,
:
enum name_list {name1,name2,...};

name1 0;
name2 1;
.....................................


? ,
.
, ,
(rainbow) ,
(" , "
, , , , , , ):
enum rainbow {red, orange, yellow, green, aqua, blue, purple};

r_col, ,
, ,
, :
enum rainbow r_col;
r_col=yellow;
...............
if(r_col==yellow)...

C++
enum:
rainbow r_col=yellow;


, .
:
enum week {sunday=1, monday, tuesday, wednesday, thursday,
friday, saturday};

124

enum month {jan=1, feb, mar, apr, may, jun, jul, aug, sep,
oct, nov, dec };

. -,
0 .
1,
. -,

. , "" ""
oct - dec - ,
.
, ,
.
,
:
enum line_style{SOLID_LINE,
//
DOTTED_LINE,
//
CENTER_LINE,
//-
DASHED_LINE,
//
USERBIT_LINE}; //,
enum COLORS {BLACK,BLUE,GREEN,CYAN,RED,MAGENTA,BROWN,...};


: ,
,
.
. -
.
:
#include <stdio.h>
#include <conio.h>
void main()
{ enum qq {zero, one, two, three};
enum qq a,b;
a=one;
b=a+two;
printf("a=%d b=%d",a,b);
getch();
}
//=== ===
a=1 b=3

, 7-
int qq, .
b=a+three;
b=4. b=5; .

125

enum
.

:
enum num12 {one=1,ein=1,two,zwei=2};

,
, .
.

9.3.
,
, .
,
EQUIVALENCE ().

.

. ,
EQUIVALENCE , ,

.

.
C, C++ union. MSDOS, ,
, .

. ,
.
,
.
MS-DOS
BIOS .
dos.h REGS:
struct WORDREGS // 16-
{ unsigned int ax,bx,cx,dx,si,di,cflag,flags; };
struct BYTEREGS // 8-
{ unsigned char al,ah,bl,bh,cl,ch,dl,dh; }
union REGS {struct WORDREGS x;
struct BYTEREGS h; };

126

,
Intel-8086.
16-
(AX, BX, CX, DX), (SI, DI)
(CFLAG, FLAGS). ,
2 ,
(AH, BH, CH, DH), (AL, BL,
CL, DL). MS-DOS
, .
, ,
:
MOV
MOV
MOV
MOV
INT

AH,2
BH,0
DH,10
DL,25
10H

;
;
;
;
;

2 AH

,

16

(
gotoxy) C :
union REGS r;
...............
r.h.ah=2;
r.h.bh=0;
r.h.dh=y;
r.h.dl=x;
int86(0x10,&r,&r);

//
//
//
//
//
//

"" AH
"" BH
"" DH
"" DL
10h

int86 r ,
, ,
MS-DOS 2,
.
r,
.
, , ,
gotoxy(x,y) .
C++
.
"C++ - ".
10. : : PDA
.
,

127


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

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

10.1.

. ()
BIOS (Basic Input Output System
-). IBM-
BIOS "" (),
BIOS . ,
BIOS ,
,
,
.
(
) ,
a, b, c, d, ... ( A, B, C, ...).
(),
.
, :

, ,
( 8 );
, 3
;
;
;
(A , R , S ,
H );
,
.

128


( 512 ).
.
,
,
. ,

.
FAT (File
Allocation Table ),
,
(EOF End-of-File). ,
.
, , FAT
. Windows XP

NTFS.
.

, ,
. . ,
-
,
, :

C, C++
,
" " :
char namef[]="c:\\bc\\bin\\bc.exe"

,
, .

,
129

, , Read-Only ( )
.
( Far, Disco Commander,
Windows Commander ..) .
, ,
, , - :


( " ");
( ) ( ) ;
(
" ").

,
. -,
(BIOS, ,
). -, MS-DOS, Windows Linux
.

. , BC 3.1 120
60 ,
.

. ,
, k , ,
:

"S1S2S3...Sk" ( ,
);
kS1S2...Sk (k ,
);
S1S2...Sk\0 (\0 ,
);
S1S2...Sk 0D 0A ( , 0D " ",
0A " ").


( , ++
),
.

. , ,
, .

130

, ,
" ", " ", "
", .
, ,
.
(DIRECT ACCESS)
(RANDOM ACCESS).
,
.
.

.
,
.
, .
,
.
, ,
(" ", " ", "
", " ").
.
BC 3.1 BCB
, ,
.

10.1.1. ()
,
, .
, ,
, ,
.
, ..
.
,
0D0A " " " ".


, .

131


. ,

.
/
fprintf.
0D0A,
\n.

FILE fopen "rt"
( ), "wt" ( ), "at" (
):
FILE *f1;
.........
f1=fopen(_, "");


(scanf) (printf).
fscanf fprintf,
:
fscanf(f1,"_", _);
fprintf(f1,"_ \n",_);


str, fprintf
fputs(f1, str).
fgets(str,n,f1). n
,
0A.
C
open, create, read, write.
1. , (..
, ) c_txt
10 .
"Line" (5 , ),
, j,
j. ,
.
, .
.

132

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
main( )
{
FILE *f;
int j,k;
double d;
char s[]="Line";
f=fopen("c_txt","wt");

//

//

//
for(j=1;j<11;j++)
{
fprintf(f,"%s %d %lf\n",s,j,sqrt(j));
//
printf("%s %d %lf\n",s,j,sqrt(j));
//
}
fclose(f);
//
printf("\n");
f=fopen("c_txt","rt");
//
for(j=10; j>0; j--)
{
fscanf(f,"%s %d %lf",s,&k,&d);
//
printf("%s %d %lf\n",s,k,d);
//
}
getch(); return 0;

}
//==
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line

===
1 1.000000
2 1.414214
3 1.732051
4 2.000000
5 2.236068
6 2.449490
7 2.645751
8 2.828427
9 3.000000
10 3.162278

Line
Line
Line
Line
Line
Line
Line
Line
Line
Line

1 1.000000
2 1.414214
3 1.732051
4 2.000000
5 2.236068
6 2.449490
7 2.645751
8 2.828427
9 3.000000
10 3.162278

10.1.
.
%s %d ,
"Line" .

133

s "Line1", "Line2", , "Line10",


k j ( ""),
d .
:
Line1 1.000000
Line2 1.414214
Line3 1.732051
Line4 2.000000
Line5 2.236068
Line6 2.449490
Line7 2.645751
Line8 2.828427
Line9 3.000000
Line10 3.162278
Line11 0.000000
Line21 0.414214
Line31 0.732051
Line42 0.000000
Line52 0.236068
Line62 0.449490
Line72 0.645751
Line82 0.828427
Line93 0.000000
Line103 0.162278

,
. feof:
if(feof(f1))...

//

10.1.2.
,
, .
,
, , ..

ASCII, ,
. ,
.
fopen
"rb" ( ),
"rb+" ( ), "wb" ( ), "wb+"
( ):
FILE *f1;
.........
f1=fopen(_, "");

134

fread fwrite:
c_w = fwrite(buf, size_rec, n_rec, f1);

buf void* ,
;
size_rec ;
n_rec , ;
f1 ;
c_w , .

fread
:
c_r = fread(buf, size_rec, n_rec, f1);

c_r , ;
buf void* ,
.

, fread fwrite.

? , ,
. ,
,
.
.
.
, ,
, , ,

.
,
. fseek
:
fseek(f1,delta,pos);

f1 ;

135

delta ,
;
pos , (0
SEEK_SET , 1 SEEK_CUR , 2
SEEK_END )

{fopen/fclose, fread/fwrite}
BC _dos_open
/__dos_close, _dos_read /_dos_write, _create /_close, _read /_write.

.
2. ,
c_bin 4*10
(, ).
.
.

.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
main( )
{ FILE *f1;
//
int j,k;
char s[]="Line";
int n;
float r;
f1=fopen("c_bin","wb");
//
for(j=1;j<11;j++)
{ r=sqrt(j);
fwrite(s,sizeof(s),1,f1);
//
fwrite(&j,sizeof(int),1,f1);
//
fwrite(&r,sizeof(float),1,f1); //
printf("\n%s %d %f",s,j,r);
//
}
fclose(f1);
//
printf("\n");
f1=fopen("c_bin","rb");
//
for(j=10; j>0; j--)
{//
fseek(f1,(j-1)*(sizeof(s)+sizeof(int)+sizeof(float)),SEEK_SET);
fread(&s,sizeof(s),1,f1);
//
fread(&n,sizeof(int),1,f1);
//
fread(&r,sizeof(float),1,f1);
//
printf("\n%s %d %f",s,n,r);
//
}
getch();
return 0;
}

136

//=== ===
Line 1 1.000000
Line 2 1.414214
Line 3 1.732051
Line 4 2.000000
Line 5 2.236068
Line 6 2.449490
Line 7 2.645751
Line 8 2.828427
Line 9 3.000000
Line 10 3.162278
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line

10 3.162278
9 3.000000
8 2.828427
7 2.645751
6 2.449490
5 2.236068
4 2.000000
3 1.732051
2 1.414214
1 1.000000

10.2.
:
fclose(f1);
f1=fopen("c_bin","rb");

//
//

freopen,
:
f1=freopen("c_bin","rb");

,
,
.

10.1.3.
,
C,
.
fread/fwrite, ..
.
,
.
3.
. () b,
137

(b.s, 5 ,
), (b.n, 2 BC 4 BCB) (b.r, 4
) .
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <conio.h>
main( )
{ FILE *f1;
int j,k;
struct {
char s[5];
int n;
float r;
} b;
strcpy(b.s,"Line");
f1=fopen("c_rec","wb");
for(j=1;j<11;j++)
{ b.n=j;
b.r=sqrt(j);
fwrite(&b,sizeof(b),1,f1);
printf("\n%s %d %f",b.s,b.n,b.r);
}
fclose(f1);
printf("\n");
f1=fopen("c_rec","rb");
for(j=10; j>0; j--)
{ fseek(f1,(j-1)*sizeof(b),SEEK_SET);
fread(&b,sizeof(b),1,f1);
printf("\n%s %d %f",b.s,b.n,b.r);
}
getch();
}

10.2.
C, C++ sprintf sscanf,

.
printf/fprintf scanf/fscanf. ,

char, . sscanf
, sprintf
:
sscanf(str,"_", _);
sprintf(str,"_ \n",_);

138


. sprintf
,
. ,
.

10.3. BCB
BCB

Windows.
. -,

( fHandle),
( handle
, ). -, ,
, AnsiString.
() , .

FileName ( ,
).

10.3.1.

,
.
,
. , ,
, .
FileExists:
if(FileExists(FileName))...

//

MS-DOS access:
k=access(_,0);

//k=0,

10.3.2.

FileCreate:
int fHandle = FileCreate(FileName);

139

,
. 1.

10.3.3.
FileOpen:
int fHandle = FileOpen(FileName, Mode);

Mode , .
, BCB,
10.1.

fmOpenRead
fmOpenWrite
fmOpenReadWrite
fmShareExclusive
fmShareDenyWrite
fmShareDenyRead
fmShareDenyNone

10.1.



,



,

, -1, .

10.3.4.
FileRead:
int k = FileRead(fHandle, buf, count);

buf void* , ;
count ;
k .

10.3.5.
FileWrite:
k = FileWrite(fHandle, buf, count);

140

buf void* , ;
count , ;
k .

k=-1, - .

10.3.6.

. FileSeek:
int k = FileSeek(fHandle, delta, pos);

delta , ;
pos , (0
, 1 , 2 );
k .

:
fLength = FileSeek(fHandle,0,2);

MS-DOS
fseek:
fseek(f1,delta,pos); //f1 FILE

10.3.7.
FileClose:
FileClose(fHandle);

10.3.8.

, , , .
AnsiString:
s=ExtractFileDrive(FileName);
//
s=ExtractFileDir(FileName); //
s=ExtractFileName(FileName); //
s=ExtractFileExt(FileName); //
s=ExtractFilePath(FileName); //

141

MS-DOS
fnsplit, (
) :
fnsplit(const char *path,char *drive,char *dir,char *name,char *ext);

10.3.9.
true,
, false (,
, Read-Only ).
bv = DeleteFile(FileName);
bv = RemoveDir(DirName);

MS-DOS
unlink:
k=unlink(const char *filename);

//k=0,

10.3.10.
CreateDir:
bv = CreateDir(DirName);

, true.

10.3.11.
RenameFile:
bv = FileRename(OldName,NewName);

, true.

10.3.12.
(Extension) ,
ChangeExt:
as = ChangeFileExt(FileName, Extension);

.
(as), .

142

MS-DOS ,
( fnsplit)
, ( fnmerge).

10.3.13.
FileGetAttr ,
:
int k = FileGetAttr(FileName);

10.2 .

faReadOnly
faHidden
faSysFile
faVolumeId
faDirectory
faArchive
faAnyFile

10.2.

0x01
" "
0x02

0x04

0x08
" "
0x10
""
0x20

0x3F


:
if(k & faHidden)...

//

10.3.14.
FileSetAttr:
k = FileSetAttr(FileName,Attr);

, .
10.2:
Attr = faReadOnly | faSysFile;

10.3.15.
, .
,
.
, .
143

,
. BCB ,
:
as = GetCurrentDir();
vb = SetCurrentDir(NameDir);

//
//

10.4.

, .
MS-DOS findfirst (
) findnext ( ).
ffblk,
:
struct ffblk {
char ff_reserved[21];
char ff_attrib;
int ff_time;
int ff_date;
long ff_size;
char ff_name[13];

};

// MS-DOS
//
// /
// /
//
//


dir.h. ,
.
,
.cpp c:\bc\bin:
#include <stdio.h>
#include <conio.h>
#include <dir.h>
void main()
{ struct ffblk qq;
int a;
printf(" *.cpp\n");
a=findfirst("c:\\bc\\bin\\*.cpp",&qq,0);
//
while(!a)
{ printf(" %s\n",qq.ff_name);
a=findnext(&qq);
//
}
getch();
}

findfirst ,
. int

(Read Only, Hidden, System, Archive, Volume, Directory).

144

, ,
.
BCB.
11. : :
PDA

. ,
.
,
,

,
.
, - .
, ,
.
,
.


. 60-
-20 .
. .. -2,
,
.
(Communications of the
ACM, ),
-60 .
,
, .

,
, ,
, ..
SSP (Scientific Subroutine Package) ,
IBM
IBM/360, IBM/370. IBM-
.
, ,
SSP . ,

145


.
.
BC BCB , , ,
(, ,
, , ),
,
. , SSP,
MathCAD, MatLab, Statistica, Mathematica ..

11.1. Borland C++ 3.1


BC 3.1 BC\LIB.
25 .lib,
.obj .c.
,
:

,
. BC BCB .c (
C) .cpp ( C-plus-plus);

.
.obj ( object ).
,
, ..
;

.
, .exe ( execute );
,
.lib ( library ) .dll (
dynamic-link library ).

, BC\LIB, ,

. MS-DOS BC 3.1
Tiny (), Small (), Medium (),
Compact (), Large () Huge ().

, , (
) .. .

146

BC 3.1 17 ,
. 11.1.
600 .
11.1.

Classification routines
Conversion routines
Directory control
routines
Diagnostic routines
Graphics routines
Inline routines
Input/Output routines
Interface routines
Math routines
Memory routines
Miscellaneous routines
Process control routines
Standard routines
String and memory
routines
Text window display
routines
Time and date routines
Variable argument list
routines

12

21
31

3
79
16
110
75
88
28
7
24
30
43




/
BIOS


(, , )


20

25
3


tlib.exe.
:
>tlib.exe lib_name [/C][/E][/P][/0] commands,listfile

lib_name , .lib ;
[/C][/E][/P][/0] ;
commands ;
listfile .

147

/C ,
C (
);
/E ,
, ;
/Pnnnn "" (
MS-DOS 200h);
/0 .

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

+ ;
;
* , ;
+ ( +) ;
* ( *) .

tlib.exe
:
>tlib.exe lib_name,1.txt

1.txt ,
lib_name .
:
Publics by module
ABS
size
_abs
ABSREAD
size
_absread
ACCESS
size
_access
ALLOCA
size
_alloca

= 14
= 292
= 55

_abswrite

= 50

11.2.
, C C++
,
. ,
,
. ,
148

tlib.exe .
.

.

. ,
. , ,
q1.obj, q2.obj q3.obj.
qqq.lib, :
>tlib qqq +q1 +q2 +q3

q1.obj q1.obj,
q4.obj q2.obj,
:
>tlib qqq -+q1 +q4 -q2

,
, 127 , .
:
+q1 +q2 +q3 ... +q15 &
+q16 +q17 ... +q30 &
+q31 +q32 ....

&,
. qq.txt,
tlib :
>tlib qqq @qq.txt



, tlink.exe,
.
BC 3.1 .prj ( project
). , ..

( ) .
Project
Open project ( , )
Project Add item.

149

BCB
.bpr ( Builder Project). Add to Project Project

.
tlink.exe
:
>tlink /k1 /k2

,q1 q2

,eq,mq,lib1 lib2

/k1 /k2... , tlink;


q1 q2 , main;
eq ;
mq , "" ;
lib1 lib2 .

tlink.exe .
. 11.1. ,
, (.obj , .exe
, .map "" , .lib ). ""

.
11.1.

/m
""
/x
""
/i

/l

/s
""
/n

/d

/c

/3
32-
/v

/e

/t
com- exe-

11.3.

150



.dll ( Dynamic-link libraries).
Windows .exe
.drv.

. ,
,
.

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

,
.

. ,

.
12. : Borland C++
3.1: PDA

Borland C++ 3.1. ,

12.1.
BC 3.1 cpp.exe,
:

;
,
;
();
Esc- ;

151

,
.
#.

#include ( . ), :
#include <file_name>
#include "file_name"

,
(, c:\bc\include).
file_name ,
, ,
PATH .
, .
, #include
.h ( header ).

,
.inc.

#define ( . ):
#define s1s2s3...sn

q1q2...qm

s1s2s3...sn
q1q2...qm.
. .
.
"\".

#define:
#include <stdio.h>
#include <conio.h>
#define Nmax 100
#define max(a,b) ((a)>(b))?(a):(b)
#define print(a) printf("\n%s=%d\n",#a,a);\
getch()
void main()
{ int x=5,y=8;
int z=Nmax;
int w=max(x*y,z);

152

print(x);
print(y);
print(z);
print(w);
}
//=== ===
x=5
y=8
z=100
w=100

.
-, - max
.
. ,

. Square(x)
(#define Square(x) x*x),
Square(1+z) 1+z*1+z=2*z+1, ..
.

. , max
,
:
w=max(x+=2,y+=3);
w=max(x+2,y+3);

// w=14
// w=11


(Lvalue required ).
-, max .
-, print
(#a
a).
(printf("\na=%d",a);), .
,
'a'.

( , ):
#define Paste(a,b)

a##b

Paste(x,4) x4.

153

"
":
#define...#ifdef...ifndef...#undef
#if...#elif...#elif...#else...#endif

,
,
.
...BC\INCLUDE, ,
math.h, (
) :
#ifndef __MATH_H
#define __MATH_H
#if !defined(___DEFS_H)
#include <_defs.h>
#endif

? ,
__MATH_H (
, .. ).
,
, ( ,
), ,
#include <math.h>, .
, math.h
.
, -
.
.
,
___DEFS_H. , defs.h
. #endif
. ,
,
defs.h.
,
:

#define name
value

12.1.

name
value ,

154

#ifdef name
#ifndef name
#undef name

, name (true,
)
, name (true,
)
name ,


,
if then else:
#if (_1)
_1
#elif (_2)
_2
#elif (_3)
_3
.............
#else

_k
#endif

// _1
// _2
// _3
//
//

,
.
( ,
). , ,
:
#define name 1
.............
#if (name==1)

#if...#endif
.

12.2. bcc.exe

bcc.exe. ,
. MS-DOS
640 580 610 (
Norton Commander),
300 .
:
>bcc [] file1 file2 ...

155

, , .12.2.

( -K- -K).
, ,
:

.obj,
;
.lib,
;
.asm,
TASM.EXE.
, ,
, C,
C++, .
12.2.

-1
-2
-Ax
-B
-C
-Dxxx
-Exxx
-G
-Hxxx
-Ixxx
-K
-Lxxx
-M
-N
-Ox
-P
-Qxxx
-S
-Txxx
-Uxxx

80186/286
80286


,






char




C++ (
.c)




156

-Vx
-Wxxx Windows
-X

-Yx
-Z

-a

-b
enum
-c

-d

-exxx
-fxx
-gN N
-iN

-jN
N
-k

-lx

-mx
-nxxx
-oxxx
-p


-r

-u

-v

-wxxx
-y

-zxxx

bcc.exe
,
, .

12.3. grep.com
BC 3.1 BCB
grep.com. ,
Global Regular Expression Print
.
Generalized Regular Expression Parser
. ,
.
157


, *.txt ?set.txt.
* (
), ? .
:
>grep [ ] _ _

,
''. , A -D.
( ) ( ). R
R+. .
: -C -D -I -CDI -CI -D.
, ..
.
. 12.3.
12.3.

-C
. .
-D+ .
-D .
-I+
(
).
-L ,
-N ,
-O Unix
-R+
-U , GREP ,
grep.com
-V ,
.
-W- . ,
.
-Z , (
0)
S1S2...Sk , ,
(
), . -R,
,
, :
158

"^S1S2...Sk" "" (caret) ,


;
"S1S2...Sk$" ,
;
. ;
\ , (, \.
);
* 0
(, qw* q, qw, qww, qwww, ...);
+ 1
(, qw+ qw, qww, qwww, ...);
[*alpha;1*alpha;2... *alpha;k] ,
;
[^*alpha;1*alpha;2... *alpha;k] ,
;

[0-9] ;
[a-jx -z] a j, x ,y, z

-u.
grep.com

grep.
.
,
. :
>grep -W fun1 *.c *.cpp

fun1
.c .cpp.
>grep -D "my book" \*.doc

.doc
. ,
.

bat-, , grep.bat:
grep.com -i -d -w -n %1 *.c *.cpp > 1.txt

:
159

grep.bat word

word , bat- %1.


, 1.txt,
.
(
).
13. : . C++: PDA
.
,

13.1. ()
, ,
, . :

sin(x) x;
dsin(dx)
;
csin(cx) cx;
cdsin(cdx)
.

,
,
, .
,
sin(z).
, .

"" 4 .
C ,
. ,
.
. C:
void print_int(char *nx,int x)
{ printf("\n%s=%d",nx,x); }
void print_float(char *nx,float x)
{ printf("\n%s=%f",nx,x); }
void print_double(char *nx,double x)
{ printf("\n%s=%lf",nx,x); }

C++ :

160

void print (char *nx,int x)


{ printf("\n%s=%d",nx,x); }
void print (char *nx,float x)
{ printf("\n%s=%f",nx,x); }
void print (char *nx,double x)
{ printf("\n%s=%lf",nx,x); }

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


.

13.2.
C ,
, .
swap,
, ..
C++ ,
.
. ,
, , ,
"" ,
.

:
template <class Type>

template ( . );
class ( . ). ,
. C++ :
typename . BC 3.1 ;
Type ,
.

, Type
:
template <class Type> void swap(Type &x,Type &y)
{ Type tmp=x; x=y; y=tmp; }

161

,
,
, . ,
swap,
, .
,
swap ..
,
.

:
template <class Type1, class Type2, class Type3>...

14. : . : PDA
.

C++
,
.
String (), Set (), Complex (
) .

14.1.
, (num)
(denum). . (+
+. -. .: -, 2004. 512 ), . .
(" ++",...).
:
struct Rational {unsigned num,denum;};

Rational
:
Rational x,y,z[20];
x.num=1; x.denum=3;

, ,
.

162

(
" "):
unsigned gcd(unsigned x,unsigned y)
{//
if(y==0) return x;
return gcd(y,x%y);
}
void reduce(Rational &c)
{//
unsigned t=((c.num>c.denum)?gcd(c.num,c.denum):gcd(c.denum,c.num));
c.num /= t; c.denum /= t;
}


. ,
, , ,
. C++
"" .
+=, :

Rational& operator+=(Rational &a, const Rational &b)


{ a.num = a.num*b.denum + b.num*a.denum;
a.denum *= b.denum;
reduce(a); return a;
}

,
.
:
Rational operator+(Rational &a,const Rational &b)
{ Rational t=a; t += b; reduce(t); return t; }

/.
-,
(istream &) (ostream &) . -,

.
:
istream& operator>>(istream &t, Rational &a)
{ char s;
t >> a.num; t>>s; t>> a.denum;
reduce(a); return t;
}

163

s, ,
"/", .
ostream& operator<<(ostream &t, const Rational &a)
{ t << a.num << '/'<<a.denum;
return t;
}

:
void main()
{ Rational A,B,C;
A.num=1; A.denum=2;
B.num=1; B.denum=3;
C=A+B;
cout << C << endl;
getch();
}
//=== ===
5/6

"=" , .
, . ,
("[]")
:
void main()
{ Rational d[5],c;
int i;
cout<<"Enter 5 rational number:"
cout<<" num / denum <Enter>"<<endl;
for(i=0; i<5;i++) cin>>d[i];
for(i=0; i<5;i++) cout<<d[i]<<' ';
c=d[0]+d[1];
c += d[2]; c += d[3]; c+=d[4];
cout<<endl<<c<<endl;
getch();
}
//=== ===
Enter 5 rational number: num / denum <Enter>
1/1
2/2
3/3
4/4
5/5
1/1 1/1 1/1 1/1 1/1
5/1


, -
:
Rational operator*(const Rational &a, const Rational &b)

164

{ Rational c;
c.num=a.num*b.num;
c.denum=a.denum*b.denum;
reduce(c); return c;
}
Rational operator*(const Rational &a, const unsigned &b)
{ Rational c;
c.num=a.num*b;
c.denum=a.denum;
reduce(c); return c;
}
Rational operator*(const unsigned &a, const Rational &b)
{ Rational c;
c.num=a*b.num;
c.denum=b.denum;
reduce(c); return c;
}


- :
Rational operator+=(Rational &a, const unsigned &b)
{ a.num=a.num+b*a.denum;
reduce(a); return a;
}

x++ ++x . ,
(operator++), -.
x,
1. x,
. ,
+ ,
, 0. (++x) ,
(x++) int.
,
:
Rational operator++(Rational &a)
{// ++a
a += 1; return a;
}
Rational operator++(Rational &a, int)
{// a++
Rational t=a; a += 1; return t;
}

, ,
.
.
, , ,
.

165

, rational.h. ,
, :
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include "rational.h"
void main()
{ Rational A,B,C;
A.num=1; A.denum=2;
B.num=1; B.denum=3;
C=A+B;
cout << C << endl;
getch();
}

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

.
.
( void).

.

166

,
.
, ,
.
.
,
. -
:
//
Rational() { num=0; denum=1; }
//
Rational(unsigned n) { num=n; denum=1; }
//
Rational(unsigned n,unsigned d) {if(d!=0){num=n; denum=d;}}
//
Rational(const Rational &r) { num=r.num; denum=r.denum; }

C++ inline-,
:
Rational():num(0),denum(1){}
Rational(unsigned n):num(n),denum(1){}
Rational(unsigned n,unsigned d):num(n),denum((d!=0)?d:1){}
Rational(const Rational &r):num(r.num),denum(r.denum){}

rational.h
:
Rational A(1,2),B(1,3),C;

, , , C
. ,
, double,
float, .
.
.
Rational unsigned,
(Rational* Rational,
Rational*unsigned, unsigned* Rational).

. , unsigned Rational
:
Rational(unsigned n=0,unsigned d=1)
{ if(d!=0){num=n; denum=d; }}

167


Rational :
Rational x(5,1);

//-

Rational x(5);
Rational x=5;

//-, unsigned
//-

Rational

Rational unsigned
, :
operator unsigned(){return num/denum;}

unsigned
- .

14.2.
C++
.

class. -
.
rational.h, ,
, .
#ifndef __RATIONAL_H
#define __RATIONAL_H
// Rational (., .)
#include <iostream.h>
#include <stdlib.h>
//---------------------------------------------------------------class Rational
{
private:
long num,den;
// ,
Rational(long num,long den);
//-
void Reduce(void);
//
long gcd(long m,long n);
//
public:
Rational(int num=0, int denom=1); // int->Rational
Rational(double x);
// double->Rational
///
friend istream& operator>>(istream& t,Rational &r);
friend ostream& operator<<(ostream& t,const Rational &r);
//
Rational operator+(Rational r)const;
Rational operator-(Rational r)const;
Rational operator*(Rational r)const;
Rational operator/(Rational r)const;

168

// ,
Rational operator-(void)const;
//
int operator<(Rational r)const;
int operator>(Rational r)const;
int operator==(Rational r)const;
// Rational->double
operator double(void)const;
//-
long GetNum(void)const;
long GetDen(void)const;
};
//
#endif

14.1.

rational.cpp, :
#include "rational.h"
//
long Rational::gcd(long x,long y)
{ if(y==0)return x; return gcd(y,x%y); }
//------------------------------------------------------// Rational+Rational
Rational Rational::operator+(Rational r)const
{ Rational t;
t.num=num*r.den+den*r.num; t.den=den*r.den;
t.Reduce(); return t; }
//------------------------------------------------------// Rational-Rational
Rational Rational::operator-(Rational r)const
{ Rational t;
t.num=num*r.den-den*r.num; t.den=den*r.den;
t.Reduce(); return t; }
//-------------------------------------------------------// Rational*Rational
Rational Rational::operator*(Rational r)const
{ Rational t;
t.num=num*r.num; t.den=den*r.den; t.Reduce();
return t; }
//------------------------------------------------------// Rational/Rational
Rational Rational::operator/(Rational r)const
{ Rational t=Rational(num*r.den,den*r.num);
t.Reduce(); return t; }
//------------------------------------------------------// ==
int Rational::operator==(Rational r)const
{ return num*r.den==den*r.num; }
//------------------------------------------------------// >
int Rational::operator>(Rational r)const
{ return num*r.den>den*r.num; }
//------------------------------------------------------// <

169

int Rational::operator<(Rational r)const


{ return num*r.den<den*r.num; }
//-------------------------------------------------------//
Rational Rational::operator-(void)const
{ return Rational(-num, den); }
//------------------------------------------------------// P/Q ( )
istream& operator>>(istream& t,Rational &r)
{ char c; // /
t>>r.num>>c>>r.den;
if(r.den==0) { cerr<<"Denominator=0!"; exit(1); }
r.Reduce(); return t; }
//------------------------------------------------------// P/Q ( )
ostream& operator<<(ostream& t,const Rational &r)
{ t<<r.num<<'/'<<r.den; return t; }
//------------------------------------------------------// Rational(p,q)
Rational:: Rational(long p,long q):num(p),den(q)
{ if(den==0) { cerr<<"Denominator=0!"; exit(1); } }
//------------------------------------------------------// Rational(p,q)
Rational:: Rational(int p,int q):num(p),den(q)
{ if(den==0) { cerr<<"Denominator=0!"; exit(1); } }
//------------------------------------------------------// double->Rational
Rational:: Rational(double x)
{ double val1,val2;
val1=100000000L*x;
val2=10000000L*x;
num=long(val1-val2); den=90000000L;
Reduce(); }
//-----------------------------------------------------// Rational->double
Rational::operator double(void)const
{ return double(num)/den; }
//-----------------------------------------------------//
void Rational::Reduce(void)
{ long bigdiv,temp;
temp=(num<0)?-num:num;
if(num==0)den=1;
else
{ bigdiv=gcd(temp,den);
if(bigdiv>1)
{ num /= bigdiv; den /= bigdiv; }
}
}
//--------------------------------------------------------//
long Rational::GetNum(void)const
{ return num; }
//--------------------------------------------------------//
long Rational::GetDen(void)const
{ return den; }
//---------------------------------------------------------

170

14.2.
class,
. .
private public
, ()
. ,
( - ), ,
( friend).
,
, .
- -
, .
- , ,
, :
Rational A;
A.num=1; A.den=3;

, -
.
,
SetNum SetDen (,
, GetNum GetDen).
,
, .
-. ,
,
Rational::. ,
- .
.
- Rational
. :
k = fun(obj1,obj2);
k = obj1.met(obj2);

// obj1 obj2
//

, "" ,
. ,

this ( . ).
Reduce
Rational : t.Reduce().
: Reduce(t).

171

- Rational ,
.
. -, ,
num/den. -,
. ,
double Rational.
, .
, .
Rational
.
, ,
- , .
.
#include <iostream.h>
#include <conio.h>
#include "rational.cpp"
void main()
{ Rational r1(5),r2,r3;
double d;
d=r1.GetNum();
cout<<"d="<<d<<endl;
cout<<"1.Rational - value 5 is "<<r1<<endl;
cout<<"2.Input Rational number: ";
cin>>r1;
d=double(r1);
cout<<"Equivalent of double: "<<d<<endl;
cout<<"3.Input two Rational number: ";
cin>>r1>>r2;
cout<<"Results: "<<endl;
cout<<r1<<" + "<<r2<<"="<<(r1+r2)<<endl;
cout<<r1<<" - "<<r2<<"="<<(r1-r2)<<endl;
cout<<r1<<" * "<<r2<<"="<<(r1*r2)<<endl;
cout<<r1<<" / "<<r2<<"="<<(r1/r2)<<endl;
if(r1<r2)
cout<<"Relation < : " <<r1<<"<"<<r2<<endl;
if(r1==r2)
cout<<"Relation = : " <<r1<<"="<<r2<<endl;
if(r1>r2)
cout<<"Relation > : " <<r1<<" > "<<r2<<endl;
cout<<"4.Input double number: ";
cin>>d;
r1=d;
cout<<"Convert to Rational: "<<r1<<endl;
d=r1;
cout<<"Convert to double: "<<d<<endl;
getch();
}
//=== ===

172

14.3.
.
. " C++". .: , 2000, 416 . ,
double.
#include <iostream.h>
#include <conio.h>
union bits {
double d;
unsigned char c[sizeof(double)];
bits(double n);
void show_bits();
};
bits::bits(double n) {d=n;}
void bits::show_bits()
{ int i,j;
for(j=sizeof(double)-1; j>=0; j--)
{ cout<<"Byte "<<j<<": ";
for(i=128; i; i>>=1)
if(i & c[j]) cout << "1";
else cout<< "0";
cout<<"\n";
}
}
void main()
{ bits qq(2006.0203);
qq.show_bits();
getch();
}
//=== ===
Byte 7: 01000000
Byte 6: 10011111
Byte 5: 01011000
Byte 4: 00010100
Byte 3: 11001001
Byte 2: 10000101
Byte 1: 11110000

//
//
//
// d

173

Byte 0: 01101111

. -,
i.
.

c. -,
. ,
( ,
, union,
, class). , ,
. , ,
.

14.4.
C++
. ..
" " Decade, 0 9
.
"" 10,
( 10),
Decade .
decade.h, :
#ifndef _DECADE_H
#define _DECADE_H
//
enum Decade {zero,one,two,three,four,five,six,seven,eight,nine};
// +
Decade operator+(const Decade &a, const Decade &b)
{ return Decade((int(a)+int(b))%10); }
// +=
Decade operator+=(Decade &a, const Decade &b)
{ return a=Decade(int(a+b) % 10); }
// ++a
Decade operator++(Decade &a)
{ a=Decade((int(a)+1) % 10); return a; }
// a++
Decade operator++( Decade &a,int)
{ Decade t=a;
a=Decade((int(a)+1) % 10); return Decade(t); }
// <<
ostream& operator<<(ostream &out, const Decade &a)
{ char *s[]={"zero","one","two","three","four","five","six","seven",
"eight","nine"};
out<<s[a]; return out; }
// Decade+int
Decade operator+(const Decade &a, const int b)
{ int t= ((int)a + b) % 10;
return Decade(t); }

174

// int+Decade
Decade operator+(const int a, const Decade &b)
{ return Decade((a+int(b)) % 10); }
#endif

:
#include <iostream.h>
#include <conio.h>
#include "decade.h"
void main()
{ Decade A=seven,B=six;
cout<<"A="<<A<<endl;
cout<<"B="<<B<<endl;
cout<<"A+B="<<A+B<<endl;
A += B;
cout<<"A="<<A<<endl;
++A;
cout<<"A="<<A<<endl;
A++;
cout<<"A="<<A<<endl;
B=A+6;
cout<<"B="<<B<<endl;
A=3+B;
cout<<"A="<<A<<endl;
getch();
}

// A=seven
// B=six
// A+B=three
// A=three
// A=four
// A=five
// B=one
// A=four

, , .

.
.
""
,
.

14.5.
(inline) ,
.
,
, ,
..
inline- .
, ,
.

#define.

(abs(x)),
175

.. , ,

. ,
, inline
:
inline int even(int x)
return !(x%2); }
inline double min(double a,double b)
{ return a < b ? a : b; }
{

- ,
,
inline.
. -,
, inline :
class sample {
int i,j;
//
public
sample(int x,int y):i(x),j(y){}
//
int is_divisor();
//
};
inline int sample::is_divisor()
//
{ return !(i%j); }


, .
, , .
:
#define Cube(x) (x)*(x)*(x)

.
. ,
x.
:
q=4;
cout << Cube(q++);

:
cout << (q++)*(q++)*(q++);


5*6*7. ?

176

,
C :
inline int Cube(int x) { return x*x*x; }

,
,
:
inline template <class T> T Cube(T x) { return x*x*x; }

:
#include <iostream.h>
#include <conio.h>
inline template <class T> T Cube(T x) { return x*x*x; }
void main()
{ int x=2;
float y=3;
double z=4;
cout<<Cube(x)<<endl;
cout<<Cube(y)<<endl;
cout<<Cube(z)<<endl;
getch();
}
//=== ===
8
27
64

14.6. ()
, ,
(, , ,
/ ..) . ,
, ,
.
, ,
.
:
_ _::operator (_)
{ // }

.

, .

177

,
, /,
, ,
:

, C++, ;
, ,
;
'.' ( ), '.*'
( ), '::' (
), '?:' ( ), '#' ( ),
'##' ( ).

a1 a2
, a1, ,
- . -, ,
- . -, this.

.
,
.

Tpoint, :
class Tpoint {
int x,y;
public
Tpoint(){x=0;y=0;}
//
Tpoint(int xx,int yy){x=xx;y=yy;}
//
void GeTpoint(int &xx,int &yy){xx=x; yy=y;} //
Tpoint operator+(Tpoint P2);
};

, ,
.
:
Tpoint Tpoint::operator+(Tpoint P2)
{ Tpoint q;
q.x=x+P2.x; q.y=y+P2.y;
return q;
}


P2:
Tpoint Tpoint::operator+(Tpoint &P2)

178

{ Tpoint q;
q.x=x+P2.x; q.y=y+P2.y;
return q;
}

-, P2 4 , P2
. -,
P2, ,
( q),
.
Tpoint a1=a2
, this,
:
Tpoint Tpoint::operator=(Tpoint &P2)
{ x=P2.x; y=P2.y; return *this; }

(
)
, ( )
():
int Tpoint::operator==( Tpoint &P2)
{ return (x==P2.x)&&(y==P2.y); }

( )
,
, , "".
,
:
Tpoint Tpoint::operator-(Tpoint &P2)
{ Tpoint q;
q.x=x-P2.x; q.y=y-P2.y; return q;
}
Tpoint Tpoint::operator-()
{ x=-x; y=-y; return *this; }

,
. ++P1
-P1, :
Tpoint Tpoint::operator++()
{ x++; y++; return *this; }

P1++ C++
, ( )
, .

179

P1++ ""
:
Tpoint Tpoint::operator++(int P)
{ Tpoint q=*this;
x++; y++; return q; }


.
, this . , ,
'='.
,
:
class Tpoint {
int x,y;
public
Tpoint(){x=0;y=0;}
//
Tpoint(int xx,int yy){x=xx;y=yy;}
//
void GeTpoint(int &xx,int &yy){xx=x; yy=y;} //
friend Tpoint operator+(Tpoint P1,Tpoint P2);
};
Tpoint Tpoint::operator+(Tpoint P1,Tpoint P2)
{ Tpoint q;
q.x=P1.x+P2.x; q.y=P1.y+P2.y; return q;
}


Rational.
"". ,
"" (x,y), x y
. ,
(istream &)
(ostream &) .

.
:
#include <iostream.h>
#include <conio.h>
class Tpoint {
int x,y;
public
Tpoint(){x=0;y=0;}
//
Tpoint(int xx,int yy){x=xx;y=yy;}
//
void GetPoint(int &xx,int &yy){xx=x; yy=y;} //
friend istream& operator>>(istream& t,Tpoint &P);
friend ostream& operator<<(ostream& t,Tpoint &P);
};
// (x,y) ( )
istream& operator>>(istream& t,Tpoint &P)
{ char c; // /

180

t >> c >> P.x >> c >> P.y >> c;


return t; }
//------------------------------------------------------// (x,y) ( )
ostream& operator<<(ostream& t,Tpoint &P)
{ t<<'('<<P.x<<','<<P.y<<')'; return t; }
void main()
{ Tpoint P(10,20);
cout<<P<<endl;
cin>>P;
cout<<P<<endl;
getch();
}
//===
(10,20)
(30,50) <Enter>
//
(30,50)

14.7. ()

C++ ,
, .
()
().

( , )
.
, ,
:

(
, );
,
;

.


:
#include <iostream.h>
class B {
int x;
public:
B(){x=0; cout<<"Def_Constr_B "<<this<<endl;}
B(int y){x=y; cout<<"Init_Constr_B "<<this<<endl;}
B(const B &z){x=z.x; cout<<"Copy_Constr_B "<<this<<endl;}
int get_x(){return x;}
~B(){cout<<"Destr B"<<this<<endl;}

181

};
void main()
{ B q1;
//
B q2(2);
//
B q3(q2);
//
B q4=q1;
//
cout<<"q1="<<q1.get_x()<<endl;
cout<<"q2="<<q2.get_x()<<endl;
cout<<"q3="<<q3.get_x()<<endl;
cout<<"q4="<<q4.get_x()<<endl;
}
//=== ===
Def_Constr_B 0012FF88
// , q1.x
Init_Constr_B 0012FF84
// , q2.x
Copy_Constr_B 0012FF80
// , q3.x
Copy_Constr_B 0012FF7C
// , q4.x
q1=0
q2=2
q3=2
q4=0
//
Destr B 0012FF7C
// q4
Destr B 0012FF80
// q3
Destr B 0012FF84
// q2
Destr B 0012FF88
// q1

14.3.
,
(
)
.
:

;

;
, ,
.
.
;
,
.
,
.

,
.

182

,
, Rational
.
, , ,
, .
. ,
,
. "",
. :
Rational v1;
Rational v2(1,2);
Rational v3(v2);
~Rational();

//
//
//
//

, ..
, .
.
,
. , ,
:
class array {
int size;
char *p;
public:
array(int dim);
//

~array(){ delete [] p; }
//
.........................
};
array::array(int dim)
//
{ p=new char[dim];
//
if(!p)
//
{ cout<<"Allocation error"; exit(1); }
size=dim;
}

15. : :
PDA
,
.


. C+
+ , ,
.
183



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

,
.

15.1.
D, B,
, .
(inheritance) . ,
, .
,
.
.

15.1.1.
, D
B?
class D: [virtual][public|private|protected] B
{ };

virtual ()
( . 15.2).
public () private () ,
(struct) (class),
protected ().
,
. D
,
. C++
:
15.1.
B
public

D
D
D=struc
D=class

public
private
184

protected
private
public
protected
private
public
protected
private
public
protected
private

public
public
public
protected
protected
protected
private
private
private

public
private

public
public
protected
protected

protected
protected
protected
protected

private
private
private
private


.
. 15.1 .
,
:
class D: public B { };

,
,
,
.

, .

. ,
B.

D . x
, setb showb D public.
D :
#include <iostream.h>
#include <conio.h>
class B {
int b;
public:
void setb(int n){b=n;}
void showb(){cout<<"in B b="<<b<<endl;}
};
class D: public B {

185

int d;
public:
void setd(int n){d=n;}
void showd(){cout<<"in D d="<<d<<endl;}
};
void main()
{ D qq;
//
qq.setb(1); // qq.x
qq.showb(); //
qq.setd(2); // qq.y
qq.showd(); //
qq.showb(); //
getch();
}
//=== ===
in B b=1
//qq.x
in D d=2
//qq.y
in B b=1
//qq.x

, setd qq.x
.
:
class D: private B {
int d;
public:
void setbd(int n,int m)
{ setb(n);
// D private,
d=m; }
void showbd()
{ showb();
// D private,
cout<<"in D d="<<d<<endl;}
};
void main()
{ D qq;
//
qq.setbd(1,2);
qq.showbd();
getch();
}

, B
. D
protected setb showb D
protected, - ,
.

15.1.2.

. B
x, ( ,

186

), .
. D,
x private,
y. ,
.
w1, w2, w3 w4 B,
q1, q2, q3 q4 D.

.
.
#include <iostream.h>
#include <conio.h>
class B {
int x;
public:
B(){x=0; cout<<"Def_B "<<endl;}
B(int n){x=n; cout<<"Init_B "<<endl;}
B(const B &y){x=y.x; cout<<"Copy_B "<<endl;}
int get_x(){return x;}
~B(){cout<<"Destr_B"<<endl;}
};
class D : public B {
int y;
public:
D(){y=0; cout<<"Def_D "<<endl;}
D(int n){y=n; cout<<"Init_D "<<endl;}
D(const D &z){y=z.y; cout<<"Copy_D "<<endl;}
int get_y(){return y;}
~D(){cout<<"Destr_D"<<endl;}
};
void main()
{ B w1;
cout<<"w1.x="<<w1.get_x()<<endl;
B w2(2);
cout<<"w2.x="<<w2.get_x()<<endl;
B w3(w2);
cout<<"w3.x="<<w3.get_x()<<endl;
B w4=w1;
cout<<"w4.x="<<w4.get_x()<<endl;
D q1;
cout<<"q1.x="<<q1.get_x()<<' '<<"q1.y="<<q1.get_y()<<endl;
D q2(2);
cout<<"q2.x="<<q2.get_x()<<' '<<"q2.y="<<q2.get_y()<<endl;
D q3(q2);
cout<<"q3.x="<<q3.get_x()<<' '<<"q3.y="<<q3.get_y()<<endl;
D q4=q1;
cout<<"q4.x="<<q4.get_x()<<' '<<"q4.y="<<q4.get_y()<<endl;
}
//=== ===
Def_B
// B w1.x
w1.x=0
//
Init_B
// B w2.x

187

w2.x=2
Copy_B
w3.x=2
Copy_B
w4.x=0
Def_B
Def_D
q1.x=0 q1.y=0
Def_B
Init_D
q2.x=0 q2.y=2
Def_B
Copy_D
q3.x=0 q3.y=2
Def_B
Copy_D
q4.x=0 q4.y=0
Destr_D
Destr_B
Destr_D
Destr_B
Destr_D
Destr_B
Destr_D
Destr_B
Destr_B
Destr_B
Destr_B
Destr_B

//
// B w3.x
//
// B w4.x
//
// B q1.x
// D q1.y
//
// B q2.x
// D q2.y
//
// B q3.x
// D w3.y
//
// B q4.x
// D w4.y
//
// D w4.y
// B w4.x
// D w3.y
// B w3.x
// D w2.y
// B w2.x
// D w1.y
// B w1.x
// B q4.x
// B q3.x
// B q2.x
// B q1.x

15.1.
,

. , ,
.
, ,
. ,
.

.
,
. (protected)
this.
. Point2D,
:
class Point2D {
int x,y;
// Point2D
public:
Point2D(int xx,int yy):x(xx),y(yy){} //
Point2D(const Point2D &P):x(P.x),y(P.y){} //
int get_x(){return x;}

188

int get_y(){return y;}

};

Point3D :
class Point3D: public Point2D {
int z;
// Point3D
public:
Point3D(int xx,int yy,int zz):Point2D(xx,yy),z(zz){}
int get_z(){return z;}
// Point3D
};

:
#include <iostream.h>
#include <conio.h>
void main()
{ Point2D P2(1,2);
Point3D P3(3,4,5);
cout<<"P3.x="<<P3.get_x()<<" P3.y="<<P3.get_y()<<"
P3.z=" <<P3.get_z()<<endl;
cout<<"P2.x="<<P2.get_x()<<" P2.y="<<P2.get_y()<<endl;
P2=P3;
cout<<"P2.x="<<P2.get_x()<<" P2.y="<<P2.get_y()<<endl;
getch();
}
//=== ===
P3.x=3 P3.y=4 P3.z=5
P2.x=1 P2.y=2
P2.x=3 P2.y=4


(x,y) .

Point2D, Point3D.
P2
P3.
, , , ""
P3.z . P3=P2 , ..
"", P3.z.
(x,y) (protected),

:
Point3D(int xx,int yy,int zz):z(zz)
{ this->x=xx; this->y=yy; }

189

15.1.3.
,
, .
.
new delete:
class A {...};
..............
A *ps=new A;
A* *pa=new A[20];
...............
delete ps;
delete [] pa;

//
// A
//
// ps
// pa

, new ,
delete .
,
:
A *ptr1=new A(5);//

.
C++
malloc ( ) free (
).
new/delete , ,
. ,
sizeof, .
, malloc
void* .
,
,
. , , ,
, ps, .
- ,
.
:
delete ps;
ps=NULL;

// ps=0;

15.1.4.
,
.
190

virtual.
, virtual .

virtual.

.
. ,
.
, ,
.
,
.
, B n
. D1
. D2,
B, .
#include <iostream.h>
#include <conio.h>
class B {
public:
B(int k):n(k){}
//
virtual void show(){cout<<n<<endl;} //
protected:
int n;
};
class D1: public B {
public:
D1(int k):B(k){}
//
virtual void show(){cout<<n*n<<endl;}
};
class D2: public B {
public:
D2(int k):B(k){}
//
virtual void show(){cout<<n*n*n<<endl;}
};
void main()
{ B bb(2),*ptr;
D1 dd1(2);
D2 dd2(2);
ptr=&bb;
ptr->show();
ptr=&dd1;
ptr->show();
ptr=&dd2;
ptr->show();
getch();
}
//=== ===
2
// B::show

191

4
8

// D1::show
// D2::show

15.2.
,
,
. .

:
B *bptr;
D1 dd1(2);
bptr=&dd1;
D1 *dptr;
dptr=(D1 *)bptr;

//
//

15.1.5.
,
,
.
: Shape (
), Circle (, Shape) Rectangle
(, Shape):
#include <iostream.h>
class Shape {
public:
Shape();
//
~Shape();
//
virtual void show() {cout <<"Shape"<<endl;
};
class Circle: public Shape {
int xc,yc,r; //
public:
Circle(int x,int y,int R):xc(x),yc(y),r(R) {}
//
~Circle();
//
void show() {cout<<"x="<<xc<<"
y="<<yc<<" r="<<r<<endl;
};
class Rectangle: public Shape {
int x1,y1,x2,y2;
//
public:
Rectangle(int ix1,int iy1,int ix2,int iy2):
x1(ix1),y1(iy1),x2(ux2),y2(iy2) {}
//
~Rectangle(); //


:
Shape *ptr_s[2];
ptr_s[0]=new Circle(20,20,10)

192

ptr_s[1]=new Rectangle(20,40,50,50);

, .
:
for(int i=0; i<2; i++) delete ptr_s[i];

,
Shape (
ptr_s). , ,
.
(virtual ~Shape();).
(
). , ,
.

,
.
, (.. ,
). .

15.1.6.
,
:
virtual name_f(1 a1,2 a2,...)=0;

, ,
. ,
,
.
Shape ( ),

(Get_Area) (Get_Perim).
class Shape {
public:
Shape(){}
//
virtual double Get_Area()=0;
virtual double Get_Perim()=0;
};
class Rectangle: public Shape {
double w,h; //
public:
Rectangle(double w1,double h1):w(w1),h(h1) {}
double Get_Area() {return w*h;}

193

double Get_Perim() {return 2*w+2*h);}


};
class Circle: public Shape {
double r;
//
public:
Circle(double r1):r(r1) {}
double Get_Area() {return M_PI*r*r;}
double Get_Perim() {return 2*M_PI*r;}
};


,
( ) .

15.2.
,
:
class B1 {//
int x;
public:
B1(int n):x(n) {cout<<"Init_B1"<<endl;}
// B1
int get_x(){return x;}
~B1() {cout<<"Destr_B1"<<endl;}
// B1
};
class B2 {//
int y;
public:
B2(int n):y(n) {cout<<"Init_B2"<<endl;}
// B2
int get_y(){return y;}
~B2() {cout<<"Destr_B2"<<endl;}
// B2
};
class D: public B1, public B2 {
int z;
public:
D(int a,int b,int c):B1(a),B2(b),z(c)
{cout<<"Init_D"<<endl;}
// D
void show() {cout<<"x="<<get_x()<<" y="<<get_y()<<" z="<<z<<endl;}
~D() {cout<<"Destr_D"<<endl;}
// D
};
#include <iostream.h>
void main()
{ D qq(1,2,3);
qq.show();
}
//=== ===
Init_B1
Init_B2
Init_D
x=1 y=2 z=3
Destr_D
Destr_B2
Destr_B1

194

15.3.

.
,
.
,
,
. , A B
C. D A C,
- A D ,
C.

. A
:
class B: virtual public A {
...// B
};
class C: virtual public A, public B {
// C
};


,
.

15.3. -

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

, .
, ,
. ,
BC 3.1 BGI (Borland

195

Graphics Interface),
( VGA 640480)
.
.
- .
,
gs.h ( Graphics System).
()
. gs.h :
#include <graphics.h>
int gs;
void open_gs()
{ int gd=0,gm;
initgraph(&gd,&gm,"");
gs=1;
}
void close_gs()
{ closegraph(); gs=0; }

graphics.h
BGI. gs, ,
( gs=1
,
gs). BGI
initgraph egavga.bgi,
.

closegraph BGI.
BGI . ""
open_gs, close_gs
( ).
GO (
Graphics Object).
class GO {
protected:
int x,y,is_v,fc,bc;
public:
GO():x(0),y(0),is_v(0),bc(15),fc(0)
{ setcolor(fc);setbkcolor(bc); }
GO(int x1,int y1,int c=0):x(x1),y(y1),is_v(0),fc(c),bc(15)
{ setcolor(fc);setbkcolor(bc); }
virtual void hide()=0;
virtual void show()=0;
void move(int x1,int y1);
};

196

x,y
( "" ,
);
is_v (
is_v=1);
fc ( [0,15]);
bc ( [0,15]).

,
( ).
setcolor (fc=0),
setbkcolor (bc=15).
GO hide (
) show ( ). move
.
:
void GO::move(int x1,int y1)
{ hide();
//
x=x1; y=y1;
//
show();
//
}

point,
"" .
GO ( point ).
point ,
.
class point: public GO {
public:
point():GO() {}
point(int x1,int y1,int c=0):GO(x1,y1,c) {}
void hide();
void show();
};

point .
putpixel, ""
.
.
void point::hide()
//
{ if(is_v) { putpixel(x,y,bc); is_v=0; } }
void point::show()
//
{ if(!is_v) { putpixel(x,y,fc); is_v=1; } }

197

move,
, point hide show.
circ, GO
"". ,
, ( r)
class circ: public GO {
int r;
public:
circ():GO(),r(1){ }
circ(int x1,int y1,int r1,int c=0): GO(x1,y1,c),r(r1) { }
void hide();
void show();
};

hide show
.
, .
void circ::hide()
{ if(is_v==0) return;
int fc1=getcolor();
setcolor(bc);
circle(x,y,r);
setcolor(fc1);
is_v=0;
}
void circ::show()
{ if(is_v) return;
int fc1=getcolor();
setcolor(fc);
circle(x,y,r);
setcolor(fc1);
is_v=1;
}

//
//
//
//
//
//
//
//
//
//

circf GO.
class circf: public GO {
int r;
public:
circf():GO(),r(1){}
circf(int x1,int y1,int r1,int c=0):r(r1),GO(x1,y1,c) {}
void show();
void hide();
};

show
fillellipse. ,
(
SOLID_FILL=1), , (

198

fc).
setfillstyle.
void circf::show()
{ if(is_v) return;
setfillstyle(1,fc);
fillellipse(x,y,r,r);
is_v=1;
}

//
//

hide , .. ,
, .
.
void circf::hide()
{ if(!is_v) return;
setfillstyle(1,bc);
fillellipse(x,y,r,r);
setcolor(bc);
circle(x,y,r);
setcolor(fc);
is_v=0;
}

//
//
//
//
//

.
gs.h , ,
.

.
.
#include "gs.h"
#include <conio.h>
void main()
{ open_gs();
//
//
point P1(21,10,2);
// (21,10)
circ C1(21,50,20,4);
// 20
circf CF1(21,100,20,12);
//
//
P1.show(); getch();
//
C1.show(); getch();
//
CF1.show(); getch();
//
//
P1.move(121,10);
getch(); //
C1.move(121,50);
getch(); //
CF1.move(121,100); getch(); //
//
P1.hide(); getch();
//
C1.hide(); getch();
//
CF1.hide(); getch();
//
//
P1.move(221,10);
getch(); //

199

C1.move(221,50);
getch(); //
CF1.move(221,100); getch(); //
close_gs();
//

GO
.
, ,
.
- :
#include "gs.h"
#include <conio.h>
void main()
{ open_gs();
point P1(21,10,2);
circ C1(21,50,20,4);
circf CF1(21,100,20,12);
GO *m[3]={&P1,&C1,&CF1};
for(int i=0;i<3;i++) m[i]->show();
getch();
m[0]->move(121,10); getch();
m[1]->move(121,50); getch();
m[2]->move(121,100); getch();
close_gs();
}

//
//
//
//
//

16. : , , : PDA
,
.

16.1.

. ,
,
, .

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

interrupt).
,
.
, (.
event ).
"1", ,
. MS-DOS

" ".

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

.
, ,
. ,

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

.
,

, .
,
ON ERROR GOTO ... ON ERROR GOSUB... .

201

,
.
ERR ( ), ERL
( ,
) . , C++

(. Exception ).


.

16.2.
C++
try (, ), catch () throw ( ).
BC 3.1 ,
Borland C++ Builder.
, ,
, try:
try { }


, ,
catch:
catch(__1 __1) { _1 }
catch(__2 __2) { _2 }
.........................................................

catch
.
, ,
throw.
,
,
catch.
fact(n), n!,
double.
,
double , n
1754, .. 1755! > 1e+308. ,

, , :
202

double fact(int n)
{ if(0>n || n>1754)
{ cerr<<"fact: "<<endl;
getch(); exit(1)
}
if(n==0)return 1;
else return n*fact(n-1);
}


.
,
- . o
:
double fact(int n)
{ try
{ if(n<0) throw "fact: Argument < 0";
if(n>1754) throw "fact: Argument too big";
if(n==0)return 1;
else return n*fact(n-1);
}
catch(const char *s)
{ cerr<<s<<endl;
//
return -1;
//
}
}


throw,
try
( break). catch
,
. catch
.
,
. fact
, try .
,
C++,
-. BCB, throw,
:
Project ... raised exception class char * with message 'Exception Object
Address: ...'
Process Stopped. Use Step or Run to continue.

203

F9 ( Run) F8 ( Step)
catch, ,
throw. fact

:
void main()
{ int x=-5;
double z=fact(x);
cout<<"fact("<<x<<")="<<z<<endl;
getch();
}
//=== ===
fact: Argument < 0
fact(-5)=-1

Visual C++

catch.
BCB ,
Exception. , EInvalidArgument,
, :
double fact(int n)
{ try
{ if(n<0) throw EInvalidArgument("fact: Argument < 0");
if(n>1754) throw EInvalidArgument("fact: Argument too big");
if(n==0)return 1;
else return n*fact(n-1);
}
catch(EInvalidArgument &s)
{ cerr<<s.Message.c_str()<<endl;
//
return -1;
//
}
}


math.hpp,
EInvalidArgument. throw

. catch
. ,
Exception ( ) Message,
AnsiString.

c_str.
Exception
, .

204


, (char*,
unsigned char*, short int, unsigned short int, ...).
,
, try
.
.
, catch,
try. catch
, catch . catch,
, . catch,
, ,
.
catch throw().
, ,
catch (catch(...)).
.

205

Оценить