. .
«
»
2014 1
004.43 32.973-018 87
: . . . .
, . .- . ., , . . ., . . ., »
; «
-
. . : . , 2014. – 171 .
87 .–
:
/
. .
-
». , . «
»
-
, .
,
-
,
-
. ,
, . , . Borland Pascal for Windows, (Free Pascal, Pascal ABC). 004.43 32.973-018
© ©
. ., 2014 , 2014
2
-
....................................................................................... 4 .................................................................. 5 ........................................................................................... 5 ................................................................................................ 12 .......................................................... 14 .................................................................... 22 ............................................................................................... 31 ........................................................................................... 42 ................................................................... 44 ............................................................................................ 57 ......................................................................................... 64 ................................................................................ 80 ...................................................... 91 ............................................................. 98 ................................................................................ 99 .................................................................. 120 .......................................................................... 126 .................................... 145 .
..................................................... 164
3
( :
,
)
,
(
-
,
.
)
. . , . -
,
, . , ,
,
-
, .
,
-
–
-
. . ( , ,
,
,
,
,
) .
,
,
. (
,
,
,
,
,
) -
.
. . 4
-
–
,
,
-
. . (
),
-
:
,
. .
(
255)
,
–
.
–
, -
(
),
. , , .
,
-
,
, , . ,
. :
1)
(
2)
, (
TRUE
,
255
), :
FALSE),
5
3)
(
ASCII),
4)
,
5)
. ,
, (REAL). ,
–
. :
1.
Var :
Var
r : Set
Of
‘a ’. . ’c’;
d : Set
Of
1 . . 4;
r
‘a’
‘c’
d
1, 2, 3, 4. ! ! 2.
Type –
Type
Var :
TSymb = Set TNumb = Set
Var
Of Of
‘a’ . . ‘c’; 1 . . 4;
r : TSymb; d : TNumb;
3. Const
: r : Set
Of
‘a ’. . ’c’ = [‘a’,’c’];
d : Set
Of
1 . . 4 = [2,1,3];
! . : a)
:
[‘c’, ‘a’, ‘e’]
b)
[76, 102, 5, 12]
: 6
[25..45, 3..10]
[‘a’..’d’, ‘k’..’n’]
c)
:
[Ord(109),’s’]
[Succ(3), Pred(9), Round(Sin(1.0))]
, .
,
r
-
: [‘a’, ‘b’, ‘c’]
[‘a’]
[‘a’, ‘b’]
[ ‘b’]
[‘a’, ‘c’]
[ ‘c’]
[ ‘b’, ‘c’]
[]
d –
:
[1, 2, 3, 4]
[1, 3]
[2]
[1, 2, 3]
[1, 4]
[3]
[1, 3, 4]
[2, 3]
[4]
[1, 2, 4]
[2, 4]
[]
[2, 3, 4]
[3, 4]
[1, 2]
[1]
2n
, n–
,
. , ,
[‘a’, ‘b’, ‘c’]
,
[‘b’, ‘c’,
.
‘a’]
: +
(
*
(
-
)
, )
,
, .
In
, ,
:
7
[3, 4, 5] * [1, 3, 5] = [3] [3, 4, 5] * [3, 4, 5] = [3, 4, 5] [2, 1, 0] * [] = [] [2, 9, 8] * [6, 7] = [] [‘a’, ‘b’, ‘c’] * [‘d’, ‘c’, ‘a’] = [‘a’, ‘c’]
, ,
:
[3, 4, 5] + [1, 3, 5] = [1, 3, 4, 5] [2, 9, 8] + [] = [2, 9, 8] [1, 2, 3] + [1, 2, 3] = [1, 2, 3] [‘a’, ‘b’, ‘c’] + [‘d’, ‘c’, ‘a’] = [‘a’, ’b’,
‘c’, ‘d’]
,
:
[3, 4, 5] – [1, 3, 5] = [4] [2, 9, 5] – [3, 7] = [2, 9, 5] [2, 4] – [5, 4, 2] = []
,
-
,
,
–
. :
([3, 4, 5]
:
+
12
[1, 3, 6, 7]) * [5, 6, 7] – [6] = [5, 7]
.
:
1)
sport = [1, 2, 3, 4, 5]
2)
otl = [2, 3, 6, 7]
3)
smok = [7, 8] :
1)
: sport * otl = [1, 2, 3, 4, 5] * [2, 3, 6, 7] = [2, 3]
2)
:
sport + otl = [1, 2, 3, 4, 5] + [2, 3, 6, 7] = [1, 2, 3, 4, 5, 6, 7] = [1 . . 7]
8
3)
:
sport * smok = [1, 2, 3, 4, 5] * [7, 8] = [] –
– !
4)
: otl – smok = [2, 3, 6, 7] – [7, 8] = [2, 3, 6]
In ,
,
TRUE,
FALSE,
-
: 5 ‘b’
In
[1, 5, 7] = TRUE In
[‘a’, ‘c’, ‘d’] = FALSE
, A
.
B –
,
A = [‘d’, ‘g’] = True
A = B
:
TRUE, [1, 2, 3] = [3, 2, 1] = TRUE
A B
TRUE, : [1, 2, 3] [5, 2, 1] = TRUE
, +
*,
-,
: V := S;
V–
,
S–
. 9
! Read
Write, .
1. : Const mnosh : Set Of 1..10 = [2, 3, 8];
2, 3, 8. : 2, 3, 8
mnosh := [2, 3, 8]; mnosh := [];
2, 3, 4, 5
mnosh := [2..5];
2. : mnosh := [1..4];
1, 2, 3, 4
mnosh := mnosh + [7, 9];
1, 2, 3, 4, 7, 9
mnosh := mnosh - [2, 3];
1, 4, 7, 9
3. ,
mnosh 0: Repeat Write(‘
:’);
ReadLn(n); If (n 0) Then
Mnosh := mnosh + [n];
Until (n = 0);
10
4. mnosh Var
mnosh : Set
Of
1 . . 100;
. : For i := 1 To 100 Do
,
If (I In mnosh)
,
Then
Write(i:5);
WriteLn;
: 1. Include(mnosh, s); mnosh
–
s
–
, (
,
)
,
,
)
,
. 2. Exclude(mnosh, s); mnosh
–
s
–
, ( .
11
,
-
. . .
,
,
,
,
,
-
.
,
,
.
,
,
.
, . ,
.
-
, , ,
-
,
.
,
.
. –
,
-
,
,
. –
(
)
( 12
,
,
,
-
):
,
,
– . ,
–
(
),
,
-
. :
-
, ,
.
,
, (
(
)
).
.
,
-
,
,
,
.
,
(
)
,
,
,
,
.
,
,
. : ,
,
,
,
-
.
.
.
, .
13
,
,
. : 1)
,
2)
,
3)
.
:
, ,
, ,
,
.
. ( ),
(
-
). :
1) Var
Var : f_int : File Of
f_int,
Integer;
f_int –
!
–
. : ...
EOF – End Of File – Var
f_char : File Of
:
EOF
26 (CTRL+Z). f_char,
Char;
f_char –
14
–
: ...
EOF
2)
–
TYPE Var :
Type TFile_int : File Var
Of
Integer;
f_int : TFile_Int;
,
,
,
: – , ,
-
,
,
,
EOF), , ,
,
-
,
-
. (
)
-
: Assign(
, ‘
’);
Assign(f_int,’D:\User\f_int.dat’);
f_int
–
(
),
–
D:\User\f_int.dat
: D:
15
(
) User
f_int_dat. ! ; .dat.
: )
:
ReWrite(f_int);
.
,
.
(
).
-
– 0. !
!
) : ReSet(f_int);
,
, –
0.
Write(
,
);
, f_int
Write(f_int, n);
!
n .
WriteLn n
,
. .
, -
16
. EOF –
, . -
,
,
: f_int
ReSet(f_int);
f_int
While Not(EOF(f_int) Do Begin
-
Read(f_int,n);
n Write(n:5); End;
–
WriteLn;
!
-
ReadLn . , –
While. EOF.
: Close(
);
, Close(f_int);
, .
–
0. .
: 3 : -1
17
: 5 : 12 : -6 : 0 : 3
-1
5
12
-6
Program
File_1;
Uses CRT; Var
n : Integer; f_int : File
Of
Integer;
Begin ClrScr; Assign(f_int, ‘d:\User\f_int.dat’); ReWrite(f_int); Repeat Write(‘
:’);
ReadLn(n); If (n 0) Then Write(f_int,n); Until (n=0); ReSet(f_int); While Not(EOF(f_int)) Do Begin Read(f_int, n); Write(n:5); End; WriteLn; Close(f_int); ReadLn; End.
18
: n– f_int -
f_int
n
n 0
n f_int
f_int
Not(EOF(f_int)
n f_int
n
f_int
19
: )–
FileSize(
– ,
) –
FilePos(
– (
-
, ), : Seek(
)–
,
(
-
), )–
Truncate(
: , EOF,
Erase(
)
–
– .
:
,
, ,
-
, (
),
, :
Program
File_1;
Uses CRT; Var
n : Integer; f_int : File
Of
Integer;
Begin ClrScr;
20
Assign(f_int, ‘d:\User\f_int.dat’); ReSet(f_int);
:
While Not(EOF(f_int)) Do Begin
n
Read(f_int, n);
n
Write(n:5); End; WriteLn; n := FileSize(f_int);
n–
–
WriteLn(‘
’, n);
ReSet(f_int);
n–
n := FilePos(f_int); WriteLn(‘
’, n);
3
Seek(f_int, 3);
n
Read(f_int, n); WriteLn(‘
3
’, n);
Truncate(f_int);
–
ReSet(f_int);
While Not(EOF(f_int)) Do Begin Read(f_int, n); Write(n:5); End; WriteLn; Close(f_int); ReadLn; End.
: 3
-1
5
12 5
-6 0
3
-1
5
3 12
12
21
,
EOLn – End
Of Line –
(
-
#13#10).
EOF – End Of File. . . : 1)
:
Var
f_text,
f_text : Text;
f_text –
–
!
.
2)
–
TYPE :
Type TFile_text = Text; Var
f_text : TFile_text;
(
)
-
: Assign(
, ‘
’);
Assign(f_text,’D:\User\f_text.txt’);
f_text
–
(
D:\User\f_text.txt
– :
User, f_text.txt
22
),
D:,
(
)
! ; .txt.
: )
:
ReWrite(f_text);
.
,
;
)
:
ReSet(f_text);
, ,
;
)
:
Append(f_text);
, ,
. :
Write(
,
);
WriteLn(
,
);
(
). ,
,
.
.
:
1)
,
2) 3)
, ,
4) 5)
, . 23
-
.
:
.
-09
: : : 4 .
: 4.12
Program
File_2;
Uses CRT; Var
ocenka : 2..5; sr_ball : Real; name : String[15]; nomer : String[10]; f_text : Text;
Begin ClrScr; Assign(f_text, ‘d:\User\f_text.txt’); ReWrite(f_text); Write(‘
:’);
ReadLn(nomer); WriteLn(f_text, nomer); Write(‘
:’);
ReadLn(name); WriteLn(f_text, name); Write(‘
:’);
ReadLn(name); WriteLn(f_text, name); Write(‘
:’);
ReadLn(ocenka);
24
WriteLn(f_text, ocenka:3);
3 Write(‘
.
:’);
ReadLn(sr_ball); WriteLn(f_text, sr_ball:6:2);
6 Close(f_text); ReadLn; End.
: -09
4 4.12
!
-
. : Read(
,
ReadLn(
); ,
);
( !
,
). , .
: .
25
Program
File_3;
Uses CRT; Var
ocenka : 2..5; sr_ball : Real; name : String[15]; nomer : String[10]; f_text : Text;
Begin ClrScr; Assign(f_text, ‘d:\User\f_text.txt’); ReSet(f_text); ReadLn(f_text, nomer); WriteLn(nomer); ReadLn(f_text, name); WriteLn(name); ReadLn(f_text, name); WriteLn(name); ReadLn(f_text, ocenka); WriteLn( ocenka:3); ReadLn(f_text, sr_ball); WriteLn( sr_ball:6:2); Close(f_text); ReadLn; End.
: -09
4 4.12
26
!
,
-
(FileSize, FilePos, Seek, Trun.
cate), ,
-
. :
baza.txt, :
1)
,
2)
,
3)
,
4)
,
5)
. ,
. , –
-
(
).
:
-09
: : : 4 .
: 4.12 :
-09
: : : 5 .
: 4.27 :
-09
: : 27
: 3 .
: 3.42 :
Program
File_4;
Uses CRT; Var
ocenka : 2..5; sr_ball : Real; name : String[15]; nomer : String[10]; baza : Text;
Begin ClrScr; Assign(baza, ‘d:\User\baza.txt’); ReWrite(baza); Repeat Write(‘
:’);
ReadLn(nomer); If (Length(nomer) = 0)
(
-
Enter), Then Break;
,
Write(baza, nomer:10);
10
( nomer)
Write(‘
:’);
ReadLn(name); Write(baza, name:15);
15 Write(‘
:’);
28
-
ReadLn(name); Write(baza, name:15);
15 Write(‘
:’);
ReadLn(ocenka); Write(baza, ocenka:3);
3 Write(‘
.
:’);
ReadLn(sr_ball);
6
WriteLn(baza, sr_ball:6:2);
-
Until (False) Close(baza); ReadLn; End.
: -09
4
4.12
-09
5
4.27
-09
3
3.42
10
15
15
3
. 6
!
.
, . . :
1)
– 10
,
2)
– 15
,
3)
–3
,
4)
–6
.
29
, ,
:
1)
: 10
String[10], 2)
(
,
,
,
), 3)
Read (ReadLn) . : .
Program
File_3;
Uses CRT; Var
ocenka : 2..5; sr_ball : Real; fam,
name : String[15];
nomer : String[10]; baza : Text; Begin ClrScr; Assign(baza, ‘d:\User\baza.txt’); ReSet(baza); While NOT(EOF(baza)) Do Begin ReadLn(baza, nomer, fam, name, ocenka, sr_ball);
WriteLn(nomer:10, fam:15, name:15, ocenka:3, sr_ball:6:2 );
End; Close(baza);
30
-
ReadLn; End.
: -09
4
4.12
-09
5
4.27
-09
3
3.42
, ,
.
,
,
,
,
-
. .
,
-
.
, .
,
–
,
. ,
. –
-
. . ,
,
,
.
,
: ,
,
.
-
,
,
,
.
, ,
. 31
, ,
( ,
,
-
,
)
, ,
, .
–
:
,
–
,
–
,
: ,
–
–
,
-
,
–
–
-
.
-
.
.
, ,
,
.
. ,
. , ,
– ,
.
. -
,
. :
Type TStudent =
Record
nomer, fam, name : String[15]; ocenka : 2..5; sr_ball : Real; End;
!
End, Begin. 32
:
TStudent, nomer, fam, name –
,
–
15
, ocenka
–
sr_ball
–
–
2 –
5, .
, .
End.
: Var
stud_1 : TStudent; gruppa : Array [1..15]
Of
baza_gruppa : File
TStudent;
Of
TStudent;
stud_1
, ,
TStudent. .
,
, stud_1.nomer := ‘
:
-09’;
stud_1.fam := ‘
’;
stud_1.name := ‘
’;
stud_1.ocenka := 4; stud_1.sr_ball := 4.12;
, .
, . , :
.nomer := ‘ .fam := ‘
-09’;
!
’;
! 33
,
– :
With
x
Do ;
With –
c,
–
x
.
: With stud_1 Do Begin nomer := ‘
-09’;
fam := ‘
’;
name := ‘
’;
ocenka := 4; sr_ball := 4.12; End;
! , ReadLn
, WriteLn
: ReadLn(stud_1);
!
WriteLn(stud_1);
!
ReadLn(stud_1.ocenka);
!
WriteLn(stud_1.ocenka);
! ,
,
gruppa. –
,
.
-
,
:
34
gruppa[5].nomer := ‘
-09’;
gruppa[5].fam := ‘
’;
gruppa[5].name := ‘
’;
gruppa[5].ocenka := 4; gruppa[5].sr_ball := 4.12;
: With gruppa[5] Do Begin nomer := ‘
-09’;
fam := ‘ name := ‘
’; ’;
ocenka := 4; sr_ball := 4.12; End;
(
)
,
–
-
–
, ,
. -
,
:
ReadLn(stud_1.nomer); ReadLn(stud_1.fam); ReadLn(stud_1.name); ReadLn(stud_1.ocenka); ReadLn(stud_1.sr_ball);
: Write(baza_gruppa, stud_1);
: Read(baza_gruppa, stud_1);
,
,
:
With stud_1 Do WriteLn(nomer:10, fam:15, name:15, ocenka:3, sr_ball:6:2);
35
.
(
,
,
,
,
,
:
),
-
. (
), :
; : , , ; : , , ; : , , , , , . ( Enter
). ,
.
:
–
-09
:
36
,
-
: : : 1993 : 2 : 23 : : : 123 : 2 : 45 : 235347 …
: -09
Program Baza_student; Uses WinCrt; Type TKadr = Record gruppa : String[10]; fio : Record fam, name : String[10]; otch
: String[15];
End; adres: Record gorod, ulica : String[20]; korp, dom, kv: String[5]; telefon
: String[10];
End; data : Record den : 1..31; mes : 1..12; 37
god : 1900..2000; End; End; Var student : TKadr; baza
: File Of TKadr;
Begin ClrScr; Assign(baza, 'D:\User\baza_student.dat'); ReWrite(baza); Repeat With student Do Begin Write('
: ');
ReadLn(gruppa);
,
If (Length(gruppa)=0) Then Break; Write('
: ');
ReadLn(fio.fam); Write('
: ');
ReadLn(fio.name); Write('
: ');
ReadLn(fio.otch); Write('
: ');
ReadLn(data.den); Write('
: ');
ReadLn(data.mes); Write('
: ');
ReadLn(data.den); Write('
: ');
ReadLn(adres.gorod); Write('
: ');
ReadLn(adres.ulica); Write('
: ');
38
ReadLn(adres.dom); Write('
: ');
ReadLn(adres.korp); Write('
: ');
ReadLn(adres.kv); Write('
: ');
ReadLn(adres.telefon); End; Write(baza, student); Until (False); ReSet(baza); While Not(EOF(baza)) Do Begin Read(baza, student); With student Do
WriteLn(gruppa:10, fio.fam:10, fio.name:10, fio.otch:15); End; Close(baza); ReadLn; End.
(
). ,
,
-
,
-
,
.
,
(
). : .
, ,
,
. 39
,
-
. .
-
: Case
:
-1: (
Of -1);
-2: (
-2);
-3: (
-3);
…………………… -n:(
-n);
End;
Type. ,
-
(x, y)
(r,
.
)
-
:
coord Type TDecpol = (decart, polar); TCoord = Record Case wid : TDecpol Of
decart : (x, y : Real); polar : (r : Real; fi : 0..360); End; End; Var coord : TCoord;
coord.wid ,
– decart decart
coord
: coord.x , coord.y
Real,
polar – coord.r coord.fi
:
Real, 0..360. 40
polar. -
: ,
-
, (
),
:
: ();
, , , ,
.
41
-
-
. ,
, , . : (
), , . -
«
» ,
:
-
, .
,
,
.
–
-
.
-
.
. . . :
-
,
( 42
)–
(
-
),
. ,
-
,
.
: , – . : –
. ,
,
,
. , ,
-
.
,
-
, , .
,
,
. ,
(
,
) (
, .
) -
–
, ,
-
. ,
,
-
,
. 43
,
-
,
-
.
,
-
. : 1)
,
2)
.
, ,
,
.
,
,
-
,
-
. Var
.
Begin
-
, .
(
),
-
. , : Function Geron(x, y, z : Real):Real; Var p : Real; Begin p := (x + y + z)/2.0; Geron := Sqrt(p*(p – x)*(p – y)*(p – z));
End; 44
,
!
.
End
– .
Function Geron(x, y, z : Real):Real;
,
Function,
-
Geron. –
.
x, y, z
Real.
,
,
-
. ,
,
.
Real.
,
, . ,
-
: s := Geron(3.0, 4.0, 5.0);
x, y, z
Geron 3.0, 4.0, 5.0,
. Geron
s.
s = 6.0. ,
Geron,
: Program Primer; Uses CRT;
45
-
Var
(
a, b, c, s : Real;
)
Function Geron(x, y, z : Real):Real; Var p : Real; Begin p := (x + y + z)/2.0; Geron := Sqrt(p*(p – x)*(p – y)*(p – z)); End;
(
Begin
)
ClrScr; a := 3.0; b := 4.0; c := 5.0;
-
s := Geron(a, b, c);
a, b, c WriteLn(‘
’, s:6:2);
ReadLn; End.
, ,
-
,
-
.
-
, ). ,
– ),
; -
.
, ),
–
.
46
(
-
,
,
-
: Label Const Type Var Function, Procedure Begin … End; : 1.
, ,
(
-
)
. 2.
-
3.
-
.
,
.
4.
, ,
.
5. Function
: Err(x, y: Word; a, b, y: Real): Real;
!
6. , ;
:
Function Summa(x, y: Real): Real; Var s : Real; Begin
!
s := x + y; End;
47
7.
,
-
.
-
Var ,
.
:
Function Fact(n: Word): Word;
n–
Var i, f : Word;
i, f –
Begin f:=1; For i:=1 To n Do f := f * i; Fact := f; End;
8.
(
For), ,
( 9.
(
).
)
,
.
( ,
)
-
.
-
,
,
. ,
:
Program Global; Uses CRT;
–
Var a, b, c, s : Integer;
Function Sum(x, y : Integer) : Integer; Begin c := c + 1; Sum := x + y + c; End;
48
Begin a := 1; b := 1; c := 1; s := c + Sum(a, b); WriteLn(‘s=’, s); ReadLn; End.
s = 5. ,
.
-
s :
s := c + Sum(a, b);
-
s = 5. : s := Sum(a, b) + c;
,
,
s = 6 –
c
-
= 2, .
(
).
-
: , , ,
-
, ,
-
: 49
64
. -
: CS –
,
DS –
,
SS –
.
: –
-
,
,
, , –
-
,
,
,
, ,
, -
Const, 50
,
, , :
Function Func(k: Word) : Word;
n –
Const n: Word = 0; Var i: Word; Begin
i
WriteLn(‘i=’,i); WriteLn(‘n=’,n); n:= n + k; Func:=n; End;
.
i n,
CONST,
,
-
k. –
: -
, –
,
,
.
10.
-
;
. 11.
,
,
,
,
,
, ;
(
,
),
,
,
-
.
12.
,
,
, 51
,
,
,
,
-
,
.
13.
(
)
-
,
;
-
.
,
.
:
Program Primer; Uses CRT; Var a, b, c : Integer;
x, y –
Function Sum(x, y : Integer) : Integer; Begin x := x + 1; y := y + 1;
Sum := x + y; End;
Begin a := 1; b := 1; c := Sum(a, b); WriteLn(‘a=’, a, ‘
b=’, b);
ReadLn; End.
x = 1, y = 1. ,
.
52
14.
;
, .
Var:
Program Primer; Uses CRT; Var a, b, c : Integer;
Function Sum(Var x, y : Integer) : Integer;
x, y –
-
Begin x := x + 1; y := y + 1;
Sum := x + y; End;
Begin a := 1; b := 1; c := Sum(a, b); WriteLn(‘a=’, a, ‘
b=’, b);
ReadLn; End.
x = 1, y = 1. x = 2, y = 2.
,
-
,
, . 53
: Program Primer; Uses CRT; Var a, b : Integer; Function Nemo(Var x : Integer; y : Integer) : Integer;
x–
,
y– Begin x := x + y; a := y; Nemo := x; End; Begin a := 1; b := 2; WriteLn(‘Nemo=’, Nemo(a, b),‘
a=’, a, ‘
b=’, b);
ReadLn; End.
: Nemo=2
a=2
b=2
,
a a
,
x 3
Nemo ,
2.
. 15.
(
Function
):
Func(x : 1..10; r : array [1..20] Of Real) : Real;
x
,
r
. :
54
Type TCount = 20; TVector
= Array [1 .. TCount] Of Real;
TInterval = 1 .. 10;
: Function
Func(x : TInterval; r : TVector) : Real;
16. , -
, –
-
. : Type, , , ,
{$F+} –
-
. :
, . :
Program Param_func; Uses CRT; Type TFun
-
= Function (x, y: Integer): Integer;
TFunc –
-
Var a, b, c: Integer;
–
{$F+}
-
55
Function Add(x, y: Integer): Integer;
Begin Add := x + y; End;
Function Mult(x, y: Integer): Integer;
Begin Mult := x * y; End;
Function Funny(x, y: Integer): Integer; Begin Funny := (x + y) * (x - y); End; {$F-}
,
operation
Function Par_func(m, n : Integer; operation : TFunc): Integer; Begin Par_func := operation(m, n); End;
Begin ClrScr; a := 5; b := 3;
-
c := Par_func(a, b, Add);
! WriteLn('c=', c); c := Par_func(a, b, Mult); WriteLn('c=', c); c := Par_func(a, b, Funny); 56
WriteLn('c=', c); ReadLn; End.
: =8 =15 =16
17.
–
-
,
.
.
,
1890
.
.
. –
,
-
.
, ,
-
. , n! = 1·2·3·…·(n-1)·n .
1
,
, 0! = 1.
-
n = 0,
n! = (n – 1)!·n
n > 0. 1, 1, 2, 3, ,
5, 8, 13… .
57
n : 1
n = 1,
Fn=
1 Fn-2
n = 2,
+
Fn-1
n > 2. , (
),
-
.
,
-
.
.
,
-
. ,
-
. , .
-
, . .
, .
-
. 0!, 1!, 2!, 3!,…,
ai = i!, i = 1, 2, 3,… 58
-
: ai = ai1·i,
-
a0 = 1, i = 1, 2, 3… , .
-
a0
.
i = n, n!
i = n : Fi = Fi-2 + Fi-1, F1 = 1, F2 = 1, i = 3, 4, 5,… ,
, ,
.
-
, :
1)
, ,–
-
, ,
,
,
2)
, ,
,
3)
, ,
-
,
.
,
-
(
). –
,
LIFO (Last in – first out: –
).
,
-
59
. , :
, .
–
– 65520
.
-
. (
)
,
.
-
Error 202: Stack overflow error. ,
n,
: Function Factorial(n: Word): Word; Begin If (n=0) Then Factorial := 1 Else Factorial := n * Factorial(n – 1); End;
:
n = 5 Factorial :=
5 * Factorial(4) 5 * 4 * Factorial(3) 5 * 4 * 3 * Factorial(2) 5 * 4 * 3 * 2 * Factorial(1) 5 * 4 * 3 * 2 * 1 = 120
: Factorial(5)
,
Factorial
: Factorial(4), Factorial(3), …
,
–
-
.
, .
60
Factorial := 1
n = 0.
, : n*Factorial(n-1). ,
-
: 1*1, 2*1, 3*2*1, 4*3*2*1, 5*4*3*2*1 – . ,
,
n
-
: Function Fibo(n: Word): Word; Begin If (n=1) Or (n=2) Then Fibo := 1 Else Fibo := Fibo(n-2) + Fibo(n-1); End;
– .
,
Func_1
– Func_2,
–
Func_2, ,
Func_1.
-
? .
Forward.
-
Forward, : Program Primer; Uses CRT; Var i: Integer;
61
-
Function Func_1(x: Integer): Integer; Forward;
Func_1 Function Func_2(y: Integer): Integer;
Func_2
Var k: Integer; Begin ………
Func_1
k := Func_1(y); ……… End;
Function Func_1; Var
Func_1
n: Integer;
Begin ……….
Func_2
n := Func_2(x); ………. End;
Begin ……..
Func_1
i := Func_1(i); …….. End.
1.
,
Cm n C 0n
=
C1n
0 max) Then Begin max:=vector[i]; i_max:=i; End;
End; End;
Begin ClrScr; Assign(out_min_max,’d:\User\out_min_max.txt’); ReWrite(out_min_max); Init(m_min, m_max, a); Writeln(out_min_max, ‘
:’);
For i:=1 To n Do Write(out_min_max, a[i]:5); WriteLn(out_min_max); Min_max(n, a, min, max, i_min, i_max); WriteLn(out_min_max, ‘min=’, min); WriteLn(out_min_max, ‘i_min=’, i_min); WriteLn(out_min_max, ‘max=’, max); WriteLn(out_min_max, ‘i_max=’, i_max); Close(out_min_max); ReadLn; End.
: : -1
-19
-19
-35
50
26
-47
min=-47 i_min=7
70
17
1
-7
max=50 i_max=5
2.
: , .
,
,
. out_sort.txt: Program Primer_2; Uses CRT; Const
n = 10; m_min = -50; m_max = 50;
Type TVector = Array [1..n] Of Integer; Var
i: Word; a : TVector; out_sort: Text;
Procedure Init(m: Word; elem_min, elem_max: Integer; Var vector: TVector); Var i: Word; Begin Randomize; For i:=1 To m Do
elem_min
elem_max
vector[i]:=elem_max - Random(elem_max - elem_min +1); End;
Procedure Sort_vybor(m: Word; Var vector: TVector); Var
i, j, k: Word; temp: Integer; 71
Begin For i := 1 To m-1 Do Begin k := i; temp := vector[i]; For j := i + 1 To m Do If
(vector[j] < temp) Then Begin temp := vector[j]; k:= j; End;
vector[k] := vector[i]; vector[i] := temp; End; End;
Begin ClrScr; Assign(out_sort,’d:\User\out_sort.txt’); ReWrite(out_sort); Init(n, m_min, m_max, a); Writeln(out_sort, ‘
:’);
For i:=1 To n Do Write(out_sort, a[i]:5); WriteLn(out_sort); Sort_vybor(n, a); Writeln(out_sort, ‘
:’);
For i:=1 To n Do Write(out_sort, a[i]:5); WriteLn(out_sort); Close(out_sort); ReadLn; End.
72
: : -15
-31
12
-10
50
-27
36
-29
2
5
-10
2
5
12
36
50
: -31
-29
-27
-15
3.
( ): . , .
–
,
.
-
n – 1 out_sort.txt:
Program Primer_3; Uses CRT; Const
n = 10; m_min = -100; m_max = 100;
Type TVector = Array [1..n] Of Integer; Var
i: Word; a : TVector; out_sort: Text;
Procedure Init(m: Word; elem_min, elem_max: Integer; Var vector: TVector); Var i: Word; Begin
73
.
Randomize; For i:=1 To m Do
elem_min
elem_max
vector[i]:=elem_max - Random(elem_max - elem_min +1); End;
Procedure Sort_obmen(m: Word; Var vector: TVector); Var
i, j, k: Word; temp: Integer;
Begin For i := m DownTo 2 Do For j := 1 To i – 1 Do If
(vector[j] > vector[j+1]) Then
Begin temp := vector[j]; vector[j] := vector[j+1]; vector[j+1]:= temp; End; End;
Begin ClrScr; Assign(out_sort,’d:\User\out_sort.txt’); ReWrite(out_sort); Init(n, m_min, m_max, a); Writeln(out_sort, ‘
:’);
For i:=1 To n Do Write(out_sort, a[i]:5); WriteLn(out_sort); Sort_obmen(n, a); Writeln(out_sort, ‘
:’);
74
For i:=1 To n Do Write(out_sort, a[i]:5); WriteLn(out_sort); Close(out_sort); ReadLn; End.
: : 47
-14
58
-69
9
-19
-72
24
97
-32
-14
9
24
47
58
97
: -72
-69
-32
-19
4.
: a1, a2, a3,…,ak ak+1. out_sort.txt:
Program Primer_4; Uses CRT; Const
n = 10; m_min = -150; m_max = 150;
Type TVector = Array [1..n] Of Integer; Var
i: Word; a : TVector; out_sort: Text;
Procedure Init(m: Word; elem_min, elem_max: Integer; Var vector: TVector); Var i: Word; Begin Randomize;
75
-
For i:=1 To m Do
elem_min
elem_max
vector[i]:=elem_max - Random(elem_max - elem_min +1); End;
Procedure Sort_vstavka(m: Word; Var vector: TVector); Var
i, j, k: Word; temp: Integer;
Begin For i := 2 To m Do Begin temp := vector[i]; j := i – 1; k := 1; While (j > 0) Do If (vector[i] > vector[j]) Then Begin k := j + 1; j := 0; End Else j := j – 1; For j := i DownTo k + 1 Do vector[j] := vector[j - 1]; vector[k] := temp; End; End;
Begin ClrScr; Assign(out_sort,’d:\User\out_sort.txt’); ReWrite(out_sort); Init(n, m_min, m_max, a); Writeln(out_sort, ‘
:’);
76
For i:=1 To n Do Write(out_sort, a[i]:5); WriteLn(out_sort); Sort_vstavka(n, a); Writeln(out_sort, ‘
:’);
For i:=1 To n Do Write(out_sort, a[i]:5); WriteLn(out_sort); Close(out_sort); ReadLn; End.
: : 115
-45
20
-39
91
75
44
138
-72
-63
20
44
75
91
115
138
: -72
-63
-45
-39
5. .
:
aj,
ai
i = 1, j = n.
ai < aj ,
, ,
.
ai > aj ,
-
, . , : i = j.
ai
: ,
–
.
.
out_sort.txt: Program Primer_6; Uses CRT;
77
-
-
Const
n = 10; m_min = -100; m_max = 100;
Type TVector = Array [1..n] Of Integer; Var
i: Word; a : TVector; out_sort: Text;
Procedure Init(m: Word; elem_min, elem_max: Integer; Var vector: TVector); Var i: Word; Begin Randomize; For i:=1 To m Do
elem_min
elem_max
vector[i]:=elem_max - Random(elem_max - elem_min +1); End; Procedure Sort_Hoar(m, bottom, top: Word; Var vector: TVector); Var
i, j: Word; Str: Boolean; temp: Integer;
Begin i := bottom; j := top; str := False; While (i < j) Do Begin If (vector[i] > vector[j]) Then Begin temp := vector[i]; vector[i] := vector[j]; vector[j] := temp; str := Not(str); End;
{If}
78
If (str) Then i := i + 1 Else j := j – 1; End;
{While}
If (i > 1) And ((i – 1) > bottom) Then Sort_Hoar(m, bottom, i – 1, vector); If (j < (m – 1)) And ((j + 1) < top) Then Sort_Hoar(m, j + 1, top, vector); End; Begin ClrScr; Assign(out_sort,’d:\User\out_sort.txt’); ReWrite(out_sort); Init(n, m_min, m_max, a); Writeln(out_sort, ‘
:’);
For i:=1 To n Do Write(out_sort, a[i]:5); WriteLn(out_sort); Sort_Hoar(n, 1, n, a); Writeln(out_sort, ‘
:’);
For i:=1 To n Do Write(out_sort, a[i]:5); WriteLn(out_sort); Close(out_sort); ReadLn; End.
: : -62
18
-48
46
-44
-58
-95
76
4
-65
-44
4
18
46
76
: -95
-65
-62
-58
-48
.
-
.
79
,
. ?
,
,
,
,
,
.
-
,
,
-
,
. -
–
. -
,
, )
Turbo Pascal (Borland
Pascal). ,
, ,
, . –
,
. ,
(
)
.
-
executable, – .exe.
-
.pas.
-
–
, . Turbo Pascal
. 80
Borland Pascal
,
-
.exe .
: 1)
,
2)
(
). -
,
,
-
, Turbo.tpl
-
Turbo Pascal).
,
,
-
.
Turbo Pascal
.
Turbo Pascal.
-
. –
.
-
(compiler).
-
–
, ,
. -
(
, linker).
–
,
. :
, , -
.
-
81
, . Turbo Pascal
-
.
-
.
,
,
,
.
,
,
-
,
.
,
, (
.
) ,
-
. –
.
: 1) ,
,
2) , . ,
,
. :
1)
, ,
2)
, ,
82
3)
,
4)
,
, . –
-
, 5)
Turbo Pascal , (
64 ). .
: SYSTEM, CRT, GRAPH, DOS, PRINTER, OVERLAY –
Turbo.tpl
(Turbo Pascal Library).
-
SYSTEM ,
Uses: Uses CRT, GRAPH; .
–
-
,
-
: 1)
,
2)
,
3)
,
4)
. , . , 83
-
, .
,
, -
.pas.
.tpu (Turbo Pascal Unit). :
Unit Unit Abc; ,
Uses
-
. ,
. .
-
, –
,
,
,
:
Interface ,
Uses Const Type Var Function ,
Procedure
, . , ,
,
-
,
,
: Implementation
84
,
Uses ) Const Type Var Function Procedure
. ,
,
:
Begin ……. End. .
, .
End
: 1)
.
Begin
My_modul,
:
Geron ,
2)
-
Swap ,
3)
f.txt Geron.
Unit My_modul; Interface Var
f: Text; 85
– a, b, c –
}
Function Geron(a, b, c: Real): Real;
x
y -
}
Procedure Swap(Var x, y : Real);
Implementation Function Geron; Var p: Real; Begin p := (a + b + c) / 2.0;
}
Geron := Sqrt(p * (p –a) * (p – b) * (p – c)); End;
Procedure Swap; Var temp: Real; Begin temp := x; x := y; y := temp; End;
Begin Assign(f, ‘D:\User\f.txt’); End.
My_modul.pas.
Turbo Pascal
,
.
86
tpu (Turbo Pascal Unit), ,
.
exe
,
Compile, : Compile Make Build Destination (Memory, Disk) Primary File… –
:
Compile –
,
, (
).
-
,
tpu (
-
pas)
. Make –
, .
-
,
.
, ,
,
.
Build –
, .
,
-
. : Destination –
tpu-
Disk
exe , Memory –
.
Borland Pascal
,
-
Compile. 87
:
Primary File –
,
.
,
-
Primary File. . tpu(
!)
-
.
Uses:
Uses CRT, My_modul;
. : Unit A;
Unit B;
Interface
Interface
Uses B;
.....
.......
End.
End.
A: Uses CRT, A;
B: Uses CRT, A, B;
: Unit A;
Unit B;
Interface
Interface
88
Uses B;
Uses A;
.......
.......
End.
End.
: Uses CRT, A;
: Error 68: Circular Unit Reference (A) , : Unit A;
Unit B;
Interface
Interface
.......
.......
Implementation
Implementation
Uses B;
Uses A;
.......
.......
End.
End.
,
-
, .
:
Unit A;
Unit B;
Interface
Interface
Const x = 1;
Const x = 2;
Implementation
Implementation
End.
End.
: Program
Primer;
Uses WinCRT, A, B; Begin ClrScr; 89
x–
WriteLn('x=', x); End.
, .
:
x=2
B,
x = 2.
: Program
Primer;
Uses WinCRT, B, A; Begin ClrScr; WriteLn('x=', x); End.
: x=1
A
x = 1. , ,
,
, Program
:
Primer;
Uses WinCRT, A, B; Begin ClrScr; WriteLn('A.x=', A.x); WriteLn('B.x=', B.x); End.
: A.x=1 B.x=2
90
, ,
-
,
,
.
,
. , ,
.
, , . -
.
. (
,
,
,
), ,
. ,
,
-
(
),
,
.
-
,
,
-
,
. –
,
. . . -
(
), .
91
-
. . ,
,
, . , (
),
,
.
-
, . ),
, .
(
)
. Type
,
^ (
=
),
,
-
:
Type TPntint = ^Integer; TPntchar = ^Char; TPntReal = ^Real;
TPntint , TPntchar – , TPntReal –
-
.
, –
Var
:
a, b: TPntint; x, y: TPntchar; s, r: TPntReal;
92
-
-
, Var
Var:
a, b: ^Integer; x, y: ^Char; s, r: ^Real;
-
a, b, x, y, s, r ,
. Var, New:
New(a); New(b); ……
,
New,
,
,
-
. ,
New
,
. –
,
-
^: x^, y^, a^.
-
,
-
),
-
( ,
,
-
. , . , ,
-
: New(a); New(b);
93
WriteLn(a^, ‘
‘, b^);
, a WriteLn(a, ‘
b
!
‘, b);
(Error 64) a^ := 3; b^ := 5; WriteLn(a^, ‘
:3
‘, b^);
5
a
a := b;
,
WriteLn(a^, ‘
b– :5
‘, b^);
5
b
b := Nil; WriteLn(a^, ‘
!
‘, b^);
b^
! -
,
,
–
:
New(a); New(b); WriteLn(a^, ‘
a
?
a^
b
?
b^
‘, b^);
a a^
b
,
b^, , -
.
a
3
a^
b
5
b^
a^ := 3; b^ := 5; WriteLn(a^, ‘
‘, b^);
94
: a^
b^ –
3,
-
.
5. a := b; WriteLn(a^, ‘
‘, b^);
a
3
a^
b
5
b^
–
a ,
b. –
-
b.
b^.
a^ .
,
-
, a^
b^: 5
,
-
5.
b := Nil; WriteLn(a^, ‘
‘, b^);
a
3
a^
b
5
b^
Nil –
b
b^,
-
. ,
-
: 1) ( ),
-
,
95
2)
New,
3)
,
4)
,
-
. –
-
– : =
. , (
Nil ).
-
.
,
,
-
,
-
,
New(a), Dispose(a).
,
a, , .
a
Dispose
,
:
New(a); New(b);
a^ := 3;
a
?
a^
b
?
b^
a
3
a^
b
5
b^
b^ := 5;
96
Dispose(a); a := b;
a
3
a^
b
5
b^
5
b^
b := Nil;
a
b
Nil
97
,
-
,
,
,
-
–
–
,
,
,
-
. , ,
-
, .
, .
, , . ,
. .
,
,
,
,
.
-
. : , (
, )
.
98
,
: , ,
.
–
,
-
,
(
),
-
:
-
.
,
, . , – (
.
)
–
. .,
–
-
(
,
)
: Type TPoint = ^TElement; TElement = Record Inf: Integer; Next: TPoint; End; Var
head, q : TPoint;
TPoint – ment,
TElement – Next –
TEle-
,
: Inf – -
TPoint.
,
TPoint
99
.
TElement, , ,
. head
q
TPoint
,
-
,
.
,
.
,
, Nil –
.
,
-
,
-
. ,
–
. ,
head. (
-
),
Nil.
5 –
-12
,
-12 –
,
5, -3,
).
.
head q .
New(q) – ,
:
New(head);
Nil
Head := Nil;
,
: New(q);
q^.Inf q^.Next
100
12 -
head
-12 :
-
q
q^.Inf := -12;
,
,
-
. Nil –
head, :
q^.Inf
12 -
q^.Next
Nil
q^.Next := head;
q
-
head : head
head := q;
,
q^.Inf
12-
q^.Next
Nil
q
,
head New(q),
-12. ,
-3: :
New(q); q ^.Inf
q^.Inf := -3;
-3
q
q^.Next
, head :
head -12 Nil
101
-
, (
head
)
-
: q^.Next := head;
12 -
3q^.Next
Nil
q
–
head (
-
):
head
head := q;
-12 Nil ,
-3 q^.Next
(
)
q
,
5:
New(q); q ^.Inf
q^.Inf := 5;
5
q
q^.Next
( )
,
-3,
head –
: q^.Next := head; head := q;
head -12
-3
5 q^. Next
Nil
102
-
q
, (
)
, head
( ,
q. ,
.Next)
,
head^.Next^.Inf , . .
-3,
head^.Next^.Next – , . .
.
,
-
.
head, ,
«
» -
,
.
,
, 1
(
n,
-
): New(head); head := Nil; For i:=n DownTo 1 Do Begin New(q); q^.Inf := i*i; q^.Next := head; head := q; End;
-
head .
–
-
,
:
(
)
–
q := head; While (qNil) Do Begin
103
Write(q^.Inf:6);
–
q := q^.Next;
End;
-
While ,
q ,
, Nil.
,
. .
,
-
q := q^.Next;
-
q ,
,
Nil,
. ,
,
. :
( –
0),
, ,
: -12 : -3 : 5 : 0 : 3 : 5
-3
-12
Program Spisok; Uses CRT;
104
.
Type TPoint = ^TElement; TElement = Record Inf: Integer; Next: TPoint; End; Var
head, q : TPoint;
Begin ClrScr;
head –
New(head); head^.Inf := 0; head^.Next := Nil; New(q); Write(‘
: ’);
ReadLn(q^.Inf);
0,
If (q^.Inf=0) Then Exit;
head^.Inf := 1; q^.Next := head^.Next;
head^.Next
head^.Next := q; Repeat New(q); Write(‘
: ’);
ReadLn(q^.Inf);
0,
If (q^.Inf=0) Then Break;
1
head^.Inf := head^.Inf + 1; q^.Next := head^.Next;
head^.Next
head^.Next := q; Until (q^.Inf = 0); WriteLn(‘
: ’, head^.Inf); 105
WriteLn(‘
:’);
–
q := head^.Next; While (q Nil) Do Begin Write(q^.Inf:5);
–
q := q^.Next; End; WriteLn; ReadLn; End.
,
,
,
,
, .
: 5, -3, -12. r: Var
head, q, r: TPoint;
,
head
q
:
head -3
-12
5 q^. Next
Nil -3 ,
17.
106
q
-
: 1.
:
New(r); r^.Inf
17
r^.Inf := 17;
r
r^.Next
2.
, ,
-3;
q: While (q Nil) Do
,
If (q^.Inf = -3)
,
Then Break Else q := q^.Next;
q
-3,
q^.Next
q^.Inf = -3,
-12: r^.Inf
17
r
r^.Next
head
-12
-3
Nil
q^.Next
5
q
3.
r^.Next
,
( ,
-12,
r^.Next := q^.Next;
107
-
q^.Next):
17
r^.Inf
r
q^.Next
r^.Next
head -12
-3
Nil
q^.Next
5
q
(17
-3)
-12.
4.
-3
q^.Next
17,
r:
q^.Next := r;
17
r^.Inf r^.Next
r
q^.Next
head -12
-3
Nil
r
5
q
:
5, -3, 17, -12 . (
– .
: -12
108
0),
-
: 17 : -3 : 5 : 0 : 4 : 5
-3
17
-12
: -2 : -3 : 5
-3
-2
17
-12
: 15 : -12
: 5
-3
-2
17
-12
15
: 9 : 5
: 5
9
-3
-2
17
-12
15
17
-12
15
: 20 : 10
: 5
9
-3
-2
: 0
109
: 5
9
-3
-2
17
-12
15
Program Spisok; Uses CRT; Type TPoint = ^TElement; TElement = Record Inf: Integer; Next: TPoint; End; Var
head, q, r : TPoint; posle: Integer;
(0 –
flag: 0..1;
Procedure Formir_spisok; Begin
head –
New(head); head^.Inf := 0; head^.Next := Nil; New(q); Write(‘
: ’);
ReadLn(q^.Inf);
0,
If (q^.Inf=0) Then Exit;
head^.Inf := 1; q^.Next := head^.Next; head^.Next := q;
head^.Next
Repeat New(q);
110
)
Write(‘
: ’);
ReadLn(q^.Inf);
0,
If (q^.Inf=0) Then Break;
1
head^.Inf := head^.Inf + 1; q^.Next := head^.Next;
head^.Next
head^.Next := q; Until (q^.Inf = 0); End;
Procedure Vyvod_spisok; Begin
–
q := head^.Next; While (q Nil) Do Begin Write(q^.Inf:5);
–
q := q^.Next; End; WriteLn; End;
Begin ClrScr; WriteLn(‘
’);
WriteLn;
Formir_spisok;
WriteLn(‘ WriteLn(‘
: ’, head^.Inf); :’);
111
Vyvod_spisok;
WriteLn; WriteLn(‘
’);
WriteLn;
Repeat New(r); WriteLn; Write(‘
: ’);
ReadLn(r^.Inf);
,
If (r^.Inf = 0) Then
Break;
Write(‘
: ’);
ReadLn(posle);
–
flag := 0;
q–
q := head^.Next; While (q Nil) Do If (q^.Inf = posle) Then Begin
:
flag := 1; Break; End Else q := q^.Next;
:
If (flag = 0) Then Begin WriteLn(‘
’);
112
WriteLn(‘
:’);
Vyvod_spisok; Continue; End;
,
r^.Next := q^.Next; q^.Next := r; head^.Inf := head^.Inf + 1;
WriteLn; WriteLn(‘
:’);
Vyvod_spisok;
Until (r^.Inf = 0);
WriteLn(‘
:’);
Vyvod_spisok;
ReadLn; End.
: 5, -3, -12. ,
head
q
:
head -12 Nil
-3
5 q^. Next
–3. 113
q
(
)
-
: 1.
q (
)
)
r
: q := head^.Next; r := head;
2.
,
-
q: While (q Nil) Do
,
If (q^.Inf = -3)
,
Then Break Else Begin
r
r := q;
q q
q := q^.Next; End;
q
-3,
q^.Next
-12. ,
5,
q^.Inf = -3, r -
r^.Next
– r^.Next = q :
head
3. q^.Next,
-12
-3
Nil
q^.Next
5 r^.Next
q
r
r^.Next -12: 114
,
r^.Next := q^.Next;
(5
-3)
-12,
head -12
-3
Nil
q^.Next
5 q^.Next
q
r
-3
.
5 4.
-
-3 q
:
r
Dispose(q);
-3
q := head^.Next; r := head;
r
head -12
5
Nil
head^.Next
q
:
5, -3, 17, -12 . –
0),
.
115
-
: -12 : 17 : -3 : 5 : 0 : 4 : 5
-3
17
-12
: 17 : 5
-3
-2
-12
: 5 : -3
-2
-12
: 10
: -3
-2
-12
: 0 : -3
-2
-12
Program Spisok; Uses CRT; 116
Type TPoint = ^TElement; TElement = Record Inf: Integer; Next: TPoint; End; Var
head, q, r : TPoint; poisk: Integer;
(0 –
flag: 0..1;
)
Procedure Formir_spisok; Begin
head –
New(head); head^.Inf := 0; head^.Next := Nil; New(q); Write(‘
: ’);
ReadLn(q^.Inf);
0,
If (q^.Inf=0) Then Exit;
head^.Inf := 1; q^.Next := head^.Next;
head^.Next
head^.Next := q; Repeat New(q); Write(‘
: ’);
ReadLn(q^.Inf);
0,
If (q^.Inf=0) Then Break;
1
head^.Inf := head^.Inf + 1; q^.Next := head^.Next; head^.Next := q;
head^.Next
Until (q^.Inf = 0); 117
End;
Procedure Vyvod_spisok; Begin
–
q := head^.Next; While (q Nil) Do Begin Write(q^.Inf:5);
–
q := q^.Next; End; WriteLn; End;
Begin ClrScr; WriteLn(‘
’);
WriteLn;
Formir_spisok; WriteLn(‘ WriteLn(‘
: ’, head^.Inf); :’);
Vyvod_spisok; WriteLn; WriteLn(‘
’);
WriteLn; New(r); Repeat WriteLn; Write(‘
: ’);
ReadLn(poisk);
,
If (poisk = 0) 118
Then
Break;
–
flag := 0;
q–
q := head^.Next;
, r –
r := head; While (q Nil) Do If (q^.Inf = poisk) Then Begin
:
flag := 1; Break; End Else Begin
r
r := q;
q
q := q^.Next; End;
:
If (flag = 0) Then Begin WriteLn(‘ WriteLn(‘
’); :’);
Vyvod_spisok; Continue; End;
,
r^.Next := q^.Next;
-
Dispose(q);
head^.Inf := head^.Inf - 1;
119
q := head^.Next; r := head;
If (head^.Inf = 0) Then Begin WriteLn(‘
’);
Break; End; WriteLn; WriteLn(‘
:’);
Vyvod_spisok;
Until (poisk = 0);
WriteLn(‘
:’);
Vyvod_spisok; ReadLn; End.
,
.
. , :
3,
-
5, 12. v, :
Var
head, q, r, v: TPoint;
:
120
head – r
–
v
–
, , ,
-
. ,
head ,
-
r
q
r
head:
q
head 3
5
12 Nil
r
,
7 12.
: 1.
:
New(v); v^.Inf
7
v^.Inf := 7;
v
v^.Next
2.
, ,
, ,
r:
12; While (r Nil) Do
,
If (r^.Inf = q^.Data)
,
Then If (q^.Right Nil)
,
Then q:=q^.Right Else
,
Begin q^.Right:=v; Break; End; End;
{While}
Until (False); WriteLn; Writeln('
: ');
Order(root); WriteLn; ReadLn; End.
. :
136
Var
poisk: Integer;
; flag=1 –
flag: 0..1; n: Word;
0–
:
: 20
: 1 : 50
......... : 0
Program Bi_Tree; Uses WinCRT; Type TRebro = ^TUzel; TUzel = Record Data : Integer; Left, Right : Rebro; End; Var
root, q, v : TRebro; poisk: Integer; flag: 0..1; n: Word;
Procedure Formir_Tree; Begin New(root); Write('
: ');
–
ReadLn(root^.Data);
137
root^.Left:=Nil; root^.Right:=Nil; Repeat Write('
: ');
New(v); ReadLn(v^.Data);
–
If (v^.Data = 0)
,
Then Break; v^.Left:=Nil; v^.Right:=Nil;
–
q:=Root;
:
While (q Nil) Do Begin
If (v^.Data < q^.Data)
,
Then If (q^.Left Nil)
,
Then q:=q^.Left Else
,
Begin
-
q^.Left:=v;
Break; End;
If (v^.Data >= q^.Data)
,
Then If (q^.Right Nil)
,
Then q:=q^.Right Else
138
,
Begin q^.Right:=v; Break; End; End;
{While}
Until (False); End;
Procedure Order(base: TRebro); Begin If (base Nil) Then Begin Order(base^.Left); Write(base^.Data:5); Order(base^.Right); End; End;
Begin ClrScr; Formir_Tree; WriteLn; Writeln('
: ');
Order(root); WriteLn;
Repeat Write(‘
: ’);
ReadLn(poisk);
0,
If (poisk = 0) Then Break;
–
q:=root; flag:=0;
139
n:=0;
:
While (q Nil) Do Begin
:
If (q^.Data = poisk) Then Begin flag:=1; n:=n+1; End; If (poisk < q^.Data) Then q:=q^.Left Else q:=q^.Right; End;
{While}
If (flag = 1) Then Begin WriteLn(‘
’);
WriteLn(‘
: ’, n);
End Else WriteLn(‘
’);
Until (False); ReadLn; End.
: 1)
,
2)
. :q–
,
,
v,
:
140
Var
root, q, v, r : TRebro;
(
poisk: Integer;
)
:1–
flag: 0..1;
,0– ,
a)
:
,
b)
,
c)
. . –
Write(‘
q:
: ’);
ReadLn(poisk);
0,
If (poisk = 0) Then Break;
q–
q := root;
v
v := q; flag := 0;
:
While (q nil) Do Begin
:
If (q^.Data = poisk) Then Begin
–
flag:= 1;
1
Break; End;
{If}
:
v:=q;
v If (poisk < q^.Data) Then q:=q^.Left Else q:=q^.Right;
End;
{While}
141
q
:
(flag=1), 1.
–
(q –
,v–
):
18 v
16
20
q
17 Nil
Nil
If (q^.Left = Nil) And (q^.Right = Nil) Then Begin
,
If (v^.Left = q)
Nil,
Then v^.Left:=Nil
Nil –
Else v^.Right:=Nil; Dispose(q); {
}
End;
v
14
2. ,
–
q
:
15 Nil
18
If (q^.Left = Nil) And
16
20
(q^.Right Nil) Then
17
Begin If (v^.Left = q)
, –
Then v^.Left:=q^.Right
q,
142
–
Else v^.Right:=q^.Right;
-
q Dispose(q); {
}
End;
3.
,
v
–
:
9
q
7 Nil
5 4
6
If (q^.Right = Nil) And (q^.Left Nil) Then Begin
,
If (v^.Left = q)
–
Then v^.Left:=q^.Left
-
q, –
Else v^.Right:=q^.Left;
q Dispose(q); {
}
End;
4.
,
.
:
. q– r–
, ,
,
v–
r. r
q: 143
12 q
4 2
9 7
v
10
5
r
3
6 4
If (q^.Right Nil) And (q^.Left Nil) Then Begin v:=q;
v
q
v
r
r
r:=q^.Right;
While (r^.Left Nil) Do Begin v:=r; r:=r^.Left; End;
{While}
-
q^.Data:=r^.Data;
q , If (r^.Right = Nil)
Nil
Then v^.Left:=Nil Else v^.Left:=r^.Right;
-
Dispose(r);
End;
144
,
,
,
-
:
,
. – .
,
,
,
,
.
-
,
.
–
,
,
-
. , .
,
-
,
, .
-
,
-
,
. –
-
–
. (
)
-
. ,
. , . , 145
-
, ,
.
-
,
, ,
. , ,
,
.
,
–
-
. . ,
.
: ,
,
,
.
, .
-
.
. –
, ,
,
.
,
,
–
.
,
,
-
,
,
. ,
,
.
146
–
,
-
,
-
.
: .
,
, .
–
,
-
.
, ,
,
(
). –
.
, )
-
(
.
(
,
)
,
-
. . , :
(Interface) –
,
(Implementation) – (
,
.
-
) (
-
).
-
, . , ( (
)
,
).
(
)
,
. , . 147
-
.
,
-
, . –
,
,
, .
–
-
. , ,
-
.
:
,
(
Graph)
.
Program Tochka; Uses CRT, Graph; Var x, y, dx, dy: Word; driver, regim: Integer; Begin driver:=detect; InitGraph(Driver,Regim,'C:\BP\BGI');
–
SetBkColor(1); ClearDevice;
–
SetColor(14); x:=100;
x
y–
y:=150; dx:=50;
dx
dy –
dy:=100; PutPixel(x,y,14);
1
Delay(1000);
148
-
PutPixel(x,y,1); x:=x + dx; y:=y + dy; PutPixel(x,y,14); Delay(1000); ReadLn; CloseGraph; End.
. ,
:
y–
x
,
Init –
– ,
Show –
,
Hide –
,
Move –
-
. . Object: Program Tochka; Uses CRT, Graph; Type TPix =
Object x, y: Word;
-
Procedure Init(a, b: Word);
: a
b – -
Procedure Show; Procedure Hide;
149
-
Procedure Move(da, db: Word); : da
db – -
End;
Procedure TPix.Init; Begin x:=a;
x
y –
y:=b;
a
b – )–
End;
Procedure TPix.Show; Begin
-
PutPixel(x,y,14);
x
y
End;
Procedure TPix.Hide; Begin
(
PutPixel(x,y,1);
x End;
Procedure TPix.Move; Begin Hide; x:=x + da;
150
) y
x
y:=y + db;
y –
da
db – )–
Show; End;
Var
x0, y0, dx, dy: Word; driver, regim: Integer;
–
pixel: TPix;
pixel
Begin driver:=detect;
-
InitGraph(Driver,Regim,'C:\BP\BGI');
–
SetBkColor(1); ClearDevice;
–
SetColor(14); x0:=100;
x0
y0 –
dx
dy –
y0:=150; dx:=50; dy:=40;
:
pixel.Init(x0, y0);
-
pixel.Show; Delay(1000);
1 dx, dy
Pixel.Move(dx, dy); Delay(1000);
1
ReadLn;
151
CloseGraph; End.
: 1)
(
TPix
)
–
, 2)
;
-
! 3)
Init, Show,
Hide, Move, 4)
(
)
(
),
,
5)
( –
) .
:
-
TPix, ,
TRing rad –
.
Init, Show, Hide,
-
, ,
.
x
– Type TRing =
-
y :
Object (TPix)
TRing – TPix
rad: Word; Procedure Init(a, b, r: Word);
: a b – 152
-
, r– Procedure Show; Procedure Hide; End;
Procedure TRing.Init; Begin x:=a;
x, y, rad –
y:=b;
a, b, r – )–
-
rad:=r; End;
Procedure TRing.Show; Begin
-
SetColor(14);
-
Circle(x,y,rad);
x
y
–
(
End;
Procedure TRing.Hide; Begin SetColor(1);
) -
Circle(x,y,rad);
x End;
: Var x0, y0, dx, dy, radius: Word; driver, regim: Integer;
153
y
–
ring: TRing;
ring
Begin driver:=detect;
-
InitGraph(Driver,Regim,'C:\BP\BGI');
–
SetBkColor(1); ClearDevice;
–
SetColor(14); x0:=100;
x0
y0 –
dx
dy –
y0:=150; dx:=50; dy:=40; radius:=10;
:
ring.Init(x0, y0, radius);
ring.Show;
1
Delay(1000);
dx,dy
ring.Move(dx, dy);
ring.Move
TRing, pixel.Move – ,
.
,
-
,
. :
Procedure TPix.Move; Begin
154
Hide; x:=x + da; y:=y + db; Show; End;
,
Move :
-
Hide),
-
Hide. ( ,
Show
( ,
-
Show).
-
Move : pixel Move Hide
ring? TRing
TPix,
-
Show, ,
,
TRing -
rad .
, – Init, Show ,
TRing ,
.
Hide Ring.Move
,
TPix.
-
-
pixel.Move. Move ring.Hide
pixel.Move ring.Hide
ring
ring.Show
TPix: pixel.Hide
pixel.Show. ,
pixel.Show
:
ring.Show.
pixel.Move, pixel.Hide ,
155
-
–
,
TPix.
,
,
-
.
,
. ,
Hide
-
Show
,
Move? (
–
(
)
)
.
-
:
Virtual:
Procedure TPix.Show; Virtual; Procedure TPix.Hide; Virtual;
(
),
,
:
Procedure TRing.Show; Virtual; Procedure TRing.Hide; Virtual;
! Init
Procedure
structor: a)
Tpix:
Constructor Init(a, b: Word); . . . . . . . . . . . Constructor TPix.Init; Begin x:=a; y:=b; End;
156
Con-
b)
Tring:
Constructor Init(a, b, r: Word); . . . . . . . . . . . Constructor TRing.Init; Begin x:=a; y:=b; rad:=r; End;
(
Move ) –
-
,
. .
,
-
,
, , . , ,
. ,
,
-
.
-
,
,
. . ,
(
)
,
-
.
-
, dure
. Constructor.
:
157
Proce-
, ,
, ,
, , ,
,
. , .
, . ,
.
Private
Type TRing =
Public: TRing –
Object (TPix)
TPix Private rad: Word; Public
Constructor Init(a, b, r: Word);
: a
b – -
, r– Private
Procedure Show; Procedure Hide; End; 158
:
rad,
Hide.
-
Private
Pub-
Show
Init. .
lic
-
,
. ,
Private, ,
.
, .
-
. ,
TPix ,
TRing,
,
,
,
-
:
Program Pix_And_Ring; Uses CRT, Graph; Type TPix =
Object Private x, y: Word; Public Constructor Init(a, b: Word);
: a
b – -
Private Procedure Show; Virtual; Procedure Hide; Virtual; Public
159
Procedure Move(da, db: Word); :da
db –
End;
Constructor TPix.Init; Begin x:=a;
x
y –
y:=b;
a
b – )–
End;
Procedure TPix.Show; Begin
-
PutPixel(x,y,14);
x
y
End;
Procedure TPix.Hide; Begin
(
PutPixel(x,y,1);
x End;
Procedure TPix.Move; Begin Hide; x:=x + da; y:=y + db;
x
y –
160
) y
da
-
db – )–
Show; End;
Type TRing =
TRing –
Object (TPix)
TPix Private rad: Word; Public Constructor Init(a, b, r: Word);
: a b –
,
r– Private Procedure Show; Virtual; Procedure Hide; Virtual; End;
Constructor TRing.Init; Begin x:=a;
x, y, rad –
y:=b;
a, b, r – )– -
rad:=r; End;
161
Procedure TRing.Show; Begin
–
SetColor(14);
-
Circle(x,y,rad);
x
y
–
(
End;
Procedure TRing.Hide; Begin SetColor(1);
) -
Circle(x,y,rad);
x
y
End;
Var
x0, y0, dx, dy, radius: Word; driver, regim: Integer;
TPix –
pixel: TPix;
-
pixel TRing –
ring: TRing;
-
ring Begin driver:=detect;
-
InitGraph(Driver,Regim,'C:\BP\BGI');
–
SetBkColor(1); ClearDevice;
–
SetColor(14); x0:=100;
x0
y0 –
y0:=150; 162
-
dx:=50;
dx
dy –
dy:=40; radius:=10;
:
ring.Init(x0, y0, radius);
ring.Show;
1
Delay(1000);
dx,dy
ring.Move(dx, dy);
x0:=200;
y0 –
x0
y0:=250; dx:=80;
dx
dy –
dy:=50;
:
pixel.Init(x0, y0);
-
-
pixel.Show;
Delay(1000);
1 dx, dy
Pixel.Move(dx, dy); Delay(1000);
1
ReadLn; CloseGraph; End.
163
1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Out of memory Identifier expected Unknown identifier Duplicate identifier Syntax error
.
. .
.
Error in real constant Error in integer constant String constant exceeds line Too many nested files Unexpected end of file Line too long Type identifier expected Too many open files Invalid file name File not found Disk full Invalid compiler directive Too many files
.
.
-
20
Variable identifier expected
164
21
Error in type
.
22
Structure too large Set base type out of range
. – 64
23
.
-
0…255 256 24
25 26 27
28
29
30 31 32
33
34
File components may not be files or objects Invalid string length Type mismatch Invalid subrange base type Lower bound greater than upper bound Ordinal type expected
. – 255 .
-
.
,
,
Integer constant expected Constant expected Integer or real constant expected Pointer type identifier expected Invalid function result type
. ,
36 37 38
-
Begin End
Begin expected End expected Integer expression expected
165
-
39 40 41 42 43 45 48 49 50 54 55 57 58 62 63 64 65 66 67 68 69
Ordinal expression expected Boolean expression expected Operand types do not match operator Error in expression Illegal assignment Object file too large 64 Code segment too large 64 Data segment too large 64 DO expected OF expected INTERFACE expected THEN expected TO or DOWNTO expected Division by zero Invalid file type Cannot Read or Write variables of this type Pointer variable expected String variable expected String expression expected Circular unit reference
-
– – – Do Of Then To
DownTo
-
.
.
Unit name mismatch
, .tpu,
, Uses
73
-
– Implementation
Implementation expected 166
74 75 76 77 78 79
81 82
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Constant and case types do not match Record variable expected Constant out of range File variable expected Pointer expression expected Integer or real expression expected Label already defined Undefined label in processing statement part Unit expected ‘;’ expected ‘:’ expected ‘,’ expected ‘(’ expected ‘)’ expected ‘=’ expected ‘:=’ expected ‘[’or ‘(‘ expected ‘]’or ‘)‘ expected ‘.’ expected ‘..’ expected Too many variables Invalid FOR control variable Integer variable expected Files are not allowed here String length mismatch
(
) Case
)
( «
-
»
«
» -
-
Unit ‘;’ ‘:’ ‘,’ ‘(’ ‘)’ ‘=’ ‘:=’ ‘[’ ‘]’
‘(‘ ‘)‘
‘.’ ‘..’
For –
.
167
-
102 103
104 106
108
109
112 123 124 132
137
140
145 146 147
String constant expected Integer or real variable expected Ordinal variable expected Character expression expected Overflow in arithmetic operation No enclosing FOR, WHILE or REPEAT statement CASE constant out of range Too many symbols Statement part too large Cannot evaluate this expression Structured variable are not allowed here Invalid floating-point operation Too many nested scopes File access denied Object type expected
-
– LongInt Break Continue
-
Case –32768…32767 – 64 – 24
–
.
168
2. )
2 3 4 5 15 18
MS-DOS
File not found Path not found Too many open files File access denied Invalid driver number No more files ) {$I-}
-
IOResult.
100 101 102
Disk read error Disk write error File not assigned
103 104
File not open File not open for input File not open for output Invalid numeric format
105 106
.
–
)
150 151 152 153 160 161 162
Disk is write protected Unknown unit Disk not ready Unknown command Device write fault Device read fault Hardware failure 169
)
200 201 202 204 205 206 207 210 215
Division by zero Range check error Stack overflow error Invalid pointer operation Floating point overflow Floating point underflow Invalid floating point operation Object not initialized Arithmetic overflow
-
170
. . – . .
.-
. . 11,76.
171
4.12.2014. 2014 .