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

. .

TURBO PASCAL

,
03130165 ,
035700
5528 .

2011

681.3 (076)
32.9737
26

:
, . . ;
.

26

, . .
Turbo Pascal :
/ . . . : , 2011. 266 .
ISBN 978-5-9795-0771-2
,
Turbo Pascal
(, ),
/ Turbo Pascal ( read/write).

, ,
, ,
.
.

681.3 (075)
32.9737
ISBN 978-5-9795-0771-2

. ., 2011
. , 2011


............................................................................................................................................................. 7
Turbo Pascal ............................................................................. 9
...................................................................................................................................... 9
..................................................................................................................... 10
................................................................................................................ 10
................................................................................................ 12
.............................................................................................................................................. 12
.............................................................................................................................................. 13
.................................................................................................................. 15
..................................................................................................................... 17
............................................................................................................................ 18
.............................................................................................................................................. 18
/ .......................................................................................................................................... 19
...................................................................................... 20
........................................................ 21
/ ............................................................................. 23
, ................................................... 24
........................................................................................................................ 25
................................................................................................................ 27
....................................................................................................... 29
.................................................................................................................... 33
.......................................................................................................... 37
.................................................................. 38
........................................................................................................... 41
........................................................................................................... 42
............................................................................................................................. 43
.................................................................................................................................. 44
...................................................................................................................... 44
..................................................................................................... 45
............................................................................................................ 46
........................................................................................................................... 46
............................................................................................................... 47
.................................................................................................................. 48
..................................................................................................................................... 48
........................................................................................................ 49
............................................................................................................................... 49
............................................................................................................................... 50

............................................................................. 51
................................................................................................. 52
.................................................................................................................................... 52
...................................................................................................................................... 52
................................................................................................................................................ 53
.................................................................................................................................................. 55
................................................................................................................................................ 56
................................................................................................. 59
................................................................................................... 60
....................................................................................................................... 61
............................................................................................................................ 61
............................................................................................................... 61
, ........................................................................... 62
............................................................................................. 62
...................................................................................................... 63
/ .......................................................................................................................................... 64
........................................................ 67
/ ............................................................................. 69
, ................................................... 72
.................................................................................................................... 73
........................................................................................................... 74
................................................................................................................... 75
................................................................................................. 77
.................................................................................................................................... 78
................................................................................................................................................ 78
.................................................................................................................................................. 79
................................................................................................................................................ 80
............................................................................................................................. 82
........................................................................................ 82
................................................................................................................................... 82
................................................................................................................................................. 88
............................................................................................................. 90
......................................................................................... 92
................................................................................................................................. 93
.................................................................................................................................................. 94
....................................................................... 97
................................................................................................ 101
........................................................................... 102

....................................................................................... 103
............................................................................................................... 103
: ......................................... 108
............................................................................................ 109
........................................... 110
- ................................................................................................................................ 114
............................................................................................................. 115
............................................................................................... 122
.................... 122
.................... 123
.................................................................................................................................. 123
.............................................................................................................................................. 123
................................................................................................................................................ 125
.............................................................................................................................................. 126
................................................................................................................................... 129
..................................................................................................................................... 129
.......................................................................................... 132
................................. 135
.......................................................................................................................................................... 137
String .................................................................................................................................. 137
string ...................................................................................................................... 141
...................................................................................................................... 143
................................................................ 144
...................................... 150
.................................................................................................................................................... 152
......................................................................................................................... 153
................................................................................................................ 155
............................................................................................................... 158
.............................................................................................................. 158
................................................................................... 166
............................................................................................... 171
.................................................................................................................................. 171
.............................................................................................................................................. 171
................................................................................................................................................ 172
.............................................................................................................................................. 173
............................................................................................................................................................... 175
.......................................................................................................................................... 176

............................................................................ 178
..................................................................................................................... 181
................................................................................................................................... 182
............................................................................................... 186
.................................................................................................................................. 186
.............................................................................................................................................................. 191
...................................................................................... 191
................................................................................................................................ 191
............................................................................... 192
........................................................................................................................... 195
.............................................................................................................................. 195
..................................................................................................... 197
........................................................................................................................................ 199
............................................................................... 204
............................................................................................... 206
:.................................................................................................. 206
: ................................................................................................................. 207
.................................................................................................................................. 207
............................................................................ 210
. ................................................................................................ 231
................................................................................................................. 240
............................................................................................... 244
.................................................................................................................................. 245
.............................................................................................................................................................. 249
............................................................................................... 256
.................................................................................................................................. 256
............................................................................ 258
.................................................................................................................................................. 265
........................................................................................................... 266



03130165

..01


300

, ,
, ,
, ,
.

-
.
.

.

.

,
-,
.
,
Turbo Pascal
(, ),
/ Turbo Pascal
( read/write). ( / ,
).
7


.

(
).
:
Turbo Pascal,
.
;

;

.
;
, ,
;
.

TURBO PASCAL

, .
, ,
, .
Turbo Pascal :
() ().

(Integer, Byte, Word)
,
1 4 .

Byte
Shortint
Word
Longint

0 ... 255
-128 127
0 ... 65535
-2147483648 ... 2147483647


1
1
2
4

(Real, Double)
,
4 10 .

Real
Single
Double
Extended
Comp

2.9E-39 1.7E38
1.5E-45 ... 3.4E38
5E-324 ... 1.7E308
1.9*10E 4951..1.1*10E4932
-2E+63+1..2E+63-1-

11-12
7-8
15-16
19-20
10-20


6
4
8
10
8

(char)
ASCII. 1
.
9

(Boolean) :
() ().
.

Turbo Pascal ,
. ( ,
,
) (
).

, .
,
.
:
Var
Season: (winter, spring, summer, autum);
Temp: (23,24,25,26);
,
.
( real).
:
Var
S:1..30; h:a..f;


.
Turbo Pascal :
(array);
(string).
10

(set);
(record);
(file);
(object).

11



,
.
,
. .
:
Var
X: Real; { X, 6 }
N: Integer; { N, 2 }

.
:
X := 10.4;
{X 10.4}
N := round (X) + 5; {N
X ( 10) + 5= 10+5=15}

, ,
, , .
Turbo Pascal
.
.

,
.
.

, , .
12

Turbo Pascal array,


:
, . of,
, .
:
Var
A : Array [1..10] of integer; { A,
10
1 10}
B : Array [5..8] of real; { B,
4
5 8}

:
Begin
A[1] := 3; { A 1 3}
A[4] := A[1] + 1; { A 4
3+1=4}
B[5] := 0.111; { B 5
0.111}
B[ A[1] + A[4] ] := B[5] * 2; { B
7 (A[1]+A[4]=3+4=7) 0.222}
End.



, Longint. , ,
.
(integer, shortint, longint, byte, word), (boolean,
wordbool, longbool, bytebool), (char),
.
13

:
Var { }
A : Array [Byte] of integer; { A, 256
, 0, 255}
B : Array [Char] of real; { B, 256
, #0( 0),
#255( 255)}
I : Byte; {, A}
C : Char; {, B}
Begin { }
A[45] := 0; { A, 45,
0}
B[t] := 2.4; { B, t,
2.4}
I := 200; {i 200}
C := #; {c #}
A[i] := 23400; { A, i=200,
23400}
B[c] := 123.456; { B, c=#,
123.456}
End.

.
:
Var { }
C : Array [-10..5] of integer; { C,
16 , -10, 5}
D : Array [A..Z] of char; { D,
26 , A,
Z}
j : -10..5; {, C}
c1 : A..Z; {,

14

D}
k : integer; {
C, .. 10..5
}
c2 : char; {
D, ..A..Z
}
begin { }
C[-4] := 3;
D[F] := %;
J := 4; C[j] := -10;
c1 := R; D[c1] := q;
K := -3; C[k] := 80;
c2 := G; D[c2] := ;
end.

,
1.
:
Var
E: Array [1..10] of integer; { E,
10 , 1,
10}


.
,
:
SizeOfArray = NumElement * SizeOfElement

SizeOfArray

NumElement

SizeOfElement
15

( )
( AdrArray). i-
( AdrI) :
AdrI = AdrArray + (i __) * SizeOfElement

A, :
A : Array [5..8] of Real;

5. ( )
A[5]. , 100. (Adr5 = 100)
Real, 6
.
Adr6 = 100 + (6-5)*6 = 100 + 1*6 = 106
Adr7 = 100 + (7-5)*6 = 100 + 2*6 = 112
Adr8 = 100 + (8-5)*6 = 100 + 3*6 = 118

100

A[5]

106

A[6]

112

A[7]

118

A[8]

: 65520 .
, , C:
Var
C: array[1..50000] of integer;

2 ,
50000, , 100000 > 65520 .

16


, ,
.
:
Type
Arr = array[1..20] of integer; {
, 20 }
Var
A, B : Arr; {A B ,
20 }
A B , :
A[3] := 2;
B[4] := A[3];
..


. .
:
Type
IndexEl = 1 .. 20; { }
Arr = array[IndexEl] of integer; { ,
20 }
Var
A, B : Arr; {A B ,
20 }
i, j : IndexEl; {,
}

17



, ,
.
,
for. ,
, ,
:
S := 0; { S }
For I := 1 to N do { N }
S := S + a[i]; { S i- }

, ,
, I (Index).
, ,
i j. ,
i j, , ,
(, k, l, z ..).
, .
:
) :
Const
maxN = 20; { }

) :
Type
IndexEl = 1 .. maxN; { }
arrInt = array[IndexEl] of integer; {
}

) :
Var
A : arrInt; { }
N : integer; { }

18

I : IndexEl; {, }

:
,
.
- ,
.
/
1: .
:
1. .
2. .
:

{1 - }
repeat
write(' n:');
readln(n);
until ( n >= 1 ) and ( n <= maxN );
{2 }
for I := 1 to n do
begin
write('a[', i, ']');
readln(a[i]);
end;

2: .
:
1. .
2. n (
n).
3. .
19

{1 }
randomize;
{2 n}
n := random(maxN);
{3 n }
for I := 1 to n do
a[i] := random(100); {
0..99}

3: .
:
1. .
2. .
:

{1 }
writeln(' [', n, ']');
{2 }
for I := 1 to n do
writeln('A[', i, ']=', a[i]);

,
.
Randomize.
Random.

20

Randomize ()
(
, .. ).
Random(Num) ,
0 .. (Num-1). (, Num=100, Random
0 99).
Num<=0, Random 0.
, [0..Num-1],
, Random,
.
1: [-50 .. 50].
101, 50.
random(101) - 50
2: [20 .. 30].
11, 20.
random(11) + 20
3: [-1000 .. -500]
501, 1000.
random(501) - 1000


4: .
:
1. S=0.
2.
S.
:
{ }
{: . .}

21

Program SumExample;
Const { }
maxN = 20; {
}
Type { }
IndexEl = 1 .. maxN; {
1 maxN}
arrInt = array[IndexEl] of integer; { ,
maxN }
Var
A : arrInt;
{}
N : interval; { }
I : IndexEl; { }
S : integer; { }
Begin
{ }
write( n=);
read(n); { }
for I := 1 to n do
read(A[i]); { }
{ }
{1} s:=0;
{2} for I := 1 to n do
s := s + A[i];
{ }
writeln( S=, S);
end. { }

5: .
.
:
1. S=0.
22

2.
S.
3. SA=S/N .
:
Var
S : integer; { }
Sa : real; { }

Begin

{1} s := 0;
{2} for I := 1 to n do
s := s + A[i];
{3} sa := s / n;

/
6: .
:
1. : max=A[1].
2. ,
max A[i].
3.
,
max=A[i].
:
Var
Max : integer; { }

Begin

23


{1} max := A[1];
{2} for I := 2 to n do
{3} if A[i] > max then max := A[i];

7: min max .
:
Var
max,min:integer; {
}

Begin

max := A[1];
min := A[1];
for i := 2 to n do
if A[i] > max then max := A[i]
else if A[i] < min then min := A[i];

,
8: , ,
10.
:
1. =0.
2. .
3. 10,
4. ( 10) 1.
:
Var
k:integer; { , 10}

24


Begin

{1} k:=0;
{2} for I := 1 to n do
{3} if A[i] = 10
{4} then k := k+1;


9: .
:
1. , , .
2. n
.
:
1. , ,
, : A[i]:=A[i+1].
2. : i:=i+1.
3. , i=n-1, i+1 i=n-1 n.
:

{1 }
{ i:=1, 1- }
for I := 1 to n - 1 do
A[i] := A[i+1];
{2 }
n := n-1;

10: .
:
25

1. .
2. .
:
Var
imax:IndexEl; { }

Begin

{1 }
imax := 1; { imax }
{ 2- }
for I := 2 to n do
{ i-
, i-
,
i- }
if A[i] > A[imax] then imax := i;
{2 imax}
for I := imax to n - 1 do
A[i] := A[i+1];
dec(n); { n 1}

: urbo ascal
.
Inc .

Inc(x);

X := x + 1;

Inc(x, n);

X := x + n;

x ,
26

n .
Dec .

Dec(x);

X := x - 1;

Dec(x, n);

X := x - n;

x ,
n .

11: ,
0.
A: 1 2 5 1 0 1 2 .
A[3]=5
: 1 2 5 0 1 0 1 2.

:
1. .
2. .
3. n 1.
:
1. .
2. max .
3. max 0.
4. .
:
{

27

:
, 0}
Program InsertExample;
Const { }
maxN = 20; {
}
Type { }
IndexEll = 1 .. maxN; {
1 maxN}
arrInt = array[interval] of integer; { ,
maxN }
Var
A : arrInt; {}
N : integer; { }
I : IndexEl; { }
Max : IndexEl; { max }
Begin
{1 }
randomize;
n := random(6) + 5; {n 5..10}
for I := 1 to n do
A[i] := random(19) - 9; { .
-9..9}
{2 max }
max := 1;
for I := 2 to n do
if A[i] > A[max] then max := i;
{3 0 .
}
for I := n downto max + 1 do
A[i+1] := A[i];
{ 0}
A[max+1] := 0;

28

{ }
Inc(n);
{4 }
writeln(' :');
for I := 1 to n do
write(A[i]:3);
readln; { Enter}
End.


: ,
. ,
. .
.
: . n=20,
n 21, , ,
(
20). ,

, n<=maxN.

12: k- z-
.
n=10, k=3,
z=7 (. . ).
d .
d : d = z - k 1 (
d = 7 - 3 - 1 = 3).
A :

29

a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
1 3 9 1 0 1 3 2 7 2
^
^
^
a[k]
a[z]
a[n]
A :
a[1] a[2] a[3] a[4] a[5] a[6] a[7]
1 3 9 3 2 7 2
^ ^
^
a[k] a[z]
a[n]

n d ( 3).
:
1. d , z-.
2. n d.
:
Var
K : integer; { , }
Z : integer; { , }
D : integer; { }

Begin

{ }
d := z - k - 1;
{1 - }
for I := z to n do
A[ I d ] := A[i];
{2 - n d}
Dec(n, d);

13: , 0.
.
30

:
1. .
2. <0, n .
3. >=0, .
:

{ }
I := 1;
while I <= n do
begin
{, i- }
if A[i] < 0 then
begin
{ i- }
for j := i to n - 1 do {}
A[j] := A[j+1];
Dec(n); { }
end
else Inc(i); { ,
}
end;

:
:
0: i=1, n=6: -1 -2 2 -3 -4 3
:
1: i=1, n=5: -2 2 -3 -4 3 ( 1)
2: i=1, n=4: 2 -3 -4 3 ( 2)
3: i=2, n=4: 2 -1 -2 3 ( )
4: i=2, n=3: 2 -4 3
( 3)
5: i=2, n=2: 2 3
( 4)
6: i=3, n=2: 2 3
( )

31

:
1. k=0.
2. .
3. A[i] 0, k 1
A[i] k- .
4.
k n.
:
Var
K : IndexEl; { }

Begin

{1 }
k := 0;
{2 }
for I := 1 to n do
{3 A[i] <0}
if not( A[i] < 0) then
begin
Inc(k); { k 1}
A[k] := A[i]; { i- k}
end;
{4 k }
n := k;

:
: -1 -2 2 -3 -4 3
:
0: k=0, i=1, n=6: -1 -2 2 -3 -4 3 { }
1: k=0, i=2, n=6; -1 -2 2 -3 -4 3 { }

32

2: k=1, i=3, n=6; 2 -2 2 -3 -4 3 {


a[1]:=a[3]}
3: k=1, i=4, n=6; 2 -2 2 -3 -4 3 { }
4: k=1, i=5, n=6; 2 -2 2 -3 -4 3 { }
5: k=2, i=6, n=6; 2 3 2 -3 -4 3 {
a[2]:=a[6]}
6: k=2, i=7, n=6: 2 3 2 -3 -4 3 { }
7: n=2:
2 3 { k n}


14: .

. n ( ).
:

{ }
for I := 1 to n do
{ i- A B i- C}
C[i] := A[i] + B[i];

15: [n]
[m].
:

{ B}
for I := 1 to m do
A[n + i] := B[i]; { A}
Inc(n, m); { n ( A)
m ( B)}

: , n
maxN.
33

, :

if n + m > maxN
then writeln(' ,
')
else { }

16:
. .
:

m := 0; {m
B }
{ A}
for I := 1 to n do
if A[i] < 0 then { i- A
}
begin
{ B}
Inc(m); { B
m 1}
B[m] := A[i]; { i- A
m- B}
end;

17: ,
.
:
1. [n].
2. [m].
3. cnt .
4. A.
34

5. i-
.
6. [i] B,
1.
7. .
:
{, }
Program TwoArrayExample;
Const
maxN = 20; { }
Type
IndexEl = 1 .. maxN; {
1 maxN}
arrInt = array[IndexEl] of integer; { ,
maxN }
Var
a, b: arrInt; { A B}
n : integer; { A}
m : integer; { B}
i, j : IndexEl; { }
cnt : integer; { A
B}
k: integer; { A[i]
B}
Begin
{1 A}
{ }
repeat
write(' n:');
readln(n);
until ( n >= 1 ) and ( n <= maxN ); {
, n [1..maxN]}
{ A }

35

for I := 1 to n do
begin
write('a[', i, ']');
readln(a[i]);
end;
{2 B}
{ }
repeat
write(' m:');
readln(m);
until (m >= 1) and (m <= maxN);
{ B }
for I := 1 to m do
begin
write('b[', i, ']');
readln(b[i]);
end;
{3 }
cnt := 0;
{4 A}
for I := 1 to n do
begin
{5 i-
}
k := 0; {k i- A
}
{ A[i]
B}
for j := 1 to m do
if A[i] = B[j] then Inc(k);
{6 [i] B,
1}

36

if k > 0 then Inc(cnt);


end;
{7 }
writeln(' cnt=', cnt);
readln; { Enter}
End.


18: , , 0,
.
:

k := 0; { }
{ A.
, ,
. n-1 ,
n, n- ,
}
for I := 2 to n - 1 do
{ i- 0
}
if (A[i] = 0) and (A[i-1] < 0 ) and (A[i+1] < 0)
then Inc(k); { }

19: ,
.
:

k := 0; {k }
I := 2; { }
{

37

}
while (I <= n - 1) and (k = 0) do
begin
{ , , }
if (A[i-1] > 0) and (A[i+1] > 0) then k := i;
Inc(i); { }
end;
{ }
if k = 0
then writeln(' ')
else writeln(' ', k);


20: .
A ()
, i [1..n-1]
A[i]<=A[i+1].
,
(
).
:
1. .
2. .
3. ( )
.
4. .
n-1 .
:
A, 1 3 0 9 2.

38


0: 1 3 0 9 2 min=a[3]=0
1: 0|3 1 9 2 min=a[3]=1
2: 0 1|3 9 2 min=a[5]=2
3: 0 1 2|9 3 min=a[5]=3
4: 0 1 2 3 9

a[1]<->a[3]
a[2]<->a[3]
a[3]<->a[5]
a[4]<->a[5]

|
.
Turbo Pascal :
Var
Buf : integer; { buf
}
imin : IndexEl; {
}

Begin

{n-1 }
for I := 1 to n - 1 do
begin
{
( i- )}
imin := i;
for j := I + 1 to n do
if A[j] < A[imin] then imin := j;
{ i- imin- }
buf := A[i];
A[i] := A[imin];
A[imin] := buf;
End;

39

21.
, .
:
, .
, .
1.
, ,
, .
.
: ,
.
, .
: 5 A: 3 4 7 9.
1.
2.
3.
4.
5.

, . A[3]=7.
1.
A: 3 4 7 9 X.
, 3-, . A: 3 4 7 7 9.
A[3] 5. : 3 4 5 7 9.

, :

{ , }
read(g);
{1. }
k := 1; {k }
{ ,
}
while (k <= n) and (a[k] <= g) do
k := k + 1;
{2. 1}
n := n + 1;

40

{3. , k-, }
for I := n downto k + 1 do
a[i] := a[i-1];
{4. A[k] g}
a[k] := g;


,
.
,
, .
,
.
,

.

,
, :
A[N] ,
1<=N<=20.
, .. , .
.
, .
,
.
.
41

.
:
1.
.
2. ,
.
, B, C.
1 20 .

1. .
:
7 2 6 5 4 4 10 3 9
7 5
39

4

2. .
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

26

3. , 3, .
:
7 2 6 5 4 4 10 3 9
, 3
6
39
7 2 0 5 4 4 10 0 0

4. , , .
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

5 4
7 2 6 0 4 0 10 3 9

42


5. .
:

7 2 6 5 4 4 10 3 9
2 6 4 4 10
2

6. , 5.
:
7 2 6 5 4 4 10 3 9
, 5
5 10

10

7. ,
3.
:
7 2 6 5 4 4 10 3 9
, 3
6
39
6
9

8. ,
.
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10
7 2 5 4 4

4

9. .
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

10

10. ,
.
:
7 2 6 5 4 4 10 3 9
2 5 4 3

5

43

11. ,
.
:
7 2 6 5 4 4 10 3 9

10

3


12. .
:
,
. .
:
7 2 6 5 4 4 10 3 9

7 5
39
1 7
2 5
3 3 9

13.
.
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

4 4 10

=3


14. .
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

10

72654439

44

15.
.

:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

6 4 10

6

7 2 5 4 4 10 3 9

16. , 3.
:
, 3

7 2 6 5 4 4 10 3 9
6
39
3
7 2 6 5 4 4 10 9

17.
3.
:
, 3



7 2 6 5 4 4 10 3 9
6
39
3
7 5
7
2 6 5 4 4 10 3 9


18. .
:

7 2 6 5 4 4 10 3 9
2 6 4 4 10
7539

19. ,
.
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

6 4 10

725439

45

20. .
:

7 2 6 5 4 4 10 3 9
72 5
3
6 4 4 10 9

21. ,
.
:
7 2 6 5 4 4 10 3 9

10

7 5

2 6 4 4 10 3 9


22. .
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10
1 2 6 2
2 4 4 10 3
( 2) 4 4 10

726539

23. ,
.
:
7 2 6 5 4 4 10 3 9

7 5
39

7 5

26

7 5 4 4 10 3 9


24. , 5,
, 0.
46

:
, 5

7 2 6 5 4 4 10 3 9
5 10
5
7 2 6 5 0 4 4 10 3 9

25.
.
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

7 2 2 2 6 6 6 5 4 4 4 4 4 4 10 10 10 3 9


26. .
. ( :
).
:
7 2 6 5 4 4 10 3 9

7 5
39

7539
( ) 7 2 6 5 4 4 10 3 9

27. .
. ( :

).
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

2 6 4 4 10
( ) 7 5 3 9

28. ,
.
.
47

:
7 3 6 5 4 3 10 3 4
,
3 4

34

7 3 6 5 4 10


29. .
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

2
10
7 10 6 5 4 4 2 3 9

30. ,
, .
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

2
10
7 2 4 4 5 6 10 3 9


31. ,

.
:
7 2 6 5 4 4 10 3 9

2 6 4 4 10

2
10
7

39
3 9 2 6 5 4 4 10 7

32. .
, , .
48

, ,
.
:
7 2 6 5 4 4 10 3 9

10

7

6544

39
2 3 9 10 6 5 4 4


33. , .
:


8

7 2 6 5 4 4 10 3 9
7 2 6 5 4 10 3 9

34. ,
.
:
.
:
7 2 6 5 4 4 10 3 9
7-1 2-1 6-1 5-1 4-2 10-1 3-1 9-1


35. .
.
:
737543

1342361

42 6

4
7 3 7 5 4 3 4
1 3 4 2 3 6 1

49

36. .
,
.
:
737543

1342361

6

13 3 1

13 3

133737543

4261

37. .
, .

.
:
7166743

1342

66 4

16 7

7643

1

7
1 3 4 2 1 7


38. .
, 2
.
:
737543

134236413

34

50

39. , .
, ,
.
:
737549

1342364135

3189
:
7
3 ,
9
:
5
4
7 3 7 9

40. .
, 2 .
, .
:
737543

134231473
2 1 4

73753
,
3 3

3373

14264

73


41. .
,
.
:
3
3
5
3 5
2
2 3 5
7
2 3 5 7
4
2 3 4 5 7

51




.

,
, .
,
.
4, 11 17
.


A1:
.
: A[5]: 1 3 4 5 6 1 3 0 5 0.

A2:
1.
: A[5]: 1 3 4 5 6 1 1 4 1 6.

A3:
, , 0.
: A[5]: 1 3 4 5 6 1 0 4 5 0.

A4:
, , 9.
: A[5]: 1 3 4 5 6 1 9 4 9 6.

52

A5:
, , 1.
: A[5]: 1 3 4 5 6 1 2 4 1 6.

A6:
, , 0.
: A[5]: 3 2 1 5 6 3 2 1 0 0.

A7:
, , 0.
: A[5]: 3 2 1 5 4 0 0 0 5 4.

A8:
, , 0.
: A[5]: 3 2 1 5 4 3 2 1 5 0.

A9:
, ,
0.
: A[5]: 3 7 1 5 4 3 7 0 0 4.

A10:
, ,
0.
: A[5]: 3 2 1 0 4 3 0 1 0 4.


B1
.
: A[5]: 1 3 4 5 6
A[4]: 1 3 5 6.

B2
.
: A[5]: 1 3 4 5 6
A[4]: 1 3 4 5.

B3
.
53

: A[5]: 1 3 4 5 6
A[4]: 1 3 4 6.

B4
.
: A[5]: 1 3 4 5 6
A[4]: 3 4 5 6.

B5
0.
: A[5]: 1 9 8 3 5
A[6]: 1 9 8 0 3 5.

B6
0.
: A[5]: 1 6 8 3 4
A[6]: 1 6 0 8 3 4.

B7
0.
: A[5]: 1 3 8 3 5
A[6]: 1 3 8 3 5 0.

B8
.
: A[5]: 2 3 4 7 5
A[4]: 2 3 7 5.

B9
.
: A[5]: 2 3 4 7 5
A[4]: 2 4 7 5.

B10
0.
: A[5]: 1 3 8 3 4
A[6]: 1 3 8 3 4 0.

54


C1
, .
: A[5]: 2 7 4 6 5
A[3]: 2 7 5.

C2
,
.
: A[5]: 8 7 2 6 5
A[3]: 7 2 5 ( =(8+7+2+6+5)/5=5.6).

C3
,
.
: A[5]: 8 7 2 6 5
A[3]: 8 7 6 ( =(8+2+6)/3=5.33).

C4
,

.
: A[5]: 8 6 9 4 5
A[3]: 8 6 9 (
=(8+6+9+4+5)/5=6.4).

C5
,
.
: A[7]: 1 8 8 4 7 0 5 A[5]: 1 8 7 0 5.

C6
, ,
.
: A[7]: 1 9 3 4 9 0 0 A[5]: 1 9 4 0 0.

55

C7
,
.
: A[7]: 9 3 4 9 1 0 0 A[5]: 9 4 1 0 0.

C8
,
.
: A[7]: 9 3 4 9 1 0 0 A[3]: 3 4 1.

C9
,
.
: A[7]: 9 1 4 9 1 9 0 A[5]: 9 1 9 1 9.

C10
,
.
: A[7]: 4 1 4 3 1 9 0 A[5]: 4 1 4 1 0.


D1
.
: A[8]: 4 1 4 2 1 2 4 6 A[5]: 4 1 4 2 1
( 6 8: 2 4 6).

D2
,
.
: A[8]: 4 3 4 2 1 2 4 6 A[5]: 3 1 2 4 6.

D3
,
.
: A[8]: 3 3 4 5 2 3 7 9 A[5]: 4 2 3 7 9.

56

D4
A , A
B 2 .
: A[8]: 3 3 4 5 2 3 5 9
B[7]: 1 2 3 4 5 2 5
2 , 5.
A : A[6]: 3 3 4 2 3 9.

D5

.
: A[9]: 3 6 4 5 2 3 4 6 4
A[6]: 3 6 5 3 6 4.

D6
A ,
B.
: A[9]: 3 2 4 5 2 3 2 6 5
B[6]: 1 3 4 7 8 9
A :
A[7]: 3 5 2 3 2 6 5.

D7
A ,
B.
: A[10]: 3 2 7 5 2 1 2 6 3 9
B[5]: 1 2 5 4 8
A :
A[7]: 2 7 5 2 1 2 6.

D8
A ,
B.
: A[10]: 3 2 7 5 2 1 2 6 3 9
B[5]: 1 2 5 4 8
A :
A[7]: 2 7 5 2 1 2 6.

57

D9
A B
.
: A[10]: 3 2 2 5 2 1 1 1 3 9
B[8]: 1 2 5 5 4 8 3 3
A :
1 1 1 ( 7 9).
B :
5 5 ( 3 4).
A B: A[9]: 3 2 2 5 2 5 5 3 9.
B A: B[9]: 1 2 1 1 1 4 8 3 3.

D10
A B
.
: A[10]: 3 2 4 6 2 1 1 1 8 9
B[7]: 1 0 5 5 4 3 3
A :
2 4 6 2 ( 2 5).
B :
0 ( 2).
A B:
A[7]: 3 0 1 1 1 8 9.
B A:
B[10]: 1 2 4 6 2 5 5 4 3 3.

58


.
,
, .
Turbo Pascal
:
A : array [1 .. 10] of array [1 .. 10] of integer;
A : array [1 .. 10, 1 .. 10] of integer;
Turbo Pascal
: A[1][7], : A[1, 7].
:
Var
b : Array [1..3, 1..5] of real; { B,
3 5 .
}
i, j : Integer; {}
Begin
b[1, 1] := 0.25; { B 1
1 0,25}
For i := 1 to 3 do
b[i, 2] := i;
{ B
0,25 1,0
?
?
?
?
2,0
?
?
?
?
3,0
?
?
?
? }
For j := 1 to 5 do
b[3, j] := j * 0.1;
{ B
0,25 1,0
?
?
?
?
2,0
?
?
?

59

0,1

0,2

0,3

0,4

0,5

}
End.


.
A, K , K * sizeof
(A[1]) . B, N M
, N * M * sizeof (B[1][1]) .
,
, .
Turbo Pascal:
Var
A : Array [1 .. 4] of longint;
B : Array [1 .. 3, 1 .. 2] of integer;
:

0100

A[1]

0104

A[2]

0108

A[3]

010C

A[4]

0110

B[1,1]

0112

B[1,2]

0114

B[2,1]

0116

B[2,2]

0118

B[3,1]

011A

B[3,2]

60


Turbo Pascal ,

.
:
Type
Arr = array[1..20] of integer; { ,
20 }
Arr2D = array[1..10, 1..10] of integer; {
, 100
10 10 }
Var
A : Arr; {A , 20 }
B : Arr2D; {B , 10
10 }



,
, .
.
:
s := 0;
For i := 1 to n do { N }
For j := 1 to m do { M }
s := s + a[i, j]; { S a[i,j]}

.
i j. , i ,
j .
61

,

) :
Const
MAXN = 10; {
}
MAXM = 10; {
}

) :
Type
{ }
Arr2D = array[1 .. MAXN, 1 .. MAXM] of integer;

) :
Var
a : arr2D; { }
n : Integer; { A}
m : Integer; { A}
i, j : IndexEl; {, }

:
,
.
- ,
.

1: .
:

Type
Arr2D = array [1 .. 4, 1 .. 3] of integer;

62

Const
a: arr2D = ( (2, 3, 4, 5),
(7, 9, 1, 0),
(8, 5, 6, 2)
);

2: .
:

for i := 1 to n do
for j := 1 to m do
a[i, j] := 0;

3: .
:

for j := 1 to m do
a[1, j] := 0;

4: .
:

for i := 1 to n do
a[i, m] := 0;


5: .
:

{ }
randomize;

63

{ n m}
n := random(10) + 1; { }
m := random(10) + 1; { 1 10}
{ }
for i := 1 to n do
for j := 1 to m do
a[i,j] := random(5) + 1; {
}

/
6: .
:

{ }
read(n, m);
{ }
for i := 1 to N do
for j := 1 to M do
read(a[i,j]);

7: .
, N
M .
.
.
:

for i := 1 to n do { }
begin
for j := 1 to m do { }

64

write(a[i,j]:4, ); { }
writeln; { }
end;

8: .


. .
, .
. ,
.
. ,

.


, .
. .

.
-_ __



IN.TXT
, 5 5
:
55
11111
23456

65

33333
45678
55555

, IN.TXT A.
:

var
f : text; { }

{ IN.TXT }
assign(f, in.txt);
reset(f);
{ }
read(f, n, m);
{ }
for i := 1 to N do
for j := 1 to M do
read(f, a[i,j]);
{ }
close(f);

9: .
:

var
f : text; { }

{ OUT.TXT }
assign(f, out.txt);
rewrite(f);
{ }
for i := 1 to N do

66

begin
for j := 1 to M do
write(f, a[i,j]:4, );
writeln(f);
end;
{ }
close(f);

OUT.TXT


10: .
:
{ }
const
N = 5; { }
M = 5; { }
Const
{ }
a : array [1..N, 1..M] of integer = (
(1, 1, 1, 1, 1),
(1, 2, 3, 2, 1),
(1, 3, 4, 3, 1),
(1, 2, 3, 2, 1),
(1, 1, 1, 1, 1)
);
var
i, j : integer; {: i - , j - }

67

s : longint; { }
begin
{ }
s := 0; { }
for i := 1 to N do
for j := 1 to M do
s := s + a[i,j]; { }
{ ()}
writeln; { }
for i := 1 to N do { }
begin
for j := 1 to M do
write(a[i,j]:4); { }
writeln; { }
end;
{ }
writeln('S = ', s);
end.

11: .
:

Var
s : Longint; { }
sa : real; { }

Begin

s := 0; { }
for i := 1 to n do
for j := 1 to m do

68

s := s + a[i, j]; { }
sa := s / (n * m); {
}

/
12:
.
:
1. :
max=A[1,X], X .
2.
max A[i,X].
3.
, max=A[i,X].
:
Var
max : integer; { }
x : integer; { }

Begin

x := m; { m}
{1} max := a[1, x];
{2} for i := 1 to n do
{3} if a[i, x] > max then max := a[i, x];

13: .
:
Var
max : integer; { }

69

Begin

max := a[1, 1];


for i := 1 to n do
for j := 1 to m do
if a[i, j] > max then max := a[i, j];

14: , .
:
{

.
: ,
.
}
uses crt;
const
MAXN = 10; { }
MAXM = 10; { }
var
a : array [1 .. MAXN, 1 .. MAXM] of integer; {}
n, m : integer; { }
i, j : integer; {: i ,
j }
min : integer; { }
imin : integer; { }
jmin : integer; { }
begin
{ }
randomize;
{ n m}
n := random(10) + 1; { }

70

m := random(10) + 1; { 1 10}
{ }
for i := 1 to n do
for j := 1 to m do
a[i,j] := random(5) + 1; {
}
{ }
min := a[1,1]; {
}
imin := 1;
jmin := 1;
for i := 1 to n do
for j := 1 to m do
if a[i,j] < min then { - }
begin
{ , }
min := a[i,j]; { }
imin := i;
jmin := j;
end;
{
}
writeln;
for i := 1 to n do
begin
for j := 1 to m do
begin
if (i = imin) and (j = jmin)
then textcolor(LightBlue) {
- }
else textcolor(LightGray); {
}
write(a[i,j]:4);
end;

71

writeln;
end;
end.

,
15: , X ,
K.
:
K X.
Cnt (CouNTer) = 0.
X.
K,
Cnt ( , K) 1.
:
Var
k : integer; { }
x : integer; { K}
cnt : integer; { (CouNTer) , K}

Begin
{ K X}
write(' K = ');
read(k);
write(' X = ');
read(x);
cnt := 0; { ,
K, }
for j := 1 to m do
if a[x, j] = k then { K,}
inc(cnt);
{ }

72

16: , , K.
:
Var
k : integer; { }
cnt : integer; { (CouNTer) , K}

Begin
{ K}
write(' K = ');
read(k);
cnt := 0; { ,
K, }
for i := 1 to n do
for j := 1 to m do
if a[i, j] = k then { K,}
inc(cnt);
{ }


17. .
:
Var
tmp : integer; { }

for i := 1 to n do { }
begin
tmp := a[i, 1]; { }
a[i, 1] := a[i, m]; { }
a[i, m] := tmp;
end;

18. .
73

for j := 1 to m do { }
begin
tmp := a[2, j]; { }
a[2, j] := a[3, j]; { }
a[3, j] := tmp;
end;


19. k- .
:

{ , k+1-, }
for i := k + 1 to n do
{ i- i 1}
for j := 1 to m do
a[i - 1, j] := a[i, j];
{ }
n := n 1;

20. k- ,
.
:

{ , k-,
}
for j := m downto k do
{ j- j + 1}
for i := 1 to n do
a[i, j + 1] := a[i, j];

74

{ }
m := m + 1;
{ k- }
for i := 1 to n do
a[i, k] := 0;


N N
. ,
a[1,1], a[2,2], , a[i,i], , a[N,N],
(). , a[1,N], a[2,N-1], ,
a[i,N-i+1], , a[N,1], .
.

.

J=1 J=2 J=3 J=4


I=1 *
I=2
*
I=3
*
I=4
*

I=1
I=2
I=3
I=4

J=1 J=2 J=3 J=4


*
*
*
*

21: .
:

for i := 1 to n do
a[i, i] := 0;

22: .

75

, ,
:
:

for i := 1 to n do
for j := 1 to n do
if i < j then { ,
}
a[i,j] := 0;

,
:
:

for i := 1 to n - 1 do
for j := i + 1 to n do
a[i,j] := 0;

23: ,
.
:

s := 0;
for i := 1 to n do
s := s + a[ i, n - i + 1];

76



,
, :
A[N][M], 1<=N<=10,
1<=M<=10.
.
.
, .
,
, .

.
.
*:

.

.

.
(!).
**:

, .
*

( )

**

( ( ))
77


.

( ).
.
,
,
,
,
.


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

6:
,
, .
7:
,
,
0.
8:
,
, ,
.
9:
,
, ,
.
10:
, ,
.

4
.
1:
,
.
2:
,
.
3:
,
.
79

4:
,
.
5:
,
.
6:
,
.
7:
.
,
.
8:
A[N,M] B[K].
A,
.
9:
A[N,M] B[K].
A,
.
10:
A[N,M] B[K].
A ,
.


5
,
.
80

D1:
,
.
D2:
,
.
D3:
, .
D4:
, .
D5:
, .
D6:
,
.
D7:
,
.

D8:
A[N,M] B[K].
B , .
D9:
A[N,M] B[K].
B ,
.
D10:
A[N,M] B[K].
B ,
.

81



Turbo Pascal,
,
, .
, :
1. (
,
).
2. (
, ,
).

. Turbo Pascal
. , .
, .
,
, , ,
.
Turbo Pascal .
, .
.

. ,
, .
:
.
.
82

.
.
,
: 1- , 2-
, 3- , 4-
.
:
{: ,
, .
.
}
Program WithoutProcedureExample;
Const { }
maxN = 20; {
}
Type { }
IndexEl = 1 .. maxN; {
1 maxN}
arrInt = array[IndexEl] of integer; { ,
maxN }
Var { }
A : arrInt; {}
N : integer; { }
I : IndexEl; { }
IndMin : IndexEl; { }
begin
{1 }
{1.1 }

83

repeat
write(' n:');
readln(n);
until (n >= 1) and (n <= maxN); {
, 1<=N<=maxN}
{1.2 }
for i := 1 to n do
begin
write('a[', i, ']=');
readln(a[i]);
end;
{2 }
indMin := 1;
for i := 2 to n do
if A[i] < A[indMin] then IndMin := i;
{3 indMin}
for i := indMin to n-1 do
A[i] := A[i+1];
dec(n);
{4 }
writeln;
for i := 1 to n do
write(A[i]:3);
writeln;
end. { WithoutProcedureExample}


.
,
: 1- ,
84

2- , 3-
, 4- . ,

.
{: , ,
.
.
}
Program SimpleProcedureExample;
Const { }
maxN = 20; {
}
Type { }
IndexEl = 1 .. maxN; {
1 maxN}
arrInt = array[IndexEl] of integer; { ,
maxN }
Var { }
A : arrInt; {}
N : integer; { }
I : IndexEl; { }
IndMin : IndexEl; { }

{ }
{==========================================}
{ReadArray - }
procedure ReadArray;
begin
{1 }
repeat
write(' n:');

85

readln(n);
until (n >= 1) and (n <= maxN);
{2 }
for i := 1 to n do
begin
write('a[', i, ']=');
readln(a[i]);
end;
end; { ReadArray}
{==========================================}
{FindIndMin }
procedure FindIndMin;
begin
{ min }
indMin := 1;
for i := 2 to n do
if A[i] < A[indMin] then IndMin := i;
end; { FindIndMin}
{==========================================}
{DeleteMin }
procedure DeleteMin;
begin
{ indMin}
for i := indMin to n-1 do
A[i] := A[i+1];
dec(n);
end; { DeleteMin}
{==========================================}
{PrintArray }
procedure PrintArray;
begin
{ }
writeln;

86

for i := 1 to n do
write(A[i]:3);
writeln;
end; { PrintArray}
{ }
begin
{1 }
ReadArray;
{2 }
FindIndMin;
{3 }
DeleteMin;
{4 }
PrintArray;
end. { SimpleProcedureExample}

,
.
.
,
. ,

.
:

( Begin).

Begin End;.
87

:
procedure _; ,
_ .
:
,
( ) ( )
, .
( ) . ,
( )
( ,
,

).
,
.
,
,
.
, 4060 .
.
,
20 .

.
.
,
{=========================================}.

,
. .
88


.

.
, .
,
,
.
:
{1 } program CallProcedure;
{2 } procedure HiWorld;
{3 } begin
{4 } Writeln(',!');
{5 } Writeln('Hi World!');
{6 } end;
{7 }
{8 } begin
{9 } Writeln(' ');
{10 } HiWorld;
{11 } Writeln(' ');
{12 } end.


{8 } begin .

{9 } Writeln(' '); !

{10 } HiWorld;
HiWorld

{3 } begin HiWorld.

{4 } Writeln(',!'); , !

89

{5 } Writeln('Hi World!'); Hi, World!


{6 } end; HiWorld.
,

{11 } Writeln(' '); !

{12 } end.
.

.

( ) .
:

.
, .
.
,

, ,
. ,
:
, , .

. ( 6070 . 20 .)
, 0,
.
.

.
90

. 4
:
Var { }
A
: arrInt; {}
N
: integer; { }
I
: IndexEl; { }
IndMin : IndexEl; { }
4 :
{1 }
ReadArray;
{2 }
FindIndMin;
{3 }
DeleteMin;
{4 }
PrintArray;

.
A .
ReadArray, FindIndMin PrintArray
DeleteMin. ,
A ReadArray FindIndMin,
DeleteMin, ,
PrintArray. , A
.
N .
ReadArray,
FindIndMin PrintArray DeleteMin.
, N, , A,
.
I .
,

91

. .
, N .
IndMin .
FindIndMin
DeleteMin. ,
.
, ( )
.

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

.
I
PrintArray:
{PrintArray }
procedure PrintArray;
Var { }
I : IndexEl; { }
begin
{ }
writeln;
for i := 1 to n do

92

write(A[i]:3);
writeln;
end; { PrintArray}


, Turbo Pascal
.
, .
, - (
) .
,
( ).
. 4
:
{1 }
ReadArray;
{2 }
FindIndMin;
{3 }
DeleteMin;
{4 }
PrintArray;

( ) FindIndMin

.

:
{FindIndMin }
{ }
Function FindIndMin:IndexEl;
Var
Ind : IndexEl; { ,

93


}
I : IndexEl; { , }
begin
{ }
Ind := 1;
for i := 2 to n do
if A[i] < A[ind] then Ind := i;
FindIndMin := Ind; {
}
end; { FindIndMin}

(
):
,
.
.
Procedure Function.

.

, .

,
. ,
.
, ,
.
:
.
FindIndMin.
94


Var
IndexMinEl : IndexEl; { ,
FindIndMin }

begin

{ }
IndexMinEl := FindIndMin;
{ }
Writeln( = ,
IndexMinEl);

, , ,
, .
IndexMinEl.
:

begin

{ }
Writeln( = ,
FindMinEl);

,

.
:
.
{1
{2
{3
{4

} program CallFunction;
} var
} x : real; { }
}

95

{5 } function tg:real;
{6 } var
{7 } xRad : Real; { }
{8 } begin
{9 } xRad := x * Pi / 180.0; { }
{10 } tg := sin(xRad) / cos(xRad);
{11 } end;
{12 }
{13 } var
{14 } y : real;
{15 } begin
{16 } Write(' x = ');
{17 } Read(x);
{18 } y := tg;
{19 } Writeln( 'tg(', x:0:2, ')=', y:0:4);
{20 } end.


{15 } begin


{16 } Write(' x = ');

{17 } Read(x);


{18 } y := tg; tg,

{8 } begin


{9 } xRad := x * Pi / 180.0; { }


{10 } tg := sin(xRad) / cos(xRad); ,


{11 } end; tg

96

Y
.

{18 }

y:=tg;

{19 } Writeln( 'tg(', x:0:2, ')=', y:0:4);

, ,
{20 } end.
.


(.. ) .
.
, .
:
143243135

1432
5

5
:
{, .
. 1 20.
.
.
}
Program SimpleFunctionExample;
Const
maxNumEl = 20; {
}
Type
IndexElement = 1 .. maxNumEl; { }
ArrayInteger = array[IndexElement] of integer; {
}

97

Var {*** ***}


Arr : ArrayInteger; { }
NumEl : integer; {
}

{*** : ***}

{==========================================}
{ReadArray }
{ Arr. NumEl
Arr}
procedure ReadArray;
var
i : IndexElement; {* *}
begin
writeln;
{* *}
repeat
write(' :');
readln(NumEl);
until (NumEl >= 1) and (NumEl <= maxNumEl);
{* *}
write(' : ');
for i := 1 to NumEl do
read(Arr[i]);
end; {* ReadArray*}
{==========================================}
{PrintArray }
{ Arr. NumEl
Arr}
procedure PrintArray;
var

98

i : IndexElement; {* *}
begin
writeln;
write(' : ');
for i := 1 to NumEl do
write(Arr[i]:3);
writeln;
end; {* PrintArray*}

{==========================================}
{NumberOfDifferentElements ,
}
{ Arr. NumEl
Arr}
Function NumberOfDifferentElements:integer;
var
i, j
: IndexElement; {* *}
num
: integer; { }
isDifferent : boolean; {
}
begin
{
:
;
,
.
,
;
,
1.}
num := 0;
for i := 1 to NumEl do
begin
isDifferent := true; {i-
}

99

for j := i - 1 downto 1 do
if Arr[i] = Arr[j] then
begin
isDifferent := false; {i- }
break;
{ }
end;
if isDifferent then num := num + 1;
end;
NumberOfDifferentElements := num; {*
*}
end; {* NumberOfDifferentElements*}
{* *}
begin
{* Arr*}
ReadArray;
{* Arr*}
PrintArray;
{* *}
Write(' : ');
writeln(NumberOfDifferentElements);
end. {* *}


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


,
. -
( 100%).

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

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

,
, ,
( Turbo Pascal Begin
End) .
.
,
- , :

101

,
, , , ,
, ..
.
, .
. ,
: i, j, k , s
( sum), x, y ..
, isDifferent (
) .
, ,
:
w,
t,
gg,
h123,
m22,
DlinnoeMnemonicheskoeImia01,
DlinnoeMnemonicheskoeImia02...

:

.

.
.

. ,
, .
.
-
.

102



:
A[nA], B[nB], C[nC]. ,
.
( : : 11, 11,
11.
.
, ,
).

( A, B C)
. 6 .
2
.

,
A, B
C .
nA, nB nC.

.

A, B C.
nA, nB nC.
:
{ 3 . ,
.

103

.}
Program ThreeArraysWithoutParameters;
Const
maxNumEl = 20; {
}
Type
IndexElement = 1 .. maxNumEl; { }
ArrayInteger = array[IndexElement] of integer; {
}

Var
{ }
Arr : ArrayInteger; { }
NumEl : integer; {
}
{==========================================}
{ReadArray }
{ Arr. NumEl
Arr}
procedure ReadArray;
var
I : IndexElement;
begin
writeln;
repeat
write(' :');
readln(NumEl);
until (NumEl >= 1) and (NumEl <= maxNumEl);
write(' : ');
for i := 1 to NumEl do
read(Arr[i]);
end; { ReadArray}

104

{==========================================}
{PrintArray }
{ Arr. NumEl
Arr}
procedure PrintArray;
var
I : IndexElement;
begin
for i := 1 to NumEl do
write(Arr[i]:3);
writeln;
end; { PrintArray}
{==========================================}
{AverageArithmetic ,
.}
{ Arr. NumEl
Arr}
Function AverageArithmetic : real;
var
i : IndexElement;
Sum : LongInt; { }
begin
Sum := 0;
for i := 1 to NumEl do
Sum := Sum + Arr[i];
AverageArithmetic := Sum / NumEl;
end; { AverageArithmetic}

Var {
}
A, B, C : ArrayInteger; { }
nA, nB, nC : integer;
{
}

105

avA, avB, avC : real; {


}
begin
{ }
writeln;
writeln;
writeln(' :');
{ A}
ReadArray;
A := Arr;
nA := NumEl;
{ B}
ReadArray;
B := Arr;
nB := NumEl;
{ C}
ReadArray;
C := Arr;
nC := NumEl;
{ }
{ A}
Arr := A;
NumEl := nA;
AvA := AverageArithmetic;
{ B}
Arr := B;
NumEl := nB;
AvB := AverageArithmetic;
{ C}
Arr := C;
NumEl := nC;
AvC := AverageArithmetic;
{ }
writeln(' :');

106

{ A}
Arr := A;
NumEl := nA;
PrintArray;
{ B}
Arr := B;
NumEl := nB;
PrintArray;
{ C}
Arr := C;
NumEl := nC;
PrintArray;
{ }
writeln(' :');
writeln(' A = ',avA:6:2);
writeln(' B = ',avB:6:2);
writeln(' C = ',avC:6:2);
{ }
if (avA >= avB) and (avA >= avC) then
writeln(' A')
else if (avB >= avA) and (avB >= avC) then
writeln(' B')
else
writeln(' C');
end.


Arr NumEl,
. , , ,
: ReadArrayA,
ReadArrayB, ReadArrayC, .

107

()
() :
{ C}
Arr := C; { Arr
C}
NumEl := nC; { NumEl
nC}
PrintArray; { PrintArray}


, ,
,
. .
:


.
.

Arr NumEl:
. ,

( ).

.

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

, ,
.
,
.
, , ,
,
.

.
.
,
.
, .


,
.
.
var,
( ).

. (
).
,
.
(
):
procedure ReadArray(Var Arr:ArrayInteger;
Var NumEl:integer);

109

ReadArray. :
Arr,
NumEl.
procedure PrintArray(Arr:ArrayInteger; NumEl:integer);

PrintArray. :
Arr, NumEl.
Function AverageArithmetic(Arr:ArrayInteger; NumEl:integer):real;

AverageArithmetic. :
Arr, NumEl.
Real.


,
:
{ 3 . ,
.

.
.}
Program ThreeArraysWithParameters;
Const
maxNumEl = 20; {
}
Type
IndexElement = 1 .. maxNumEl; { }
ArrayInteger = array[IndexElement] of integer; {
}

110

{==========================================}
{ReadArray }
{ Arr. NumEl
Arr}
procedure ReadArray(Var Arr : ArrayInteger;
Var NumEl : Integer);
var
I : IndexElement;
begin
writeln;
repeat
write(' :');
readln(NumEl);
until (NumEl >= 1) and (NumEl <= maxNumEl);
write(' : ');
for i := 1 to NumEl do
read(Arr[i]);
end; { ReadArray}
{==========================================}
{PrintArray }
{ Arr. NumEl
Arr}
procedure PrintArray(Arr:ArrayInteger; NumEl:integer);
var
i : IndexElement;
begin
for i := 1 to NumEl do
write(Arr[i]:3);
writeln;
end; { PrintArray}

{==========================================}

111

{AverageArithmetic ,
.}
{ Arr. NumEl
Arr}
Function AverageArithmetic(Arr : ArrayInteger;
NumEl : integer) : real;
var
i : IndexElement;
Sum : LongInt; { }
begin
Sum := 0;
for i := 1 to NumEl do
Sum := Sum + Arr[i];
AverageArithmetic := Sum / NumEl;
end; { AverageArithmetic}

Var {
}
A, B, C
: ArrayInteger; { }
nA, nB, nC : integer; {
}
avA, avB, avC : real; {
}
begin
{ }
writeln;
writeln;
writeln(' :');
ReadArray(A,nA);
ReadArray(B,nB);
ReadArray(C,nC);
{ }
avA := AverageArithmetic(A,nA);

112

avB := AverageArithmetic(B,nB);
avC := AverageArithmetic(C,nC);
{ }
writeln(' :');
PrintArray(A, nA);
PrintArray(B, nB);
PrintArray(C, nC);
{ }
writeln(' :');
writeln(' A = ', avA:6:2);
writeln(' B = ', avB:6:2);
writeln(' C = ', avC:6:2);
{ }
if (avA >= avB) and (avA >= avC) then
writeln(' A')
else if (avB >= avA) and (avB >= avC) then
writeln(' B')
else
writeln(' C');
end.

, ,
( ) 1/4 ( ):
140 110.
.
!
,
, ,
. ..
,
.
113



const.
,
.
,

.
,
, .
.
:
type
Arr100I= array[1..100] of integer; { 100
. :
100 * 2/ = 200 }
procedure pp1(a : Arr100I);
...
procedure pp2(var a:Arr100I);
...
procedure pp3(const a:Arr100I);
...
var
a,b,c: Arr100I;
begin
pp1(a); {*}
pp2(b); {**}
pp3(c); {***}
...

114

{*} pp1 A.
.
A .
{**} pp2 B.
B.
,
B.
, B
.
{***} pp3 C.

C.
, .

.
, pp1,
pp2 pp3. pp1 A. 200 .
pp2 pp3 B C .
Turbo Pascal 4 . ,
pp2 pp3 4 , 50 ,
pp1.
, , ,
, ,
.


.
: 23 22 20 17 17 21 23 25
,

115

20 .
, .
.
. 30.
,
20.
:
.
, .
. ,
FALSE. , TRUE,
.
FindChain ( ).
FindChain,
. :
{
.
,
N .
}
program FindingLongestChain;
const
MaxDays = 31; {
}
MinDays = 28; {
}
type
Temperature = - 200 .. + 500; {
,
}

116

IndexElement = 1 .. MaxDays; { }
ArrayTemperatures = array[IndexElement] of Temperature;
{ }
{==========================================}
{ T, Start,
, N ().
, False.
, True
( BeginChain),
( LengthChain).
NumDays ( )}
function FindChain(const T : ArrayTemperatures;
NumDays : Integer;
N
: Temperature;
Start : IndexElement;
var BeginChain : IndexElement;
var LengthChain : Integer) : Boolean;
var
i : IndexElement;
begin
{ , }
i := Start;
{ i ,
,
( , N),
(i , NumDays)}
while ( T[i] >= N ) and ( i <= NumDays ) do
i := i + 1;
{ i (
)}
BeginChain := i;
{ }
{ i ,

117

, (..
N), i }
while ( T[i] < N ) and (i <= NumDays ) do
i := i + 1;
{ i ,
( ).

}
LengthChain := i - BeginChain;
{ 0, ,
True.
False }
if LengthChain > 0 then FindChain := true
else FindChain := false;
end;
{==========================================}
{ T, NumDays
, ,
N .
.
, 0.}
function LongestChain(const T : ArrayTemperatures;
NumDays : Integer;
N
: Temperature) : Integer;
var
startFinding : IndexElement; { }
beginChain : IndexElement; { }
lengthChain : Integer; { }
maxLength : Integer; {
}
begin
maxLength := 0; { = 0}
startFinding := 1; { }

118

{ }
while FindChain(T, NumDays, N, StartFinding,
beginChain, lengthChain)
do
begin
{
, }
if lengthChain > maxLength
then maxLength := lengthChain;
{ , ,
}
startFinding := beginChain + lengthChain;
end;
LongestChain := maxLength; { }
end;
{==========================================}
{ReadArrayTemperatures
}
{ T. NumDays
( T)}
procedure ReadArrayTemperatures(Var T: ArrayTemperatures;
Var NumDays: integer);
var
i : IndexElement;
begin
writeln;
repeat
write(' :');
readln(NumDays);
until (NumDays >= MinDays) and (NumDays <= MaxDays);
write(' : ');
for i := 1 to NumDays do
read(T[i]);

119

end; { ReadArrayTemperatures}
{==========================================}
{PrintArray .
T. NumDays
T.
}
procedure PrintArrayTemperatures (
const T : ArrayTemperatures;
NumDays : integer);
var
i:IndexElement;
begin
writeln(' : ');
for i:=1 to NumDays do
begin
write(T[i]:4);
{ 10 }
if i mod 10 = 0 then writeln; { i = 10, 20 30,

}
end;
writeln;
end; { PrintArrayTemperatures}
Var
T: ArrayTemperatures; { }
NumDays: Integer; { }
MaxLengthChain: Integer; { }
begin
{ }
writeln;
writeln;
ReadArrayTemperatures(T, NumDays);
{

120

20 }
MaxLengthChain := LongestChain(T, NumDays, 20);
{ }
PrintArrayTemperatures(T, NumDays);
{ }
writeln(' ');
writeln(' 20 ',MaxLengthChain,
' ');
end.

121


.
.

,
, :
A[N],
1<=N<=20.
, , .
.
, .
,
.
.
.

.
.

.

122

.

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

. .

, .

.

.


B1
2 .
: A[5]: 1 3 4 5 6 1 3 8 5 12.
123

B2
10.
: A[5]: 1 3 4 5 6 1 13 4 15 6.
B3
, ,
0.
: A[5]: 1 3 4 5 6 0 3 0 5 6.
B4
, ,
2.
: A[5]: 1 3 4 5 6 1 1 4 3 6.
B5
, ,
2 .
: A[5]: 1 3 4 5 6 1 3 2 5 6.
B6
, , 10.
: A[5]: 3 2 1 5 6 3 2 1 -5 -4.
B7
, , 3 .
: A[5]: 3 2 1 5 4 9 6 3 5 4.
B8
, , 1.
: A[5]: 3 2 1 5 4 3 2 1 5 3.
B9
, ,
10 .
: A[5]: 3 7 1 5 4 3 7 10 50 40.
B10
, ,
2 .
: A[5]: 3 2 1 0 4 3 1 1 0 4.
124


C1
.
: A[5]: 1 3 4 5 6 A[4]: 1 3 5 6.
C2
.
: A[5]: 1 3 4 5 6 A[4]: 1 3 4 5.
C3
.
: A[5]: 1 3 4 5 6 A[4]: 1 3 4 6.
C4
.
: A[5]: 1 3 4 5 6 A[4]: 3 4 5 6.
C5
0.
: A[5]: 1 9 8 3 5 A[6]: 1 9 8 0 3 5.
C6
0.
: A[5]: 1 6 8 3 4 A[6]: 1 6 0 8 3 4.
C7
0.
: A[5]: 1 3 8 3 5 A[6]: 1 3 8 3 5 0.
C8
.
: A[5]: 2 3 4 7 5 A[4]: 2 3 7 5.
C9
.
: A[5]: 2 3 4 7 5 A[4]: 2 4 7 5.
C10
0.
: A[5]: 1 3 8 3 4 A[6]: 1 3 8 3 4 0.
125


D1
.
: A[8]: 4 1 4 2 1 2 4 6 A[5]: 4 1 4 2 1.
( 6 8: 2 4 6).

D2
,
.
: A[8]: 4 3 4 2 1 2 4 6 A[5]: 3 1 2 4 6.
D3
,
.
: A[8]: 3 3 4 5 2 3 7 9 A[5]: 4 2 3 7 9.
D4
A , A,
B 2 .
: A[8]: 3 3 4 5 2 3 5 9
B[7]: 1 2 3 4 5 2 5.
2 , 5.
A : A[6]: 3 3 4 2 3 9.

D5

.
: A[9]: 3 6 4 5 2 3 4 6 4
A[6]: 3 6 5 3 6 4.

D6

126

A ,
B.
: A[9]: 3 2 4 5 2 3 2 6 5
B[6]: 1 3 4 7 8 9.
A :
A[7]: 3 5 2 3 2 6 5.

D7
A ,
B.
: A[10]: 3 2 7 .5 2 1 2 6 3 9
B[5]: 1 2 5 4 8.
A :
A[7]: 2 7 5 2 1 2 6.

D8
A ,
B.
: A[10]: 3 2 7 5 2 1 2 6 3 9
B[5]: 1 2 5 4 8.
A :
A[7]: 2 7 5 2 1 2 6.

D9
A B
.
: A[10]: 3 2 2 5 2 1 1 1 3 9
B[8]: 1 2 5 5 4 8 3 3.
A :
1 1 1 ( 7 9).
B :

127

5 5 ( 3 4).
A B: A[9]: 3 2 2 5 2 5 5 3 9.
B A: B[9]: 1 2 1 1 1 4 8 3 3.

D10
A B
.
: A[10]: 3 2 4 6 2 1 1 1 8 9
B[7]: 1 0 5 5 4 3 3.
A :
2 4 6 2 ( 2 5).
B :
0 ( 2).
A B: A[7]: 3 0 1 1 1 8 9.
B A: B[10]: 1 2 4 6 2 5 5 4 3 3.

128




() . Turbo Pascal CHAR.
.
Turbo Pascal 1 .
, ,
/ ,
.
:
Var
C, c1, c2: char; {c, c1, c2 }
S: array[1..10] of char; {s }
Const
Space = ; { }
XChar = @; { }
F = ;{ }
BigK = #75; { 75
K}


. .
.
. ,
.

ASCII (MS DOS), ANSI


(Windows), KOI8 (Unix), Unicode ( ,
). ASCII,
ANSI, KOI8 :
129

. Unicode .
256 ,
65536 .
ASCII

0
16
32
48
64
80
96
112
128
144
160
176
192
208
224
240

10 11 12 13 14 15

0
@
P
`
p

!
1
A
Q
a
q

2
B
R
b
R

#
3
C
S
c
s

$
4
D
T
d
t

%
5
E
U
e
u

&
6
F
V
f
v

7
G
W
g
w

(
8
H
X
h
x

)
9
I
Y
I
y

*
:
J
Z
J
Z

+
;
K
[
k
{

,
<
L
\
l
|

=
M
]
m
}

.
>
N
^
n
~

/
?
O
_
o

. 32
. 32 .
127 255. 241
.
176 223 .
MS DOS .
Turbo Pascal
Alt Alt
(
) .
, Alt.
, ,
.
130

,
, , , Turbo
Pascal . ,
++, ,
. ,
, , .

-.
, , ,

. Turbo Pascal
, , / ++.
Turbo Pascal .
Turbo Pascal .
:
, @ , 1 , A A,
.
. ,
.
, .
,
#:
#32 , #64 , #49 ,
#65 A,
#128 , #39 .
,
Ord.
, Chr.
, Succ Pred.

131

Inc Dec. ,

UpCase.
, .
. , C
D, .

Ord
Function Ord(C: char): LongInt;

C (Order ).
:
C := '!'; {C = #33}
X := Ord(c); {X 33 !
ASCII}
Write('Ord(', c, ')=', x); { Ord(!)=33}
C := '0'; {C = #48}
B := Ord(c); {B 48}
Write('Ord(', c, ')=', x); { Ord(0)=48}

Chr
Function Chr(B: Byte): Char;

B .
:
B := 33; { B 33}
C := Chr(B); {C = #33 = ! C 33
.. }
Write('Chr(', x, ')=''', c, ''''); {
Chr(33)=!}

132

Succ
Function Succ(C: Char): Char;

, , 1
C.
:
C:='5'; {C 5. 53}
C1 := Succ(c); {C1 6 . 54}
C2 := Succ(F); {C2 G}

Pred
Function Pred(C: Char): Char;

.
:
C:='5'; {C 5. 53}
C1 := Pred(c); {C1 4 . 52}
C2 := Pred(F); {C2 E}

UpCase
function UpCase(Ch: Char): Char;

Ch .
.
:
C:=UpCase(r); {c=R}
C:=UpCase(R); {c=R}
C:=UpCase(); {c=}
C:=UpCase(); {c=}


,
( ).
133

Inc
Procedure Inc(Ch: Char [; N: LongInt] );

Ch N. :
N .
1.
:
C:=7; {c=7}
Inc(C); {c=8}
Inc(C); {c=9}
Inc(C); {c=:}
Inc(C); {c=;}

N .
N.
:
C:=7; {c=7}
Inc(C, 2); {c=9}
Inc(C, 8); {c=A}
Inc(C, 5); {c=F}

Dec
Procedure Dec(Ch: Char [; N: LongInt] );

Inc,
Ch N. :
N .
1.
:
C:=2; {c=2}
Dec(C); {c=1}
Dec(C); {c=0}
Dec(C); {c=/}
Dec(C); {c=.}

134

N .
N.
:
C:=7; {c=7}
Dec(C, 7); {c=0}
Dec(C, 5); {c=+}
Dec(C, 5); {c=&}



1. ,
:
{
16x16}
var
i: integer; { }
j: integer; { }
begin
writeln;
{ 16 }
for i:=0 to 15 do
begin
{ 16 }
for j:=0 to 15 do
write(chr(i*16+j):2); {
16
}
writeln; { }
end;
end.

135

2. ,
, .
{ ToUpper.
Ch
. , .}
function ToUpper(Ch: char): char;
begin
{ Ch ,
,
32( ASCII )}
if (Ch>='a') and (Ch<='z') then Dec(Ch, 32)
{ Ch
,
, 32 ( ASCII
)}
else if (Ch>='') and (Ch<='') then Dec(Ch, 32)
{ Ch
,
, 80 ( ASCII
)}
else if (Ch>='') and (Ch<='') then Dec(Ch, 80);
{ Ch ,
}
{ }
ToUpper := Ch;
end;

ToLower
.

136


String

, ,
. Turbo Pascal 7.0 .
. -
-
.
Turbo Pascal String
ASCIIZ. String Turbo Pascal
, ASCIIZ .
, string , ASCIIZ.

String.
String Turbo Pascal .
256 , 0 255,
:
Type
String = array [0..255] of char;

string
255 256 1 255.
0
, String
n
:
Var
A: Array[1..255] of char; { , 0
255 }
N:Byte; { , A}

137

, S ,
:
Const
S = ;

S :

, T ,
:

Var
T : String;
Begin
T := Turbo Pascal;

T :

String, ,
, char (), ,
, , .
.
:
Var
S:string;
N:Byte;
Begin
Write( );
Readln(S); { S}
N:=s[0]; {
}
N:=Ord(s[0]); {
, , .. }

138

Writeln( ,S,); {
}
Writeln( ,N, ); {
}
Writeln( ,ord(s[0]), );{ }
Writeln( ,Length(S), );{
,
Length}
End.
..

String, , ,
.
,
. ,
.
1:
. #.
.
var
s : string; {}
I : byte; { S}
begin
{ }
write(' : ');
readln(s);
{ #}
for i:=1 to ord(s[0]) do
if s[i]=' ' then s[i]:='#';
{ }
writeln(' : ',s);
end.

139

, ,
,
. ,
ReadLN,
Read (!).
.
2. . , ,
. .
var
s : string; {}
i, j : byte; {}
begin
{ }
write(' : ');
readln(s);
{ }
I := 1;
while I <= ord( s[0] ) do
{ s[i] }
if ( s[i] = '.' ) or ( s[i] = ',' ) or ( s[i] = ':' )
or ( s[i] = '-' )
{**1**}
then
begin
{ }
for j := i to ord(s[0]) - 1 do
s[j] := s[j+1];
{ }
s[0] := chr( ord(s[0]) - 1);
{**2**}
end

140

else
i:=i+1; { }
{ }
writeln(' : ',s);
end.

2 ,
. :
{**2**} s[0]:=chr(ord(s[0])-1); dec(s[0])
.
:
{**1**} if (s[i]='.') or (s[i]=',') or (s[i]=':') or (s[i]='-')
if s[i] in [.,,,:,-] .
. ,
Turbo Pascal
.
string
Turbo Pascal String
.
+. ,
:=.
:
var
a, b, c: string; { }
name: string; { }
begin
a := ''; { A }

141

b := ''; { B }
c := a + b; { C }
c := a + ' + ' + b; { C + }
readln(name);
{ }
if name = '' { }
then writeln('!!!') { }
else
begin
{}
writeln('!!!!!'); { }
Halt; { }
end;
readln(name); { }
if name<>'' { ,
}
then writeln(', ...') { }
else writeln(', !!!'); {
}
if a<b { }
{ , ,
, }
then writeln('''',a,'''<''',b,'''') { :
''<''}
else writeln(', ''',a,'''<''',b,'''');
a := ''; { A B }
b := '';
if a > b { A B A B}
then writeln('''',a,'''>''',b,'''') {
>}
else writeln(', ''',a,'''>''',b,'''');
if a <= b { }

142

then writeln('''',a,'''<=''',b,'''')
else writeln(', ''', a, '''<=''', b, '''');
{,
', ''<=''}
if a >= b { , }
then writeln('''',a,'''>=''',b,'''') {
>=}
else writeln(', ''', a, '''>=''', b, '''');
end.

.

( A B)

(A[1] B[1]), (A[2] B[2]) (A[i] B[i]).
ASCII
,
: ord(A[I])=ord(B[I]). ,
. , .
,
, .
, ,
ASCII .
, .
:
= ( )
< (ord()<ord())
< (ord( )<ord())
< ( ,
143

, )
>angel ( , a)

Turbo Pascal 4 4
string.
:
Delete .
Insert .
Str .
Val .
:
Length .
Concat .
Copy .
Pos .
, Turbo Pascal STRING
.
Delete
Procedure Delete(var S: string; Index, Count: Integer);
S , Index
Count.
Index .
Count .
:
var
s: string; {}

144

begin
s := ' '; {
}
writeln(s); { }
delete(s, 9, 10);{ 10 , 9-,
}
writeln(s); { }
end.

Insert
Procedure Insert(Source: string; var S: string; Index:Integer);
Source S, Index.
:
S: = ' ';
insert(' ', s, 8);
writeln(s); { }

Str
Procedure Str(X[:Width[:Decimal]]; var S: string);
X () S
Width Decimal.
( - , ,
, ).
:

outtext outtextxy,
. ,
:
K := 1034;
Str(k, s);
OuttextXY(100, 200, s); { s=1034,
X=100, Y=200}

145

Val
Procedure Val(S; var V; var Code: Integer);
S
V. Code ,
. , Code=0.
:
.

read/readln
(runtime error)
. ,
,
read/readln, , read/readln
.
, x ,
: read(x), ,
, Invalid numeric format
.
, Turbo
Pascal. ,
.
. ,
,
. .
:
( ).
. ,
, , ,
.
146

{ X
Msg
[ minVal .. maxVal ] X}
procedure ReadInt(msg: string; var x: integer;
minVal, maxVal: integer);
var
s: string;
{ }
errCode: integer; { }
isError: boolean; { }
begin
repeat
write(msg); { }
readln(s); { , }
val(s,x,errCode); { }
{ , 0, 0,
}
isError := (ErrCode<>0) or
(ErrCode=0) and ( (x<=minVal) or (x>=maxVal) );
{ , }
if isError then writeln('Error! Please repeat input!');
until not isError; { , ,
, }
end;

{ X
}
function IntToStr(x: integer): string;
var
s: string;
begin
Str(x, s);
IntToStr := s;
end;

147

{
}
var
a: array[1..10] of integer; {}
n: integer; { }
i: integer; {}
begin
{ N}
ReadInt(' n=', n, 1, 10);
{ }
for i:=1 to n do
ReadInt(' a[' + IntToStr(i) + ']=',a[i],10,99);
{ }
for i:=1 to n do
write(a[i],' ');
writeln;
end.

Length
function Length(S: String): Integer;
.
:
S := ;
D := Length(S); { S 13 ,
D = 13}

148

Concat
function Concat(s1 [, s2,..., sn]: String): String;
s1, s2 ... sn .
:
S1 := ;
S2 := ;
S3 := ;
S4 := Concat(s3, ,s1, ,s2); { S4
}

,
+.
:
S1 := ;
S2 := ;
S3 := ;
S4 := s3 + + s1 + + s2; { S4
}

Copy
function Copy(S: String; Index: Integer; Count: Integer) : String;
S. Index ,
Count .
:
S1 := ;
S2 := Copy(s3, 4, 3); { S2 }
S3 := Copy(s3, 9, 2); { S3 }

149

Pos
function Pos(Substr: String; S: String): Byte;
S SubStr.
. ,
0.
:
S1 := ;
K := Pos(, s1); {K = 6
s1 6- }



1: S W Z. S
W Z.
: , S W.
var
s : string; {}
w : string; { }
z : string; {}
f : integer; { W}
l : integer; { W}
begin
{ }
write(' s= ');
readln(s);
{ }
write(' w= ');
readln(w);
{ }
write(' z= ');
readln(z);

150

{ W S}
F := Pos(W, S);
{ W}
L := Length(W);
{ S W}
Delete(S, F, L);
{ W Z}
Insert(Z, S, F);
{ W Z}
writeln(' s= ',S);
end.

151


Turbo Pascal
. Turbo Pascal
. -
,
.
256, (.. ord
) 0 255 ( ,
,
).
:
type
__ = set of _;

:
type
alpha = set of A .. Z; { .
. }
oper = set of (plus, minus, mul, div); { .
}
SetChar = set of char; { . }


.
,
( [A,F]).
[A..F] ,
[A,B,C,D,E, F]).
:
var
a, b, c: alpha; {a,b,c }

152

begin
a := [A,B,E..G] { A,B,E,F,G}
b := [Z] { Z}
c := []; { }


urbo
Pascal
+

<>

>=

<=

In


.
Var
a, b, c, d, e, f : Set of char; { }
begin

153

a := ['A' .. 'D']; { A 4 A,B,C,D}


b := ['C', 'D', 'R', 'W']; { B 4
C,D,R,W}
c := ['C', 'D']; { C 2 C,D}
d := a + b; {: D A
B. , ..
D 6 A,B,C,D,R,W}
e := a - b; { : E A,
B. .. E
A, , A B. E 2
- A,B}
f := a * b; {: F A,
B. F 2 C,D}
if a = b {: A B.
, True, False.
A B, .. A=[A..D], B=['C','D','R','W'], .. A=B False}
then writeln('A=B')
else writeln('A<>B'); { A<>B}
if c <> a * b {: A B.
, True,
False. C=[C,D], A*B=['C','D'] .. C
A*B, , c<>a*b False}
then writeln('C<>A*B')
else writeln('C=A*B'); { C=A*B}
if a >= c { : A C.
A C ( A,
C, ),
True, False. A=[A..D], =['C','D'], ..
A C, , a>=c True}
then writeln('A>=C') { A>=C}

154

else writeln('not A>=C');


if a<=b { : A B.
A B ( B,
A, ),
True, False. A=[A..D], B=['C','D',R,W],
.. A B, , a<=b
False}
then writeln('A<=B')
else writeln('not A<=B'); { not A<=B}
if 'A' in A {: , A
A. , True, False.
A=[A..D], , A A. , A in A
True}
then writeln('''A'' in A') { A in A}
else writeln('''A'' NOT in A')
end.


: ,
.
: ,
. ,
.
:
0 9 (
A F), ( a f).
:
Program digitsOfHex;
Type

155

SetChar = set of char; { }


Var
S: string; { }
I: integer; { }
HexSymb: SetChar; { }
InputSymb: SetChar; { }
TrueSymb: SetChar; { }
ErrSymb: SetChar; { }
MissSymb: SetChar; { }
{ , S, .
S , Message , }
procedure WriteSetChar(message: string; S: SetChar);
var
C: char; { }
begin
Write(message); { }
{ }
For c := #0 to #255 do
If c in S { C S}
then write(c,' '); { }
Writeln;
end;
Begin
{ }
HexSymb := ['0'..'9','a'..'f','A'..'F'];{
}
InputSymb := []; { }
{ }
Writeln;
Writeln( ,);
Writeln( );
Write ( :);

156

ReadLn(s);
{ }
For I := 1 to length(s) do
InputSymb := InputSymb + [s[i]]; {
,
InputSymb}
{ }
TrueSymb := InputSymb * HexSymb; {
,
}
{ }
ErrSymb := InputSymb - HexSymb; {
,
}
{ () }
MissSymb := HexSymb - TrueSymb; {
,
}

{ }
Writeln;
{, }
If TrueSymb <> []
Then { , }
WriteSetChar(' :',
TrueSymb)
Else { , }
Writeln(' !');
{,

157

}
If ErrSymb <> []
Then { , }
WriteSetChar(' :',
ErrSymb)
Else { , }
Writeln(' ,
!!!');
{,
}
If MissSymb <> []
Then { , }
WriteSetChar(' :',
MissSymb)
Else { , }
Writeln(' ,
!!!');
End.

,
,



:
n (n<=10).
70 . (
), .
,
.
158

,
.

:
1. ReadStrings.
2. PrintStrings.
3. GetAllChars.
4. ,
, PrintColorStrings.
5. WriteSetChar.
6. WaitPressed.
, .
.
:
{
10 .
,
.
}
uses crt; {
-,
textcolor clrscr, readkey}
Const
MaxLengthString = 70; { }
MaxNumberStrings = 10; { }
Type
TString = string[MaxLengthString]; {,
MaxLengthString }
SetChar = Set of char; { }

159

ArrayTString = array [1..MaxNumberStrings] of TString;


{ }

{ ReadString.
str. N .}
procedure ReadStrings(var str: ArrayTString;
var N: Integer );
var
s : TString; { }
begin
clrscr; { }
{ }
textcolor(LightGray); {
}
writeln;
writeln(' ', MaxNumberStrings,
' .');
writeln(' :');
{ }
n:=0; { }
repeat
{ }
textcolor(DarkGray); { - }
write(n+1,'> '); { - }
{ }
textcolor(LightGray); { }
readln(s); { }
if s <> '' then { }
begin
inc(n); { SS}

160

str[n] := s;
end;
until (n = MaxNumberStrings) or (s = ''); {
10 ,
}
end;

{ PrintStrings.
str.
N str.}
procedure PrintStrings(const str: ArrayTString;
N: Integer );
var
i: integer;
begin
clrscr; { }
textcolor(LightBlue); { }
writeln;
writeln(' ', n, ' (,). :');
for i := 1 to n do
begin
{ }
textcolor(LightBlue);
write(i:2, '> ');
{ }
textcolor(LightGray);
writeln( str[i] );
end;
end;

{ GetAllChars.
.

161

, str,
AllChars. N str.}
procedure GetAllChars(const str: ArrayTString; N: Integer;
var AllChars: SetChar);
var
i, j : integer;
begin
AllChars := []; { }
for i := 1 to n do { }
for j := 1 to length( str[i] ) do {
i- }
AllChars := AllChars + [ str[i,j] ]; {
AllChars}
end;

{ PrintColorStrings.
str.
N str.
,
S.}
procedure PrintColorStrings(const str: ArrayTString;
N: Integer;
var S: SetChar);
var
i, j: integer;
begin
writeln;
textcolor(LightBlue); { }
writeln(' ');
{ str}
for I := 1 to n do
begin

162

{ }
textcolor(LightBlue);
write(i:2,'> ');
{ i- }
for j := 1 to length(str[i]) do {Length(str[i])
str[i]}
begin
if str[i,j] in S { j- i-
S}
then textcolor(LightGreen) {
-}
else textcolor(DarkGray); { -}
write(str[i,j]); { }
end;
writeln; { i-
}
end; { }
end;

{ WriteSetChar.
, S,
.
S .
Message , .
ColorMessage Message.
ColorSet S}.
procedure WriteSetChar(message: string; S: SetChar;
ColorMessage, ColorSet: Integer);
var
C: char; { }
begin
{ }

163

textcolor(ColorMessage); { }
Write(message);
{ , }
textcolor(ColorSet); { }
For c := #0 to #255 do
{ , }
If c in S then write(c, ' ');
Writeln;
end;

{ WaitPressed.
message .
ColorMessage .
.}
procedure WaitPressed( message: string;
ColorMessage: Integer);
begin
textcolor(ColorMessage); { }
write(message); { }
readkey; { }
writeln;
end;

const
{ }
AllGlasn : SetChar = ['', '', '', '', '', '', '',
'', '', '',
'', '', '', '', '', '', '',
'', '', '' ];
var
ArrayStr : ArrayTString; { }
N
: integer; { }

164

FoundChars : SetChar; { ArrayStr }


FoundGlasn : SetChar; { ArrayStr
}

begin { }
{ }
ReadStrings(ArrayStr, N );
{ }
PrintStrings(ArrayStr, N );
{ , ArrayStr}
GetAllChars(ArrayStr, N, FoundChars);
{ , ArrayStr}
FoundGlasn := FoundChars * AllGlasn;
{ }
PrintColorStrings(ArrayStr, N, FoundGlasn);
{ , ArrayStr}
WriteSetChar(' ' +
' : ',
FoundGlasn, Red, LightRed);
{ }
WaitPressed(' ' +
' ...', LightBlue);
end. { }

165


: ,
, .
{
.
,

}

{=====================================================}
{ IsLetter.
TRUE, Ch .
False.}
function IsLetter(Ch: char): boolean;
begin
IsLetter := ch in [''..'',''..'',''..'','',''];
end;

{=====================================================}
{ IsSoglasn.
TRUE, Ch .
False.}
function IsSoglasn(ch:char):boolean;
begin
IsSoglasn := ch in ['','','','','','','','','',
'','','','','','','','','',
'','','','','',
'','','','','','','','','',
'','','','','','','','','',
'','','','',''];
end;

166

{=====================================================}
{ IsGlasn.
TRUE, Ch .
False.}
function IsGlasn(ch:char):boolean;
begin
IsGlasn := ch in
['','','','','','','','','','',
'','','','','','','','','',''];
end;

{=====================================================}
{ FindNextWord.
S , Start.
, True
( BeginWord)
( LengthWord).
, False.}
function FindNextWord( const S : String;
Start : Integer;
var BeginWord : Byte;
var LengthWord : Byte) : Boolean;
var
i : Integer; { 255,
Byte !}
Len : Byte; { }
Begin
{ }
Len := length(s);
{ , }
i := Start;
{ i ,
}

167

while not isLetter( S[i] ) and (i <= Len ) do


inc( i );
{ i }
BeginWord := i;
{ }
{ i ,
i }
while isLetter(S[i]) and ( i <= Len ) do
inc( i );
{ i ,
( i
).

}
LengthWord := i - BeginWord;
{ 0, ,
. True.
. False.}
if LengthWord > 0
then FindNextWord := true
else FindNextWord := false;
end;

{=====================================================}
{ StringWithoutGlasn.
,
S .}
function StringWithoutGlasn(const s:string):string;
var
res : string; {}
i : integer;
begin

168

res := ''; { }
for i := 1 to length(s) do { }
if not IsGlasn(s[i]) then, { }
res := res + s[i]; { ,
}
WordWithoutGlasn := res; { }
end;

{=====================================================}
{ DeleteGlasn.
S , ,
.
.}
function DeleteGlasn(const s:String):String;
var
newstr : string; { }
i : integer; {}
beg : byte; { }
len : byte; { }
w : string; { }
begin
newstr := ''; { }
{ S }
I := 1;
while FindNextWord( S, i, beg, len) do {
}
begin
{ ,
}
newstr := newstr + Copy(S, i, beg-i);
{ W}
w:=Copy(s,beg,len);
{ }

169

if IsSoglasn(w[1]) and IsSoglasn(w[2]) then


newstr := newstr + WordWithoutGlasn(w) {
}
else
newstr := newstr + w; {
}
I := beg + len; {
, }
end;
DeleteGlasn := newstr; { }
end;

{ }
var
s : string; {}
begin
s := ' ';
s := DeleteGlasn(s);
writeln(s); { ' '}
end.

170


,
. (
)
. .
.
0, 1, 2 ,
.
,
.
.
.


B1
#.
B2
#.
B3
&.
B4
&.
171

B5
$.
B6
@.
B7
X.
B8
.
B9
,
_ ().
B10
.

C1
,
.
C2
,
.
C3
,
.
C4
,
.
C5
, .
C6
,
.
172

C7
,
.
C8
,
, .
C9
,
.
C10
,
.

D1
,
.
D2
,
.
D3
,
.
D4
,
.
D5
,
.
D6
,
.
173

D7
.
D8
, ,
. .
D9
,
. ,
, .
D10
,
.

174



,
. ,
,
,
. , , ,

.
,
( )
,
.
,

,
,
. .
,
, .
.
, ,
.
, :
.
,
, , ,
,
. , ,
, ,
175

.
.
. -
,
.

.
, ,
, .

,
, ,
. ,
.

. -,
,
. -, .
Turbo Pascal,
. , . -,


.

:
Type <__>=file of<_>;
<__>=text;
<__>=file;

176

<__> ( ); file, of
(, ); <_> Turbo Pascal,
.

Turbo Pascal
Type
Product= record
Name: string;
Code: word;
End;
Text80= file of string[80];
Var
F1: file of char;
F2: text;
F3: file;
F4: Text80;
F5: file of Product;


Turbo Pascal:

( file of..);

( text);

( file).

,
Turbo Pascal
. Turbo Pascal
,
, / . ,

Input, Output.
, Turbo Pascal, ,

. Turbo Pascal
- ,
177

, ,
,
, .
() . ,

.

.


1.
:
Assign(<_>, <__>)
,
, .
, ..
. :
Assign (chf, 'G:\Home\ Student\ Lang\ Pascal\ primer.dat');
,
autoexec.bat.

, :
CON , .. ;
PRN .
, LPT1, LPT2, LPT3.

.
178

2. ,
Close(< >);

.
, close
, assign,
, ,
assign.
, ,
.
, , ..
, ( ),
( ). ,
( )
, .
.
3.
Rewrite(<__>);
Rewrite(f) ( f )
f ,
.
- ,
.
4.
Write(<__>, < >);
write(f, x) ,
, ,
. ,
179

.
:
,
, ,
, .
5.
Reset(<__>);

, .

, /,
.
6.
Read(<__>,< >);
read()
, ,
, ,
.
, ,
.
,
- ,
.
7.
Eof(<__>);
end
of file. true,
, .. ,
. false.
180

8.
Rename(<__>, <__>);
_ _ ,
, .
,
.
9.
Erase(<__>);
,
.
10.
Truncate(<__>);
11.

Append(<__>);

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

0.
.
read() write() ,
181

. ,

.


:
fileSize(<__>) ,
;
filePos(<__>)
;

seek(<__>,n) ,

n.
,

seek(<__>,0) ,
seek(<__>, FileSize(<__>))
.


. , ,
.
, .

, .
assign, reset, rewrite, read, write eof.
seek, filepos, filesize .
()
EOLN (end of line ).
EOLN
182

(<__>), true,
.
write read
,
.

, ,
.
,
.
,
;

, , ;

,
.

, 6, 65 165
. ,
, .
.
( byte): 2, 12, 2, 128.
2122128.
byte
, .
, -, ,
. ,
.

.

183

read write
readln writeln.
, writeln
.
. readln

.
Program primer;
Var f1,f2:text;
I,n: integer;
S: string;
Begin
{ }
Assign(f1, file1.txt); {
}
Rewrite(f1);{ }
Readln(n) { }
for i:=1 to n do
begin
readln(s); { }
writeln(f1,s); { }
end;
close(f1); { ,
file1.txt, .
, ,
, }
{ : }
Reset(f1); { }
Assign(f2, file2.txt); {
}
Rewrite(f2); { }
{ ,
.

184


}
While not eof(f1) do
Begin
Readln(f1,s);{ }
If (s[1]=A) or (s[1]=a) then
Writeln(f2,s); { , }
End;
Close(f1,f2); { }
{ : }
Writeln;
Writeln( :);
Reset(f2); { }
While not eof(f2) do { }
Begin
Readln(f2,s);{ }
Writeln(s); { }
End;
End.

185



.

,
. (
) .
.
.
,
.
.
.

1
input.dat .
.
output.dat, , , - ,
, .
2
input.dat .
.
output.dat, ,
.
3
input.dat .
.
output.dat, ,
.
186

4
input.dat .
.
output.dat, , ,
.
5
input.txt .
.
output.txt, ,
.
6
input.dat .
.
output.dat, , ,
.
7
input.txt .
.
output.txt,
.
8
input.txt .
.
output.txt, ,
diction.dic.
9
input.dat .
.
output.dat, ,
.
10
input.txt .
.
187

output.txt, , , .
sinonim.dat :
1 1
2 2
.......................
N N
11
input.dat .
.
output.dat, ,
.
errors.dat.
12
input.dat .
.
output.dat, ,
.
13
input.dat .
.
output.dat, ,
.
14
input.dat .
.
output.dat, , .
15
input.dat .
.
output.dat, , ,
.

188

16
input.dat .
.
output.dat, ,
.
17
input.dat .
.
output.dat, ,
words.dat.
18
input.dat .
.
output.dat, ,
.
19
input.dat .
.
output.dat, ,
.
20
input.dat .
.
output.dat, , ,
,
40 .
21
input.dat .
.
output.dat, - (, ,
).

189

22
input.dat .
.
output.dat, ,
.
23
input.dat .
.
output.dat, ,
(,
, , ).
24
input.dat .
.
output.dat,
.

190


Turbo Pascal,
.
, ( )
. ,
,
,
.
,
,
, , .
,
.
Turbo Pascal .
,
, , ,
.



. , ,
- . , ,
:
Type
TPoint2D = record
X,Y: Real; { }
End;

TPoint2D.
2 X Y .
191

TPoint2D:
T () Turbo Pascal (T
Type).
Point .
2D
(2 direction).
,
. .
,
.
Type
TPoint = record
X,Y:Integer; { }
End;

TPoint X Y ,
.
TPoint, ,
:
Type
TLine = record {}
P1,p2:TPoint; { }
Color: Byte; { }
End;
TCircle = record {}
Center:TPoint; { }
R:Integer; { }
Color: Byte; { }
End;


,
Var.
192

:
Var
P1,p2:TPoint2D; { TPoint2D}
X,y:TPoint; { TPoint}
L1,L2:TLine; { TLine}
C1,C2:TCircle; { TCircle}
A:array[1..10] of TLine; { 10 }
F: File of TCircle; { }


. ,

( ):
: / / TPoint.
Var
A:Array[1..10] of integer;
N,i:Byte;
Begin
{ }
Read(N);
For i:=1 to N do
Read(a[i]);
{ }
For i:=1 to N do
Write(a[i], );
End.

Var
L:TPoint;
Begin
{ }
Read(L.X); { X}
Read(L.Y); { Y}
{ }
Write(L.X, );{ X}
Write(L.Y, );{ Y}
End.

,
, , , .

, , , ,
.

193

:
:
Write(a[i], );

:
Write(L.Y, );{ Y}

(
TLine TCircle),
, ,
.
: TLine
Var
LL:TLine;
Begin
{ }
Read(LL.p1.X); { X p1}
Read(LL.p1.Y); { Y p1}
Read(LL.p2.X); { X p2}
Read(LL.p2.Y); { Y p2}
Read(LL.Color); { }
{ }
Writeln(LL.p1.X); { X p1}
Writeln (LL.p1.Y); { Y p1}
Writeln (LL.p2.X); { X p2}
Writeln (LL.p2.Y); { Y p2}
Writeln (LL.Color); { }
End.

, .

, .
:
:
Read(kk[1,4]);

:
Read(LL.p1.X);

194


,
, (
),
.
:
Var
N: Byte;
A:Array[1..10] of Tcircle;
I:Byte;
begin
Read(n);
For i:=1 to n do
Begin
Read(a[i].Center.X);
Read(a[i].Center.Y);
Read(a[i].R);
Read(a[i].Color);
End;


, (
) .
.
1: A (
) circles.dat
Var

F:file of TCircle;

195

Begin

assign(F,'Circles.dat'); { F
}
rewrite(f); { }
for i:=1 to n do { }
write(f , a[i]);
close(f); { }

2: A
circles.dat

assign(F,'Circles.dat'); { F
}
reset(f); { }
n:=FileSize(f); { }
for i:=1 to n do { }
read(f , a[i]);
close(f); { }

With
With .
: With X do Y;

X
Y
Y X X
.
: A TCircle (
)
For i:=1 to n do

196

With a[i] do
Begin
Write( a[,i,].Center.X=,Center.X); { a[i].Center.X}
Write( a[,i,].Center.Y=,Center.Y); { a[i].Center.Y}
Write( a[,i,].R=, R);
{ a[i].R}
Write( a[,i,].Color=, Color);
{ a[i].Color}
End;

, With
With
a[i] .

, , ,
.
Turbo Pascal,

64
,
64 . ,
,
. ,
,
sizeof(_) sizeof(_).
, ,
, ,
. , ,
TPoint,
Type
TPoint = record
X,Y:Integer; { }
End;

:
197

sizeof(TPoint) = sizeof(integer)+sizeof(integer) = 2 + 2 = 4

, TCircle,
Type
TCircle = record {}
Center:TPoint; { }
R:Integer; { }
Color: Byte; { }
End;

:
sizeof(TCircle)=sizeof(TPoint)+sizeof(integer)+sizeof(byte) = 4+2+1 =
= 7

, ,
,
( ), .
, TArrayCircles :
Type
TArrayCircles = array[1..10] of TCircles;

, ,
:
sizeof(TArrayCircles) = sizeof( TCircles)*10 = 7 * 10 = 70

:
, Turbo
Pascal, :
Byte, ShortInt, Char, Boolean 1
Word, Integer 2
LongInt, Single 4
Real 6
Double, Comp 8
Extended 10
1 32 (
).
198

.
string Var
s:string;

256 .

Var
s1:string[20];

.
20+1 = 21 .

,
.
:
) ,
) .
.
,
.

.
, .
. case,
-.
of, end, ,
.
:
.

199


.

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

.
4
, ,
.
4
,
.
, 4
. , 5
.
. ,
.
- .
- .
- :
FigureType = { }
(FTRect, { }
FTFillRect, { }

200

FTTextInRect { }
);

TColor,
:
TColor = 0..15; {}

:
TRect = record {, }
{ }
Left,Top,Width,Height:Word; { }
ClBorder:TColor; { }
{ }
case fig:FigureType of { -}
{ }
FTRect:
(); {
}
FTFillRect: {
}
(ClFill:TColor; { }
StyleFill:Byte { }
);
FTTextInRect: {
}
(ClText:TColor; { }
Str:String[20] {}
);
end;

.
{ Rect TRect }
procedure ReadRect(var Rect:TRect);
var
case_type :Byte; { }
begin
With Rect do

201

begin
writeln;
writeln;
writeln(' :');
{ }
writeln(' :');
write (' X ='); readln(Left);
write (' Y ='); readln(Top);
write (' ='); readln(Width);
write (' ='); readln(Height);
write (' ='); readln(ClBorder);
writeln;
{ }
writeln(' :');
writeln(' 1 ');
writeln(' 2 ');
writeln(' 3 ');
readln(case_type); { }
{ }
case case_type of
2: { }
begin
fig:=FTFillRect;
write(' :'); readln(ClFill);
write(' :'); readln(StyleFill);
end;
3: { }
begin
fig:=FTTextInRect;
write(' :'); readln(ClText);
write('
:'); readln(Str);
end;
else { }
fig:=FTRect;
end;{case ... end}
end; {With ... end}

202

end; {Procedure ... end}

:
{ , Rect TRect}
procedure ShowRect(Rect:TRect);
begin
With Rect do
begin
{ }
setcolor(ClBorder);
rectangle(Left,Top,Left+Width-1,Top+Height-1);
{ :}
case fig of
FTFillRect: { }
begin
{ }
setfillstyle(StyleFill,ClFill);
bar(Left+1,Top+1,Left+Width-2,Top+Height-2);
end;
FTTextInRect: { }
begin
{ }
setcolor(ClText);
OutTextXY(Left+2,Top+2,Str);
end;
end;{case ... end}
end; {With ... end}
end; {Procedure ... end}


:
var
r: array[1..10] of TRect;
n,i:Integer;
begin

203

{ }
write(' n=');
readln(n);
for i:=1 to n do
ReadRect(r[i]);
...
{ }
...
{ }
for i:=1 to n do
ShowRect(r[i]);
...


, ,
:
sizeof(_) = sizeof(_) + sizeof(-)
+
Max{sizeof(_1),
sizeof(_2)
..
sizeof(_N)}.

, ,
, ,
, - ,
.
,
TRect. ,
:
sizeof(_)= sizeof(Word)*4 + sizeof(TColor) =
= 2*4+1 = 9

, -:
sizeof(-) = sizeof(FigureType) = 1

, :
sizeof(_ FTRect) = 0

204

sizeof(_ FTFillRect) = sizeof(TColor) +


+ sizeof(Byte) = 1+1 = 2
sizeof(_ FTTextInRect) = sizeof(TColor) +
+ sizeof(string[20]) = 1+21 = 22


FTTextInRect 22 .
:
sizeof(_) =
= sizeof(_) + sizeof(-) +
+ Max{sizeof(_1),
sizeof(_2)
...
sizeof(_N)} =
= 9 + 1 + 22 = 32

205


:
.
:
, , ,
.
, .
.
20 .

, .
(array[1..20] of _).

(File of _).
:
.
:
1.
2.
3.
4.
5.
6.
7.
8.
9.
.
206

:
3 :
1.
2.
3.
/
.

:
1. .
)
( );
)
(
);
)
( ).
2.
)
)
:

207

10

,
:
,
. ,
( ,
):

208

6, 12



/
10000

100

300


100

5 2001 .

(/)

4
.
1995 .

30
.

3

++.

209

10


300


:
,
.
:
:


.
, :
,
:






.
:
, ,
1970 .
210

:
3 :
Menu_Txt ( Menu_Txt.pas)
Info ( Info.pas)
( LabRec.pas).

Menu_Txt
Menu_Txt.pas Menu_Txt
{ Menu_Txt.pas
Menu_Txt
}
unit menu_txt;
interface
type
{ }
TItemMenu = record
str:string[76]; { }
x,y:byte; { }
res:byte; {, }
end;
{ }
TMenu = record
nItems:Byte; { }
item:array[1..10] of TItemMenu; { }
Left,Top:integer; { }
Width,Height:integer; { }
clMenu,clText:byte; { }
clMarkedItem,clMarkedText:Byte; {
}

211

MarkedItem:byte; { }
end;

{ }
procedure BlackScreen;
{
Left,Top
Width
Height
BckColor ( )
}
procedure ColorWindow(Left,Top,Width,Height,BckColor:Byte);
{
:
0
menu.item[menu.markedItem].res
}
function MenuVert(var menu:TMenu):byte;
{
str
x,y
TxtColor
BckColor
:

}
procedure ShowText(const str:string; x,y,TxtColor,BckColor:byte);

implementation
uses crt;

212

const
{ }
keyUp = #72; { }
keyDown = #80; { }
keyLeft = #75; { }
keyRight = #77; { }
keyEnd = #79; { End}
keyHome = #71; { Home}
keyEnter = #13; { Enter}
keyEsc = #27; { Esc}

{ }
procedure BlackScreen;
begin
window(1,1,80,25);
TextBackground(0);
TextColor(7);
clrscr;
end;
{
Left,Top
Width
Height
BckColor ( )
}
procedure ColorWindow(Left,Top,Width,Height,BckColor:Byte);
begin
window(Left,Top,Left+Width-1,Top+Height-1);
TextBackground(BckColor);
clrscr;
end;
{
str

213

x,y
TxtColor
BckColor
:

}
procedure ShowText(const str:string; x,y,TxtColor,BckColor:byte);
begin
TextBackground(BckColor); { }
textcolor(txtcolor); { }
gotoxy(x,y); { }
write(str); { }
gotoxy(x,y); { }
end;

{
:
0
menu.item[menu.markedItem].res
}
function MenuVert(var menu:TMenu):byte;
var
c:char; { }
i:byte; {}
begin
with menu do
begin
{ , }
ColorWindow(Left,Top,Width,Height,clMenu);
{ }
for i:=1 to nItems do
ShowText(item[i].str, item[i].x,item[i].y, clText,clMenu);
while true do
begin

214

ShowText(item[markedItem].str,
item[markedItem].x,item[markedItem].y,
clMarkedText, clMarkedItem);
{ , - }
c:=readkey;
if c=#0 then c:=readkey;
{ , }
ShowText(item[markedItem].str,
item[markedItem].x,item[markedItem].y,
clText,clMenu);
{ }
case c of
keyUp: dec(markedItem);
keyDown: inc(markedItem);
keyHome: markedItem:=1;
keyEnd: markedItem:=nItems;
keyEnter: begin { Enter}
MenuVert:=item[markedItem].res; {
Menu
}
exit; { }
end;
keyEsc: begin { Esc}
MenuVert:=0; { Menu
0 , }
exit; { }
end;
end;
{ }
if markedItem<1 then
markedItem:=nItems;
if markedItem>nItems then markedItem:=1;
end;
end;
end;

215

begin
end.

Info
Info.pas Info
{ Info.pas
Info

}
unit Info;
interface
const
maxNInfo = 20; { }
type
{ }
MedicalInfo = record
familia : string[30]; {}
imja
: string[20]; {}
otchestvo : string[25]; {}
YearBirth : integer; { }
GroupBlood: string[5]; { }
Height : byte;
{}
Weight : byte;
{}
end;
{ }
ListOfPerson = record
n:byte; { }
person:array [1..maxNInfo] of MedicalInfo; { }

216

end;

{ list}
procedure Clear (var list:ListOfPerson);
{ list person
true
false
}
function AddPerson (var list:ListOfPerson;
const person:MedicalInfo):boolean;
{ del list
true
false
}
function DelPerson (var list:ListOfPerson;
const del:byte):boolean;
{ list}
procedure SortList (var list:ListOfPerson);
{ list filename
true -
false -
}
function SaveList (var list:ListOfPerson;
const filename:string):boolean;
{ list filename
true -
false
}
function LoadList (var list:ListOfPerson;
const filename:string):boolean;

217

implementation

{ list}
procedure Clear(var list:ListOfPerson);
begin
list.n:=0;
end;
{ list i j}
procedure Exchange(var list:ListOfPerson; const i,j:byte);
var
tmp:MedicalInfo;
begin
if i<>j then
begin
tmp
:= list.person[i];
list.person[i] := list.person[j];
list.person[j] := tmp;
end;
end;
{ list person
true
false
}
function AddPerson(var list:ListOfPerson;
const person:MedicalInfo):boolean;
begin
if list.n=maxNInfo then
AddPerson:=false
else
begin

218

inc(list.n);
list.person[list.n]:=person;
AddPerson:=true;
end;
end;

{ del list
true
false
}
function DelPerson(var list:ListOfPerson;
const del:byte):boolean;
begin
if (del<1) or (del>list.n) then
DelPerson:=false
else
begin
list.person[del]:=list.person[list.n];
dec(list.n);
DelPerson:=true;
end;
end;
{ list}
procedure SortList(var list:ListOfPerson);
var
imin:byte;
i,j:byte;
begin
with list do
begin
if list.n<2 then exit;
for i:=1 to n-1 do
begin

219

imin:=i;
for j:=i+1 to n do
if person[j].familia<person[imin].familia
then
imin:=j;
Exchange(list,i,imin);
end;
end;
end;
{ list filename
true
false
}
function SaveList(var list:ListOfPerson; const filename:string):boolean;
var
f:file of MedicalInfo;
i:byte;
begin
assign(f,filename);
{$I-}
rewrite(f);
{$I+}
if ioresult<>0
then
begin
SaveList:=false;
exit;
end;
for i:=1 to list.n do
write(f,list.person[i]);
close(f);
SaveList:=true;
end;

220

{ list filename
true
false
}
function LoadList(var list:ListOfPerson; const filename:string):boolean;
var
f:file of MedicalInfo;
n,i:byte;
begin
assign(f,filename);
{$I-}
reset(f);
{$I+}
if ioresult<>0
then
begin
LoadList:=false;
exit;
end;
n:=FileSize(f);
if (n<0) or (n>maxNInfo)
then
begin
close(f);
LoadList:=false;
exit;
end;
Clear(list);
for i:=1 to n do
read(f,list.person[i]);
close(f);
list.n:=n;
LoadList:=true;
end;

221

begin
end.


LabRec.pas .
{ LabRec.pas





,

1.
2.
3.
4.
5.
6.
7.
. .

:




222




}
uses crt,menu_txt,info; { Crt
Menu_Txt Info}
const
{ }
MainMenu: TMenu=( nItems:9; { }
item:( { }
(str:'
'; x:2; y:2; res:1), {1- }
(str:' '; x:2; y:3; res:2), {2-
}
(str:'
'; x:2; y:4; res:3), {3- }
(str:' '; x:2; y:5; res:4), {4-
}
(str:' '; x:2; y:6; res:5), {5- }
(str:'
'; x:2; y:7; res:8), {6- }
(str:' '; x:2; y:8; res:6), {7-
}
(str:' '; x:2; y:9; res:7), {8-
}
(str:'
'; x:2; y:10; res:100), {9- }
(str:'';
x:0; y:0; res:0) {10- }
);
Left:20;
Top:5; { }
Width:40;
Height:11; { }
clMenu:2;
clText:14; { }
clMarkedItem:4; clMarkedText:15; {
}
MarkedItem:1 { }
);

223

var
persons: ListOfPerson; { }
{ }
procedure ClearArray;
begin
Clear(persons);
ColorWindow(20,1,40,3,1);
ShowText(' !',13,2,14,1);
readkey;
end;
{ }
procedure InsertToArray;
var
person:MedicalInfo; { }
begin
{ }
ColorWindow(1,16,80,10,3);
ShowText(' :',20,1,14,3);
ShowText(' : ',3,3,15,3);
ShowText('
: ',3,4,15,3);
ShowText(' : ',3,5,15,3);
ShowText(' : ',3,6,15,3);
ShowText(' : ',3,7,15,3);
ShowText('
: ',3,8,15,3);
ShowText('
: ',3,9,15,3);
{ }
gotoxy(20,3);
readln(person.familia);
gotoxy(20,4);
readln(person.imja);
gotoxy(20,5);
readln(person.otchestvo);

224

gotoxy(20,6);
readln(person.yearbirth);
gotoxy(20,7);
readln(person.groupblood);
gotoxy(20,8);
readln(person.Height);
gotoxy(20,9);
readln(person.Weight);
if AddPerson(persons,person) { }
then
begin { }
ColorWindow(20,1,40,3,1);
ShowText(' !',6,2,14,1);
end
else
begin { }
ColorWindow(20,1,40,3,4);
ShowText(' !',3,2,15,1);
end;
readkey;
end;
{ }
procedure DeleteFromArray;
var
del:byte; { }
begin
{ }
ColorWindow(1,16,80,10,3);
ShowText(' :',10,3,14,3);
gotoxy(45,3);
readln(del);
if DelPerson(persons,del)

225

then
begin { }
ColorWindow(20,1,40,3,1);
ShowText(' !',6,2,14,1);
end
else
begin { }
ColorWindow(15,1,50,3,4);
ShowText(' !',3,2,15,4);
end;
readkey;
end;
{ }
procedure SortArray;
begin
SortList(persons);
ColorWindow(20,1,40,3,1);
ShowText(' !',13,2,14,1);
readkey;
end;
{ }
procedure ViewArray;
var
i:byte;{}
begin
BlackScreen;
ColorWindow(1,1,80,23,1);
{ }
gotoxy(1,1);
write('|
|
| |',
'_|_||');
gotoxy(1,2);
write('+-----------------+--------------+-----------+',
'------------+------------+----+---');

226

{ }
for i:=1 to persons.n do
begin
gotoxy(1,i+2);
write('|',persons.person[i].familia);
gotoxy(19,i+2);
write('|',persons.person[i].imja );
gotoxy(34,i+2);
write('|',persons.person[i].otchestvo );
gotoxy(46,i+2);
write('|',persons.person[i].YearBirth );
gotoxy(59,i+2);
write('|',persons.person[i].GroupBlood );
gotoxy(72,i+2);
write('|',persons.person[i].Height);
gotoxy(77,i+2);
write('|',persons.person[i].Weight);
end;
ShowText(' !',15,23,15,4);
readkey;
end;
{ ,
1970
4
}
procedure ViewPartArray;
var
i:byte;{}
begin
BlackScreen;
ColorWindow(1,1,80,23,1);
{ }
ShowText(' , 4 , '+

227

' 1970 ',5,1,14,1);


textcolor(15);
gotoxy(1,2);
write('|
|
| |_|');
gotoxy(1,3);
write('+-----------------+--------------+-----------+------------+');
{ }
for i:=1 to persons.n do
with persons.person[i] do
if (YearBirth>=1970) and (GroupBlood='4') { }
then { <=1970, =4}
begin
gotoxy(1,i+3);
write('|',familia);
gotoxy(19,i+3);
write('|',imja );
gotoxy(34,i+3);
write('|',otchestvo );
gotoxy(46,i+3);
write('|',YearBirth );
end;
ShowText(' !',15,23,15,4);
readkey;
end;
{ }
procedure SaveArray;
var
filename: string;{ }
begin
{ }
ColorWindow(1,16,80,10,3);
ShowText(' :',10,3,14,3);
gotoxy(50,3);

228

readln(filename);
if SaveList(persons,filename) {}
then
begin { }
ColorWindow(20,1,40,3,1);
ShowText(' '+filename,2,2,14,1);
end
else
begin { }
ColorWindow(15,1,50,3,4);
ShowText(' '+filename,3,2,15,4);
end;
readkey;
end;

{ }
procedure LoadArray;
var
filename: string;
begin
{ }
ColorWindow(1,16,80,10,3);
ShowText(' :',10,3,14,3);
gotoxy(50,3);
readln(filename);
if LoadList(persons,filename) {}
then
begin { }
ColorWindow(20,1,40,3,1);
ShowText(' '+filename,2,2,14,1);
end
else

229

begin { }
ColorWindow(15,1,50,3,4);
ShowText(' '+filename,3,2,15,4);
end;
readkey;
end;

{ }
begin
Clear(persons); { }
repeat
BlackScreen; { }
case MenuVert(MainMenu) of {

}
1: ClearArray;
2: InsertToArray;
3: DeleteFromArray;
4: SortArray;
5: ViewArray;
6: SaveArray;
7: LoadArray;
8: ViewPartArray;
0,100: Exit; {, }
{ Esc}
end;
until false;
end.

230

.
,
.
,
.
(Pascal, C, .)
, .

. .
, ,
;
( ).

.
-,


. -, -
,
. -,
.

. ,
, .
,
.
.
,
. .
.
Turbo Pascal Pointer

.
231

PSomeType SomeType
:
type
PSomeType = ^ SomeType;

:
.
TWorker (), ,
. PWorker
TWorker, , , PInteger Integer
PChar Char.
:
Type
TWorker = record
Fio:string[60];
Post:string[60];
Wages:Integer;
end;
PWorker = ^ TWorker;
PInteger = ^ Integer;
PChar = ^ Char;

{ }
{ }
{ }
{ }
{ TWorker}
{ Integer}
{ Char}

,
P. P ,
(P Pointer).
P, , -

P
( , ).

.
:
1) @.
232

:

(
,
, , PWorker
TWorker, PChar Char).
Var
{}
pw:PWorker;
pi:PInteger;
pc: PChar;
p : Pointer;
{ }
w:TWorker;
i:Integer;
c: Char;

, Turbo Pascal 4 .
,
TWorker 124 . ,
Integer Char 2 1 .

, ,
.
,
, , .
pw $561F005E (
).
:


pw
4
pi
4
pc
4
p
4
w
124 (127=$7C)
i
2
c
1

233

$561F005E
$561F0062
$561F0066
$561F006A
$561F006E
$561F00EA
$561F00EC

, ,
:

begin
p:=@p;
{ p p
$561F006A}
p:=@c;
{ p c
$561F00EC}
pc:=@c; { pc c
$561F00EC}
pi:=@i;
{ pi i
$561F00EA}
pw:=@w;{ pw w
$561F006E}
p:=@pw;{ p pw
$561F005E}
end.

..
..
..
..
..
..

2)
:
2.1) Pointer

:
begin
pw:=@w;
$561F006E}
pc:=@c;
$561F00EC}
p:=pw;
$561F006E}
p:=pc;
$561F00EC}
end.

{ pw w ..
{ pc c ..
{ p w ..
{ p c ..

2.2)
Pointer

234

:
begin
p:=@w; { p w .. $561F006E}
pw:=p; { pw w ..
$561F006E}
pc:=p; { pc w ..
$561F006E}
end.

2.3)

:
var
i:integer;
c:char;
pc1,pc2:PChar;
pi1,pi2: PInteger;
begin
pc1:=@c;
{ pc1 c}
pc2:=pc1;
{ pc2 c}
pi2:=@i;
{ pi2 i}
pi1:=pi2;
{ pi1 i}
end.

3) nil
: nil
. :
const
nil : longint = 0;

Nil , ,
.
:
var
p:Pointer;
pi: PInteger;
pc:PChar;

235

begin
p:=nil;
pi:=nil;
pc:=nil;
end.

4) New Getmem.

-
, ,

.
,
.
,
,
New Getmem.
New
procedure New(var P:Pointer)
P .
Dispose
procedure Dispose(var P:Pointer) ,
, P.
1:
var
pi: Integer; { }
begin
New(pi);
{ }
pi^:=100;
{ 100}
writeln(pi^); { =100}
Dispose(pi); { }
end.

2:
236

var
pw: PWorker; { }
begin
New(pw);
{ TWorker}
pw^.Fio:='';
{ Fio ''}
pw^.Post:='';
{ Post ''}
pw^.Wages:=2500;
{ Wages 2500}
writeln(pw^.Fio);
{ ''}
writeln(pw^.Post);
{ ''}
writeln(pw^.Wages);
{ 2500}
Dispose(pw);
{ }
end.

1:

, ^.
2:
,
. Dispose (
New) FreeMem (
GetMem ( )).
3:
,
(.. ,
-
).
:
var
pi:PInteger;
i:integer;
begin
pi^:=100; { , }
{ , pi
- , pi^:=100;
.
100.
, 100
. , ,
,
.

237

}
pi:=@i;
pi^:=200; { . 200
i}
New(pi);
pi^:=300; { . 300
,
pi}
Dispose(pi); {
.

.
,
,
, .
, ,
,
.
, , ,
,
}
end.

4:

.
:
var
pi:PInteger;
begin
New(pi);
pi^:=10;
writeln('pi^=', pi^ );
Dispose(pi);
pi^:=20;

{ }
{ 10}
{ 10}
{ }
{!!!
, 20}

end.

238

GetMem:
procedure GetMem(var P:Pointer; Size:Word)
Size
P.
:
var
pi: Integer; { }
begin
GetMem(pi,sizeof(integer)); {
}
pi^:=1000;
{ 1000}
writeln(pi^);
{ =1000}
FreeMem(pi,sizeof(integer)); {
}
end.

:
, GetMem,
FreeMem.
FreeMem:
procedure FreeMem(var P:Pointer; Size:Word)
Size.
, , ,
: = <>. ,
:
1) pointer pointer
2) pointer
3) pointer
4)

239

:
var
p,p1:pointer;
pi,pi1:PInteger;
pc,pc1: PChar;
pw,pw1:PWorker;
b: Boolean;
begin
...
if p = p1 ...
while p <> pi ...
while pi = pi1 ...
b:= pc = p; ...
until pw <> pw1 ...
until pc = pi ...
if pw <> pi1 ...
...
end.

{pointer pointer }
{pointer PInteger }
{PInteger PInteger }
{PInteger pointer }
{PWorker PWorker }
{PChar PInteger !!!}
{PWorker PInteger !!!}

,
. Turbo
Pascal 300-500 .
, ,
, 64 .

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

Program DynamicMemoryExample;
const
maxN = 100; {

A B}
type
Arr = array[1..maxN] of integer;{}
PArr = ^Arr; { }
var
a,b:PArr;
{a,b }
n,m:integer; {n,m - }
i:integer;
{i }
begin

Program StaticMemoryExample;
const
maxN = 100;
type
Arr = array[1..maxN] of integer;
var
a,b:Arr;
n,m:integer;
i:integer;
begin

{ n}
repeat
read(n);
until (n>=1) and (n<=maxN);

repeat
read(n);
until (n>=1) and (n<=maxN);

{ A}
GetMem(a,sizeof(integer)*n);
{ A}
for i:=1 to n do
read(a^[i]);

for i:=1 to n do
read(a[i]);

{ A}
m:=0;
for i:=1 to n do
if a^[i] mod 2 = 0
then inc(m);
{ B}
GetMem(b,sizeof(integer)*m);
{ B
A}
m:=0;
for i:=1 to n do
if a^[i] mod 2 = 0
then
begin
inc(m);
b^[m]:=a^[i];
end;

m:=0;
for i:=1 to n do
if a[i] mod 2 = 0
then
begin
inc(m);
b[m]:=a[i];
end;

{ B}

241

for i:=1 to m do
write(b^[i],' ');

for i:=1 to m do
write(b[i],' ');

{ A B

}
Freemem(a,sizeof(integer)*n);
Freemem(b,sizeof(integer)*m);
end.

end.

,
, .

.
,
?
.
, ,
, A B
,
. A
B 200 .
:
1.
.
2.
.
3.

,
.
4.
, ,
.

242

:

. ,
,
.

:
1) . ,
, .
var
P:Pointer;
begin
GetMem(P,100);
GetMem(P,200);
FreeMem(P,200);

{ 100 }
{ 200 }
{ 200 100
... }

end.

2) .
var
pw:PWorker;
begin
New(pw);
Dispose(pw);
Dispose(pw);
end.

{ TWorker}
{ - TWorker}
{ }

3) , .
var
PL:^LongInt;
L:LongInt;
begin
PL:=@L;
L:=100000;
writeln(PL^);
Dispose(PL);

{PL L}
{ 100000}
{ ,
}

end.

243



,
:
1. ( )
2. ()
3. ()
4.
5. ()
6.
7.
:
1) ,
..
,
, , ,
.
2) 20 .
3) .
4) .
5)
.
6)
.
7)
,
.

244


1
. ,
.
, .
2
. ,
.
, .
3
. B ,
,
. ,
.
4
. ,
,
. ,
.
5
. ,
,
, .
, .
6
. ,
.
, -.
7
. ,
.
, -.
245

8
. ,
.
, .
9
. ,
(min+max)/2, min
, max
. ,
.
10
. ,
(min+max)/3, min
, max
. ,
.
11
. ,
, . .
,
.
12
. ,
, .
. , ,
.
13
. ,
, ,
. . ,
,
.
246

14
. ,
, ,
2 . ,
. ,
, .
15
. ,
2 ,
, 1 .
, .
, ,
.
16
. ,
,
, .
, ,
.
17
. ,
,
, .
, , .
18
. ,
,
, .
, , .
247

19
. ,
. ,
, .
20
.
, .
,
(min+max)/2, min , max
. ,
, .

248


,
.
.
(, )
.
, .

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

Inf (
, ), Next
; First
.
,
,
. , ,
: ,
.
:
, .
:
Type U = ^Zveno;
Zveno = Record Inf : BT; Next: U End;

249

BT .
(
),
,
.
Nil, ,
. ,
.

.
:
;
;
,
(, , );
,
(, , );
, ;
;
.
.
,
. , .
, .
1.
{ ; x
}
Procedure V_Nachalo(Var First : U; X : BT);
Var Vsp : U;
Begin
New(Vsp);
Vsp^.Inf := X;

250

Vsp^.Next := First; { ,
, }
First := Vsp; {
}
End;

2.
{ ;
x }
Procedure Iz_Nachala(Var First : U; Var X : BT);
Var Vsp : U;
Begin
Vsp := First; {
}
First := First^.Next; { ,
, }
X := Vsp^.Inf; {
}
Dispose(Vsp); { }
End;

3. ,
( , )
{
,
Pred;
x
}
Procedure V_Spisok(Pred : U; X : BT);
Var Vsp : U;
Begin
New(Vsp); { }
Vsp^.Inf := X; { }
Vsp^.Next := Pred^.Next; {
,

Pred}

251

Pred^.Next := Vsp; {
Pred}
End;

4. ,
( , )
{
,
Pred;
x
}
Procedure Iz_Spiska(Pred : U; Var X : BT);
Var Vsp : U;
Begin
Vsp := Pred^.Next; {
}
{

,

}
Pred^.Next := Pred^.Next^.Next;
X := Vsp^.Inf; {
}
Dispose(Vsp); { }
End;

.
Unit Spisok;
Interface
Type BT = LongInt;
U = ^Zveno;
Zveno = Record Inf : BT; Next: U End;
Procedure V_Nachalo(Var First : U; X : BT);
Procedure Iz_Nachala(Var First : U; Var X : BT);
Procedure V_Spisok(Pred : U; X : BT);
Procedure Iz_Spiska(Pred : U; Var X : BT);

252

Procedure Ochistka(Var First: U);


Function Pust(First : U) : Boolean;
Procedure Print(First : U);
Implementation
Procedure V_Nachalo;
Var Vsp : U;
Begin
New(Vsp);
Vsp^.Inf := X;
Vsp^.Next := First;
First := Vsp;
End;
Procedure Iz_Nachala;
Var Vsp : U;
Begin
Vsp := First;
First := First^.Next;
X := Vsp^.Inf;
Dispose(Vsp);
End;
Procedure V_Spisok;
Var Vsp : U;
Begin
New(Vsp);
Vsp^.Inf := X;
Vsp^.Next := Pred^.Next;
Pred^.Next := Vsp;
End;
Procedure Iz_Spiska;
Var Vsp : U;
Begin

253

Vsp := Pred^.Next;
Pred^.Next := Pred^.Next^.Next;
X := Vsp^.Inf;
Dispose(Vsp);
End;
Procedure Ochistka;
Var Vsp : BT;
Begin
While Not Pust(First) Do Iz_Nachala(First,
Vsp)
End;
Function Pust;
Begin
Pust := First = Nil
End;
Procedure Print;
Var Vsp : U;
Begin
Vsp := First;
While Vsp <> Nil Do
Begin
Write(Vsp^.Inf : 6);
Vsp := Vsp^.Next
End; WriteLn
End;
Begin
End.

. ,
, ,
.

254


. .
Program Ex_sp_1;
Uses Spisok;
Var S1, S2, S3, V1, V2, V3 : U; A : BT; I, N : Byte;
Begin
Randomize;
N := 1 + Random(20);
S1 := Nil; A := -100 + Random(201);
V_Nachalo(S1, A); V1 := S1;
For I := 2 To N Do
Begin A := -100 + Random(201); V_Spisok(V1, A); V1 := V1^.Next End;
WriteLn(' : '); Print(S1);
V1 := s1; S2 := Nil; S3 := Nil;
While V1 <> Nil Do
Begin
If V1^.Inf > 0
Then If S2 = Nil
Then Begin V_Nachalo(S2, V1^.Inf); V2 := S2 End
Else Begin V_Spisok(V2, V1^.Inf); V2 := V2^.Next End;
If V1^.Inf < 0
Then If S3 = Nil
Then Begin V_Nachalo(s3, V1^.Inf); V3 := S3 End
Else Begin V_Spisok(V3, V1^.Inf); V3 := V3^.Next End;
V1:= V1^.Next
End;
WriteLn(' : '); Print(S2);
WriteLn(' : '); Print(S3);
Ochistka(S1); Ochistka(S2); Ochistka(S3);
End.

255


:
.
:
, , ,
.
,
.
.


4,5
150

30


10000


(/)

256


100000


2000 .

10

11

12

13

14





(/)

257


300
700


100



10


10
-12

Mersedes
2000

15



5
2000


{ ,
------------------------------------------------------------------------------| | ... | | |
------------------------------------------------------------------------------- .

< , ., - , 3 >
, .
.
}
program Lab7Var24;
uses Crt; { Crt}
const
lenTitle=20;

{lenTitle
' '}
lenAuthor=20; {lenAuthor ''}
lenPublisher=20; {lenPublisher
''}
maxN=20;
{maxN

}

type
TPbook = ^Tbook; {TPbook
Tbook}
Tbook = record {Tbook
}
{ :}
Title: string[lenTitle]; {Title ' '}

258

Author: string[lenAuthor]; {Author ''}


Publisher: string[lenPublisher]; {Publisher ''}
Number: integer;
{Number '- '}
next: TPbook;
{next . }
end;
var
First: TPbook; {First }
{ insertBook
b ,
First
b ,

}
procedure insertBook(b:Tbook);
var
P: TPbook; {P Ppred }
Ppred: TPbook; { }
{P }
{Ppred , }
newP: TPbook; {newP }
begin
New(newP); { newP }
newP^:=b; { newP }
P:=First; { }
Ppred:=nil; { ,}
{ }
while P<>nil do {P=nil }
{ }
begin
if b.author<=P^.author then P:=nil { }
else

259

{ }
begin
Ppred:=P;
P:=P^.next;
end;
end;
if Ppred=nil then { }
begin
newP^.next:=First;
First:=newP;
end
else
{ }
begin
newP^.next:=Ppred^.next;
Ppred^.next:=newP;
end;
end;

{ readBooks
n
.
''
}
procedure readBooks;
var
i: integer; {i - }
n: integer; {n - - }
b: Tbook;
{b - ()}
begin
repeat
write(' - ( 1 ',maxN-1,')');
readln(n);

260

until (n>=1) and (n<maxN);


clrscr;
{ }
writeln('-------------------------------------------',
'-----------------------------------');
writeln('| | ... |',
' | |');
writeln('-------------------------------------------',
'-----------------------------------');
{ n }
for i:=1 to n do
begin
{ }
write('|
|
|',
'
|
|');
gotoxy(2,3+i);
{ ''}
readln(b.Title); { ''}
{ }
gotoxy(3+lenTitle,3+i);
readln(b.Author);
{''}
gotoxy(4+lenTitle+lenAuthor,3+i);
readln(b.Publisher);
{''}
gotoxy(5+lenTitle+lenAuthor+lenPublisher,3+i);
readln(b.Number);
{'- '}
{ }
insertBook(b);
end;
{ , }
writeln('-------------------------------------------',
'-----------------------------------');
end;
{ readBook

261

Tbook


}
procedure readBook;
var
y: integer; {y }
b: Tbook; {b ()}
begin
{ }
writeln('-------------------------------------------',
'-----------------------------------');
writeln('| | ... |',
' | |');
writeln('-------------------------------------------',
'-----------------------------------');
write('|
|
|',
'
|
|');
{ , }
y:=wherey;
gotoxy(2,y); { ''}
readln(b.Title); { ''}
{ }
gotoxy(3+lenTitle,y);
readln(b.Author);
{''}
gotoxy(4+lenTitle+lenAuthor,y);
readln(b.Publisher); {''}
gotoxy(5+lenTitle+lenAuthor+lenPublisher,y);
readln(b.Number);
{'- '}
{
}
insertBook(b);
{ }
writeln('-------------------------------------------',
'-----------------------------------');

262

end;
{ writeBooks
books
n
(books n )

}
procedure writeBooks;
var
i: integer; {i - }
P: TPbook; { }
begin
{ }
writeln('-------------------------------------------',
'-----------------------------------');
writeln('| | ... |',
' | |');
writeln('-------------------------------------------',
'-----------------------------------');
{ }
P:=First; { }
while P<>nil do
begin
{ - }
writeln('|',P^.Title:lenTitle,'|',P^.Author:lenAuthor,'|',
P^.Publisher:lenPublisher,'|',P^.Number:13,'|');
P:=P^.next; { }
end;
{ }
writeln('-------------------------------------------',
'-----------------------------------');
end;
{ free
, ,

263

Ferst
}
procedure free;
var
P,P1: TPbook; { }
begin
P:=First; { }
while P<>nil do { }
begin
P1:=P^.next;
Dispose(P); { }
P:=P1; { }
end;
First:=nil; { First nil}
end;
{ }
begin
First:=nil; { }
{ }
clrscr;
readBooks; { }
writeln;
writeln(' :');
writeBooks; { books}
writeln;
writeln(' :');
readBook; { }
writeln;
writeln(' :');
writeBooks; { books}
readkey;
{, ,
}
free;
{ }
end.

264


,
Turbo Pascal , , .
,
.
Turbo Pascal,
.

,
.

265


1. , . . . : .
/ . . . . : , 2007. 335 .
2. , . .Turbo Pascal 7.0. : .
/ . . . . : , 2005. 575 .
3. : : .
/ . . . . . [ .] : , 2003. 333 .
4. , . . : . . . 1 :
/ . . ; . . . . .
3- ., . . . [ .] : , 2005. 712 .
5. , . . Turbo
Pascal : / . .
, . . . : , 2002. 44 .
6. , . . Turbo Pascal :
/ . . ,
. . . : , 2002. 44 .
7. , . . Turbo Pascal :
/ . . ,
. . . : , 2002. 44 .
8. , . . :
/ . . , . . .
: , 2001. 44 .
9. , . . Turbo
Pascal :
/ . . , . . . : , 2003.
52 .

266




TURBO PASCAL

. .
020640 22.10.97.
03.04.2011. 6084/16.
. . . 15,58. 100 . 478.

432027, . , . . , . 32.
, 432027, . , . . , . 32.