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

205

681.3.06(075.3+075.8)
32.973.26-018.1
23
, . .
23 Turbo Pascal / . . ,
. . . - .: -. 2013. - 352 : .
ISBN 978-5-94157-240-3


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

681.3.06(075.3+075.8)
32.973.26-0IS.1


.
.




.

29.03.13.
70100 1/16. . . . . 28.38.
. 1200 . 3576
"-, 190005, -, ., 9.
""
199034, -. 9 , 12/28

ISBN 978-5-94157-240-3 .., .. 2002


, - , 2002

................................................................................................................... 9

I. TURBO PASCAL:

............................................................. 11
1. .................................................................... 13
. 1 . ...............................................................13
1.1.1. ...............................................................................13
2 - ...............................................................................................14
1.2.1. .................................................................................14
1.2.2. -...........................................................................................................14
1.2.3. , , ..............................................................................15
1.2.4. - ..........................................................................16
. 5. .....................................................................................17
1.3.1. . ................................................................17
1.3.2. .................................................................................................17
4. ...........................................................................................................................19
1.4.1. . ................................................................19
1.4.2. .......................................................................................................19

2. Turbo Pascal....................................................................20
2.1 .................................................................................................... 20
2.1.1. ...............................................................................20
2.1.2. .................................................................................................. 22
. . .......................................... 22
........................................................................................................... 22
........................................................................................................ 22
. ................................................................................... 23
. .................................................................................... 24
2.1.3. ....................................................................................................... 24
. ............................................................................... 24
.............................................................................................. 25
4________________________________________________________________

. ............................................................................................................. 27
................................................................. 28
. ............................ 29
...................................................................................... 30
............................................................................................... 31
2.1.4. ..................................................................................................... 32
........................................................................ 34
......................................................... 35
2.2. ................................................................................................. 36
2.2.1. ................................................................................................... 36
............................................................................................................. 36
............................................................................................ 37
........................................................................................ 37
2.2.2. USES......................................................................................................... 38
2.2.3. ....................................................................................... 39
2.2.4. .................................................................................. 39
.................................................................................. 39
............................................................................. 40
..........................................................................40
2.2.5. .......................................................................... 41
2.2.6. ............................................................................. 42
...................................................... 43
2.2.7. .............................................................. 45
2.2.8. ............................................................................................... 46
3. ......................................................................................................... 47
3.1. ................................................................................................................. 47
3.2. .............................................................................................................. 49
3.2.1. .................................................................................... ...............50
3.3. ............................................................................................... 50
3,3.1. .............................................................................. 52
............................................................................... 52
DIV MOD....................................................................................... 54
.......................................................... 54
...............................................................56
TRUNC ROUND.............................................................................57
. .............................................................58
{$Q+} {SR+} .................................................................59
..........................................................................................61
.............................................................................................61
..........................................................................................61
...........................................................................................64
3.4. . GOTO......................................................................67
3.5. ........................................................................................68
3.6. ...........................................................................................................68
3.7. ......................................................................................................69
3.8. .....................................................................70
3.8.1. .................................................................70
...........................................................................................71
_______________________________________________________5

3.8.2. IF.........................................................................................73
3.8.3. CASE ................................................................................................... 78
3.9. () ........................................................................................81
3.9.1. REPEAT. ...............................................................................................82
3.9.2. WHILE.................................................................................................90
3.9.3. FOR ......................................................................................................99
3.9.4. .............................................................................................107
3.9.5. , .....................................................112
4. .......................................................................................................... 114
4.1. ...................................................................... 115
4.1.1. VAR ..................................................................... 115
4.1.2. ....................................................................................117
4.1.3. ...............................................................................................117
4.1.4. ................................................. 117
4.1.5. ........................................................ 118
4.1.6. ...............................................................................................................119
4.2. ...........................................................................................121
4.2.1. ........................................................................... 121
4.2.2. ................................................................................................... 122
4.2.3. ............................................................................................. 123
............................................................. 123
............................................................... 126
............................................................... 128
.......................................................................................... 130
.................................................. 134
........................................................135
.......................................................................................... 137
4.2.4. , .................................... 140
5. ................................................................................. 143
5.1. .........................................................................................................
5.2. .
........................................................................................... 144
5 3. .............................................................................................. 151
5.4. ............................................................................... 157
5.4.1. - ......................................................................................... 158
5.4.2. -.................................................................................... 160
5.5. .................................................................................. 166
5.6. ...................................................................................................... 171
6. ...........................173
6 . 1 . ................................................................... 173
6.2. ........................ 175
6.2.1. ............................................................................................................. 175
6.2.2. .................................................................................181
6.2.3. - ............................................................182
6 _______________________________________________________

6.2.4. - - ............................................... 183


6.2.5. -......................................................187

II. ....................................................................191

7. ..................................................................................193
7.1. .................................................................. 194
7.2. ......................................................................................................... 195
7.3. ....................................................................................................... 197
7.4. ....................................................................................... 199
7.4.1. ........................................................................ 200
7.5. ................................................................................................ 201
8. ............................................................................. 203
8.1. ....................................................................................... 203
8.2. ................................................................ 205
8.3. CRT ............................................................................................................... 206
8.3.1. ....................................................206
8.3.2. .................................................................................................210
8.3.3. ............................................................211
8.3.4. ....................................................................................212
8.3.5. ............................................................................................215
8.4. GRAPH ...........................................................................................................217
8.4.1. .................................................................................................217
.............217
.......................................................219
............................................................................................220
8.4.2. ...................................................................................220
.............................221
8.4.3. ................................................................................224
8.4.4. ............................................................................226
8.4.5. .......................................................................................................227
8.4.6. ......................229
8.5. DOS ................................................................................................................231
8.5.1. .............................................................231
8.5.2. .................................232
8.5.3. Turbo Pascal .............................233

9. .............................................................................. 235
9.1. CHAR STRING.................................................................................235
9.1.1. ................................................................................................235
9.1.2. ...................................................................................................236
9.2. .............................................................................................239
9.2.1. (+) ..................................................................................239
9.2.2. ........................................................................................ 241
9.3. ............................................................................ 242
9.3.1. ....................................................... 243
_________________________________________________________7
9.3.2. 9.3.2 244
9.3.3. ....................................................................245
9.4. ......................................................................246
9.4.1. , ...............................................246
9.4.2. ....................................................248

10. .................................................................................................... 251


10.1. ..................................................................................................251
10.2. ....................................................................................253
10.3. ...........................................257

11. ............................................................................................................ 259


11.1. ...............................................................................259
11.2. ................................................................................................263

12. ............................................................................................................ 267


12.1. ............................................................269
12.1.1. .............................................................................269
12.1.2. .........................................................................................................270
12.1.3. ...................................................................................................... 272
12.2. .......................................................................................273
12.2.1. . ...........................................................273
12.2.2. . ..........................................................................275
12.3. ...................................................................................276
12.3.1. ...................................................................... 276
12.3.2. ...................................................................... 277
12.3.3. ...................................... 279
12.3.4. ..................................................... 280
12.4 .281
12.5. .............................................282
12.5. ..........................................................................285
12.5. ..293
12.5.1. .................................. 294
12.5.2. ................................................................ 295
12.6. .................................................................................... 301

13. ..................................303
13.1. ................................................................ 303
13.1.1. ......................................................................................... 303
13.1.2. Turbo Pascal .............................. 305
13.1.3. ..................................................................................... 307
13.1.4. ....................................... 308
13. 2. ........................................................................ 310

......................................................................................................................319
_8______________________________________________________________________________________

I.
Turbo Pascal ....................................................................................................... 321
1.1. , Turbo Pascal........321
1.1.1. ...........................................................................................321
1.1.2. .............................................................................................322
1.1.3. ........................................................................322
1.1.4. ..................................................................323
1.1.5. ..........................................................................................324
1.2. ................................................................................................325
1.3. , ......................................326
!.4. , , .................327
1.4.1. .................................................................... 328
1.5. ....................................................................................................329

2. Turbo Pascal ............................. 332


2.1. CRT. ...................................................................................332
2.1.1. ............................................................................................ 332
0127..........................................................................332
128255 .....................................................................333
2.1.2. CRTPLUS .......................................................................................333
2.2. Turbo Pascal..........................336
2.3. GRAPH BMP..................339

........................................................................................................ 344

.................................................................................................. 346

Turbo Pascal ( ),
, -
.
-
: .
, , -
.
-
; Turbo Pascal, -
;

Turbo Pascal.
, , -
.
() () -
. " -
" " ".
-
. -
:
, ;
;
, ,
-
;
"", , " -
", : -
;
10________________________________________________________________


;
, ,
;
.

.

Turbo Pascal:



1.
2. Turbo Pascal
3.
4.
5.
6.

1.1.

1.1.1.
- ()
() "", -
.
-
, -
. -
-
. , ,
, , . .
, : .
"" -
,
, ;
-
, , ;
;

;

, "" ;
-
( -
, , .).
14____________________________ I. Turbo Pascal

1.2. -
1.2.1.
. -
- -

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

.

1.2.2. -

-. - -
(), -
. , -
, , . - -
,
(), -.
,
, . 1.1, () ;
~ /; ; (-
) ; ( i
, ). "
" ,
() -
, -
. -
(. . 3.9.3). 19.701-90 ( 5807-85),
01.01.1992 ., -
. (. 1.1, ).
1. _______________________________ 15

. 1.1.

1.2.3.,,
(. 1.1, , 6, ) -
, -
. . -
. -
,

.
-
(. 1.1, ),

. ,
16___________________ _____ I. Turbo Pascal

-
- .
(. 1.1, ) -
.

1.2.4. -
- " ".
: , ""
0 1000.
. 1.2. ,

. 1.2. - " "


1. ___________________________________17_

. , , .
: ? ,
. . "", ""
, . "",
: ?
"", "",
.
"", , , .
" ", .
, -, 3.12.

1.3.
1.3.1. .
, , -
, -
. . ,
.
, (
) ( ). -
"", -
.
,
, -
.
() -
.

1.3.2.
.

(, . .). -
, . -
, (),
. -
, , -
( ).
-
, , -
() ,
.
_18____________________ I. Turbo Pascal

-
. -
. .
, -
-
, .
-
, -
"" , (-
, . .).
. -
, Delphi, -
,
, - .
-
(. . . 1).
. -
. ,
, ,
.
-
, .
, . .
, ,
. , ,
. ,
, . -
"-
" (fool-tolerance),
. -
, -
, , -
(. 1).
-
,
. -
.
, .
-
.
, , .
1. __________________________________ 19

1.4.
1.4.1. .

.
, . -
() -
.
, "" ,
. -
.
, -
. .
, -
, (. 1).
, , .,
"", -
, .
,
.

1.4.2.
, -
.
: ( ), -
().
( -
Turbo Pascal), , -
- "" .
.
,
-
. Turbo Pascal 200
(. 1).

.
, . . -
, , , -
.
.

Turbo Pascal (. 1).
2

Turbo Pascal

2.1.
2.1.1.
.
-
.
,
<Enter>, -
. -
, b. -
b,
sum, " ..."
sum ( 2.1).

2.1.

{ }
program example; { }
var
a,b,sum: integer; { a,b,sum - }
begin { }
{ }
write(' : ');
{ }
readln(a);
{ b }
2. Turbo Pascal____________________ 21

write(' b: ');
readln(b);
{ sum }
sum:=a+b;
{ }
write('cya ',,' ',b, ' = ',sum);
end. { }

, (. . 2.2).
1.
Turbo Pascal , -
:
program , .
, . .
;
var (
variable, );
integer , ,b,sum , . .
, 2, 3, 0, 287, 21,
32 . [-32 768, 32767);
write('')- -
''. ,
' ;
readln(a) -
.
2. b
sum. -
:= sum := +b.
3. , -
. -
. , ,
.
. -
; ( ), -
. (). ,
, : { }.
, -
.
22 _______________________ I. Turbo Pascal

2.1.2.
. .
.
:
;
;
.
. -
, ,
.
. -
{. 2.1). -
,
.
(. . 12).
-
.
,
.

, ,
. , -
.
Turbo Pascal : , -
(), . -
( ). -
,
(-, -, 10).
.

, , -
.
, -
().
, "" -
( ) ,
"" . -
, .
2. Turbo Pascal _________________________________ 23

, , "" ,
, ,
. -
, , , -
.
-
. := + 1 -
. , -
,
1 .
-
, . . -
. .
Turbo Pascal , -
, .
(. 2.1)
, -
(. . 2.2.4).

.

, ,
, -
. -
.
-
.
, - "",
,
; , ,
.
, , ,
, -.
, , -
, , -
.
, Turbo Pascal,
, -
, .
, -
: , -
(), , . . . Ee-
24_____________________________ I. Turbo Pascal

,
.


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

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

, .
,
. , ,
( ),
.

.
.

2.1.3.
.
,
( ), , -
.
, 1 3 ,
. ''
'' -
, , (-
+).
2. Turbo Pascal__________________________ 25

, Turbo Pascal,
: () ().
-
( ). -
Turbo Pascal. : -
, , (), ()
. -
. : , , ,
.
, ,
( )
, , .
, -
.
, ,
(. . 2.12.3). -
-
. , .



. .
Turbo Pascal (. 2.1).
(. . 3.1) ,
() .
, . -

(. . 3.3.1).

2.1.
,

byte () 0-255 1

shortint ( ) -128-127 1

integer () -32 768-32 767 2

word () 0-65 535 2


longint ( ) -2 147 483 648-2 147 483 647 4
26________________________________ I. Turbo Pascal


. -
(. 2.2).
, real 2 4 8 =
= 281474 976 710 656 ( 1014)
6 , 48 .
, .

2.2.
,
- -

single (1,5-45-3,438
) 7-8 4
( )
real () 2,3-39-1,738 11-12 6
double 5,0-324-1,7308 15-16 8
( )

extended 3,4-4932-1,14932 19-20 10


( )

m () -263+1-263-1 19-20 8

() : true ( , 1)
false ( , 0), , . . -
true > false (. 2.3).
() , -
(. . 2.3). -
. -

.
, -
(. 2).

2.3. ()
,

char(, ) 1
boolean () 1
true, false
2. Turbo Pascal__________________________________________ 27

, byte, shortint, integer, word,


longint, . byte,word .
, single, real, double, extended,
comp -
.
integer
real.


, ,
real single,double extended. -
23 .
, . . ,
. :
{$N+} []
Turbo Pascal: Options | Compiler |
8087/80287. (.
. 2.1.4).

comp, ( 9,2xl018 9,21018),


"" ,
920 . -
. -
.
,
.


$,
(
$00000000 $FFFFFFFF,
$0000 $FFFF).
0 9. -
0 9 : ( ,
10 ), ( 11), ( 12),
2 1
D ( 13), ( 14), F ( 15). : 123 (123=1 10 +210 +10)
1
, $7 ($7=716 +16) , , -
.

.
(, , )
( binary
digit. ). -
. . -
28_ ________________________ I. Turbo Pascal

,
- 11111111, 255, -
- 0. 0
255 ( 256).
,
(
). ( ).
, ( char )
256 ( ). -

(8- ). integer -
-32768 32 767 (65 535 ).
(16- ).
,
-
-2n-1 2n-1-1.
16
word [0.. 2 -1] [0; 65535]. ,
,
. , longint [231; 2311] -
[-2 147 483 648; 2 147 483 647].


-
.
. -
, , . ,
.
(
) (: 25x10-3)
. -
: , ,
10 , ( ).
, , " 10 ".
, 123,456 -11,9 , 5.18+02 (518)
10-03 (0,01) .
:
123 ;
1,23 ;
0.12303 ;
12.341.2 .
2. Turbo Pascal _______________________________29

-
: .

. :
( s " -
"), (. 3.24).
;

(. 3.19).
, -
;

. -
.
, , -
(. . 3.1) .
:
;
-
, . .
- ;
-

. ,
, ,
, , -
, - . -

(. 3.29).

.

.
, -
,
#, .
, '' , ';' , ' ' ,
#22 #$20 (32 , -
, 20 32).
30____________________________ I. Turbo Pascal

. , 'a' <'b' -
.
# ( ) (-
) ,
, . -
:
#07 (BEL) ;
#08 (BS) ;
#09 () :
8, 1 (9, 17, 25 . .);
#10 (LF) ,
;
#11 (VT) ;
#12 (FF) ;
#13 (CR) , -
(
<Enter>);
#26 (SUB) ,
<CtrI>+<Z>;
#27 (ESC) , <Esc>;
#32 (BL)
. .
# ^ (
), , ,
.
-
. 0 31 (. -
2).
, -
writeln (' ', ^G^G^G) ; .
^ G 0,25
800 .


, ,
string .
, , -
.
2. Turbo Pascal _________________________________31

, -
. (. . 2.1.4), -
.
, .
255 . 1
.
, ' Turbo Pascal', '12345', '+'. -
(. . 9).


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

,
( ). .
Turbo Pascal
:
() ;
.
, -
, .
-
:
..

: 1..12 ( 1 12)

'a'..' z' ( z).
,
.
, . color= (red,blue,green,black).
()
co1 . 4 : red,
blue, green black.
Turbo Pascal
, . 2.2.6).
32 I. Turbo Pascal

2.1.4.
,
, . ,
. .
Turbo Pascal -
, , :
(, , ..., Z, , b, ..., z)
;
(0, 1, ..., 9) (0, 1, ..., 9, , , ..., F).
, (. 2.4) -
(. 2.5).

2.4.

+ { }

- .
* ,
/ :

= ;
> '

< #
[ ] $
() ^ ()
@ ( )

2.5.

:= <=

<> >=
{..) [ ]
..
(* *) {}
2. Turbo Pascal 33

, -
.
, , . -
, , -
, .
.
, -
() .
Turbo Pascal -
,
(. 2.6).

2.6.

absolute if
and implementation
array in ( )
asm. inherited
begin inline
case interface
const interrupt
constructor label
destructor library
div mod
do nil
downto not
else object
end of
export or
external packed
file procedure
for program
function record
forward repeat
goto set
34 _________________________ I. Turbo Pascal

2.6 ()

shl until
shr uses
string var
then while
to with
type xor
unit

.
, goto ; :
goto 10 ; goto 10 ; ;
goto10 ; go to .
10;

-
, , .
, .
, sin(x) , -
, . -
, , -
.
sin (. . 5). . -
.


, , ,
, .
,
(. . 2.2.1).
():
, , ,
, .
Turbo Pascal,
;
. -
;
, ,
;
2. Turbo Pascal ________________________________ 35

127 , , -
Turbo Pascal, 63, -
;
,
. -
;
, -
. . ,
MYVAR, MyVar, myvar -
.
Turbo Pascal :
, (, Textcolor).
, ,
, .
, , , . . -

, . . -
, :
Error 4: Duplicate identifier ( 4: ).
, metka3, Blok_15 .
:
3DGraph ;
Nomer.Doma ;
blok#1 ;
My Program ;
Div .


, , -
.
-.
{ } (* *):
{ } (* *).

:
(* 2 *) 1 }

* { 2 } 1*)
36 _________________________ I, Turbo Pascal

:

, ,
{ } (* *) , , ,
;
,
. -
, , ,
$.
,
.
. :
{$R+} ;
{$I-} /;
{$F+} .

2.2.
2.2.1.
:
{ }
begin
{ }
end.
, ,
, .
() -
.
. -
. . . : .

, -
program, , -
.
program (input, output);{ /)


. ,
2. Turbo Pascal _____________________________________ 37_

.
,
. , ,
. ,
.


Turbo Pascal :
{ }
{ }
;
;
;
;
;
;
{ }
() , be-
gin end;

. ().

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


:
program ;
uses
1, . .;
l a be l
1, .. ;
const
= ;
38 __________________________ I. Turbo Pascal
type
= ;
var
: ;
( )
begin
( }
end.


(
uses, ). -
, , . , -
, .
Turbo Pascal .
,
.
( ).
,
, -
, ,
.
, :
:=1
:=2;
,
, :
:=1:=2;

"", -
1 ( -
).

2.2.2. USES
uses -
. uses -
:
uses
!, 2,.;
aa 2. Turbo Pascal __________________________________ 39

:
uses crt;

crt (. 3.1)
. . 7).

2.2.3.
Turbo Pascal , -

goto (. . 3.4).
,
. , ,
.
label :
label
1, 2, ...;


, , .

,
. , -
(. . 5).

2.2.4.
,
.
,
.

.


const
constants, ) :
const
= ;
40 _________________________ I. Turbo Pascal

:
const
g=9.8; { }
count=maxint/2+l;{maxint ,..2.2}
nmax=100; { }
nmin=-nmax;
s=''; { }
kod=$123; { }

=,
:=. , ,
, -
.


,
. ,
:
const.
: = ;

:
const
ocenka : byte = 5;
predmet : string = '';

,

.


, (. . 12).
-,
(. . 11).


( ,
), -
const.



(. 2.7).
2. Turbo Pascal ____________________________________41_

2.7.

true boolean true


false boolean false
maxint integer 32 767
maxlongint integer 2 147 483 647

2.2.5.
Turbo Pascal ,
, ,
. , -
, . -
, -
.
, Turbo Pascal
, .
, -
(. . 2.1.3).
, ,
.
.
Turbo Pascal -
.

.
,
, .
type :
type
1 = 01;
2 = 02;

:
type
matr = array [1..maxrow, 1..maxcol] of real;
{ matr maxrow maxcol }
42_ ____________________________ I, Turbo Pascal

-
var.



type, -
var. , -
. -
type ,
.

2.2.6.
, ,
. -
. ,
,
. ,
,
(. . 2.2.4).
:
var
1,, N: ;

:
var
matrix: matr; { matrix matr, }
{ type }
xl,x2: integer; yl: longint; { }
sum: real; root: double; { }
znak: char; { }
flag: boolean; { }

:
, -
var { variable, )
,
;

( );
, , -
, real integer ;
2. Turbo Pascal_________________________________43


Turbo Pascal -
, :
(. . 2.1.3). -
, , -
, -
.
. -
, .
, .
0, 1 . . ( -
65 535).
:
= (1., 2,... ,N);
var : ;
':

type days=(monday,tuesday,Wednesday,thursday,friday,Saturday,Sunday) ;
var day: days; Season:(Winter,Spring, Summer, Autumn);

days. -
, -
day. -
. ( )
var .
Season, Winter, Spring, Summer
Autumn. .

case (. . 3.8.3).

(. . 3.8.1). -
.
, Winter < Spring, . . spring
, winter.
Turbo Pascal / .
, (. -
3.39, 4.11, 4.12)., readln(day);
44 ____________________________ 1. Turbo Pascal

writeln (day); : Error 64: Cannot Read or Write


variables of this type ( 64: He -
).
: day:=monday;. -
: day:=monday+tuesday; -
: Error 41: Operand types do not match operator ( 41:
).
. , -
. -
-
:
. -
,
. -
var count:
10..100; , var count: integer;, -
count 10 100. -
. -
. real . type
price=1.99..5.99 ; : Error 27: Invalid subrange
base type ( 27: ). -

(. . 2.2.4).
type = 1.. 2;
var : ;

:
const min=1; max=31;
type days=min.,max;
var rab_day,bol_day : days;

rab_day bol_day days,


1 31. -
.
, .
,
,
(. . 2.1.3).
Turbo Pascal :
ord(s) s ,
s. - longint.
2. Turbo Pascal_________________________________45

s. ord(s) , -

0 1, 0 255 0 65 535 . -
ord(s) ;
pred(s) , s -
. . -
s , -
;
succ(s) , s -
. .
s , .

2.2.7.
, -
.
( ), -
, .
, -
.
. -
-
.
, .
:
procedure () ;
{ }
begin
{ }
end;

:
Function {) : ;
{ }
begin
{ }
:= ;
end;

(. . 5).
46_______________ I. Turbo Pascal

2.2.8.
, . .
, , -
, ,
. begin,
, .
end :
b egin
1;
2;
. . .
N;
end.


.
,
.
3

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

3.1.
, , -
. -
. -
(. . 12). Turbo Pascal
.
,
.
, , .
Turbo Pascal
/: read,readln,write writeln.
"", " " (read line), "", " " (write line) -
.


() -
, , , -
. Turbo Pascal
_48_________________________________ I. Turbo Pascal

, , ,
, , , -
, (. . 2.1.4).
, /, "-
", . . .

:
read(xl,2,...,xN);
readln;
readln(xl,x2,...,xN);

x1,x2, ..., XN , -
(integer,real,char,string). , ,
, .
:
var i:integer; a:real; ch:char;
begin
readln(i,a);
readln(ch);
. . .
:
readln read, . .
,
(.
. 8.3). read
<Enter>, ;
read readln -
. ,
( <Tab>)
<Enter> ;
readln; ( ) -
, -
<Enter>. , -
-
;
, , -
, ;
, -
, ,
/. -
3. ___________________________________________49

, . . Turbo Pascal, Error 106: Invalid


numeric format ( 106: ). -
Run time error 106 (
106). -
/ (. . 3.9.1).
-
. write.

3.2.
-
(, , ).
:
write(yl, y2,...,yN);
writeln;
writeln(yl, y2,...,yN);

y1, 2,, N . , , -
, .
:
write(a + b);
writeln(' :', sum);

:
write writeln -
, .
;
(,
);
,
, .
(. . 3.3.1);
writeln write. ,
, -
;
writeln ; ( ) -
. , ,
-
.
50_________________________ I. Turbo Pascal

3.2.1.
-
(),
. -
( ).
, -
.
, -
-
:
write(yl:w:d, y2:w:d,...,yN: w:d);
writeln(yl:w:d,y2:w:d,...,yN:w:d);

w ; d ; w
d . d -
,
.
w, , , -
. d, .
.
:
:=1.234; write('=':10,:7:3);

:
xxxxxxxxC=xxl.234

().

3.3.
read readln , -
. -
. -

:
:= ;

:= " ". ,
, .
:
sort:=1; :=12.34; :=+1; :=;
result:=sin(a)+cos(b); name:='1';
3, ______________________________________________________ 51


Turbo Pascal, . .
.
: ;
-
, -
, , -
, ;
:

;

;
;
, ,
,
, , -
- ;
:
mismatch ( ).



, .
:
var x: integer; :real;
begin
:=5; :=0.5; :=+; { }
:=; { ,.. }
{ , }
{ , }
{ }

-
. , , -
trunc round. x:=trunc(y); x:=round(y); (.
. 3.3.1).

Turbo Pascal
. .
52_____________________________ I. Turbo Pascal

, i:=j:=k:=0;. -
: i:=0; j:=0; k:=0;.

3.3.1.
, . . -
. -
.

.
:
(, , );
;
.


, .

.
, (+)*5-10 ,
5 10; +, * -
. * (-
) . ,
, ( -
), -
.

. , -
.
, , ,
.
, - , +b .
, : *-b.
: * ( - b ) .
, (.
. 3.3).
. 3.1 .
: integer
real .
3. ___________________________________________________53

3.1. ,

+ real real
integer integer
- real real
integer integer
real real
*
integer integer
integer real
/
real real
Div integer integer
Mod integer integer
And integer integer
Shl integer integer
Shr integer integer
Or integer integer
integer integer
2 Xor

real real
+
integer integer
real real
-
integer integer
Not integer integer


+ (. . 9).
+, -, * (. . 10).
54 ____________________________ I. Turbo Pascal

DIV MOD
div ( division, ) -
, ,
-13 div 3 = 4, 4,(3). div ,
.
:
11 div 5 = 2
10 div 3 = 3
2 div 3 = 0
123 div 4 = 30
17 div -5 = -3
-17 div 5 = -3
-17 div -5 = 3

mod ( modulus, ) , -
.
:
10 mod 5=0
11mod 5=1
10 mod 3 = 1
14 mod 5=4
22 mod 5 = 2
31 mod 16 = 15
17 mod -5=2
-17 mod 5 = -2
-17 mod -5 = -2

div mod . -
div mod . >0 b> :
a mod b = -(a div b)* b
(a div b)* b +(a mod b)= a

mod , ,
b . , b ,
a mod b = 0 (. 3.6).



(. 3.2).
3. __________________________ 55

3.2, ,



real real
abs(x) [|
integer integer
real real
sqr() 2
integer integer
real real
sqrt() 1/2
integer real
real real
() x
integer real
real real
1() 1()
integer real
pi - real
real real
sin(x) sin(x)
integer real
real real
cos() cos()
integer real
real real
arctan(x) arctg(x)
integer real

-
(abs, ln, exp .) , -
. -
,
(. 3.1).
:
sin cos -
. -
180/pi.
-
pi/180 (. 3.1);
arctan .
56____________________ I. Turbo Pascal

. 3.2
:
random() , -
<=<. word.
,
<=<b, , random(b-a)+a. -
, random -
0<=<1. real. ,
: <=<b,
random*b+a. -
random randomize -
(. 3.4).
-
. -
;
dec(x,n)
n. , :=10; dec(, 2); {:8}. -
dec(x),
;
inc(x,n)
n. , :=10; inc(x,3);(: 13}. -
I inc(x),
;
frac(x) .
real. write{frac(0.25*11):4:2); { 0.75};
int(x) . real.
, write (int (422.117) :4:2); { 422.00}.
,
x=int(x)+f().



,
, word, byte
( ), , longint integer,
shortint.
, .
, day ( ),
byte, integer longint.
, , .
, -
3. __________________________________________________57

.
, ,
(. . 2.12.3).
sizeof (it) ,
it. .
, :
const a:integer=123; b:real=12.3e01; : char='c';
begin
writeln('integer: ',sizeof(a),'; real: ',sizeof(b),'; char: ',sizeof
(c));
end.

:
integer:2; real:6; char:1

-
,
(. . 3.1 3.2). -
.
, , -
. -
,
(. . 2.12.3).

, .

, ,
. -
, , .

TRUNC ROUND

. i ,
. -
trunc round:
i-trunc(x) i+round(x).
trunc ()
, >=0, -
<=0 ( truncate, ). , -
. real,
longint .
, trunc(6.7);{:6}, trunc( - 1 . 6 ) ; {:-1}.
58____________________ I. Turbo Pascal

round (x) , -
( round, ). real,
longint.
, round(6.7); {:7},round(-1.6);{:-2}.

.
trunc round,
, , .
Turbo Pascal -
.
, :
const a:integer=32767;{ integer}
var ,: real;
begin
:=+2; { }
y:=longint(a)+2; { }
writeln(x:10:0, y:10:0);
end.

: -32767 32769.

? , -
. , -
,
( -
). integer. 32769 -
, , , longint, -
.
- shortint,byte,word,integer,
longint -
.
.
, (,
real extended).
, :
var a,b: integer; : real;
begin
write(' = '); readln(a);
write(' b = '); readln(b);
3. _______________________________________________59

c:=a/b;
writeln('c= ',c:5:2)

=33000 b=33 1000,


, -985.94. -
. , , -
b integer,
,
-32 768 32 767. 33000, -
.

{SQ+} {$R+}
, , ,
, .
( ) -
, . -
integer ( byte,shortint,word,longint)
real ( ).
, , .
, :
{$Q-}
var a,b: word; c,d: real;
begin
a:=40000; b:=40000; c:=a*b;
writeln ('a * b = =',c:5:2);
a:=l; b:=2; c:=a-b;
writeln ('a b = c=',c:5:2);
d:=-a+b;
writeln('-a + b = d = ',d:5:2);
end
:
a * b = c = 4096.00
a-b = = 65535.00
- + = d = 1.00

, -
word , -
( word -
).

60__________________ I. Turbo Pascal

. -
.

{$Q+}
Turbo Pascal Options | Compiler | Overflow checking.
:=*b; -
: Error 215: Runtime error ( 215: -
). -
:=a-b;. d:=-a+b; -
.
:
:=; :=-b;


(. . 2.12.3) {$R+}
Turbo Pascal
Options | Compiler | Range checking.
, :
var
i: word; k: integer;
1: 224..265;
j: 24..65;
begin
k:=32000; { - 111110100000000 }
{SR-}
i:=k+k; l:=k; j:=k;
writeln('i = ',i,' 1 = ',1,' j = ',j);
end.

: i = 64000 l= 32000 j= 0.
j=0 , j 65 255
( 1). ,
32 000 0. -
{$R+}, i:=k+k; -
: Error 201: Range check error ( 201:
).
, {$R+}
integer
longint. -
, ( -
).
3. _____________________________________________________61


{$R-}.
{$R+|
. -
.
. ,
,
- .


Turbo Pascal
(. 5.8):
c= ab ln(c)= bln(a)c = ebln(a) (bln()).
, .
(. . 3.9).


:
loga(x) = ln(x)/ln(a)/

Turbo Pascal : sin,


cos, arctg (. 3.2).
:
tg() = sin(x)/cos(x);
ctg(x) = cos(x)/sin(x);
csc(x) =l/sin(x);


Turbo Pascal
.
62_________________________________________ I. Turbo Pascal

(and) -
( ):
1 and 1 = 1
1 and 0 = 0
0 and 1 = 0
0 and 0 = 0

, -
. .
, a and b, =12 b=22. -
b , ,
(. . 2.1). :
0000000000001100 { ( 12) }
0000000000010110 ( ( 22) }
0000000000000100 ( ( 4) }

: 12 and 22 = 4.

(k shl n) , -
n ( )
k. , 2n.
, 2 shl 7.
2 7 -
:
0000000000000010 { 2 }
0000000100000000 { ( 256)}

: 2 shl 7 = 256 = 2*2 7 .

(shr) , -
( ).
2n.
, 160 shr 2:

0000000010100000 ( 160 }
0000000000101000 ( ( 40)}

: 160 shr 2 =40 = 160 div 22.

, -
.
3. ______________________________________________________________63_


, , ,

.
, -
, , -
. ,
+.

(or) -
( ):
1 or 1 = 1
1 or 0 = l
0 or 1 = 1
0 or 0 = 0
, -
. .
, 12 or 22:
0000000000001100 { ( 12) }
0000000000010110 { ( 22) }
0000000000011110 { ( 30) }

: 12or22 =30 .
2, -
, (xor) -
( ):
1 xor 1 = 0
1 xor 0 = 1
0 xor 1 = 1
0 xor 0 = 0
, -
. .
, 12 xor 22:
000000000001100 { ( 12) }
0000000000010110 { ( 22) }
000000000011010 { ( 26) }

: 12 22=26.
64______________________ I. Turbo Pascal

(not) -
. , not 0 = -1.
hi(i) i -
. .
. , x:=hi($1020); { =$0010 }.
lo(i) i -
. .
. , x:=hi($7893);{ =$0093}.
swap(i) -
i. . ,
x:=swap($7893); { =$9378 }.
, Turbo Pascal @.
.
. 13.



(). . 3.3 -
( ). -
(. . 3.8.1).

3.3.

, not, @ ()

*,/,div,mod,and,shl,shr
+, , or, xor
=, < >, <, >,<=, >=, in


in ( ) set
(. . 10).


. , , -
. .
, . -
:
.
3. _________________________________________________65

:
3+4*5 * +,
. :
(3+4)*5;
*b/ (*b)/ , ..
,
. :
*(b/).
3.1
.
.

3.1.
uses crt;
var
a ,b: real; { }
angle: real; { }
area: real; { }
begin
textbackground(blue); ( }
textcolor(green); { }
clrscr; { }
writeln(' ');
write(' (.):');
readln(a,b);
write(' : ');
readln(angle);
{ }
angle:=angle*pi/180;
{ area=a*h/2, h ( ) }
{ : h=b*sin(angle) }
area:=a*b*sin(angle)/2;
writeln(' :', area:7:3,' .. ');
readln;
end


clrscr .
clrscr , -
uses crt;. crt -
, , -
: textbackground textcolor (. . 8).
_66__________________________ ____ I, Turbo Pascal

3.2 , m
s n .
:

3.2.
var
m,s,p,r,n,a,d: real;
rub,kop: integer; { ( ) }
begin
writeln{' , : ');
readln(s,p,n);
:=/100;
:=(l(1+r)*); { }
m:=(s*r*a)/(12*(a-l));
m:=trunc(100*m+0.5)/100; { }
d:=m*n*12-s; { }
writeln;
rub: = round(s*100) div 100; kop:= round(s*100) mod 100;
write(' ',rub,' . ',kop,' . ');
write (' ',:5:2,'% ',n:5:2, ' ');
writeln;
rub:= round(m*100) div 100; kop:= round(m*100) mod 100;
writeln(' = ',rub,' . ',kop,' . ');
rub:= round(d*100) div 100; kop:= round(d*100) mod 100;
writeln{' = ',rub,' . ',kop, ' . ');
end.


, :
n, :
Run time error 200 ( 200),
. ,
.
, m, -
.
, , -

.
(. . 3.8).
3 _______________________________________________ 67_

( )
div mod ,
-
(rub ), -
s, m, d (, )
, ""
rub ,
s, m d
Turbo Pascal ( -
5.4)
3.3 ,

3.3
var i,first,second,third,sum: integer,
begin
write(' : '); readln(i);
first:=i div 100, { }
second:=I div10 mod10, { )
third:=i mod 10; { }
sum:=first+second+third,
writeln(' ',100*first+10*second+third,' = ',sum),

3.4. . GOTO

, ,
. 2.2.3), goto -
, ,
label,
goto ;

label metka0l;
. . .
begin
metka01 ,
. . .
goto metka0l;
end
68_____________________ I. Turbo Pascal

:
, , -
( ,
), ;
;

goto , . .
. -
(. . 6.1).

3.5.
:
(1,2,..,N;


. . 5.

3.6.
. -
.
.
:
Begin
. . .
goto metka; { }
. . .
metka: { }
end.

, ,
.
, :
:=1;;
:=2;

: -
:=1; ( ,
3. ______________________________________________ 69

), -
: =2;.
, -
.
.
-
end ,
.
:
:=-; ( }
label0l: { }
end.

3.7.

, ,
begin end.
begin
1;
02;
. . .
N;
end;

-
, -
, .
begin end -
, -
: (01; 02; . . .; OnepaopN).
-
.
.


Turbo Pascal begin end, -
, . . end , case (.
. 3.8.3) record (. . 11), begin .
70_____________________ I. Turbo Pascal

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

.

3.8.1.
,
(. 3.4).
,
true false.

3.4.

= = true,

< > true,

> > true,

< < true,

>= >= true,

<= <= true,

in A in true,

, (. 3.5)
.
, ,
boolean.
, :=<5; -
true,
5, false.
3. __________________________________________________71

3.5.

true false
not not A
false true

true true true



and A and
() true false false

false true false

false false false

true true true

true false true


or A or
() false true true

false false false

true true false

true false true



xor
false true true

false false false


, -

(. . 3.3). -
. .
Turbo Pascal -
, (. . 3.3).
.
, m,mm,n, nm m > mm and n > mm
, . .
nm and n. , ,
: (m > mm) and (n > nmm). -
:
(m>mm), ( > nmmax)
and.
72______________________ I. Turbo Pascal


, ,
, . -
(m > mmax) and (n > nmm) false,
m <= mmax, nmmax. , -
or = true true -
( ). Turbo Pascal -
, {$B}: Options | Compiler |
Complete boolean evaluation. ,
, -
, (. .
).

:
Turbo Pascal
1<<2. (x>1)and(x<2);

x=y=z. (x=y)and(x=z);
, ,
-2 2, not((x>-2)and(x<2)) (<=-2)or(>=2);

: sqr(x)+sqr{y)<1.

.
, :
const a=l; b=2;
var flag: boolean;( flag }
begin
flag:=a>b; { flag}
writeln('a = ',a,'; b = ',b);
writeln('a > b - ',flag,'; a < b ',a < b);
end.

:
= 1; b = 2
> b - FALSE; a < b - TRUE

Turbo Pascal . -
if case.
3. ____________________________________________73

3.8.2. IF
() if :
1.
if
then
begin
{ , }
{ }
end
else
begin
{ , }
{ }
end;

2.
if
then
begin
{ , }
( }
end;

.
if, then, else "", "", "" -
. .
(true, "", 1), ,
then. (false, "", 0),
, else. ,
else, .

.
-
. 3.1, , 6).
:
if a>b then writeln (' b')
else writeln (' b');

:
if (a>=0)and(a<10) then writeln (' );
74 _____________________________ I. Turbo Pascal

. 3.1. :

if if. -
.
1.
if 1 then
if 2 then 1
else 02
else ;

2.
if 1 then 01
else if 2 then 02
else ;

3.
if 1 then
if 2 then 01
else 02;

1., 2... , . . -
, -
. ,
true, ,
. . -
.
:
else then, -
(
3);
3. __________________________________________________75_

23
- ;
begin.. .end.
begin end , begin
end ;

then
else;

, ;

/ , and, or, not (
);
(. . 1.4)
if (. -
3.5, 3.10).
3.4 , -
. ,
, .

3.4. ( 1)

var
s1,s2: integer; { }
otvet: integer; { }
begin
randomize; { }
{s1, s2- 2 19 }
s1:=random (18)+2; s2:=random(18)+2;
write(' ',sl,'*',s2,'?: '); readln(otvet);
if otvet=sl*s2 then writeln('!')else witeln('...');
end

3.5 ,
. , ,
.
if.

3.5.
var
a,b: integer; { - }
result: real; { }
76______________________ I. Turbo Pascal
begin
write(' : '); read();
write(' : '); read(b);
if b=0 { }
{ }
then writeln(' : ')
{ }
else
{ }
begin { }
result:=a/b;
writeln(' ' ,, ' ',b,' = ', result:7:3);
end; { }
end.

3.6 ,
.

3.6.
var n: integer;
begin
write(' : '); readln(n);
write(' ',,' - ');
if n mod 2=0 then writeln('') else writeln('');
end.


odd
if odd(n) then writeln('') else writeln('');

7 , -

3.7. ( 1)
var age1,age2: integer;
begin
write(' : '); readln(age1);
write (' : '); readln(age2) ;
if age1>age2 then writeln(' .')
else if age1<age2 then writeln(' .')
else writeln(' .');
end.
3. ___________________________________________________77

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

3.8. ( 2)
va age1,age2: integer;
begin
write(' : '); readln(agel);
write(' : ');readln(age2);
if age1>age2 then writeln(' .');
if age1<age2 then writeln(' .');
if agel=age2 then writeln(' .');

, -

.
3.9 , , -
. , -
. -
.

3.9.

va a,b,c: real;
begin
write(' : '); readln(a,b,);
if (a+b>c) and (a+c>b) and (b+c>a) then
writeln(' !)
else
writeln(' ...');
end

3.10 -
. , -
goto.
. , , , halt (.
. 6.2.1).
- 5.10.
78______________________ I Turbo Pascal

3.10. ( 1)

label labl0l;
var
a,b,c: real; { }
xl,x2: real; { }
d: real; { }
begin
writeln(' , b, ');
readln(a,b,c);
if {a=0)and(b=0)and(c=0) then
begin
writeln(' '); goto labl0l;
end,
if (a=0)and(b=0)and(c<>0) then
begin
writeln(' '), goto labl0l;
end;
if (a=0)and(b<>0)and(c<>0) then
begin
x1:=-c/b;
writeln(' :',xl:7:3); goto labl0l;
end;
d:=b*b-4*a*c; { }
if d>=0 then
begin
x1 =-b+sqrt(d)/(2*a); x2:=-b-sqrt(d)/(2*a);
writeln(' :');
writeln('xl=',xl:7:3); writeln('x2=', x2:7 :3) ;
end
else writeln(' ');
labl0l { }
end.

3.8.3. CASE
-
if -
, -
- , -

? Turbo Pascal -
case
3. _______________________________________________79_

case :
case - of
1: begin
{ 1 }
end;
2: begin
{ 2 }
end;
N: begin
{ N}
end
else
begin
{ }
end;
end

case -.
begin end ,
case
. , , else,
begin end. else , -
, case.
case end,
begin.
:
:
case i of
1: z:=i+10;
2: z:=i+100;
3: z:=i+1000;
end;

:
Case i of
1..10: writeln(' ', i:4,' 1 - 10');
11..20: writeln(' ', i:4,' 11 - 20');
21..30: writeln(' ', i:4,' 21 - 30')
else(' ')
end;
80_____________________ I. Turbo Pascal

:
case if
;

-
() ,
;
- : - integer,
char, boolean (. . 2.1.3).
- 32 768 32 767. -
;

, .
"..".
;
-
, "" .
-
;
.
3.11 , -
( ).

3.11.

var d,m,y: integer; n: longint;


begin
writeln(' , , (: 3 12 1964)');
readln(d,m,);
if(m>=2)then m:=m+l
else
begin
m:=m+13; y:=y-l;
end;
n:=trunc{365.25*y)+trunc(30.6*m)+d-621050; n:=n-trunc(n/7)*7+l;
case n of
1: write('');
2: write('') ;
3: write('');
4: write('');
5: write{'');
3. ________________________________________________________81

6: write('');
7: write(');
end; writeln;

3.26
case.

3.9. ()
-
. , -
, -
.
,
, "" -
. -
.
(. . 6.1).
, -
.
Turbo Pascal -
( repeat), (-
while) ( for).
:
-
, ;
, ,
;
-
;
while,
;
repeat ,
;
for -
;
,
(
, ""). Turbo Pascal
82________________________________ I. Turbo Pascal


<Ctrl>+<Break>.
, <Ctrl>+<Alt>+<Del>.
Reset, -
, ,
, , .

3.9.1. REPEAT
repeat (repeat), -
(until). repeat, until "" "-
" .
repeat
{ }
until ;

,
repeat until, .
, false (), . .
. ,
true (). . 3.2 -
repeat.

. 3.2. repeat

" " (. . 1.2). -


, ,
comp, ""
1000. ,
igrok, , -
comp ( 3.12).
3. _____________________________________________83

3.12. " " ( 1)


var
comp: integer; { , "" }
igrok: integer; { }
begin
randomize; { }
comp:=random(1000); { |
repeat
write(' : '); readln(igrok);
if igrok>comp then writeln(' ...')
else if igrok<comp then writeln(' ...')
else writeln (' !');
until igrok=comp;
end

repeat -
. ,
.
, , . -
:
var :integer;
begin
. . .
repeat
write (' '); readln (x) ;
until (x>9)and(x<100);
. . .
end

repeat -
, -
, . -
, { } repeat.
, :
uses crt;
va flag: char;
begin
repeat
{ }
writeln(' (Y/y) ?'); readln(flag);
until upcase(flag)<>'Y';
end
84_____________________ I. Turbo Pascal


upcase ,
( ),
, , -
Y , {
}. .

:
() repeat
;
repeat , ,
until, ;
until ;
: -
;
repeat
,
, -
. , ,
, .
repeat succ
pred (. . 2.2.6) inc dec (. . 3.3.1).
, :
var i: integer;
begin
i:=0;
repeat
i:=succ(i); { inc(i); i:=i+l; )
write(i,' ');
until i>=10;
writeln;
i:=10;
repeat
write(i,' ');
i:=pred(i); { dec(i); i:=i-l; }
until i<=0;
end.

:
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
3. __________________________________________85_

repeat ( -
), . .
. , -
;
repeat ,
;
until,

begin end.
.
pa repeat -
/ -
. -
106.
-
{$I-} (. . 2.1.4) lOResult.


"" /, lOResult,
( {$I-}).
/ -
, / ,
lOResult.
. , lOResult, -
, {$I-} /
, lOResult 0. .
.

:
var : integer; ok: boolean;
begin
. . .
repeat
writeln(' ');
{$i-} { / }
readln(x);
ok: =(ioresult=0);
{ , }
{ ioresult }
if not ok then writeln(' . .')
{$i+} { / }
86____________________ I. Turbo Pascal
until ok;
writeln('x= ',x:3)
. . .
end.

3.13 " ",



(. 3.4). -
,
. ,
randomize , . .
.
.

3.13. ( 2)

uses crt;
var s1,s2,otvet, kol,prav: integer; yn: char;
begin
randomize; { }
clrscr; { }
repeat
kol:=kol+l;
sl:=random(18)+2; s2:=random(18)+2;
write(' ',s1,'*',s2,'? '); readln(otvet);
if otvet=sl*s2 then
begin
write ('! ') ; prav:=prav+l;
end
else write ('. ..');
write(' TecT?(Y/N)'); readln(yn);
until (yn='n')or(yn='N');
clrscr; { }
writeln(' : ');
write{' : ',kol,'. : ',prav,'.');
readln;
end.

3.14 ,
( , ).

3.14.
var
,,result: real;
operation,ans: char;
3 _______________________________________________________87
begin
repeat { }
write (' = '); read(x); { }
write(' = '); readln(y); { }
write(' {+,-,*,/) > ');
readln(operation); { }
case operation of { }
'+' : result:=+;
'-' : result:=-;
'*' : result:=*;
'/' : result:=x/y;
else
writeln(' ');
end;
{ }
writeln(:7:3,operation:3,:7:3,' = ',result:7:3);
write(' (/)');
readln(ans); { }
until (ans='n')or(ans='n'); { }
end

3 .15 , -
. 999,
.

3.15.

var count,x: integer; sum: real;


begin
count:=1; sum:=0;
repeat { }
{ }
write(' ',count,' : ');
readln(); { }
count:=count+l;
if x<>999 then sum:= sum+x;
until x=999; { }
writl(' = ',sum:7:3);

3.1 " "


1 10
5
88__________________________________________ I. Turbo Pascal

3.16. " " ( 2)

const npop=5; { }
var
comp: integer; { , "" }
igrok: integer; { }
n: integer; { - , }
begin
n:=0;
randomize; { }
comp:=random(9)+1; { }
writeln(' "" 1 10');
writeln(' ',,' ');
writeln(' ');
repeat
n:=n+l;
write('->'); readln{igrok);
until(n=npop)or(comp=igrok);
if comp=igrok then writeln(' !')
else writeln(' ... ', comp);
end.

3.17 ,
, maxint (. . 2.7),
,
.

3.17

var k,s,n: integer; a: longint;


begin
write(' <= ',maxint,' : ');readln(n);
k:=0; s:=0; a:=0;
repeat
k:=k+l;
s:=s+n mod 10;
a:=a*10+n mod 10;
n:=n div 10
until n=0;
writeln(' : ',k,', : ',s);
writeln(' : ',);
end.
3. _________________________________________________89

-
.
:

! , . . 12...n.
, -
.
.
. n-
, . . eps, ,
.
3.18 , .

3.18.

const eps =le-7; { }


var x,s,u :real; n: word;
begin
write (' : '); readln(x);
x:=x*pi/180; { }
:=1; u:=1; :=0;
repeat
n:=n+2;
u:=-u*x*x/((n-l)*n);
s:=s+u;
90_____________________ I. Turbo Pascal

until abs(u)<eps;
writeln('cos(',x*180/pi:6:2,') =', s:7:3);
end.

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

3.19,

const base=10; { }
=1.0; { }
var p: real; { 1.0, 0.1, 0.01, 0 . 0 0 1 , . . . }
n: integer; { )
begin
:=1.0; :=0;
repe at
n:=n+l;
p:=p/base;
until a=a+p;
writeln(n-1) ;
end.


, 1.0=1.0...01. ,
1.0...01 . 12,
(. . 2.2).

3.9.2. WHILE
while .
while do " , " "" .

while do
begin
( }
end;

while repeat, -

true (), ,
3. __________________________________________________91

begin end .
,
false (). . 3.3 -
while

. 3.3. while

while repeat:
while do ;
if then repeat until Not ;
, 1 10.
:
. . .
s:= 0; i:= 1;
while i<=10 do { 1 10 }
begin
s:=s+i;
i:=i+l; { }
end;

:
() while
, , ;
while -
, while repeat;
: -
( ),
: true false;
92_____________________ I. Turbo Pascal

while
, -
.
while succ
pred (. . 2.2.6) inc dec (. . 3.3.1).
, :
var i: integer;
begin
i:=0;
while i<10 do
begin
i:=succ(i); { inc(i); i:=i+l; }
write(i, ' ');
end;
writeln;
i:=10;
while i>0 do
begin
write (i, ' ');
i:=pred{i); { dec(i); i:=i-l; }
end;
end.

:
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

while , . . -
,
;
while -
;
while , repeat ( -
).
, ;
while -
do.
3.20 " ", -
, , ,
.
3 _________________________________________________93

, , , -
.

3.20 ( 3)

uses crt;
v sl,s2,otvet,kol,prav: integer; yn: char;
begin
randomize, ( }
clrscr; { }
write(' '(Y/N)'); readln(yn),
while (yn<>'n') and (yn<>'N') do
begin
kol:=kol+l;
s1:=random(18)+2; s2:=random(18)+2;
write(' ',s1,'*',s2,'? '), readln(otvet),
if otvet=sl*s2 then
begin
write ('1 '); prav,=prav+l,
end
else write (' .');
write(' ec?(Y/N)'); readln(yn);
end; { }
if kol>0 then
begin
clrscr; { }
writeln (' . ');
write(' : ',kol);
writeln ('. : ', prav,'.');
readln,
end; { , }

3 21 , -
, . -

3.21.
va count,item,sum: integer;

count.=0; { }
sum:=0, { }
94_____________________ I. Turbo Pascal

while true do { }
begin
count:=count+l;
write(' ', count, ' -e : ');
readln (item) ; ( }
if item<0 then break; ( }
sum:= sum+item;
end;
writeln (' ', sum);
end.


(
)
break (). , , -
goto. -
:
label lab01;
. . .
if item<0 then goto lab01;
. . .
lab01: writeln(' ', sum);

3.22 , -
. :
, 0,3
3,7 0,4.

3.22.
uses crt;
var ,,z,lg3,a,b,dx: real;
begin
clrscr;
write (' : '); readln(a);
write (' : '); readln(b);
write(' ; ') readln(dx);
writeln (' ----------- ':20);
writeln ('x':9,'|':4,'y':4);
writeln (' ----------- ':20);
3. ______________________________________________95_
lg3:=ln(3.0)/ln(10.0); { 1g(3) }
:=;
while x<(b+dx/2) do
begin
z:=sin((pi*x)/3);
if(z < 0) then writeln(x:10:3,' | ':22)
else
begin
y:=lg3+x*sqrt(5.0*z);
writeln(x:10:3,' | ',y:7:3);
end;
x:=x+dx;
end;
writeln (' ------------ ':20);
end


-
, -
,
" ".

(. 3.18), -
repeat. 3.23 -
, , while, .

3.23.
const s =1-7; { }
va ,s,u:real; n: word;
begin
write(' : '); readln(x);
x:=x*pi/180; { }
:=; u:=; :=1;
while abs(u)>eps do
begin
n:=n+2;
u:=-u*x*x/((n-l)*n);
s:=s+u;
end;
writeln{'sin{',x*180/pi:6:2, ') = ',s:7:3);

, 3.19,
repeat , -
96_____________________ I. Turbo Pascal

. 3.24 , ,
while, " ". -
, , ,
, .

3.24. " "


var eps: real;
begin
eps:=1.0;
while eps/2+1 > 1 do eps:=eps/2;
writeln (' epsilon = ', eps);
end.

3.25 , -
. 500 D, 100
C,50-L, 10-, 5- V, 1 -I.

3.25. ( 1)

var i: integer;
begin
write(' : '); readln(i);
write(i,' ');
while i>=500 do
begin
write('D'); i:=i-500
end;
while i>=100 do
begin
write ('C') i:=i-100
end;
if i>=50 then
begin
write('L'); i:=i-50;
end;
while i>=10 do
begin
write ('X'); i:=i-10
end;
if i>=5 then
begin
write ('V'); i:=i-5;
end;
3. ______________________________________________97

case i of
0:;
1: write('I') ;
2: write('II') ;
3: write('III');
4: write 'IV') ;
end;
writeln; readln;
end.


write, writeln -
,
, .
i ,
. 0 case
. case , -
.

,
. 3.26 , -
. -
, -
X , V I, -
(. 3.6).

3.6.
X V I

1 n:=10 state:=2 n:=5 state:=3 n:=1 state:=6


2 n:=n+10 state:=2 n:=n+5 state;=3 n:=n+1 state:=6
3 ok:=false ok:=false n:=n+1 state:=4
4 ok:=false ok:=false n:=n+1 state:=5
5 ok:=false ok:=false n:=n+1 state:=7
6 n:=n+8 state:=7 n:=n+3 state:=7 n:=n+1 state:=5
7 ok:=false ok:=false ok: =false

XIV. -
1 ( ). X,
X n:=10; state:=2. , n 10,
98____________________ I. Turbo Pascal

(. . state:=2) ,
I, n:=n+1; state:=6. -
n, , 10+1=11.
(state:=6) V n:=n+3; state:=7.
11+3=14. (state:=7) , -
X, V I (, XIVX)

3.26.

var n,state: integer, symbol: char, ok: boolean;


begin
state:=l; ok:=true; n:=0;
while not eoln do
begin
read(symbol);
if((symbol='X')or {symbol='V')or(symbol='I'))
then
case state of
1: case symbol of { }
'X': begin n:=10; state:=2 end;
'V': begin n:=5; state:=3 end;
'I': begin n:=l; state:=6 end;
end;
2: case symbol of { }
'X': begin n:=n+10; state:=2 end;
'V': begin n:=n+5; state:=3 end;
'I': begin n:=n+l; state:=6 end;
end;
3: case symbol of { }
'X', 'V' : ok:=false;
'I': begin n:=n+l; state:=4 end;
end;
4: case symbol of { }
'X','V: ok:=false;
'I': begin n:=n+l; state:=5 end;
end;
5: case symbol of ( }
'X', 'V' . ok:=false;
'I', begin n:=n+l; state:=7 end;
end;
6: case symbol of { }
'X': begin n:=n+8; state:=7 end;
3. _______________________________________________99

'V: begin n:=n+3; state:=7 end;


'I': begin n:=n+l; state:=5 end;
end;
7: ok:=false; ( }
end (case state}
else
begin
if ok
then writeln(n:2)
else writeln(' '};
state:=1; ok:=true;
end; (else)
end; {while not}
end


<Enter>, ,
.
eoln ,
" ". ,
true, false. not eoln -
while, . . ,
<Enter>.
case.
=1000, D=500, =100 l_=50 -
.

3.9.3. FOR
-
. -, -

.
for .
1 ( ).
for := to do
begin
{ }
end;

for, do "", "" .-


for...do, , ,
100_____________________ I. Turbo Pascal

do . .
, begin end
.
-
(. 3.4).

. 3.4.

begin end , -
[( - ) + 1].

.
, ,
begin end .
, :
for i:=10 to 14 do write(i: 3);
:
10 11 12 13 14 ' .
2 ( ).
for := downto do
begin
{ }
end;

begin end , -
[( - ) + 1].
, ,
begin end .
3. _________________________________________________101_

, :
for i:=14 downto 10 do write(i: 3);

:
14 13 12 11 10
- char , :
for ch:= 'a' to 'e' do write (ch: 2);

:
a b d e
:
fo ch:= 'e' downto 'a' do write {ch: 2);

:
e d b
for -
. if readln -
.
, -
-
<Enter>.
for i:=l to 50 do
begin
writeln(i);
if i mod 24=23 then readln;
end;

:
() for -
,
;
-
-. for
, :
;
- : integer ,
char, boolean (.
. 2.1.3). ;
,
,
102______________________ I. Turbo Pascal

.
;
for
. for ,
;
for ( )
.
, repeat
while.
, , -
"" for i 2
( -
).
for i:= 1 to 10 do
begin
write(i:4); i := i + 1;
end;

1 3 5 7 9.
.

9, 10: for i := 1 to 9 do, -
"", . .
i -
9 ( 8, 10).


goto -
break. -
repeat while. continue

,
().

, :
. . .
for I := 1 to 45 do
begin
f:=f +i;
if (f>100)or(i=39) then break;
end;
3. _________________________________________________103

3.27 " ", -


, ,
. -
,
for.

3.27 ( 4)
uses crt;
var sl,s2,otvet,k,prav: integer;
begin
randomize; { }
clrscr; { }
for k:=l to 5 do
begin
s1:=random(18)+2; s2:=random (18)+2;
write(' ',s1,' * ',s2,'? '); readln(otvet);
if otvet=sl*s2 then
begin
write('! '); prav:=prav+l;
end
else write('...');
end;
clrscr; ( }
writeln(' ; ',prav); readln;
end

3.28 , -
-
, .
3.28. ,
var
s: real; { }
n.: integer; { }
sum,average: real;{ }
min,max: real; { )
i: integer; { }
begin
write{' : '); readln(n);
writeln(' ');
write('->'); readln{a); { }
104____________________ I Turbo Pascal

{ , : }
min:=a; { }
m:=; { |
sum:=a;
{ }
for i:=l to n-1 do
begin
write('->'); readln(a);
sum:=sum+a;
if a < min then min:=a;
if a > max then max:=a;
end;
average:=sum/n;
writeln(' : ',);
writeln(' :',average:7:3);
writeln(' : ' ,min:7 :3) ;
writeln!' :',max:7:3);
end.


-
, , ,
, -
, ""

, ,
,
( 4)

3 29 ,
n 1, 1/3, +1/5, 1/7, + ., p/4.

3.29.
var
: real; ( }
summ: real; { }
n: word; { }
i: word; { }
begin
write(' - : '); readln(n);
:=1; summ:=0;
for i:=l to n do
begin
summ:=summ+l/x;
3. __________________________________________________105

if x>0 then x:=-(x+2) else :=-(-2);


end;
writeln (' : ',summ: 10: 6);
writeln(' pi/4:',pi/4:10:6);


,
mod, -
(. 3.6).
:
. . .
summ:=0;
for i:=n downto 1 do
begin
x:=l/(2*i - 1);
if (I mod 2) = 0 then x:=-x;
summ: =summ+x ;
end;
. . .

i,
.
-

, .
-
-
(. . 2.1,3). "" , ,
, -
-
. ,
. "" -
,
. , -
, , .
10 000 :
7.85373163397708-0001 { for..downto };
7.85373163409531E-0001 { for..to };
7.85398163397448-0001 { pi/4 }.

-
. ,
. 3.30 ,
-
106_________________ 1. Turbo Pascal

,
()=2.

3. 30. ( 1)

var
a,b: real; { }
h: real; { }
sl,s2,s3: real; { a }
n: integer; { }
,1,2: real; { }
i: integer; { }
begin
writeln(' ');
write(' : '); readln();
write(' : '); readln(b);
write(' : '); readln(n);
h:=(b-)/n;
1:=a+h/2; 2:=;
s1:=3*x1*1;
s2:=((3*2*x2)+(3*b*b))/2;
for i:=1 to n-1 do
begin
1:=1+h; 2:=2+h;
s1:=s1+(3*1*x1); s2:=s2+(3*2*2);
end;
s1:=h*s1; s2:=h*s2; s3:=(2*s1+s2)/3;
writeln('3 :');
writeln(' ');
writeln(' s1 = ',s1:8:5,'; s2 = ',s2:8:5, ; s3 = ',s3:8:5);
end.



xx .

.
, (. 5.9).

3.31 , * (-
)
sin()/(). -
. ; -
.
3. _________________________________________________107

3.31. sin(x)/exp(ax)
*

uses crt;
const offset=40; { -
}
scale=15; { )
degreestep=40; ( }
alpha=0.15;
var i: integer; k: real;
begin
clrscr; ( }
{ }
k:= degreestep*pi/180;
for i:= 0 to 23 do
writeln!'*':round(offset+scale*sin(k*i)/exp(alpha*k*i)));
readln;
end.

3.9.4.
.
, , , , -
().
, .
-
: -
.
,
. for
:
va i, j : byte;
begin
for i:=l to 10 do
for j:=l to 10 do
writeln(i,' * ',j,' = ',i*j);
end

- (. 3.5) ,
-
, .
, -
108____________________ I. Turbo Pascal

. , -
, -
for .

. 3.5.

, ,
" ", .
writeln i
.
3.32 , -
.

3.32.

var
i,j: integer; { }
begin
for i:=l to 10 do
begin
for j:=l to 10 do write(i*j:5);
writeln; { )
end;
end.
3. _________________________________________________109

3.33 , :
ox { = 00 + = 0x10101 + 1010 }
- { = 10101 + 1010 )
=a { = A0A0A0 + 00 = 101010 + 10101 }

, , X .

3.33.

var ,,,ox,ax: longint;


begin
for o:=l to 9 do { , }
for a:=l to 9 do { 1 9 }
for :=0 to 9 do { 0 9 }
begin
:=*10101+*1010;
:=*10101+*1010;
if (+)=(*101010+*10101) then
writeln(ox,'+',,'=',+);
end;
end.


. 9x9x10=810
: 90909+10101=101010.

3.34 ,
n.


, , -
, 6=1+2+3. 24 -
(6, 28, 496, 8128...). .
.

3.34. , n
var i,j,n,m: integer;
begin
write (' : ') ; readln(n) ;
for i:=2 to n do { }
begin
m:=0; { }
110___________________ I. Turbo Pascal
{ : }
for j:=1 to i-1 do
{ i- j, j ,}
{ m j }
if i mod j=0 then m:=m+j;
if m=i then writeln(i);
end;
end.

3.35 ,
: (1/1)n + (1/2)n + ...+(1/n)n.

3.35.
var n,i,j: integer; a,s,p: real;
begin
write(' : '); readln (n);
s:=0; { }
for i:=l to n do { }
begin
a:=l/i; p:=a;
for j:=2 to n do p:=p*a; { }
s:=s+p; { }
end;
writeln(' : ',s);
end.

3.36 , -
f() = (- 1)2(1)3 [0,3; 0,5] 10-7.

3.36

const eps=le-7; { }
var a,b,x0,xmin,y,ymin,x,h: real;
begin
write(' : '); readln(a,b);
write(' : '); readln(h);
0:=; { }
while h>=eps do
begin
ymin:=le7; x:=x0;
repeat
y:=x*sqr(x-l)*sqr(x-2)*(x-2);
if y<ymin then
3. ______________________________________________111

begin
ymin:=; xmin;=;
end;
:=x+h;
until (y>ymin)or(x>b);
x0:=xmin-h; h:=h/2;
end;
writeln('xmin = ',xmin:8:5,'; ymin = ',ymin:8:5);
end.


.
while .
,
.
x0:=xmin-h, h:=h/2
.
.
y>ymin, . . -
, .

1912 " " 48 (


) 13 ( ). 3.37 ,
" " 1912 .,
* () _ ( ).
, , .

3.37. " "

uses crt;
va row,col: integer;
begin
clrscr; { }
for col:= 1 to 19 do write('__'); { }
writeln;
for row:= 1 to 13 do
begin
for col:= 1 to 19 do
{ 1 6 1 8 48 }
{ }
if (col<9)and(row<7) then write ('* ') else write ('__');
writeln;
end;
readln;
end
112____________________ I. Turbo Pascal

3.9.5. ,
3.38 , :
( 10 ), ( 5 )
( 0,5 ), 100 100 ?

3.38;

var b,k,t: integer;


begin
for b:=0 to 10 do { }
for k:=0 to 20 do { }
begin
t:=100-(b+k); { }
if 20*b+10*k+t=200 then { }
begin
writeln(' ',b:3);
writeln(' ',:3);
writeln(' ',t:3);
end;
end;
end.


2 3 :
10b+5k+0,5t = 100 b+k+t = 100. ,
2 : 20b+10k+t = 200.
. : -
100 -
10 20 200 .
, ( ) -
.
b k .
, , -
: 19b+9k = 100 -
:

for b:=0 to 10 do
for k:=0 to 20 do
if 19*b+9*k=100 then
begin
writeln(' ',b:3);
writeln(' ',k:3);
writeln(' ',100-(b+k):3);
end;
3. _______________________________________________113


.
,
.

3.25 , -
, -
while, if case. ,
, ( 3.39).

3.39. ( 2}

var n: 0..3999;
k: (M,CM,D,CD,C,XC,L,XL,X,IX,V,IV, I) ;
s: integer;
begin
write(' : '); readln();
write(': ');
for k:=M to I do
begin
case k of
M :s:=1000;CM:s:=900;D :s:=500;
CD:s:=400; :s:=100;XC:s:= 90;
L :s:= 50; XL:s:= 40;X :s:= 10;
IX:s:= 9; V :s:= 5;IV:s:= 4;
I :s:= 1
end;
while n-s>=0 do
begin
n:=n-s;
case k of
M :write('M'); CM:write('CM');D :write('D');
CD:write('CD');C :write('C); XC: write {'XC );
L :write('L'); XL:write('XL');X :write('X');
IX:write('IX') ;V :write('V); IV:write ('IV ) ;
I :write('If)
end;
end;
end;


0
3999, n
. k .
4

-
,
.
,
, i1,
i2..., i100, .
,
i, , .

.
,
( , -
).
, (-
).
, -
b ,
.
(. . 12).
:
;
() ;
() -
.
( ) , -
.
4. _________________________________________________115

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


-
(), .
:
, . .

() , ,
.
integer, char boolean.
-
. , [3], b [ 1 , 2 ].

. , -
, , -
.
.
,
:
[ i ] ;
a[2*i] , ;
a[2*i-1] , .

4.1.
4.1.1. VAR

var
array (. . ). :
:
var : array[.. ]
of ;

:
var : [1..100] of integer;{ 100 }
b: array[0..50] of char; { 51 - }
: array[-3..4] of boolean; {8 - }
116______________________ I. Turbo Pascal

:
var : [1.. 1,
2.. 2]
of ;

, :
1 2 3 4
5 6 7 8
9 10 11 12

(integer), -
.
( ). 1 -
1 3. 2 1
4.
. -
, . , -
. ,
:
var : array[1..3,1..4] of integer;

,
, -
, ( ) z, a
( ) ,
.
var count: array ['a'..'z']of integer;


.
, 100 ( , real)
100 600 . -
20 30 , 600
, 255,
600 ,
byte (600 ).

, . .
, ,
.
4. ________________________________________________117

4.1.2.
Turbo Pascal , -
64 ,
. , .
3 , -
, :
var huge: array[1..105,1..105] of real;

Error 22: Structure too large ( 22:


).
byte shortint (. . 2.1), , -
,
.

4.1.3.

.
, .

(. . 2.2.4), -
. , . .

,
.
, matrix maxrow
maxcol :
const
maxrow=10; maxcol=15;
var
m,n: integer; { )
matrix: array [1..maxrow, 1..maxcol] of real;

4.1.4.

-
.
.
118 I. Turbo Pascal

:
const x: array[1..5] of integer=(l,3,5,7,9);
: array[1..2,l..3] of integer=((1,3,5),(2,4,6));

:
1 3 5
2 4 6

, -
.
,
.
:
, ,

;
, "-
" .

4.1.5.
var -
. ""
. -
(. . 2.2.5).
, ,
(. -
5.17).
:
type
= array[ .. ]of ;
var
: ;

:
type
= array[1..1,
2.. 2]
of ;
var
: ;
4. _________________________________________________________ 119

, matrix matr
maxrow maxcol :
const
maxrow=10; maxcol=15;
t
matr = array [1..maxrow, 1..maxcol] of real;
var
matrix: matr;

, -
, -
.
, supply energy
days hour
:
t
days= (monday,tuesday,wednesday,thursday,friday,saturday,sunday);
hour=l..24;
energy=array[days,hour] of real;
var supply: energy;

supply [ Friday, 18] -


, 17.00 18.00 (
18- ).

4.1.6.
:
:
a: array [] of real;

:
b: array [10..1] of integer;

, :
: array [l..a + b] of real;

:
d : array [1.0..100.0] of integer;

.
, [3]:=1; 3 ()
, 1 , .
120___________________ I. Turbo Pascal

:
:
var f: array [1..10] of real;
. . .
...f[5.0] ...

:
var g : array [1..100] of real;
. . .
...g[l,4] ...

:
,
;
I, . . -
, .
.
, var x: array[0. .5]of integer; 6 -
, - . -
;
-
, {$R+}.
,
, .
, var a: array [1. .100] of integer;,
200
[200], ;
, , -
for, . .
;
, , . . -
, -
.
, b :
type mas = array[1..100] of integer;
type vec = array[1..100] of integer;
var a,b: mas; c: vec;

a:=b;. -
b. :=;
4. _________________________________________________121

Error 26: Type mismatch ( 26:


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

4.2.
4.2.1.
, , .
, , -
, - .
:
(. 4.1);
(. 4.2);
;
(. . 12).
. -
for,
.
, "", , :
5 :
for i:=l to 5 do readln(a[i]);

3x2 ( 6 ):
for i:=l to 3 do
for j:=1 to 2 do
readln(a[i,j]);

-
(. 4.2).
, -
.
.
, n
0 99 -
:
randomize; { )
for i:=l to n do x[i]:=random(100);
122____________________ I. Turbo Pascal

-
"" (.
5.16).

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

4.2.2.
for -
write writeln.
, 5 :
:
For i:=l to 5 do writeln(a[i]);

, -:
for i:=l to 5 do write(a[i],' ');

, 4 :
for i:=l to 5 do write(a[i]:4);

-
writeln; ( ). -

:
for i:=l to n do
begin
for j:=l to m do
write{a[i,j]:4);
writeln;
end;



.
4. _________________________________________________123

4.2.3.
, -
oe -
, . .

, .


, .
:
s:=0;
for i:=l to n do s:=s+a[i]; { s}

:
S:=1
for i:=l to n do s:=s*a[i]; { s )

, - .
, :
k:=0;
for i;=l to n do
if a[i] mod 2=0 then k:=k+l; { 1 }
{ , 2 }

. -
(. 4.1).
, , -
. , -
.
i:=0; ( }
repeat
i:=i+l;
until(a[i]=0) { } or (i=n) { };
if a[i]=0 then writeln(' = ',i)
else writeln(' ');


, , -
, repeat. ,
, 1 -
124______________________ I. Turbo Pascal

i. , -
,
, repeat for. -
for. -
:
writeln(' , :');
for i:=l to n do
if a[i]=0 { 0 } then write{i,' '); { }

(. 4.2).
max , k :
m:=[1]; :=1; { }
for i;=2 to n do { , }
if a[i]>max then
begin
max:=a[i]; k:=i; { , }
{ }
end;

, a[i]<min -
min.


, -
, k -
. a [i] >=max,
. -
, -
,
.

.
, n . -
, ,
minzp.
minzp:=3000;
for i : = l to n do
if a[i]<minzp then a [i]:==minzp;

4.1 , ,
, , -
.
.
4. __________________________________________________125

4.1. ,

const count=10;
var n, { }
a, { }
b, { }
i: integer;
m : array [1..count] of integer;
begin
writeln(' :');
for i:=l to count do
begin
write(' # ',i,': ');
readln(m[i]);
end;
a:=0; b:=0;
write {' > '); readln(n);
for i:=l to count do ( , n }
if m[i]=n then
begin
if b=0 then a:=i;{ 1- , }
b:=b+l; { 1}
end;
if b=0 then writeln(' ')
else
begin
writeln(' ,
? ,n, ' =, b:3);
writeln(' ', :3);
end;
end

4.2 ,
, , -
.

4.2.

const count=10;
var m: array [1..count] of byte;
max,i,numer_max: byte;
begin
{ random(count*2) }
{ }
126_______________________________ I. Turbo Pascal

randomize; { }
for i:=l to count do
begin
m[i]:= random{count*2)+l;
write(m[i],' ');
end;
writeln;
max:= m[1]; { }
numer_max: =1 ;
{ , }
for i:=2 to count do
begin
{ }
if m[i]>max then
begin
{ max }
max:=m [i];
{ }
numer_max:=i;
end;
end;
write (' ', max);
writeln{' ', numer_max,' ');
end.


, n m .
.
d, n
:
for i:=1 to n do
begin
s:=0;
for j:=l to m do s:=s+a[i,j];
d[i]:=s;
end;


,
j ( ), i ( -
).
4. _______________________________________________127

.
min -
, k , l , :
min:=a[l,l]; k:=l; 1:=1;
for i:=l to n do
for j:=1 to m do
if a[i,j]<min then
begin
min:=a[i,j];
k:=i; l:=j;
end;

, -
y:
for i:=l to n do
begin
s:=0;
for j:=1 to m do
s:=s+a[i,j]*x[j];
y[i]:=s;
end;

4.3 ,
. -
,
, , . -
.


,
: 11, 22,... .

4.3.

uses crt;
const row=3; { ]
col=row; { }
var a: array[1..row,1..col]of integer;
b,c: array[1.,row*col]of integer;
i,j,n,m: integer;
begin
clrscr; writeln(' ');
128___________________ I. Turbo Pascal

for i:=l to row do


begin
write(' ',i, ' ') ;
for j:=l to col do read(a[i,j]);
readln;
end;
n:=0; { )
m:=0; { }
for i:= 1 to row do
for j:= 1 to col do
if j>=i then { }
begin
n:=n+l; { }
b[n]:=a[i,j];{ b n}
end
else { }
begin
m:=m+l; ( }
c[m]:=a[i, j];( m }
end;
writeln{' :');
for i:= 1 to n do write(b[i],' '}; writeln;
writeln{' :');
for i:= 1 to m do write(c[i],' '); writeln;
readln;
end.


, <Enter>.


. -
,
.
, -
-
.
, , -
buf.
buf:=a[l]; a[l]:=a[2]; a[2]:=buf;
4. ________________________________________________129


.
, , , , -
. -
, . . -
.
for j:=1 to m do
begin
buf:= a[1,j]; a[l,j]:=a[2,j]; a[2,j]:=buf;
end;


. -
,
. , -
,
. ,
.
4.4 ,
.

4.4.

const row=3; col=row;


var a: array [1..row, 1..col] of integer;
i,j,buf: integer;
begin
randomize; { }
writeln(' :');
for i:= 1 to row do
begin
for j:= 1 to col do
begin
a[i,j]:=random(100);{ }
write(a[i,j]:4); { }
end;
writeln;
end;
{ }
for i:=l to row do { }
{ , }
for j:=d+l to col do
130_____________________ I. Turbo Pascal

{ , }
begin
buf:=a[i,j]; a[i,j]:=a[j,i]; a[j,i]:=buf;
end;
writeln(' :');
for i:= 1 to row do
begin
for j:= 1 to col do write(a[i,j]:4);
writeln;
end;
end.


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

, . .

;
, -
;
""
, ,

.
-
:
4. ___________________ ________________________131

, , -
.

m 20 . -
. -
, -
( ). -

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

4.5.
Const count=20;
m: array [1..count] of byte =
(9,11,12,3,19,1,5,17,10,18,3,19,17,9,12,20,20,19,2,5);
var i,j,buf,l: byte; a: integer;
begin
writeln(' :');
for i:=l to count do write(' ', m[i]);
writeln; readln;
a:=0; { }
{ }
for i:=l to count-1 do
{ i- }
{ i+1- }
for j:=i+l to count do
begin
a:=a+l;
if m[i]<m[j] then { }
{ , , i-, }
begin
buf := m[i]; { buf }
m[i] := m[j];
m[j] := buf;
end;
132_____________________ I. Turbo scal

for 1:=1 to count do write(' ', m[l]); {


writeln ('; # ',a);
end;
end.

,
190 .
""
""
, "" -
"". -
, , , (-
4.6).

4,6. ""

const count=20;
m: array [1..count] of byte =
(9,11,12,3,19,1,5,17,10,18,3,19,17,9,12,20,20,19,2,5);
var i,j,buf,l: byte; a: integer;
begin
writeln(' :');
for i:=l to count do write{' ',m[i]);
writeln; readln;
a:=0;
for i:=2 to count do
begin
for j:=count downto i do
begin
a:=a+l;
if m[j-l]<m[j]then{ }
{, "" ""}
begin
buf:=m[j-l]; { }
m[j-l] :=m[j];
m[j]:=buf;
for 1:=1 to count do write(' ',m[l]);
writeln(; # ',a);
end;
end;
end;
end.
4. _________________________________________________133


for j
, , ,
"" , ,
("" ). -
, . .
.
for -
i. ,
170 -
.


, -
. . , -
" ". 3

.
-
(. . 5, 6), -
( 4.7).

4.7.
uses crt;
const count=20;
m: array [1..count] of byte =
(9,11,12,3,19,1,5,17,10,18,3,19,17,9,12,20,20,19,2, 5);
var i,a: integer;
prccedure quicks(first,last:integer);
var i, j ,x,buf, l: integer;
begin
i:=first; { }
j:=last; { }
x:=m[(first+last) div 2]; { }
repeat
while m[i]>x do i:=i+l;
while x>m[j] do j:=j-l;
{ }
:=+1;
if i<=j then
begin { }
buf:=m[i]; m[i]:=m[j]; m[j]:=buf;
134______________________ I. Turbo Pascal
i:=i+1; j:=j-1
for 1:=1 to count do write(' ',m[l]);
writeln('; # ',a);
end; ,
until i>j;
{ quicks }
if first<j then quicks (first, j) ;
{ quicks }
if i<last then quicks(i, last);
end; { }
begin { }
clrscr; { }
writeln(' :');
for i:=l to count do write (' ',m[i]);
writeln; readln;
a:=0;
quicks(1,count); { quicks }
writeln; writeln (' : ') ;
for i:=l to count do write (' ',m[i]);
writeln; readln;
end.

, -
28 .



("")
, "" (. -
4.1). , . -
( )
.
, -
(
).
-
, .

. ,
, , -
, . . .
, ,
4. ___________________________________________135

,
.
4.8 , -
.

4.8.

const count=20;
m: array[1..count] of byte =
(20,20,19,19,19,18,17,17,12,12,11,10,9,9,5,5,3,3,2,1);
var n, i, first, last: byte;
a: integer;
found: boolean;
begin
writeln( ' : ') ;
for i:=l to count do write(' ',m[i]); writeln;
write (' : '); readln (n);
a:=0; first:=1; last:=count;
found:=false; { }
repeat { }
i:=(first+last) div 2; { }
if m[i]=n then found:=true
else
if m[i]>n then first:=i+l { }
else last:=i-l; { }
:=+1; { }
{, }
until (found)or(first>last);
if found then
writeln{' ',n,' ',i,' - ')
else writeln(' ...',);
writeln{' ',,' '); readln;
end


, -
. , , ,
.
-
"", , ,
, .
.
136_____________________ I. Turbo Pascal

4.9 , ,
.
(" ") .

4.9.

const count=10;
m: array [1..count] of byte=(33,43,2,33,90,78,60,33,33,21);
var x, { , }
j, { }
i, { }
n: byte; { }
begin
writeln(' :');
for i:=l to count do write{' ',m[i]); writeln;
write(' : '); readln(x);
n:=count; j:=0; i:=0; { }
repeat { }
i:=i+l;
while m[i]=x do
begin { , }
j:=i; { 1
{ }
repeat
m[j] :=m[j+l]; j:=j+l;
until j>=n;
n:=n-l; { }
end;
until i>=n;
for i:=n+l to count do m[i]:=0; { '' }
writeln(' :');
{ '' , count }
for i:=l to count do write(' ',m[i]); writeln; readln;
end.


-
repeat, -
i-ro .
a[i]=x , , i-ro,
. i- i+1- . .
, n:=n-1 -
1.
4. ________________________________________________137


, -
. .
(. 4.1).

1 2 3 4
1 5 2 0 10
2 3 5 2 5
3 20 0 0 0

. 4.1.

(. 4.2) ,
.

1 1,5 0,2
2 2,8 0,4
3 5 1
4 2 0.5

. 4.2.

(. 4.3):

1 5x1,5 + 2x2,8 + 0x5 + 10x2 = 33,1

2 3x1,5 + 5x2,8 + 2x5 + 5x2 = 38,5


3 20x1,5 + 0x2,8 + 0x5 + 0x2 = 30

. 4.3.

, . 4.4.
-
, . 4.5.
,
, , , -
, .
138_____________________ I. Turbo Pascal

1 5x0,2 + 2x0,4 + 0x1 + 10x0,5 = 6,8

2 3x0,2 + 5x0,4 + 2x1 + 5x0,5 = 7,1


3 20x0,2 + 0x0,4 + 0x1 + 0x0,5 = 4

. 4.4.

1 2 3 4 1 2 1 2
1 5 2 0 10 1 1.5 0,2 1 33,1 6,8
2 3 5 2 5 2 2,8 0,4 2 38,5 7,1
X =
3 20 0 0 0 3 5 1 3 30 4
4 2 0,5

. 4.5.

4.10 , "-
-" "-",
.

4.10. ( 1)
uses crt;
const mmax=5; nmax=5; { . - }
type massiv=array[l..mmax,l..nmax] of real;
var a,b,c: massiv; { }
m1,n1,m2,n2: integer; { }
i,j,k: integer;
begin
clrscr;
writeln(' -'};
repeat
write{' - <=',mmax,': '); readln(m1);
write{' - <=',nmax,': '); readln(nl);
until (ml<=mmax)and(nl<=nmax);
for i:=l to ml do
for j: =1 to nl do
begin
write('- ',j,' ',i, ' ');
readln(a[i,j]);
end;
writeln(' -');
{ [ 1) }
4. _____________________________________________________139
for i:=l to ml do
begin
for j:=l to nl do write(a[i,j]:7:3); writeln
end;
writeln;
writeln(' -');
m2:=nl; { }
2:=2; { 2 }
for i:=l to m2 do
begin
write (' ',i,' ');
for j:=l to n2 do read(b[i,j]); readln;
end;
writeln{' -');
{ ( 2) }
for i:=l to m2 do
begin
for j:=l to n2 do write(b[i,j]:7:3); writeln
end;
writeln;
{ }
for i:=l to ml do
for j:=1 to n2 do
begin
c[i,j):=0.0;
for k:=l to nl do c[i,j]:= c[i,j]+a[i,k]*b[k,j];
end;
writeln(': '};
{ ( 3) }
for i:=l to ml do
begin
for j:=l to n2 do write |c[i,j]:7:3); writeln
end;
end.


mmax nmax , ,
-
, const.

,
, . , -
13, -
140______________________ I. Turbo Pascal

.
(. . 5.2).
5.17 -
/ .

4.2.4. ,

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

4.11.

const a: array [1..12] of integer =


(31,28,31,30,31,30,31,31,30,31,30,31);
{ }
b: array [1..12] of string[10] =
('', '', '',
'', '', '',
'', '', '',
'','', '');
{ }
var day,month,year: integer;
ok: boolean;{ }
begin
writeln(' (, , )');
readln{day,month,year);
if (day<=0)or(month<=0)or(year<=0)or(month>12) then ok:=false
{ , , }
{ - }
else
begin { }
if(month=2)and(year mod 4=0) then a[2]:=29;
{ )
if day>a[month] then ok:=false { }
else ok:=true;
end;
if not ok then writeln(' ')
else write(day,' ',b[month],' ',year,' ');
end.
4. ______________________________________________141


-
.

4.12 , ,
(. . 2.2.6) case (. . 3.8.3), -
-
, .

4.12.
type days=(monday,tuesday,wednesday,thursday,friday,saturday,sunday) ;
var taux: array[monday..sunday] of real;
day,j: days;
k: integer; sum: real;
begin
taux[sunday]:=0; day:=monday;
repeat
write(' ');
case day of
monday : writeln('B '};
tuesday : writeln(' ');
Wednesday: : writeln(' ');
thursday : writeln(' ');
friday : writeln('B ');
Saturday : writeln(' ')
end;
readln{taux[day]);
{ }
day:=succ(day) ;
until day=sunday;
writeln (' '); readln(k);
day:=days(k-1); { }
sum:=0;
for j:=day to saturday do sum:=sum+taux[j];
writeln(' ', sum:7:3); readln;

4.13 ,
. , -
2,
, .
.
142______________________ I. Turbo Pascal

4.13.

var a: array[1..20] of 0..1; { }
, { , }
, { - }
1 { ): integer;
begin
writeln{' '); readln();
l:=0; { }
while n>0 do { 2 (0 1)}
begin
1:=1+1;
[1]:=n mod 2;
n:=n div 2;
end;
writeln(' :');
{ }
for k:=l downto 1 do write(a[k]);
end.
5

5.1.

, ,
,
.

.
(. . 6.1).
, -
,
, .
,
( -
), -
.
, ,
. -
. , -
. ,
, .
:
, ,
, ;

, -
144_______________________ I. Turbo Pascal

, , -
;
-
.
, , ;
.
, ,
,
.
-
(,
. .) Turbo Pascal -
, -.

5.2.
.

Turbo Pascal
. , -
. -
: -
.
()
. , -
, -
. 3.2.
. -
-
Turbo Pascal, -
(. 5.3).
-
(.
. 2.2.7).
. ,
.
-
(. . 5.4).
, -
-
5. _____________________________________145_

.
.
, "
" .
.
procedure,
() , ,
:
procedure () ;

, (. -
5.1):
procedure horline;

, (. 5.2):
procedure horline(len:integer; s:char); (len,s }

, . . -
.
:
procedure ();
{ }
begin
{ }
end;

, , end,
.
.
,
. -
, .
();

:
horline; { , . . }
{ }

horline(10,'-'); ( }
146_____________________ I. Turbo Pascal

,
. -
-
-
. -
.

.
- , -

var (. . 5.4).

.
, , -
.
, , . .
.
(. . 5.5).
-
exit, ( -
).
5.1 ,
3
, .

5.1.

var inch: integer; { }


sm: real; { }
{ }
procedure horline;
var i: integer;
begin
for i:=l to 30 do write('-'};
writeln;
end;
{ }
begin
horline;
writeln('| | |');
horline;
5. _______________________________________________147

{ }
for inch:=l to 10 do
begin
sm:=2.54*inch;
writeln ('|', inch: 13, '|', sm:14:3, '|' );
end;
horline;
end.



. -
.

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

5.2. ,

procedure horline(len:integer; s:char);


{len , s , }
va i: integer;
begin
for i:=l to len do write(s);
writeln;
end:
{ }
begin
horline (10,'-');
horline(20,'*');
horline(30,'#');
end

,
(, Turbo Pascal
). ,
, ,
: 1 =210 , 1 =220 , 1 =230 (-
5.3).
148__________________ I. Turbo Pascal

5..

procedure degree{x: real; n: byte; var res: real);


{ n,}
{ - res }
var i: integer;
begin
res:=1;
for i:=l to n do res:=res*x;
end;
{ }
var kb,mb,gb: real;
begin
degree(2,10,kb);
degree(2,20,mb);
degree(2,30,gb);
writeln ('l Kb = ',kb:4:0, ' byte') ;
writeln(' 1 Mb = ',mb:7:0,' byte');
writeln('l Gb = ',gb:10:0,' byte');
end.


, . . -
(. . 5.3).

3.2 ,
m s n p.

, , , p -
s, m n
( 5.4).

5.4. , ,
const eps: real=le-06;{ }
var
s,m,ml,r,percent: real;
n,rub,kop: integer;
procedure formula(var m: real; n: integer; s,r: real);
var a: real;
5. ____________________________________149
begin
:=(ln(l+r)*n); { }
n:=s*r*a/{12*(a-l});
end;
procedure rub_kop(money: real; var r,: integer);
begin
r:=round(money*100) div 100; k:=round(money*100) mod 100;
end;
{ }
writeln(' ,, : ');
readln(s,m,n) ;
r:=0.1; { }
repeat
formula(m1,n,s, r) ;
:=r*m/ml;
until abs(m/ml-l) < eps;
percent:=r*100;
writeln;
rub_kop(s,rub,kop); writeln(' : ',rub,' . ',kop,' .');
rub_kop(m,rub,kop); writeln('c.: ',rub,' . ',kop,' .');
writeln(' : ',:3);
writeln;
writeln(': ',percent:5:2,'%'); { }
end

r, -
. r, -
, m1.
m1 m r . m1 , -
r , r, -
m/m1, . m1 ,
r , m/m1,
, m. ,
m m1 , r m/m1 .
r -
.
, -
( ) .
, , , ,
.
rub_kop.
150______________________ I. Turbo Pascal

1202 . , -
, . -
( ),
.
, -
? , ,
, 1, 1, 2, 3, 5, 8, 13, 21,
34, .... , , -
. -
, . -
. : F0 = F1 =1;
Fn = Fn-1 + Fn-2 .
5.5 ,
.

5.5. ( 1)

procedure fibon(n: integer);


{ }
var fn,fnl,fn2,k: integer;
begin
fnl:=l;
fn:=0;
for k:=l to n do
begin
fn2:=fnl;
fnl;=fn;
fn:= fnl+fn2;
writeln (fn);
end;
end;
{ }
var n: integer;
begin
write(' : '); readln(n);
fibon{n); { }
end.


, ,
. -
.
,
5. _______________________________________151_

, : F-1 = 1, Fo = 0. -
, , . -
.
, , 24
integer (. . 2.1).

5.3.
,
, -
. , .

, .
. -
.
, , -
. function, ,
-
, ,
:
function () : ;

:
function fibo(n: integer): integer; { n }
function instep(a,b: real): real; { a,b }
function normrandom : double; { }

.
:
function () : ;
{ }
begin
{ }
:= ;
end;


, , -
. -
,
.
, , , .
152_____________________ I. Turbo Pascal

,
. -
.
-
. ,
.
:
writeln(fibo(i)); { }
c:=instep(2,8); { }
write(normrandom:10:3); { ,. . )
{ }

, 5.5, -
. 5.6 -
.

5.6. ( 2)
function fibo(n: integer): integer;
{ }
var fn,fnl,fn2,k: integer;
begin
fnl:=l;
fn:=0;
for k:=l to n do
begin
fn2:=fn1;
fnl:=fn;
fn:= fnl+fn2;
end;
fibo:= fn; { }
end;
{ }
var i, n: integer;
begin
write{' : '); readln(n);
for i:= 1 to n do writeln(fibo(i)); { }
end.


, (. -
5.5). ( 5.6) -
, "". , -
5. _____________________________________153

.
, -
6.3.

Turbo Pascal random -


,
(. . 3.3.1). , -
random .
, -
. , 1,5 2 -
. random ,
. , 1,75 .
, 1,5
2 . -
. 5.7 , -
-
.

5.7.

uses crt;
var i: integer;
{ }
function normrandom: real;
var s: real; i: byte;
begin
s:=0;
for i:=l to 12 do s:=s+random;
normrandom:=s-6.0;
end;
{ }
begin
clrscr;
randomize; { }
for i:= 1 to 192 do { : 1
write(normrandom:10:3); { 8 24 }
end.

, Turbo Pascal -
(. . 3.3.1).
. 5.3 -
, . -
5.8 -
.
154_____________________ I. Turbo Pascal

5.8.

function instep(a,b: real): real;


{ }
begin
if a>0 then instep:= exp(b*ln(a)) else instep:=0;
end;
{ }
var a,b,c: real;
begin
writeln(' :');
readln(a,b);
c:=instep(a,b);
if <> 0 then
writeln{a:6:3,' ',b:6:3,' = ',:6:3)
else
writeln(' ...'};
end.

(. 3.30)
-
, ()=32.
-
( 5.9).

5.9. ( 2)

var
a,b: real; { }
h: real; { }
sl,s2,s3: real; { }
n: integer; { }
x,xl,x2: real; { }
i: integer;
{ }
function f(x: real): real;
begin
f:= 3*x*x; { }
end; { }
{ }
begin
write(' : '); readln();
write{' : '); readln(b);
write(' : '); readln();
5. ____________________________________155

h: = {b-a)/n;
xl:=a+h/2; 2:=a;
sl:=f{xl); { f(x) }
s2:=(f(x2)+f(b))/2;
for i:=l to n-1 do
begin
xl:=xl+h; x2:=x2+h;
sl:=sl+f{xl); s2:=s2+f{x2);
end;
sl:=h*sl; s2:=h*s2; s3:=(2*sl+s2)/3;
writeln(' :');
writeln('s1 = ',sl:8:5,'; s2 = ',s2:8:5,'; s3 = '(s3:8:5);


, -
f, , 3.30 -
f (x). -
() ,
,
.
-
(. 6.7).

3.10 -
. (-
5.10).

5,10. ( 2)

function kvadrur(a,b,c: real; var xl,x2: real): integer;


{ }
{a,b,c }
{x1,x2 }
{ }
{-1, }
var d: real; { }
begin
if a=0 then kvadrur:=-l
else
begin
d:=b*b-4*a*c;
if d<0 then kvadrur:=0 { }
else
begin
156_______________________________ I. Turbo Pascal

if d>0 then kvadrur:=2 { }


else kvadrur:=l; { }
xl:=(-b+sqrt(d))/(2*a);
x2:=(-b-sqrt(d))/(2*a);
end;
end;
end;
{ }
var
a,b,c: real; { }
xl,x2: real; { }
begin
writeln(' , b, '); readln(a,b,);
case kvadrur{a,b,c,xl,x2) of
-1: writeln{' ');
0: writeln(' ');
1: writeln{'x=',xl:7:3,', ');
2: writeln('xl=',xl:7:3,'; 2=',2:7:3);
end;
end.


.

: 2 , 1 ,
0 . ,
. , -1.
kvadrur ,
x1,x2 -
-: var xl,x2: real; (. . 5.4).

-
, . -
, -
.
5.11 , -
f(x) = 4 x 22 = 0 ().

5.11 .
function f(x: real): real;
begin
f:=4-exp(x)-2*x*x; { }
end;
5. ______________________________________157
{ }
var a,b,fa,fc,c,eps: real;
begin
write(' b: ');readln(a,b);
write(' : '); read{eps);
fa:= f(a); { f }
while b-a > eps do
begin
c:=(a+b)/2; fc:=f(c); { }
if fa*fc <= 0 then b:=c
else
begin
a:=c; fa:=fc;
end;
end;
writeln ('f = ',f (a) :10:7,' =', a: 10:7);
end.



2
f(x)=0. f(x) kx+b x +b+ (. 5.10).
, -
, , -
.
. . -
, f{x) [, b} f(a)f(b)<0, f(x)=0
[, b]. ,
, [, ] [, ] -
, . .
, ; b-a>eps.
, b-a<eps, -
(, ). ,
.
. ,
, ,
f(a) f(c)<=0.

5.4.
, -
,
, . "-
" ,
-
, -
.
158______________________ I. Turbo Pascal

:
-, ;
-,
var ;
-,
procedure (. . 6.2.4);
-,
function ,
(. . 6.2.4);
,
var . -
,
(. . 6.2.5).
-
. -
,
. ,
.
:
function kvadrur (a,b, : real; var xl,x2: real): integer;

:
-
;

;
-
.

5.4.1. -
-
.
:
procedure horline(len:integer; s:char); { len,s -}

-, -
. -
-
5. _____________________________________159

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

-
.
.
, :
procedure input_array(m,n: integer; var mas: array[1..5,1..5]of real);


(. 5.14, 5.18).
const nmax=5;nmax=5 { };
type massiv = array[1. .mmax, 1. .nmax] of real;
procedure input_array(m,n: integer; var mas: massiv);

5.12 , -
-.

5.12. -

var a,b: real;


{ }
procedure square(x,y: real); { , - }
begin
:= *; := *;
writeln(:7:3, ' ',:7:3) ;
end; { }
{ }
begin
:=1.0; b:=3.0;
{ square }
{ b }
square(a,b);
writeln(a:7:3, ' ',:7:3);
end.
160______________________ I. Turbo Pascal

:
1.000 9.000
1.000 3.000


square a, b

, , -
square , b.

5.4.2. -
- , , -
.
-
var .
:
procedure degree(x: real; n: byte; var res: real);
{ x,n -, res - }

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

5.13. -

var a,b: real;


procedure square(var x,y: real); { x,y - }
begin
x:= x*x; y:= y*y;
writeln(x:7:3, ' ',y:7:3);
end;
5. 161

begin
:=1.0; b:=3.0;
square{a,b);
writeln{a:7:3,' ',b:7:3);
end.

:
1. 000 9.000
1. 000 9.000

, Turbo Pascal,
random (. . 3.3.1). -
, -
( 5.14).

5.14.
( .1)
uses crt;
var i,s: integer;
function next (var seed: integer): integer;
const multiplier=37; increment=3; cycle=64;
next:=seed;
seed:=(mutiplier*seed+increment) mod cycle;
end
begin
clrscr;
for i:=l to 64 do write{next (s):4);
end


next, s, 16, -
16. , , ,
seed, 19. , -
s, 19 s 2. -
next, -
, s.
,
0 63 . ,
next 16 . -
, ,
0 63.
"" .
-
.
162______________________ I. Turbo Pascal


0 1, . -
( 5.15).

5.15.
( 2)

uses crt;
var i,s: integer;
function next(var seed: integer): real;
{ : - real}
const multiplier=37; increment=3; cycle=64;
begin
{ : }
next:=seed/cycle;
seed:=(multiplier*seed+increment} mod cycle;
end;
begin
clrscr;
for i:=1 to 64 do write(next[s):10:3);
end.

5.16 ,
.

5.16.

uses crt;var score,throws,seed,a,b,,d,e,f,g,h,i,j,k: integer;


function rnd(var seed: integer): real;
var a,b,c,d: integer;
begin
rnd:=seed/32767;
a:=seed div 256;
b:=seed mod 256;
c:=((b*93) mod 256)+13;
d:=(b*26)+((b*93) div 256)+{a*93)+(c div 256)+27;
seed:=((d mod 128)*256)+(c mod 256);
end;
begin
clrscr;
seed:=0;a:=0;b:=0;c:=0;d:=0;e:=0;f:=0;g:=0;h:=0;i:=0;j:=0;k:=0;
for throws:=1 to 3600 do { }
5. ____________________________________163

begin
{ }
score:=trunc(l+6*rnd(seed))+trunc(l+6*rnd(seed));
{ )
case score of
2: :=+1;
3: b:=b+1;
4: :=+1;
5: d:=d+l;
6: :=+1;
7: f:=f+l;
8: g:=g+l;
9: h:=h+l;
10: i:=i+l;
11: j:=j+l;
12: k:=k+l;
end;
end;
{ }
writeln(2:4,3:4,4:4,5:4,6:4,7:4,8:4,9:4,10:4,11:4,12:4);
( }
writeln(a:4,b:4,c:4,d:4,e:4,f:4,g:4,h:4,i:4,j:4,k:4);
end.


, -
. ,
2, 12.
rnd,
32 768 , 64, 5.15.
3600 , -
, 7, -
:
2 3 4 5 6 7 8 9 10 11 12
99 215 327 404 479 583 489 386 308 207 103
{ " " }
100 200 300 400 500 600 500 400 300 200 100

4.10 , -
: "-" "-", -
. -
, -
, . 5.17
164 ___________________________ I. Turbo Pascal

/ -
. -
, ,
.

5.17. ( 2)

uses crt;
const mmax=5;nmax=5 { };
type massiv = array[1..mmax,1..nmax] of real;
var
ml,nl,m2,n2: integer;
a,b,c: massiv;
(* *)
procedure input_array(m,n: integer; var mas: massiv);
( input_array mas }
{ m,n - (
var i,j: integer;
begin
for i:=l to m do
for j:=1 to n do
begin
writeln(' ',i:2,',',j:2);
readln(mas[i,j]);
end;
end;{input_array}
procedure mas_mult(soml,som2: massiv; var rez: massiv; ml,nl,n2:
integer);
{ mas_mult }
var i,j,k: integer;
begin
for i:=l to ml do
for j:=1 to n2 do
begin
rez[i,j]:=0.0;
for k:=l to nl do rez[i,j]:=rez[i,j]+soml[i,k]*som2[k,j];
end;
end; (mas_mult}
procedure out_array(m,n:integer; mas:massiv);
{ out array mas }
var i,j: integer;
begin
writeln(m:2,n:2);
5. ______________________________________165

for i:=l to m do
begin
for j:=l to n do write(mas[i,j]:7:3);
writeln
end;
end;{out_array}
(* *)
begin
clrscr;
writeln(' 1 ');
repeat
writeln(' - <=',mmax:2);
readln(m1);
writeln(' - <=',nmax:2);
readln(n1);
until (ml<=mmax) and (nl<=nmax);
{ )
input_array (ml,nl,a);
writeln(' 1 ');
{ }
out_array(ml,nl,a);
writeln;
writeln(' 2 ');
repeat
writeln(' - <=',nmax:2);
readln(n2);
until (n2<=nmax);
{ 2- : }
{ nl }
{ m2 }
m2:=nl;
{ }
input_array(m2,n2,b);
writeln (' 2 ');
{ }
out_array(m2,n2,b);
writeln;
{ }
mas_mult(a,b, , ml, n1, n2);
{ }
writeln{' ');
out_array(ml,n2,);
readln;
end
166______________________ I, Turbo Pascal


mas_mult :

som1 ,
;
m1 , a n1 ;
som2 , m2
n1 -
;
2 ;
rez (: m1 2 -
).

: var i, j: integer;. -
i, j , -
mas_mult . -
var.
.
.
. 5.5.

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

, ,
, .
5. ____________________________________167

:

;
,
, ,
, -
() ,
.
, :
var a,b,c,d: integer;
procedure p(x: integer; var : integer);
var : integer;
begin
c:=l;d:-l;x:=l;y:=l;
writeln(x:3,y:3,c:3,d:3);
end;
begin
a:=0;b:=0;c:=0;d:=0;
p(a,b) ;
writeln{a:3,b:3,c:3,d:3);

:
1 1 1 1 1
1 0 1 0 1


-, -
=0. 1,
. ,
- , -, -
-
b, 1. -
, ,
, d -
, , -
.

,
c .
, -
. , -
168_____________________ I. Turbo Pascal

, . -
,
.
,
.
Turbo Pascal .
, , ,
. . , -
,
(. . 6.2.3).

Turbo Pascal . 5.1.

. 5.1.

, , -
:
, ,
, ;
, ,
;
,
, , -
5. _____________________________________169

( -
).
. 5.1, , -
, , ()
D, . , F, -
.
" ". -
, ,
,
,
-. -
-,

.
, :
var a,b: integer;
function d(x: integer): integer;
begin
d:=a+x; a:=a+l;
end;
begin
a:=1; b:=d(1)+d(a);
writeln(b); { 5 }
a:=1; b:=d(a)+d(1);
writeln(b); { 6 }


d. ,
d(1)+d{a) d(a)+d(1).
" " , -
(, )
.


. ,
( ),
, .
-
.
5.18 ,
, 1000.
170______________________ I. Turbo Pascal


, , ,
("-
"), . , 13 31 .

5.18.

uses crt;
var x,k: integer { x,k }
{ }
function prost(: integer): boolean;
var d,i: integer; ( d,i,flag }
flag: boolean;
begin
d:=0; flag:=false;
for i:=2 to y-1 do if mod i=0 then d:=d+l;
if d=0 then flag:=true;
prost:=flag;
{ , : }
{ }
end;
{ "" }
function povorot(y: integer): integer;
var s:integer; { s }
begin
if y<10 then s:=y;
if (y>10) and (y<100) then s:=y mod 10*10+y div 10;
if (y>=100) and (y<1000) then
s:=y mod 10*100+y mod 100 div 10*10+y div 100;
povorot:=s;
end;
procedure pause; { }
var ch: char;
begin
writeln; writeln{' - '};
repeat ch:=readkey until ch <>';
end;
( )
begin
writeln(' ':50);
k:=2; { 2 3 }
for x:=4 to 999 do
begin
if prost(x) then { }
5. _______________________________________________171

{ "-", }
{ povorot() }
if prost(povorot(x)) then
begin
writeln(x); { }
k:=k+l;
if k mod 24=23 then pause;
end;
end;
writeln(' ',k,' < 1000');
end.


,
, ""
prost (povorot (x)), d, i,
flag, prost,
povorot.
, -
, -
pause.

5.6.
-
. -
, , :
,
end ;
-
. , -
;
-
-, --
. -
-;
, . . , -
, , -
;
,
. ,
, , ;
172______________________ I. Turbo Pascal

-
;
-
, -
, ;

, -

var;
-
var, -
() -
. var ,
.
6

6.1.
. -
-
.
:

,
-
;
, -
, -
, -
;
-
,
;
-
, -
;
-
,
;
.
. , -
, -
.
174_______________________ I. Turbo Pascal


. ,
,
. -
:

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

. -
, .


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

.
.
" goto" , -
.
: "
, : (-
), () (, -
)" . .
6. ____________175


: , , -
:
, , -
;
, -

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

6.2.

5.2.1.

-
.
, , , , -
. , -
. , ,
,
. . :
1. .
2. , .
3. , . . ,
, .
4. , ,
, -
14.
176______________________ I. Turbo Pascal


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

. , -

. , -
, , .
,
-
.
.
-
.
(n-1) n, 1.
S n = S n-1 +n;.S 1 = l.
, , ,
, ,
. , ,

.
-
,
:
.
:
-
:
6. ____________177

:
,
-
;
, , ;
, -
.
, -
, .
:
-
{$+} -
Turbo Pascal Options | Compiler| Stack checking;

{R+}
Turbo Pascal Options | Compiler | Range checking;

if keypressed then halt;, ""
, .

keypressed true, -
, , false .
, repeat until keypressed;
.
halt(n) -
( -
pas-) ( -).
, -
. =0 .
halt halt(0). , -
255, bat-
errorlevel if.

6.1 , n -
1+1/2+1/3+...
sumset , for
recursion.

6.1.
var n: integer; { - }
s: real; { }
178______________________ I. Turbo Pascal

procedure sumset(n: integer; var sum: real);


var i: integer; { }
begin
sum:=0;
for i:=l to n do sum:=sum+l/.i;
end;
procedure recursion(n: integer; var sum: real);
begin
if n=l then sum:=l+sum
else
begin
sum:=sum+l/n;
recursion(n-1,sum) ( }
end
end;
begin
writeln(' 1+1/2+1/3+...');
write(' - : '); read(n);
sumset(n,s);
writeln{' ',:3,' () : ', s:7:3);
s:=0; recursion(n,s);
writeln(' ',:3,' () : ', s:7:3);
end.


=1,
{ "").

. -
.
, .
.
sumset , .
recursion
.
6.2 ,
n .

6.2.

var n: integer;
procedure reverse(n: integer);
6. ___________179
begin
write{n mod 10) ;
if (n div 10) <> 0 then reverse (n div 10)
end;
begin
writeln(' <= ',maxint); readln(n);
reverse(n); writeln; readln;
end.

5.5
. , , -
. , -
, n , -
. fibon, fn1 fn
, ( 6.3).

6.3. ( 3)

procedure fibonachi(n,fnl,fn: integer);


{ }
begin
if > 0 then
begin
writeln(fnl+fn);
fibonachi(n-1, fn, fn1+fn);
end;
end;
{ }
var n, a, b: integer;
begin
write(' : '); readln(n);
write('... : '); readln(a,b];
fibonachi(n,a,b); readln;
end.


=10 -
, b 1 0 -
. , , -
n, a, b 10, 3 5 .
-
, ,
, -
, .
180______________________ I. Turbo Pascal

6.4 -
.

6.4.

var n: integer; f: longint;


{ }
function fakt(n: integer): longint;
{ , n - integer, }
{ longint }
begin
if n=l then fakt:=l { }
else fakt:=n*fakt(n-1) { ! }
end;
{ }
begin
write(' n n! '); readln(n);
f:=fakt(n); { }
writeln(' ',,' = ',f); readln;
end.


=1. ,
fakt 1, -
. =1 ,
n*fakt(n-1). -
, . . fakt (n-1), -
, , fakt,
fakt, . ., , -
=1. fakt
fakt =1 ,
fakt .
fakt , 1 n, fakt
, k -
k-1.
fakt f 1 , . .
n.

, -
. , ! ,
=13 longint {.
. 2.1).
6. ___________181

6.2.2.
, ,
, . . , ,
, .
. . , -

, forward
),

.

.
,
. , .
:
var a,b: real;
{ p1 }
procedure p1(x: real); forward;
{ 2}
procedure p2(: real);
begin
...
p1(a); { p1 }
...
end;
{ p1 }
procedure p1
begin
...
2(); { 2 }
end;
...
2(); pl(b); { 2 p1 }
end.

, p1
2, . . -
p1
,
182_______________________ I. Turbo Pascal

. p1 -
, . . .

6.2.3. -
- -
,
" " ( 6.5).
( 1, 2, 3), (,
3) , -
, , . . -
, .
( 3) ( 1),

,
.
( 2) .

6.5. " "

type position=(left,centre,right); { }
var n: integer;
procedure movedisk(from,tol: position); { }
procedure writepos(p: position);
begin
case p of
left : write('l') ;
centre: write('2') ;
right : write('3');
end;
end;
begin
writepos(from); write('->'); writepos(tol); writeln
end;
procedure movetower(hight; integer; from,tol,work: position);
begin
if hight>0 then
begin
movetower(hight-1,from,work,tol); ( )
movedisk(from,tol);
movetower(hight-1,work,tol,from); { }
end;
end;
6. ____________183
begin
writeln(' : '); readln(n);
( }
movetower(n,right,left,centre);
end.

6.2.4. -
-
, Turbo Pascal -
,
-
.
, -
. . -
-
. ,
, -
. , , -
, -
.
,
.
,
-
. -
, ,
,
, . -
-
, -
:
far (
) ($F+};
:
;
;
inline ;
interrupt .
184_______________________ I. Turbo Pascal


Inline
Turbo Pascal. Interrupt
(. . 8.5).

- - -
-
procedure function.
:
procedure (,1:integer;var m:real;procedure pr;function st:real);

ex:
k, l -;
m -;
-;
st -, -
.
.
, -
, , , , -
-.
,
. -
, F -
G, F G
F
G.
- ,

(, -
), -
( 6.6).

6.6.

uses crt;
{ func }
{ }
type func=function( x , y : integer):integer;
{ }
{$F+}
6. ____________185

{ }
function add(x,y: integer): integer;
begin
add:=x+y;
end;
{ }
function mult(x,: integer): integer;
begin
mult:=x*y;
end;
{ , }
function funny(x,y: integer): integer;
begin
funny:=(x+y)* (x-y);
end;
{$F-}
{ }
{ 1 10, }
{ - operation }
procedure type_tabl(w,h: integer; operation: func);
var x, y: integer;
begin
for y:=l to h do
begin
for x:=l to w do
write(operation{x,y):5); writeln;
end;
end;
{ }
beqin
clrscr;
{ }
type_tabl(10,10,add); readln;
{ }
type_tabl(10,10,mult); readln;
{ }
{ 1 10 }
type_tabl(10,10,funny); readln;
end


type_tabl -
, add, mult, funny. -
type_tab1
186_____________________ I. Turbo Pascal

10,10 add, -
10 10 ,
operation func
add. 1 10.
type_tabl -
10, 10 mult,
1 10.
type_tabl 10,10 funny
1 10.
{SF+} () Turbo Pascal
far
type_tabl -.

(. 5.9) -
,
. ( 6.7) ,
f1()
f2(x) , 1 = x2 = b .
. , f1() = 2.
f2(x) = , 1 = 0 2 = 1, , , -
, . (0,0), (1,1), (1,2).

.

6.7. ( 3)

{ func }
{ }
type func = function(x: real): real;
var
s,square: real;){ }
tn,tk: real; { }
: integer; ( }
{ }
{$F+}
{ #1 }
function fl(t: real): real;
begin
fl:=2*t;
end;
{ #2 }
function f2(t: real): real;
begin
f2:=t;
end;
6, ___________187
{$F-}
procedure simps(f: func; a,b: real; n: integer; var int: real);
var
sum,h: real;
j: integer;
begin
if odd(n) then n:=n+l;
h:=(b-a)/n;
sum:=0.5*(f(a)+f(b)];
for j:=l to n-1 do
sum:=sum+(j mod 2+1)*f(a+j*h);
int:=2*h*sum/3
end;
{ }
begin
write(' : '); readln(tn,tk);
write(' : ']; readln();
{ f func }
{ f1 }
simps(fl,tn, tk,n, s);
writeln(' S1: ',s:10:7]; square:=s;
simps(f2,tn,tk,n,s);
writeln(' S2: ',s:10:7|; square:=square-s;
writeln(' S1-32: ',square:10:7);

6.2.5. -
Turbo Pascal -
"" -
. , ,
10 10 , 9
9 ""
.
(. . 13),
.
, -
, -
. -
-.
-
-
188_______________________ I. Turbo Pascal

. ,
. ( 6.8)
stdeviat -
, .

6.8.

uses crt;
const nmax=100; { }
var i,j,n: integer; a: array[1..nmax] of real;
function stdeviat(var x; n: integer): real;
var a: array[1..2*maxint div sizeof(real)] of real absolute x;
i: integer; s1,s2,average,variance: real;
begin
sl:=0; s2:=0;
for i:=l to n do
begin
sl:=sl+a[i]; s2:=s2+sqr(a[i]);
end;
average:=sl/n; { }
variance:=(s2-n*sqr(average))/(n-1); { }
stdeviat:=sqrt(variance); { }
end;
begin
clrscr;
for i:=l to 10 do
begin
n:=random(nmax)+2; { )
{a[j] 0 1}
for j:=l to n do a[j]:=random;
writeln(' = ',stdeviat(a,n):10:7,',
= ',n:2)
end;
end.



,
. -
.
absolute, -
. -
,
6. ____________189

.

.
stdeviat
. -
. -
,
real. -
(. . 2.2). -
sizeof (. . 3.3.1).

7.
8.
9.
10.
11.
12.
13.
7

" " , -
, , , -
5 6, .
, ,
. ,
. -
: " , -
?" ,
, ,
. , ,
, .
Turbo Pascal -
. -
, , -

Turbo Pascal (Borland Pascal).


Borland
DOS Turbo Pascal (
Turbo.exe) Borland Pascal ( .).
Turbo Pascal
.
,
Borland Pascal . ,
Borland Pascal ,
Turbo Pascal
( , ).
194____________________________________________ II.

7.1.
, , -
, , . ..
.
, "" -
. , -
. "" -
.
"" executable.
: "", "", -
.
, ,
Java
. Turbo Pascal , -
pas, ;
,
.
, ,
Turbo Pascal.
( -
).
Borland Pascal -
,
. Turbo Pascal.
,
, .


-
Compile Destination Memory ( -
) Disk. -
.


. :
;
.

, . .
,
Turbo.tpl ( Turbo Pascal), Turbo.exe -
7. _____________________________________195

. , , -
. Turbo Pascal .

Turbo Pascal, -
.
.
- (Compiler),
, -
; , . -
- ( ,
Linker),
, .

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

7.2.
-
, , -
, . ,
, , , .
, .
, , -
, .
, ( pas
exe ).
, , ,
, -
. -
( -
, , ) .

. -
196_________________________________________ II.

, ,
.
-
. -
:

, ;
-
- ,
, -

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

, . . , a
.
Turbo Pascal
, ,

.
.
-
-
, . . .
Turbo Pascal .
, -
, , -
7. _____________________________________197

/ -
-.
, . -
.

7.3.
:
;
;
;
.
,
.
, -
,
. , -
. -
-
.
:
unit <>; { }
{$R+} { , }

interface { }
uses ... { }
label ... { }
const ... { }
type ... { }
var ... { }
procedure... { }
function ... { }

implementation { }
uses ... { }
label ... { }
const ... { }
type ... { }
var ... { }
procedure... { }
198____________________________________________ II.
function ... { }
begin { }
{ }
end. { }

- , -
.
:
-
, ( -
);
,
. , , unit triangle.
-
Triangle.pas, ;
.
. . , -
.
;
, -
. -
, , , ,
, ;
, -
, , . .
,
;
.
begin begin
end. ( );
, -
.

. ,
, . .


, ,

. . .
- , -
, , !
7. _______________________________________199


: uses ;.
.
, uses crt, graph, triangle;

7.4.
triangle,
, -
.
: . -
:
;
;
;
.
( 7.1) -
, - .

7.1.
unit triangle;
interface
{ }
procedure getabc (xa,ya,xb,yb,xc,:real; var a,b,c:real);
{ }
function exist(a,b,:real):boolean;
( }
function perimetr(a,b,:real):real;
{ }
function square (a, b, : real) :real;
{ }
function rv(a,b,c:real) :real;
{ }
function ro (a,b, : real) :real;
{ , }
implementation
{ }
function len(x1,y1,x2,y2:real):real;
begin
len:=sqrt(sqr(x1-x2)+sqr(y1-y2)) ;
end;
200_______________________________________ II.

procedure getabc;
begin
a:=len(xa,ya,xb,yb);
b:=len(xb,yb,xc,yc);
c:=len(xc,yc,xa,ya) ;
end;
function exist;
begin
exist:=(a<b+c) and (b<a+c) and (c<a+b);
end;
function perimetr;
begin
perimetr:=a+b+c;
end;
function square;
var p:real;
begin
p:=(a+b+c)/2; { }
square:=sqrt{p*(p-a)*(p-b)*{p-c)); { }
end;
function rv;
begin
rv:=square(a,b,c)/perimetr(a, b,c)*2;
end;
function ro;
begin
ro:=a*b*c/4/square(a,b,c);
end;
begin
{ }
end.


len , -
.

7.4.1.
. -
, . -
. :
( );
(,
);
7. ______________________________________201

-
( ).
( 7.2) .

7.2. ,

uses triangle;
var xa,ya,xb,yb,xc,yc,
xd,yd,xe,ye,xf,yf,a,b,c,d,e,f: real;
begin
writeln(' ');
readln(xa,ya,xb,yb,xc,);
getabc(xa,ya,xb,yb,xc,yc,a,b,);
if not exist(a,b,c) then
writeln (' ')
{ }
else { }
begin
writeln(' ');
readln(xd,yd,xe,ye,xf,yf);
getabc(xd,yd,xe,ye,xf,yf,d,e,f);
if not exist(d,e,f) then
writeln(' ')
else { }
begin
if perimetr(a,b,c)>perimetr(d,e,f) then write('y ')
else write(' ');
writeln(' ');
if square(a,b,)>square(d,e,f) then write('Ha ')
else write(' ');
writeln(' ');
if rv(a,b,c)>rv(d,e,f) then write('Ha ')
else write(' ');
writeln(' ');
end;
end; readln
end.

7.5.
Turbo Pascal ,
. -

202_______________________________________ II.

tpu (Turbo Pascal Unit),


, -.
Compile, ,
:
Compile ( <Alt>+<F9>); Destination (Memory, Disk);
Make ( <F9>); Primary file...
Build;
.
Compile -
. -
tpu ,
pas, . . ,
.
Make
. - ,

. , -
, .
.
Build ,
. ,
.
Destination tpu
( ) . Destination
Disk ( Memory). Borland Pascal
tpu ,
Compile.
, Primary file... ,
-
. , -
Primary file.

.
,
tpu-,
-
. -
, . . tpu- .
tpu- : -
Turbo Pascal .
8

3.1.
Turbo Pascal ,
, : system,crt,graph,dos,
printer. graph tpu-,
Turbo.tpl (tpl Turbo Pascal Library, -
Turbo Pascal).
system
,
, uses.
system. -
Turbo Pascal, -
. -
,
Turbo.tpl -
File not found (SYSTEM.TPU) -
system, .
system
( /, ,
. .). ,

Turbo Pascal.
crt. ,
, -
. , ,
,
, , ,
.
204________________________________________ II.


crt, Turbo.tpl,
, 300
Division by zero (-
). -
,
Turbo.tpl, crt . Borland Pascal
Windows wincrt,
Windows. ,
crt, Windows
Turbo Pascal .

graph. , ,
. -
, , -
.
dos. , -
MS-DOS. -
-
Windows.
printer.
.
:
uses printer;
begin
writeln(LST,'Turbo Pascal');
end.


-
.
-
( ).
.

crt graph
, -
: , , . -
-
,
.
crt graph ,
-
.
8. _________________________________________205

3.2.
()
- (Cathode Rate Tube, CRT),
, -
. . -
, -
(),
Picture Element PixEl),
. , -
.
-
, , ,
, . -
, -
.
, , ,
. -

.
,
.


Turbo Pascal ,

.
crt graph, -
{. . 13.1).
Mem,
MemW MemL, -
(, ),
Port PortW .

-
, -
().
.
,
, ,
.
, : ..,
, , -
, , . -
206_______________________________________ II.

, -
-
.
-
, -
. crt ,
, -
. graph -
Turbo Pascal.

8.3. CRT
8.3.1.

Windows , -
. ,
Turbo Pascal, , -
.
, ,
.
-
(), , .
, ,
.
-
. .
, ,
.
( ), (3 ),
, -
. , ,
, -
.
. 8.1.

. ,
: , .
16 (-
16 , 0000
1111). 0 15 , -
8. ______________________________________207

crt (. 8.1). -
(). -
, . -
8 , . .
3 .

. 8.1. ( )

:
.
25 80
.
textmode :
textmode{mode) ;
mode word (. 8.2).
:
textmode (BW40); textmode (0); - -
25 40 ;
textmode (259) ; textmode (CO80+Font8x8) ; -
50 80 .
( textmode ) mode=3 .

8.1.

Black 0

Blue 1
Green 2
Cyan 3
Red 4
Magenta 5
Brown 6
208______________________________________ II.

8.1 ()

- LightGray 7

- DarkGray 8

- LightBlue 9

- LightGreen 10

- LightCyan 11

- LightRed 12

- LightMagenta 13

Yellow 14

White 15

8.2.


,


Mode Mode

BW40 0 25x40 -

40 1 25x40

BW80 2 25x80 -

80 3 25x80

Mono 7 25x80 -

Font8x8 +256 50


, ,
(1,1). X , -
, -
.
Turbo Pascal -
.
crt (. 8.3).
8. _________________________________________209

8.3.

LowVideo, NormVideo

HighVideo

TextBackGround(Color) (. . ,
). Color -
0 7, -
,
crt
TextColor(Color) .
Color , -
0 15,
crt

, -
textattr, crt.
, -
. -
(. . 2.1.3) ( -
),
.
:
textattr:=$lE;{ }
textattr:=7;{ : }

0 (black)
, 7
$07.
8.1 , -
, , -
(. . 8.4).

8.1.

uses crt;
begin
textcolor(lightcyan); { textcolor(13) textcolor($D) }
textbackground(green); { textbackground(2) }
{ textattr:=$2D; }
210_______________________________________ II.

clrscr; { }
gotoxy(35,13);writeln {'!'); readln;
end.

8.3.2.
,
, . -

window(x1,y1,2,2);

. -
x1,y1 ,
2,2 , -
(1,1),
.
x1,y1
2,2 (, x1=85 2=30) window .
.
:

,
-
;
-
, , ;
.
, -
. 8.4. ,
window, ,
window(1,1 , 8 0 , 2 5 ) ;. -
window .
. 8.4 , -
( , ).

8.4.

ClrScr -

8. ______________________________________211

8.4 ()



ClrEol

GoToXY(x,y) X,

WhereX , -
( )

WhereY , -
( )

, 8.2, 7
(), ( ). -
,
.

8.2. window
USES crt;
var k, x:integer;
begin
textbackground(0);
clrscr;
x:=2;
for k:=l to 7 do begin
textbackground(k);
window(x,l,x+ll,25];clrscr;
x:=x+ll;
end; readln
end.

3.3.3.
.
crt,
:
delay(time) ;

time .
212________________________________________ II.

-
. , -
crt.
( 8.3) delay -
. -
delay -
.

8.3.

uses crt;
type direction=(right,down,left,up);
var c,x,y,h,k:integer;
procedure go(d:direction; n:integer);
begin
for k:=l to n do begin
case d of
right: x:=x+2;
down: y:=y+l;
left: x:=x-2;
up: y:=y-l;
end;
c:=random(15)+1; textcolor(c);
gotoxy(x,y); write('*'); delay(30);
end;
end;
begin
clrscr; x:=39; y:=13; h:=l;
gotoxy(x,y); write('*');
repeat
go(right,h); go(down,h); h:=h+l;
go(left,h); go(up,h); h:=h+l;
until h>24;
end.

8.3.4.
:
(, );
( , -
, , . .);
<Ctrl>, <Alt>, <Shift>, <NumLock>, <CapsLock> .
8. ___________________________________________213

- , -
(-).
, -

. , - -
(, <Esc>,
, - 01).
, -
ASCII.
-, . .
ASCII- -
. , < 1 >
<!> ( <Shift>).
- ASCII-
, , -
ASCII- -
, .
Turbo Pascal -
.


, , ASCII-,
-
( $417 $418). -
, -
$60 (port[$60]).

crt , -
-
, read readln.
keypressed -
true, -
- (. . ), false .
, -
:
repeat
until keypressed;

keypressed
.
readkey,
.
214_________________________________________ II.

readkey, char, -
("" -
). , -
.
:
c:=readkey; { }
readkey; ( }
{ }

(-
, . .) -
, -
, #0 ( 0).
readkey (#0), -
() .
, 8.4,
.

8.4. , ,

uses crt;
var ch: char;
begin
repeat
ch:=readkey;
if ch=#0 then
begin
ch:=readkey; write('. ');
end
else
begin
write('char = ');
if ch < #32 then write('^',chr(ord(ch)+64))
else write(ch);
end;
writeln (' ASCI = ',ord(ch));
until ch=#27;
end.


-
readkey, repeat . . . until keypressed, -
8. ________________________________________215

"" .
while keypressed do readkey;.

readkey ,
. ,
, (-
8.6). 2 crtplus,
, crt.

8.3.5.
:
sound(Hz) , . Hz
.
,
nosound ( ,
, );
nosound , .
:
sound(5000]; delay(l00); nosound;

, 8.5 8.6,
Turbo Pascal .

8.5. ,

uses crt;
{ , ,
}
const octava_l:array[1..7) of integer=
(262,294,330,349,392,440,493);
var n,octava,koef: integer;
begin
koef:=l;
for octava:=l to 3 do ( }
begin
for n:=l to 7 do { 7 }
begin
sound(octava_l[n]*koef);
delay(300); nosound;
end;
216________________________________________ II.

koef:=koef*2;{ }
end;
sound(octava_l[1]*koef);delay(300); nosound;{"" )
end.

8.6. , @

uses crt;
const
{ - }
left =#75; right=#77; up =#72; down =#80;
ch ='@'; ( }
var : char; curx, cury: byte; badkey: boolean;
procedure beep(badkey: boolean);
begin
if badkey then { }
begin
sound(500); delay(l00);
sound(440); delay(200);
nosound;
end
else
begin { 360 0.02 }
sound(360); delay(20); nosound;
end;
end;
begin
clrscr; curx:=40; cury:=12;
repeat
gotoxy(curx,cury); write(ch);
badkey:=false; c:=readkey;
if c=#0 then { ? } c:=readkey;
case of
#27 :; { esc }
right: if curx < 80 then inc(curx);
left : if curx > 1 then dec(curx);
up : if cury > 1 then dec(cury);
down : if cury < 25 then inc(cury);
else badkey:=true;
end;
beep(badkey);
until c=#27
end.
8. _______________________________________ 217

8.4. GRAPH
8.4.1.
-
. -
, ,
.
( !) . , -
,
.
Turbo Pascal , -
.
graph Turbo.tpl,
Graph.tpu.
File Graph.tpu not found ( Graph.tpu ),
: , -
.
Options | Directories | Unit Directories,
, Graph.tpu.

.
bgi (Borland Graphic Interface), -
.
Egavga.bgi, VGA,
.

chr.


Turbo Pascal ,
, ,
.

initgraph, :
initgraph(,,);

.
integer,
.
218________________________________________ II.

integer, -
. .
string, -
; .
, .
Egavga.bgi ( bgi)
, . -
BGI Error: Graphics
not initialized (Use InitGraph). -
initgraph .
( graph ) , -

.
. ,
. -
VGA, -
, Turbo Pascal,
.
:
const
detect =0; { }
vga = 9; { vga }

detect ( 0), initgraph


,
, , -
. -
,
Egavga.bgi, 640x480
16 .
.
Windows, -
. -
. , graph -
, , -
. -
, / ;-
, Egavga.bgi. graph
RegisterBGIDriver InstallUserDriver ( . 2 ).
, -
, . 8.5.
8. __________________________________219

8.5.

InitGraph(gd,gm,path)

GraphResult ,
.
GrOK=0

GetGraphMode

SetGraphMode(Mode)

RestoreCrtMode

CloseGraph ,
,

ClearDevice


-
, (X ).
. -
, .
, . . -
(0,0). (X) -
, (Y) . ,
640x480 X 639,
Y 479. . 8.6 -
.

8.6.

SetMaxX X

SetMaxY Y

GetX X

GetY

GetPixel(X,Y) (X, Y)
220_______________________________________ II.


,
, , , . -
, ,
Getx Getx
.
.

8.4.2.
, -
. graph
. -
. 8.7.

8.7.

PutPixel(X,Y,Color)
(X, ) Color,

Line(X1,Y1,X2,Y2) -
(1.1)
(2,2).

MoveTo(X,Y)
(XY)
LineTo(X,Y) , -
, -
(X, Y).
(X, )
LineRel(Dx,Dy) , -
, -
Dx ( X) Dy
( ).

Rectangle(X1,Y1,X2,Y2)
(1.1) (2,2)

Bar(X1,Y1,X2,Y2) -
(1,1)
(2,2) , -

8. __________________________________________221

8.7 ()

Bar3d(x1,y1,2,2,h,Top) ()
h,
,
TopOff, ,

Circle(X,Y,Radius) (,) -
Radius
Arc(X,Y,StAngle,EndAngle,Radius) StAngle
EndAngle (X, ) -
Radius;

Ellipse (,)
(X,Y,StAngle,EndAngle, Xradius ( X), Yradius
Xradius,Yradius) ( ) StAngle
EndAngle.
StAngle=0 EndAngle=360

FillEllipse(X,Y,Xradios,Yradius) ,

PieSlice(X,Y,StAngle,EndAngle, ,
Radius)
Sector(X, Y,StAngle,EndAngle, , -
Xradius,Yradius) , Ellipse
DrawPoly(N,ArrayOfPoint) , N ,

ArrayOfPoint.

FillPoly(N,ArrayOfPoint) , -

FloodFilll(X,Y,Border_Color)
Border_Color. (,)
. -
-
(. . 8.8)


, .
, -
222_________________________________________ II.

-
( 8.78.10). , -
. ,
BGI
Turbo Pascal. He .

8.7. ""

uses graph;
var gd,gm: integer;
begin
gd:=detect;
initgraph(gd,gm,'');
if graphresult <> grok then begin
writeln(' ');
readln; halt; { }
end;
{ }
rectangle(280,180,360,300); bar(305,210,335,270);
line(280,180,320,150) ; line(320,150,360,180) ;
circle(320,165,8);
readln; closegraph;
end.


-
GraphResult.

8.8. DrawPoiy-

uses graph;
const { pointtype, graph }
triangle: array[1..4] of pointtype = { }
(: 50; : 100), (: 100; : 100), { }
(: 150; : 150), (: 50; : 100)); { }
var gd, gm: integer;
begin
gd := detect; initgraph(gd, gm, '');
drawpoly(4, triangle);
readln; closegraph;
end.
8. _________________________________________223

( 8.9) ,
"" -
cleardevice ( "-
"). delay, -
, crt.

8.9. ( 1)

uses crt, graph;


const t=20;d=5;
( t ,d X, }
{ , }
var ,gd,gm:integer;
procedure vagon;
begin
bar(x,100,x+70,130);
circle{x+15,140,10);
circle(x+55,140,10];
end;
begin
gd:=detect; initgraph (gd, gm,' ' );
x:=0;
repeat
vagon; { }
delay(t); { }
cleardevice; { }
x:=x+d; { }
until x>639;
end.

( 8.10) . -
. -
,
, -
. .
,
, -
moveto lineto, -
.
. ,
( 640 ) -
.
224__________________________________________ II.

8.10.
uses graph;
var gd,gm:integer;
function f(x:real):real; { }
begin
f:=x*x; ( }
end;
procedure drawgrafik(a,b:real); {a,b - }
var ,dx,max,min,koef:real;
k,x0,y0:integer; { x0,y0 }
begin
dx: =(b-a)/639; { (640 ,
639) }
:=; max:=f(a); min:=f(a);
for k:=l to 640 do { f(x) }
begin
if f(x)>max then max:=f(x);
if f(x)<min then min:=f(x);
x:=x+dx;
end;
koef:=479/(max-min); { }
x:=a; { }
moveto(0,round(479-koef*(f(a)-min))); { }
for k:=l to 639 do ( }
begin
x:=x+dx;
lineto(k, round(479-koef*(f(x)-min)));
end;
x0:=round(639*a/(a-b)}; { a/(a-b)=(0-a)/(b-a) }
line(x0,0,x0,479); { }
y0:=round(479-479*(min/(min-max)));
line(0,y0,639,y0); { }
end;
begin
gd:=detect; initgraph{gd,gm,'');
drawgrafik(-4,4);
readln; closegraph
end.

8.4.3.
-
, )-
( ).
8. _______________________________________225


. ,
. -

, .
. 8.8 .

__ 8.8.

SetColor(Color) -
, Color

SetBkColor(Color)
SetLineStyle(Style, Pattern, -
Thickness) (), 16- (
),
SetFillStyle(Style, Color)
SetFillPattern(Pattern, Color)
. Pattern -
array [1. .8] of byte
GetColor, GetMaxColor, GetBkColor ,
-

, crt
(. . 8.2).
12 4 .
.
, -
, (1
, 0 ).
: 1 3 ( ).
( 8.11, 8.12), -
.

8.11. ,

uses graph;
var gd,gm,x,y,color,style:integer;
begin
initgraph(gd,gm,''); x:=0;
226_______________________________________ II

{ 0 }
for color:=1 to 15 do begin
y:=0;
for style:=l to 11 do begin
setfillstyle(style,color);
bar(x,y,x+30,y+35);
y:=y+44;
end;
x:=x+43;
end; readln;
end.

8.12. ,

uses graph;
var gd,gm,,thickness,style:integer; { thickness }
procedure linedemo(thickness:word);
begin
for style:=0 to 3 do begin
setlinestyle(style,0,thickness);
line(0,y,639,y) ;
y:=y+44;
end;
end;
begin
initgraph(gd,gm, '');
setcolor(red); y:=0;
linedemo(l); linedemo(3); readln;
end.

8.4.4.
, ,
( ).

.
. 8.9.
-
setbkcolor, . . .
setfillstyle bar.
8. ________________________________________227

:
setviewport(200,100,440,380,true);
setfillstyle(l,4);
bar(200,100,440,380);

8.9.

SetViewPort(X1,Y1,2, (1, V1)


Y2, Clip) (X2.2) .
Clip=True, -
, , , -

ClearViewPort

8.4.5.

graph.
system crt
,
. -
. -
write, writeln, clrscr, gotoxy .
. 8.10. ,
, 8.13.

8.10.


OutText(Text) Text,
. -

OutTextXY(X, Y, Text) Text,


(X, ). -

SetTextStyle(Font,Direction, ,
CharSize) ( ),
(
Font)
228__________________________________ II.

8.10 ()

SetTextJustify(Horiz, Vert)
(,) outTextXY

TextWidth(Stroka)
,

TextHeight(Stroka) ,

, , DefaultFont
( 0), chr (. 8.11).
, -
,
. , -
,
InstallUserFont, -
( chr).
8.11.

()

DefaultFont 0

TriplexFont 1 Tripp.chr
SmallFont 2 Litt.chr
SansSerifFont 3 Sans.chr
GothicFont 4 Goth.chr



str (. . 9.3),
,
OutText OutTextXY.

8.13.
uses graph;
var gd,gm:integer;
8. _______________________________________229

begin
gd:=detect;
initgraph(gd,gm, '') ;
settextstyle(DefaultFont,izDir, 3) ;
settextjustify(CenterText,CenterText);
setcolor(red);
outtextxy(320,240,' '); readln
end.

8.4.6.

, . , -


.
, .

(. 2). ,
graph
(. 8.12). . 8.13
BitBit, , . 8.12.

8.12.

ImageSize(X1,Y1, 2, Y2) , -
( ) -
-
(1, 1) {2, Y2.)

GetImage(Xl, Yl, X2, -


Y2, BitMap) (1.Y1) (X2.Y2)
, BitMap. -

. 13)
PutImage(X, Y, BitMap; ,
BitBit) , BitMap.
(X,Y) -
, ; BitBit
-
( ,

,
)
230________________________________________________ II.

8.13. BitBit

NormalPut=0; CopyPut=0
-

XORPut=l " " (XOR)


OrPut=2 (OR)
AndPut=3 (AND)
NotPut=4 (NOT)

Putimage
XORPut, . .
, , -
.
GetImage PutImage "-
" . ( 8.14) -
. -
. 13. -
.

8.14. ( 2)

uses graph,crt;
const t=100; d=15;{ }
var p:pointer; { () }
gd,gm, ,size:integer;
procedure vagon;
begin
bar(x,100,x+70,130);
circle(x+15,140,10);Circle(x+55,140,10);
end;
begin
gd:=detect; initgraph(gd,gm,''); x:=0;
vagon; size:=imagesize(0,100,70,150);
{ Size }
getmem(p,size) ;
{ }
getimage(0,100,70,150,^); :=0;
repeat
delay(t); { )
putimage(x,100,p^,xorput); { }
x:=x+d; { }
8. _________________________________________231

putimage(x,100,p^,xorput); { }
until x>639;
{ }
freemem(p,size) ;
end.

8.5. DOS
, -
Turbo Pascal , -
. , -
MS-DOS,
, . . Win-
dows. :
;
;
Turbo Pascal;
;
;
(environment).
. -
dos . 12. -
2
( , ).

8.5.1.
, -
,
(. 8.14). Turbo Pascal -
,
, . , ,
. . -
.

8.14.

GetDate(year,month,day,dayofweek) : , ,
(0
, 1 . .)
232_______________________________________ II.

8.14 ()

SetDate(year,month,day)
( !)

GetTime (hour,minute,sec,sec100) : , -
,
SetTime(hour,minute,sec,sec100)
( !)

word ( ).


GetTime
.
,
.

8.5.2.

(. 8.15). -
, Turbo Pascal -
, -
. -
(, Windows -
Far Norton Commander), -
.
: format :. Format.com
, .

8.15.

ParamCount

ParamStr(nomer) ( -
)

, -
.
8. _________________________________________233

uses dos;
var i:integer;
begin
for i:=l to paramcount do writeln (paramstr(i)) ;
... { }
end.


-
Turbo Pascal Run I Parameters.
ParamStr(0) , -
.

8.5.3.
Turbo Pascal
exec.
com
, .
.
exec :
e(path,params);

path , params . -
, params
( ).
-
DOSError,
. -
: 2 ; 3 -
; 8 .
8.
(. . 13.1), -
. exec
Turbo Pascal.
-
. ,
.
, DOSError
8.

, exec .
234_______________________________________ II.

, , -
{$M} , -
. ,
{$M $4000,0,$8000} 16 32 -
. -
.
( 8.15)
, . . .

8.15.

($ $2000,0,0} { 8 0 }
uses crt, dos;
var path, cmdline: string;
begin
writeln(' : '); readln(path);
writeln(' :'); readln(cmdline);
swapvectors; { "" Turbo Pascal
)
exec(path,cmdline); { }
swapvectors; { "" }
if doserror<>O then { }
writeln(' DOS ',doserror]
else
begin
writeln(' ',path,' .');
writeln(' ',doseditcode);
end; readln
end.
9

9.1. CHAR STRING


Turbo Pascal :
char ;
string .

9.1.1.
char . -
0
255. , . -
(, Java) char -
, , . .
. , -
, ,
, .
-
: =, <>, <, >, <=, >=, .
-
. ,
, . .
,
(. 2). -
, -
( , ).
236________________________________________ II.

:
chr(x) ;
ord(ch) ch;
pred(ch) ;
succ(ch) ;
upcase(ch) .
.
:
ord{'A')=65
chr(128)=''
pred('')='A1
succ (' ')= ''
upcase(' n ' ) = ' N '

9.1.2.
.
( ) 1 255. -

.
type
= string [ ];
var
, ... : ;


:
var
, ... : string[MaKc. ];

var
, ... : string;

, 255 .
-
.
:
const stradres = '. , 35'; { }
sadr: string[12] = ' . ' { ,
}
9. _________________________________________237

type strl25 = string [125];


var str1 :str125; { }
st1 : string; { = 255}
st2,st3 : string[50];
strnazv : string[280]; { , 255 }


s.
Turbo Pascal .
n+1 ; -
,
. 0,
1, 2 . .
, sadr string [12] -
(. 9.1).

9.1.
0 1 2 3 4 5 6 7 8 9 10 11 12

8

13 , 4 -
, . . 8 ,
. ""
, "",
"", . . .


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

array [0 .. n] of char
() . -
, ,
.

/.
, readln (st1 ; writeln (st2);
238_________________________________________ II.

,
.
length, -
(. . 9.3).
,
, , . -
. -
, -
(. 9.9).
- .
, , -
( 9.1).
"" (, ,
). ,
. , ,
. , -
.
.

9.1.

var s:string;
i:integer;
begin
write(' ');readln(s);
for i:=length(s) downto 1 do write(s[i]);
for i:=l to length(s)-l do
if (s[i])=' ') and (s[i+l]<>' ') then writeln
else write(s[i]);
readln;
end.

,
. ( 9.2) -
( -
, . .
).
(. 13.3)
.

9.2.

var k, i: integer; { , i }
s:string;
9. _________________________________________239

begin
writeln(' :'); readln(s);
k:=l; i:=0;
while (k<=length(s)) and (i>=0) do
begin
{ i<0, : ')' '(' }
if s[k]='(' then i:=i+l;
if s[k]=')' then i:=i-l;
k:=k+l;
end;
if i=0 then writeln(' .')
{ i>0, }
else writeln(' ');
readln
end.

9.2.
()
. , , -
, .

9.2.1. (+)
.
:
: 'Turbo' + Pascal' + ' 7 . 0'
: 'Turbo Pascal 7.0'
, -
, : "
", -
.



"+" ,
Turbo Pascal . -
-
"+" ,
. .
-
.
240_________________________________________ II.


.
: str1:=' '; str2:= str1 + ' -';
( 9.3). -
, '*'
.
,
.

9.3.

var s:string;
procedure dopstr(var s:string; n:integer);
begin
while length(s)<n do s:=s+'*';
end;
begin
writeln(' :');readln(s);
dopstr(s,80); writeln(s); readln
end.


s+' *' ' *' +s, -
, .
dopstr : , , -
(, , ).

-
, . .
. , .
-
, ,
, !
:
const sl:string[13]='Turbo Pascal';
var s2:string[5];
begin
s2:=sl; . . .

s2 'Turbo', ..
'Turbo Pascal' ,
s2. -
, .
9. _________________________________________241


-
($R+} .
.
!



{$V-}. {$V+},
( , )
. ,
, . . ,
, .

9.2.2.
=, <>, >, <, >=, <= -
.
,
,
. . -
,
.
:
: :
''=' ..' false
''<' ..' true
'program'>'PROGRAM' true


. -
:
;
;
.
,
(. . 4.2.3), -
string. ,
,

.
242_______________________________________ II.

-
( 9.4). , -

, () .

9.4.

const m=4; n=3;


a:array [1..m) of string=('','','','');
b:array [1..n] of string=('','','');
var c:array [l..m+n] of string;
k,i,j:integer; { }
begin
i:=l; j:=l;
for k:=l to m+n do
( i b[j]<a[i] }
if (i>m) or (b[j]<a[i]) then
begin
c[k]:=b[j]; j:=j+l;
end
else
begin
c[k]:=a[i]; i:=i+l;
end;
writeln(' :');
for k:=l to m+n do write(c[k],' '); readln
end.

9.3.
, , -
, .

Turbo Pascal, ,
-
.
, -
, -
. , ,
.
.
9. __________________________________________243

system, -
.
.

9.3.1.
, -
. -
, "".
, , , -
.
:
deiete(st,poz,n) n st ,
poz. poz , , .
:
st: = ' '; delete (st, 1, 5) ; ( st='' }


st ( ):
while s t [ l ] = ' ' do delete( s t , 1,1);


( ):
while st[length(st)]=' ' do delete(st,length(st),1);

-
. while -
, , -
.
insert(stri,str2,poz) str1 str2,
poz. He : , -
.
:
s1 := ' '; s2 := ' ';
insert(s2,sl, 16);
{ s1=' ' }

insert -
. ,
{ 9.3)
s:=s+'*' insert('*',s,length(s)+1).
244_________________________________________ II.

-
, .
-
{$R+}.

9.3.2.
:
length (st) st.
.
, n:=length('123456789'); { =9 }
length ord(s[0]},
. . s[0] char. -
length,
.
copy(st,poz,n) st n ,
poz. poz ,
.
:
s1:= 'Turbo Pascal';
s2:=copy(s1,1,5);
s3:=copy(s1,7,3);
{ s2='Turbo'; s3='Pas' }

concat(str1,str2,..., strn) str1, str2, ...


strn , .

: s:=concat('AA', '', ''); { s='AAXXY';}


concat , -
. , -
: s:='AA'+'XX'+'Y';.
pos(str1,str2) str2 -
str1. -
, str1. str2 -
str1 , 0.
:
sl:= 'Turbo Pascal';
nl:=pos('Pascal',s1); n2:=pos('pascal',s1);
{ nl=7; n2=0 (pascal Pascal )|
9. __________________________________________245

-
.
:
while st[l]=' ' do delete(st,1,1); delete(s1,l,pos(' ',s1));

9.3.3.
Turbo Pascal
, , -
, .
: str val.
str , -
(. . 8.4). str
- , . . -
insert .
:
str (number, st) number
st. number ,
. -
,
.
:
var S1,S2,s3,s4 : string; num1:integer;num2:real;
...
numl:=5; num2:=5.78;
str(num1,S1);str(numl:3,s2); str(num2,s3);str(num2:3:1,s4);
{ s1='5'; S2=' 5';s3='5.780000000000E+00';s4='5.8'; }

, val ,

.
val(st,number,code) st -
numder. Code -
.
, code , -
( ), code -
, number
.
246_________________________________________ II.

:
s1:='5.78'; s2:= '5,78';
val;(s1,num1,cod1];
val(s2,num2,cod2);
( cod1=0, cod2=2 }

val -
Invalid numeric format, -
. -
(
). .
,
. , -
. repeat, . ,
.
:
var s:string; n,error:integer;
begin
repeat
write(' '); { , }
readln(s); { )
val(s,n,error]; { }
if error>0 then writeln(' ',error)
until error=0;
{ }


, ioresult repeat,
(. . 3.9.1). -
, . .
,
.

9.4.
9.4.1. ,

-
.
-
. -
.
9. _________________________________________247

( 9.5) -
. (,
). ,
" ", " " ". .", ". .". -
, ,
.

9.5. ,

{$V-}{ }
var s:string[80];{ }
procedure delspace(var s:string);
begin
( , pos
}
while pos(' ',s)>0 do delete(s,pos(' ',s),l);
if s[l]=' ' then delete(s,1,1); { }
if s[length(s)]=' ' then delete(s,length{s),1); { }
end;
procedure itd_itp(var s:string);
const a:array[1..4] of string[15]=
(' . .', ' ','. .', '. .');
a1:array[I..4] of string[5] = (' .', ' .', '.', '.');
var k,p:integer;
begin
for k:=l to 4 do begin { 4 }
while pos(a[k],s)>0 do begin { }
p:=pos(a[k],s); { }
delete(s,p,length{a[k]));{ }
{ , '.') }
if s[p]='.' then insert(a1[k],s,p) else insert(a1[k]+'.',s,p);
end;
end;
end;
begin
writeln(' :'); readln(s);
delspace(s); itd_itp(s) ;
writeln(' :'); writeln(s); readln
end.

( 9.6) -
, . .
248_______________________________________ II.

.
.

9.6. ,

{ }
const ch:string[6]='.,:;!?';
var s:string;
procedure insspace(var s;string);
var k,l:integer;
begin
k:=l;
repeat
for 1:=1 to 6 do
{ ,
}
if (s[k]=ch[l]) and (s [k+l]<>ch[l]) then insert (' ',s,k+l);
k:=k+l;
until k=length(s);{ ,
}
end;
begin
writeln(' :'); readln(s);
insspace(s);
writeln(' :');
writeln(s); readln
end.

9.4.2.
, -
. ,
, , -
, ,

. ,
- ,
.
.
. -
upcase, (-
9.7).
9. ________________________________________249

9.7.

var s : string;
procedure upstring(var s:string);
{ }
var i: integer;
begin
for i:=l to length{s) do
s[i]:=upcase(s[i]); { }
end;
begin ( )
write(' :');readln(s);
upstring(s); writeln(s); readln;
end.

.
. upcase -
. ,
.
:
. ,
( -
), . .
( 9.8).
9.8.
var s:string;
function upstringrus(s:string):string;
const small:strings'';
big:string='';
var i,n:integer;
begin
for i:=l to length(s) do begin
n:=pos(s[i],small);{ }
if n>0 then s[i]:=big[nj;{ }
end;
upstringrus:=s;
end;
begin
write(' ');readln(s) ;
writeln(upstringrus(s)); readln;
end.
250________________________________________ II.

, .
getupstr
. , -
. -
, , ( 9.9).

9.9

uses crt;
var s:string;
procedure getupstr(var st: string);
var :char;
begin
st:='';( }
repeat
c:=readkey;{ crt }
case of
{ 32, }
'' .. '': c:=chr(ord(c)-32);
'' .. '': c:=chr(ord(c)-80); ( - 80 }
'' .. 'z': c:=upcase();
end;
if c<>#13 then { <Enter> }
begin
st:=st+c; write();{ }
end;
until c=#13;{ <Enter> }
writeln;
end;
begin
writeln(' <Enter>'); getupstr(s);
writeln(' s:'); writeln(s); readln
end.
10

, -
, Turbo Pascal .

10.1.
, -
. , -
, .
,
(,
, -
. .).
.
, -
.
Turbo Pascal .
, 256.
char, byte, boolean. -
(
256 ).
, -
. -
-
.

.
:[1,2,3,4],['','b',''],['' .. 'z']
252________________________________________ II.

, -
[]. .
set of
( ...). :
type
= set of ;
var
, ... : ;


:
var , . . . : set of ;

:
const =[];

:
const :=[];

:
const number = [1,4,7,9];
type simply = set of 'a'..'h';
var pr : simply;
letter : set of char; ( }

pr -
'' 'h'; letter -
. .


. -
1 ( !). , -
, ,
32 . -
, 1
, 0 .

, -
:
,
;
,
.
10. ______________________________________________253

10.2.
:
(=, <>, >=, <=);
(+);
(*);
();
(in).
.
"" (=). ,
.
(. 10.1).

10.1. ""


[1,2,3,4] [4,3,2,1] = true

['', 'b', ''] ['',''] = false

[1...4] [1,2,3,4] = true

" " (<>). ,



(. 10.2).

10.2. " "


[1,2,3] [3,1,2,4] <> true

['a' ..'z'] ['b' .. 'z'] <> true

[1..4] [1,2,3,4] <> false

" " {>=). -


.
>= true, -
. false (. 10.3).
254________________________________________ II.

10.3. " '


[1,2,3,4] [2,3,4] >= true

['a' ..'z'] ['b' .. 't'] >= true

['2','X',''] ['',''] >= true

" " (<=).


, <= true,
. -
false (. 10.4).

10.4. " '


[1,2,3] [1,2,3,4] <= true

['d' ..'h'] ['z' .. ''] <= true

['', 'V'] ['','n','v'] <= true

in.
- .
(. 10.5).

10.5. in


2 A in [1,2,3] true

'v' A in ['' .. ''] false

X1 A in [x0,xl,x2,x3] true

in
, . ,
if (a=l) or (a=2) or (a=3) or (a=4) or (a=5) then ...
if a in [l .. 5] then ...
in : not in m.
, :
not (x in m)
10. ___________________________________________________255

(+).
, (. 10.6).

10.6.


[1,2,3] [1,4,5] + [1,2,3,4,5]

['' ..'d'] ['e'..'z'] + ['a' ..'z']

[ ] [ ] + [ ]

(*).
, ,
(. 10.7).

10.7.


[1,2,3] [1,4,2,5] * [1,2]

['a' ..'z'] ['b'.. 'r'] * ['b' ..'']

[1,3,5] [ ] + [ ]

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

10.8.


[1,2,3,4] [3,4,1] - [2]

['a' ..'z'] ['d'.. 'z'] - ['' ..'']

[1,2,,4] [x4,x1] - [2,]

10.1 ,
. : D1
2, 4, 6, 8; D2 0, 1 , 2 , 3, 5; -
D3 1, 3, 5, 7, 9. -
, , .
256______________________________________ II.

10.1.
type digits=set of 0 .. 9;
var dl,d2,d3,d : digits;
begin
d1:=[2,4,6,8]; { }
d2:=[0 .. 3,5];
d3:=[l,3,5,7,9];
d:=dl+d2; { d1 d2 }
d:=d+d3; { d d3 }
d:=d-d2; { d d2 }
d:=d*dl; { d dl }
end.

Turbo Pascal / -
, , -
dl, d2, d3, d
(. 1).
, -
. ,
:
type charset=set of char;
procedure writeset(a:charset) ;
var c:char;
begin
for c:=chr(0) to chr(255) do
if in a then write(c,' ');
writeln;
end;

-
. ,
. . .
, -
. , -
, .
10.2.

10.2.
const
glasn=['','','','','','','','','',
'', '', '', '', '' , '' '', '', ''] ;
10. _______________________________________________257

var s:string; p,i:integer;


begin
write(' : '); readln(s);
p:=0;
for i:=l to length(s) do
if s[i] in glasn then p:=p+l;
writeln('B ',, ' '); readln;
end.


glasn, -
. -
in. ,
-
pos . -
, . . , ,
,
.

10.3.

1
n, .
, , -
.
. -
. ,
.

10.3.

var a:set of byte;


k,x,n:byte;
begin
randomize; a:=[]; k:=l;
write(' : '); readln(n) ;
while k<=n do begin
x:=random(n)+1;{ 1 n }
if not (x in a) then begin { ? }
write(,' '); :=+[]; { } k:=k+l;
end;
end; readln;
258________________________________________ II.

-
255.
.
.
. -
, , , . .

, .
""
.
.
, ,
-
( ) (-
10.4).

10.4.

var a:array[1..10000] of integer;


k,1,i,n:integer;
ok:boolean;
begin
randomize;
write(' : ']; readln(n);
for k:=l to n do
a[k]:=k; { 1 }
for k:=l to n do
begin
l:=random(k)+1; { }
i:=a[k]; a[k]:=a[l]; a[l]:=i; { a[k] [1] }
end;
writeln(' :');
for k:=l to n do write (a[k],' ');
readln;
end.
11


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

11.1.
~ , -
,
. , () -
.
, ( , ).
.
.
type
= record
1: 1;
260________________________________________ II.

2: 2;
. . .
N: N;
end;

.
var
: ;

:
type avto=record
number: integer; { )
marka: string[20]; { }
fio: string[40]; { , }
address: string[60]; { }
end;
var m,v: avto;

, -
. , avto
:
const car:avto=(number:1000; marka:'';
fio:' ..'; address:'. , .1,.3');

.
-
, . .
, avto, :
m.number, m.marka, m.fio, m.address

,
. -
.
:
m.number:=1964;
m.marka:='Audi 100';
m.fio:= ' ..';
m.address: = '. 53 .1 73';

/:
readln (m.number,.marka,m.fio,m.address) ;
write(m.number:4,m.marka:10,m.fio:13,.address:23);
11. _________________________________________________261

/ -
( ).
:
writeln(m) { }

-
, :
v:=m;

v
m.
. ,
, :
type person = record
fio: string[20];
age: 1. .99;
prof: string[30];
end;
var list:array[1..50] of person;

.
Turbo Pascal with, :
with do ; { }

with,
.
:
with m do
begin
number:=1964;
marka:='Audi - 100';
fio:=' ..';
address:='. 53 .1 - 73';
end;

-
. -
.
, ,
- . , -

.
262________________________________________ II.
type complex=record
re:real; { }
im:real; { }
end;
var a,b,c: complex; { a,b,c complex }
begin
a.re:=6.8;
a.im:=l.6;
...


,
.
(, , ) -
(, , ).
-
, Turbo Pascal
.
. ,
: , , , -
, ,
.
pupil () -
, , 10
pupil ( 11.1).

11.1.

type pupil = record { }


name: string[10]; { }
surname: string[20]; { }
school: integer; { }
class: byte; { }
end;
var school:array[1..10]of pupil;
n: 1 .. 10;
:byte;
procedure input_data;
{ }
begin
writeln (' ', ,' :'];
write(' ?'); readln(school[n].name);
write(' ?'); readln(school[n].surname);
11. 263

write(' ?'); readln(school[n].school);


write{' ?'); readln(school[n].class) ;
writeln;
end;
procedure write_data;
( with }
begin
with school[n] do begin
writeln(' :',name);
writeln(': ',surname);
writeln(': ',school);
writeln(': ',class);
end;
end;
begin ( }
for n:=l to 10 do input_data;
writeln;
writeln(' 5 :');
writeln; n:=5; write_data;
writeln(' :');writeln;
writeln(' :'); readln(c);
for n:=l to 10 do if school[n].class=c then write__data;
readln;
end.

11.2.
, ,
. Turbo Pascal
, .
-
, , .
.
, .
case . . . of
. -
, , -
. -
case .
, , -
.
264________________________________________ It.

, , ,
.
, . O-
, -, . . , -
.
:
type
=d

case : of
1 : ( : ,...);
N : ( : ,...);
end;

( ) -
. case end, -
case. end
. ,
.
-
:
, , -
, ;
,
;
, - .
, : -
N();.

, ,

.
: , .
.
(book).
(author).
(title).
(year).
(publishinghouse).
(pages).
11. ________________________________________________265

(article).
(author).
(title).
(year).
(journal).
(numberofjournal).
(begpagej).
(endpagej).
(paper).
(author).
(title).
(year).
(numberofpaper).
(begpagerj).
(endpagerj).
: , -
, -
, ( 11.2).

11.2.

type
kind = (book,article,paper);
publication=record
{ )
autor,title : string;
year : word;

( }
case kindp : kind of
book : (publishinghouse: string;
pages: word);
article : (journal : string;
numberofjournal : byte;
begpagej, endpagej : word);
paper : (numberofpaper : byte;
begpagerj, endpagerj : word);
end;
var pub: publication; j: word;
begin
writeln(' - ');
266______________________________________ II.

repeat
write(' (0 .. 2)='); readln(j);
if j in [0..2] then
with pub do begin
kindp:= kind(j) ;
write(' :'); readln(autor);
write(' :');readln(title);
write(' :');readln(year);
case kindp of
book : begin
write(' :'); readln(publishinghouse);
write(', . :'); readln(pages);
end;
article : begin
write(' :'); readln(journal);
write(' :');readln(numberofjournal);
write (' : ') ; readln (begpagej, endpagej);
end;
paper : begin
write(' . : ');readln(numberofpaper);
write(' :'); readln(begpagerj, endpagerj);
end;
end; { case .. of }
end; { with pub .. }
until not (j in [0..2]);{ }
end.
12

,

(. . 4). ,
:
. -
. , ,
,
. ,
/ , -
,
, .
( file , ).
, -
: -
, , . . -
. ,
. -
. , -
. ,
, " "
. , -
, ,
.
, (
, , - . .) .


(, ,
. .) (. . 12.1).
268________________________________________ II.

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


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

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

, :
-
, , .
,
;
, ,
, -
;
, -
;
.
12. __________________________________________________269

12.1.

Turbo Pascal -
(Disk Operating System) Microsoft MS-DOS. -
Turbo Pascal FAT16
(File Allocation Table, ).
Turbo Pascal Windows,
MS-DOS. He , -
, FAT16 .

12.1.1.
: .
1 8 .
, 1 3 . ,
Example.pas Example.com. MS-DOS -
, -
, ,
. -
( _) . -
, -
. , , ,
. ,

, , (. 12.1).
, Turbo Pascal
pas. (. 1) -
( execute, ).

12.1.

pas Pascal

, C++

asm Assembler

for Fortran

bak (backup), -
. .
(. 1}
270_______________________________________ II.

12.1 ()

bat (batch) . .
MS-DOS

exe, com , -

txt . :
.
. -
#13#10 (CR/LF /
). , ,

dat
doc ( ).

bmp, jpg , , -

zip, arj, .

12.1.2.
,
. -
.
, , -
. -
, -
, , , -
.
-
(directory), Windows (folders).
.
1 8 , ,
. , -
. . ,
, -
(). ,
, -
12. _________________________________________________271

(). , -
, .

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

:
( );
;
, : " ",
"", "" "".
directory,
;
.
.


.
, , ,
, .
, (
).

,
. , -
,
: :\1\. . ..\N
\.
-
. ".." .
(. . ) 79 .
, c:\turbo\pas\example.pas
example.s \pas,
.
272_______________________________________ II.

\pas \turbo. , \pas


( ), \turbo -
. :\ \turbo -
.
: , ,
, . ,
. A Z. -
:. , ,
( ),
(D:, :, . .). -
() :. ,
:, : . -
,
( ) .
, -
, .
, \turbo\pas\table.txt table.txt
Turbo Pascal -
, ,
:
, '\turbo\pas\table.txt' 'table.txt'


MS-DOS Windows -
. Windows
, . -
255 .

12.1.3.
-
. , , .
, .
Turbo Pascal , , -
,
: , , , -
/. Turbo Pascal ,
, .
. ,
( )
() .
12. _________________________________________________273

, -
.
CON. -
, .
, -
<Enter>. -
<Ctrl>+<Z> #26 (SUB)
(. . 2.1.3).
PRN. . -
,
LPT1, LPT2 LPT3. PRN LPT1 .


, LPT1, LPT2, LPT3 ,
1, 2 3.
. LPT1 .

01, COM2 COM2. , -


.
. , , -
,
. 1 AUX.
NUL. , "" .
, , , -
.
-
,
, , , -
, .

12.2.
12.2.1. .
Turbo Pascal :
( text);
( file of );
( file).
-
( ) :
274_________________________________________ II.
type
1 = text;
2 = file of ;
3 = file;
var
1 : 1;
2 : 2;
3 : 3;

var
= text;
2 = file of ;
= file;

, , Turbo Pascal, .
:
type filetype=text; { }
var ftmp,f: filetype; { }

var fl,lst: text; f2: file;


. ,
Turbo Pascal . -
-
.

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

( ).

input output (. . 2.2.1). Input () -
, . Output ()
, .


input output , ,
(. . 12.1).
12. _________________________________________________275

Turbo Pascal -
/ ,
-
.

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


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


(. . 12.4).
,
, -
.
,
.
.
, -
(). ,
, .
-
.
276_________________________________________ II.

12.3.
12.3.1.
,
:
1. ( ). -
,
, , ,
. , , , -
() . ,
( )
text (. . 12.4).
2.
assign ( )
.
3. .
, . .
/ .
. reset (-
), rewrite ().
-
,
.



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

4. . -
.
, -
. -
. -

. () () -
read readln, write writeln (. . 3.1. 3.2).
12, _________________________________________________277

- -
(. . 12.4 12.5). , -
, .
, .
5. -
close. -
, ,
.

12.3.2.
Turbo Pascal , -
: assign, reset, ioresult, rewrite, close, rename, erase,
eof. .

assign (, )
, . .
, -
(
). .
-
. ,
MS-DOS (.
. 12.1). assign
. , -
close. assign
,
assign .
, -
assign close.
reset ()
( )
.
Error 2: File not found ( 2:
).
,
reset .
ioresult . -

/, , -
. -

278_________________________________________ II.

, , , .
, ,
. -
{ $ 1 - } (. . 2.1.4) ioresult (. . 3.9.1)
repeat -
(. 12.3).
rewrite () (-
) . -
.
rewrite -
. , -
, .
-
,
(. 12.3). -
bak (. . 12.1).
close (). g close, -
, -
. -
. , -
.
( ) .
rename (, ) ,
.
.

erase ()
, .
rename erase
. .
, (. -
12.3).
eof () ,
(End Of File) .
true, , -
.
, , -
.
false.
eof ,
-
12. _________________________________________________279

(. 12.1).
. ,
.
fiie01.txt, -
,
. str.
var f: text; str: string;
begin
assign(f,'file01. txt'); rewrite(f);
writeln(f,' );
close(f);
assign(f,'file01.txt'[); reset(f);
readln(f, str);
close{f);
writeln{' / :'); writeln(str);
end.

12.3.3.

-
con (. . 12.2). -
( input), -
( output).
, .
var f : text; str: string;
begin
assign(f, ''); { input }
reset(f);
readln(str);
close(f);
assign(f, ''); { output }
rewrite(f) ;
writeln(f, ' : ',str);
close(f);
end.

, , printer,
-
(. . 8):
280_______________________________________ II.
uses
printer;
begin
writeln(lst,' ...');
end.

lpt1 -
1st , ,
:
Var 1st: text;
begin
assign(lst,'lptl'); { assign(1st,'prn'); }
rewrite(1st);
writeln(1st,'Printing...');
close(1st];
end.

...
, lpt1 (. . 12.1).

12.3.4.

(. 12.2).

12.2.


Getdir
Chdir
Mkdir
Rmdir
Settextbuf /
Flush , ( dos)
Fsplit , ( dos)
Getfattr ( dos)
Getftime ( dos)
Setftime ( dos)

Setfattr ( dos)
12. _________________________________________________281

12.2 ()
,


Filesize (
)
Diskfree (
dos)
Disksize ( dos)
Fexpand ( dos)
Findfirst , -
( dos, findr.ext -
)
Fsearch ( dos)

12.4.
, Turbo Pascal.
-
(. . 2.1.3),
,
char.
"
", :
#13 (CR) (carridge return) #10 (LF)
(line feed). eoln
(End Of LiNe). eoln char. -
readln, writeln -
eoln.


input output .
,
: assign(input,' '} ; reset (input);
assign (output,''); rewrite (output);.
.

char,
, eoln.
282________________________________________ II.

, ,
#26 (SUB) - eof (End Of File) (. . 2.1.3).
-
text () :
type = text;
var : ;

var : text;


-
MS-DOS,
, , Turbo Pascal (. 1)
Windows .
, -
"". MS Word (
) |
DOS (*.txt).

0 255 . -
. -
-
, , -
.
, , -
. -
.

12.4.1.

-
,
(. . 12.3) .
.
: reset rewrite. reset -
. rewrite
append .
append ().
. , ,
.

assign. append
12. _________________________________________________283

. reset rewrite,
append
, .
append ,
eof true. reset
rewrite eof true , ,
false.
:
d (, xl, 2, . . ., xN) ;
readln(};
readln(, xl, x2, ..., xN);

x1,x2,...,XN , -
(integer,real,char,string), read
, ,
. text.
readln , read, -
.
readln () -
(
, ).
.
read readln
, -
readln
eoln ,
.
read readln
. readln read -
.
,
. , -
readln.
:
write(, y1, 2, ..., yN);
writeln();
writeln(, yl, y2, ..., yN);

y1,y2, ...,yN ,
(integer, real, char, string, boolean),
284__________________________________________ II.

, .
(. . 3.2.1).
.
writeln , write, -
eoln.
-
, read (readln) write (writein)
.
, , -
,
read (readln). -
write (writeln) -
.
,
var fl: text; a,b,c: real;
...
readln(fl,a,b,c);

readln -
, f1, , -
,
a,b,c. ,
-
, /. -
-
, .
,
, .
:
eof, true.
(. . 12.3), -
.
1(), -
true,
(CR/LF), false. eof true, eoln
true;

seekoln() eoln,

.
;
12. ________________________________________________285

seekeof () eof,
, -
. -
.
seekeof seekeoln
, -
(. -
12.6).

12.4.2.

, ,
.

:
,
;
, -
;
, -
.

, ,
,
(, ).
, -
.
12.1 , file.txt
, , -
.

12.1. ,

const sumstr: word=0; { }


sumchr: word=0; { }
var f: text; { text }
stroka: string; { )
begin
writeln(' , #');
assign(f,'file.txt');
286 _____________________________________________ II.

{ , }
rewrite(f);
repeat
readln(stroka);
if (stroka <>'#') then writeln (f, stroka); { }
until stroka='#';
close(f);
{ , }
reset (f);
writeln(' :');
while not eof(f) do
begin
readln(f,stroka); { }
writeln(stroka);
inc (sumstr, 1); { }
inc (sumchr, length(stroka)); { }
end;
writeln('B : ',sumstr,', ',sumchr);
close(f); readln;
end.

12.2 , -
, , -
. -
analysis.txt .

12.2.

const sum: word=0; { : }


var fl,f2: text; name,str,search: string[80];
begin
write(' : '); readln(name);
write(' : '); readln(search);
{ f1 f2 }
assign (fl,name);assign(f2,'analysis.txt');
( fl f2 }
reset(fl); rewrite(f2);
writeln(' :'); writeln(f2,' :');
{ fl, }
while not eof(fl) do
begin
readln(f1,str); { str f1 }
if pos(search,str)>0 then
12. _________________________________________________287
begin { search str }
inc(sum) ; writeln (' ():',sum);
writeln(str]; { }
writeln{f2,str); { }
end;
end;
{ }
close(fl); close(f2); readln;
end.


, -
.
(. . 12.3).
ioresult ( 12.3).

, append -
. 12.3 ,
.
.

12.3.

type filetype=text; { }
var
ftmp,f: filetype; { }
txtbuf: string; { )
name: string; { }
ok: boolean; ch: char;
function fileexists(filename : string): boolean;
( , true, }
( false; )
var f: file;
begin
{$i-} assign(f, filename);
reset(f); close(f); {$i+}
fileexists := (ioresult=0) and (filename <>' ') ;
end;
begin
{ )
assign(ftmp,'tmpfile.txt'); rewrite (ftmp);
writeln(' . <Enter>:');
repeat
write('->'); readln(txtbuf); writeln{ftmp, txtbuf);
until txtbuf='';
288_____________________________________ II.

close(ftmp); writeln(' ');


{ }
repeat
write(' : '); readln(name];
ok:=fileexists(name);
if not ok then writeln(' ',name,' ');
until ok;
writeln(' ',name,' ');
{ }
assign(ftmp,'tmpfile.txt'); append(ftmp);
assign(f,name); reset(f);
while not eof(f) do
begin
{ , }
readln(f, txtbuf);
if txtbuf <>'' then writeln(ftmp, txtbuf );
end;
close(f); close(ftmp);
writeln{' ',name);
writeln(' :');
writeln(' , / (erase)');
writeln(' , bak- B/b (backup)');
writeln(' H/h (halt)');
readln(ch);
case ch of
'E','e':
begin
{ }
erase(f); rename(ftmp,name);
end;
B','b':
begin
txtbuf:=name; delete(txtbuf,length(txtbuf)-3,4);
rename(f,txtbuf +'.bak');( bak)
rename(ftmp,name); ( )
end;
'H','h':halt;
end;
end.


tmpfile.txt,
name. -
fileexists name
12. _________________________________________________289

. , -
. name , -
, . append -
tmpfile.txt. while name -
tmpfiie.txt .
name.
case .
, ,
name, a tmpfile.txt name.
name , bak, a
tmpfile.txt name.
name tmpfile.txt, -
( Turbo Pascal) .

-
. 12.4 ,
fiie01.txt , -
file02.txt.

12.4.

var chr: char; fl,f2: text;


begin
assign(fl,'file01.txt'); assign(f2,'file02.txt');
reset(fl); rewrite(f2);
while not eof(fl) do { }
begin
while not eoln(fl) do { }
begin
read(fl,chr); write(f2,chr);
if chr=' ' then { ?}
begin
repeat read (fl, chr) ; until chr <>' ';
write{f2,chr);
end;
end;
readln(fl); writeln(f2); { |
end;
close(fl);close(f2);
end.

-
. , -

290______________________________________Il.

, - , b, .
12.5 ,
h
.
, .

12.5. c

var f1,f2: text;


filenamel, filename2: string[12];
a,b,c,ha,hb,hc,p,buf: real; i:integer;
begin
write(' x: '); readln(filename1);
write(' yy :');
readln(fileme2);
assign(fl,filenme1); reset(f1);
assign(f2,filename2); append(f2);
i:=0
while not eof(f1) do
begin
redln(f1,,b,c) ; inc(i) ;
p:=(+b+c)/2; buf:=2*sgrt(*(-)*(-b)*(-c));
ha:=buf/; hb:=buf/b; hc:=buf/c;
( }
writeln(f2,'--',i:2,'- . :',filenme1:12, --');
writeln(f2,'= ',:7:3,' b= ',b:7:3,' c= ',:7:3);
writeln(f2,'h=',ha::7:3,' hb=',hb:7:3,' hc=',:7:3);
end;
close(fl);close(f2);
end.


-
infile1.txt, , -
infile2.txt,re .
{ infilel.txt } ( infile2.txt )
3 4 5 3 6 8
3 2.2 5 4 6.2 7
7 8 9

c . K
.
12. _________________________________________________291

p a. , -
. p, aa ,
. B "",
. "" . -
,
ax . , "" -
. ,
, , -
. : -
. (
12.6), ,
, ,
.

12.6.

const m=10;
type massiv=array[1..m,1..m] of integer;
var m: integer; matrix1,matrix2:massiv;
procedure square(m:integer; var mas:massiv);
{ - mas )
var i,j:integer;
begin
for i:=1 to m do
begin
ms[i,1]:=1; mas[1,i]:=1;
end;
for i:=2 to m do
for j:=2 to m do
mas[i,j]:=mas[i-1,j]+ms[i,j-1];
end;
procedure inmtrfiletxt(m,n:integer; ms:mssiv; riz:char; name:string);
{ mas }
{ c riz='r' , - }
var i,j:integer; file_text:text;
begin
assign (file_text, name);
if riz='r' then rewrite(file_text) else append (file_text);
writeln(file_text,m:2,n:2); { m,n - - }
292_____________________________________ II Ha
for i:=1 to m do
begin
for j:=1 to n do write(file_txt,mas[i,j}:6);
writeln(file_text) ;
end;
close (file_text)
end;
procedure outmatrfiletxt(var m,n:integer; var mas:massiv; name:string);
{ o a mas name }
var i,j:integer; file_text:text;
begin
i:=1;j:=1;
ssign(file_txt,name); reset(file_text);
while not seekeoln(file_text) do read(file_text,m,n);
readln(file_text) ;
while not seekeof (file_text) do
begin
while not seekeoln(file_text) do
begin
read(file_text,mas[i,j]) ; j:=j+1;
end;
readln(file_text) ;
i:=i+1; j:=1
end;
close (file_text)
end;
procedure outmas(m,n:integer; mas:massiv);
{ outmas - o o a mas a }
var i,j:integer;
begin
writeln(m:2,n:2);
for i:=1 to m do
begin
for j:=1 ton do write(mas[i,j]:7); writeln
end;
end;
begin
writ(' p: ');readln(m);
{ - matrix1 }
squar(m,matrixl);
{ p square.txt }
inmatrfiletxt (m, m, matrix1, ' ,'square.txt');
writeln(' ',m,' a ',m,':');
12. _________________________________________________293

{ p square.txt }
outmatrfiletxt(m,m,matrix2,'square.txt');
( - matrix2 )
outmas(m,m,matrix2);
end.

12.5.
p oopoo , a
, .
p
a,
y p a p,
, , a ,
.
p e -
. o , , p
. a .
. B p - -
, .
p N,
py N+1, , ,
.
- . . , -
.
a o , .

u - , -
.
,
. K o
. c .
- c -
. -
(. . 11),
c (. 12.9).
B p .
a y
- , p .
, , po text, pp -
. a, p Turbo
scal, pa .
294_______________________________________ 11.

-
Turbo Pas-
cal.

file f , - Turbo sl.
:
type = file of ;
var ;

var ee = file of ;
-
file f char . -

. -
readln writeln .

12.5.1.

-
, (. . 12.3)
.
-
: reset rewrite. reset -
, rewrite - -
.
:
-
, , , -
c reset rewrite;
-
read write. readln writeln - . -
,
read write e -
. -
.
.
.
filepos() - -
.
12. _______________________________________________295

. .
- , , reset,
, . -
. -
- ,
. , eof true, filepos
( -
filesize). - longint.
filesize () -
, . , 0.
. -
. - longint.
seek(,) eeee
c , -
. e 0,
seek(, filesize())
, . -
. . -
longint.
truncate() -
. .
. ,
.

12.5.2.
, c ,
.

(. 12.6). ,
-
.
const m=10;
type massiv=array(1..max,1..max] of real;
var m:integer; matrix1,matrix2:massiv;
...
procedure inmatrixfiletyp(m,n:integer; mas:massiv; name:string);
{ mas name }
var
i,j:integer; mrel,nrel:rel; file_typ:file of real;
begin
mreal:=m; nreal:=n;
296_____________________________________ II. H y

assign(file_typ,name); rewrite(file_typ);
write (file_typ,mreal,nreal);
for i:=1 to m do
for j:=1 to n do
write(file_typ,mas(i,j]);
close (file_typ)
end;
procedure outmatrixfiltp(var m,n:integer; var ms:massiv;
nam:string);
{ y mas name }
var i,j:integer; mreal,nreal:real; file t:file of real;
begin
assign(file_ty,name); reset(file_ty);
seek(file_ty,0); read(file_ty,mreal,nreal);
m:=rund(mreal); n:=rund(nreal);
seek(file_t,2);
for i:=1 to m do
for j:=1 to n do
read(file_ty,mas[i,j]);
clse(file_ty);
end;
begin
writ(' : ');readln(m);
{ - matrix1 }
square (m,matrix1);
{ square.dat }
imatrixfilety(m,m,matrixl,'square.dat');
writeln( ' ',m,' a ',m,':');
{ square.dat }
utmatrixfilet(m,m,mtrix2,'square.dat');
{ a - a matrixl }
utmas(m,m,matrix2);
end.

12.7 ,
, .
e . 0. : -
,
, .

12.7.

type file_tye=file of integer;


var f: file_type; sum,mult,r,k1,k2,max: integer;
12. __________________________________________________297

begin
writeln(' , - 0');
{ }
assign(f,'data.dat'); rewrite(f);
repeat
readln(r); if r<>0 then write(f,r);
until r=0;
{ }
seek(f,0); sum:=0; mult:=1;
read(f,r); max:=r; seek(f,filepos(f)-1);
while not eof(f) do
begin
read(f,r); sum:=sum+r; mult:=mult*r;
( }
if max<r then max:=r;
end;
seek(f,1); read(f,k1); { }
seek(f,filesize(f)-2); read(f,k2); { }
lose(f);
( }
writeln(' = ',sum,#10#13' = ',mult);
writeln(' = ',k2-k1,)#10#13' = ',max);
end.


seek(f,0) .
B c reset
. seek(f,filepos(f)-1) ,
read (f,r), ,
c (-
) .

(. . 4.2.3)
.
,
.12.8 ,
k "-
". .

12.8. ""
var f: file of integer; buf1,buf2: integer;
n,k: longint; ok: boolean;
298____________________________________ II.

begin
assign(f,'bube.dat'); rewrite(f);
write(' : '); readln(k);
for :=1 to k
begin
bufl:=random(100); write(f,bufl); write)' ',buf1);
end;
repeat
ok:=true;
for :=0 to k-2
begin
seek(f,n);read(f,bufl);read(f,buf2);
if bufl>buf2 then
{ }
begin
seek(f,n);write(f,buf2);write(f,buf1);
ok:=false;
end;
end;
until ok; ( - )
writeln; writeln(' bubble.dat :');
{ }
seek(f,0); { - c 0 )
for n:=1 to k
begin
read(f,bufl); write)' ',buf1);
end;
lose(f); readln;
end.

-
(. . 11). 12.9
, -
. - c -
, ( , ,
). -
"4" "5" ( "3",
). , -
(c
"2" "3") - , , , ,
.
12. _________________________________________________299

12.9.

uses rt;
type studrec=reord
group : byte;
surname: string[20];
name : string[20];
{ : 3 ( 2 5 ) }
o : rry[1..3] of 2. .5;
end;
foolsrec=record
group : byte;
surname : string[20];
name : string[20];
( }
average: real;
end;
file typ1 = file of studr; file typ2 = file of foolsre;
var f1: file_typ1; f2: file_t2;
nmfile1, namfil2: string[12];
s: studr; { , }
fs: foolsr; { o a }
i,nomr: integer;
proedure rad_dat(var s: studre);
{ }
begin
lrsr;
with s do
begin
writln(' - 0, n ');
write('N : '); rdln(group);
if group <> 0 then
begin
writ('a: '); readln(surnm);
writ(': '); redln(name);
writln(': '); for 1:=1 to 3 do read(oc[i]);
end;
end;
end;
produr opy_data(s: studre; var fs: foolsr);
{ o -o }
begin
with fs do
300____________________________________ 11. Ha

begin
group:=s.group; surname:=s.surnam; name:=s.nam;
{ }
average:=(s.o[1]+s.o[2]+s.[3])/3;
end;
end;
proedure write_list(var f: file_typ2);
{ no }
begin
reset(f); seek(f,0);
writeln (' :');
writeln('', '' :16, '' :8, ' ':18);
while not of(f) do
begin
{ }
red(f,fs);
{ }
with fs do writeln(group:4,surname:l5,nme:10, wrage:16:2);
end;
end;
proedure reate_file (var f: file typ1; var n: integer);
{ no }
begin
n:=0; rewrite(f); read_data(s);
while s.group <> 0 do
begin
{ }
write(f,s);in(n);
{ }
read_data(s);
end;
lose(f) ;
end;
proedure write_dt(var f1: file_typ1; var f2: file_typ2; n: integer);
{ no ,}
{ % }
var priz:har; k:integer;
begin
reset(fl); rewite(f2); k:=0;
while not eof(fl) do
begin
red(f1,s);
{ }
priz:='n',
12. ___________________________________________________301

for i:=1 to 3 do if (s.oc[i]>=2)and(s.oc[i]<=3) then priz:='y';


if priz='y' then
begin data(s,fs); write(f2,fs) end
else inc(k);
end;
close(f1) ; l(f2);
clrscr; writeln(' "4""5" ',k/*100:3:0, '% ');
end;
begin
clrscr;
write(' : '); readln(namefile1);
assign(fl,namefile1);
create file(f1,nomrec);
write(' : '); readln(namefile2);
assign(f2,namefile2);
write_data(f1,f2,nomrec);
write_list(f2);
end.


o , ,
studrec: group,surname,name -
. o
, ,
foolsrec: group,surname,name average . -

create_file, a -
read_data.

write_data, "4" "5".
_data ,

.
write_list.

12.6.

, , -
y .
, , -
, c c -
.
302_______________________________________II.

, ,
, .

,
.
file:
type = file;
var e : ;


var = file;

reset rewrite
, ,
. 128
.
read write
,
. read write -
blockread ( -
) blockwrite ( -
),
.



, 6
. , ,
. ,
o ,
, , .
13



-
, . . -
. ,
-
-
p.

13.1.

- . B
, -
. H ,
,
. B
, -
.
c Turbo Pascal,
. , C -
.
B Turbo Pscl , e -
c -
, -
.

13.1.1.
.
, (-
304______________________________________ II.

).
( , c ). -
, -
.
B Turbo Pascal -

system Mem, MemW MemL , -
(,
).
B , ,
MS-DOS: : . -
c ,
MS-DOS , 64
. :
(
), . -
1 .
, $B800:$000, -
: Mem[$B800:$0000] ,
- MemW[$B800:$0000], -
MemL[$B800:$0000]. , , -
$B8000.
- mem[0:$41]:=mem[0:$41];
. -
. ,
crc (. .
8.3.4).


-
Port[ ] PortW[ ].

-
absolute . B -
. -
absolute. -
. 6 (. -
6.8). B 2
(. 2.1). , -
absolute - ,
.
13. _______________305

B Turbo Pscl -
( ), @,
a d d r .
, @x addr (x) - x .
c
. .
, -
, . e. ,
.
.

13.1.2.
Turbo Pascal
, , -
, Turbo as l C,
(. . 7.1).
,
. -
. 13.l .
(heap)


( )

( )
system
( Turbo Pascal )
, uses


( )

. 13.1.
Turbo Pascal

, ,
Turbo Pascal c -
, ,
306______________________________________ II

.
.
( ),
.

.
.
(, Turbo Pascal 64
), -
. , -
Structure too large -
(. . 4.1).

,
(stck). "" .
,
, -
.
, -
.
Stack overflow, -
( c
).
, , Turbo Pascal
16 , ,
. ,
64 .
- -
,
Turbo Pscl -

. -
, -
() c .
-
, .
- -
, .
,
,
, . K , c
. ,
13. ______________307

,
.


Turbo Pascal -
( MS-DOS 300 ). B -
-
, , Turbo Pascal
(.
. 8.5.3). B :
{$ , ., .}
, ,
, : {$ 65520, 0, 65536} - -
, a 64 .

13.1.3.
B Turbo Pascal :
. - -
, , , .
- ,
.
-:
var p1: ^integer; { }
p2: ^string; { }
p3: pointer; { }

, pointer . B -

p (pointer).
( -
) 4 (,
: ). -
. ,
. , -
-
. :
type massiv=array[1..1000] of real;
var pmassiv=^massiv;

-
. .
308_____________________________________ II.

13.1.4.

B c
:
new (p) ; - ;
getmem(, size); - .

size , , -
. size 64 .
, , , -
(. a. 8.4.7).
.
:
new(p1); new(p2); new(pmas);
getmem(3,200);

new -
,
(. .. 2.1-2.3). ,
, .
-
, getmem ( -
- 200 ), -
.
, 1, p2, 3, pmas -
.
-
, ,
.
,
^ ( ).
:
p1^:=5; p2^:=' '; pmas^[3]:=5.;

new getmem
c . ,
"" nil, ( -
c -
). K , ,
() -
13. ______________309

. ,
. ,
.

:
memavail - ;

maxavail - -
.
:
var p:^real;
. . .
if maxavail>sizeof(real) then new();

-
. -
:
dispose () - , p;
mark() release() -
. -
mark() -
, release() , c -
p. mark , release, -
, .

: freemem(p,size); , size , -
.
,
, . -
nil.
: " -
?" .
:
c ;
c , -
.
-
, .
310___________________________________ II. y

13.2.
-
Turbo Pscl. , -
, -
-
, . , -
, -
, c -
.
-
. -
:
, -
(
);
-
, -
;
, , pecae -
, . e. -
;
c -
, . -
, , , -
c .
, .

, .
, -
.
- ( , ). -
, ,
- , .
. , - -
.
, -
.
, -
. - , -
,
13. ___________________311

c c , .
.
-
. ,
.
: (-
) (. 13.2). B a
, -
.

() -


()

. 13.2.
B , .
,
, . -
nil ( -
). ( )
, -
. . 13.3.

. 13.3.

B , , -
, , .
, . .
: . B
312___________________________________________ II.

-
, -
(il).
. -
. ,
.
C , c -
, - -
. -
:
;
;
( ) ;
( ) ;
;

;
c .
,
. .
- , (
) .
- , -
, .
( c ) - ,
.
, -
, -
(.. 13.1).

13.1. ,
13.1-13.3

item, pitem ()
data ( )

next, prev , (
)
13. ______________ 313
13.1 ()

had,top
1,2


:
type pitem=^item;
item=record
data:... { }
next:pitem;( prev:pitem; }
end;

- , -
(item). , -
.
. ,
( 13.1) .
, -
. -
(, -
). B , ,
, . -
, -
.
, , .

13.1.

type pitem=^item;
item=record { }
data:integer; { }
prv:pitm; { }
end;
var top,:pitem;
n,k:integer;
procedure add(x:integer); { }
begin
new(); { }
^.data:=x; ^.rev=to;
314___________________________________ 11.

top:=p; { p }
end;
procedure deltop; { c )
begin
if top<>nil then begin { }
p:=top^.prev; { }
dispos(top); top:=p; { p }
end;
d;
procedure writestack; { }
begin
writln(' ( c ): ');
p:=top;
while <>nil begin
write(^.data,' '); p:=^.prev;
end;
writeln;
end;
begin { }
top:=nil;
for k:=1 to 10 add(k); { 1 10 }
writestack;
writln{' :'};
radln(n); add();
writstack;
writln(' ?'); readln(n);
for k:=1 to n do deltop;
writstack;
readln
d.


B : -
.
pitm (top) , () -
, -
.
while. p:=p^.prv; -
( -
, a , . . ).
, , .

B ( 13.2)
, -
13. _______________315

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

13.2.

type item=^item;
item=record
data: integer;
next: pitem;
end;
var head,p,pl:pitem;
n, k,l:integer;
procedure add(x,i:integer);
var j:integer;
begin
if (i>0) and (i<=n+1) then begin
new(p); p^.data:=x;
if i=1 then begin
p^.next:=head;head:=p;
end
else begin
p1:=head;
for j:=2 to i-1 do { a i-1- }
p1:=p1^.next;
p^.next:=p1^.next;
p1^.next:=p;
end;
n:n=1;
end;
end;
edure: delitem(i:integer);
var k:integer;
begin
if (i>=1) and (i<=n) and (head<>nil) then
if i=1 then begin { c a }
:=head^.next; dispose(head);head:=p;
end
else begin
p:=head;
316___________________________________I I .

for k:=2 to i-1 do { i-1- }


p:=^.next;
1:=^.next; { p1-i- }
^.next:=p1^.next; { p^.next i+1- }
dispos(1);
end;
end;
procedure writelist;
begin
p1:=had;
writeln(' ');
while 1<>nil do
begin
write(1^.data,' '); 1:=1^.nxt;
end;
writeln;
end;
begin
:=0; head:=nil;
for k:=1 to 10 do add(k,k); ( 1 10 )
wrltequu;
write)' :'); readln(k);
writ(' '); radln(l);
add(k,l); writelist;
write(' :'); rdln(k);
dlitm(k); writlist;
readln
end.



, , -
- -
. , c -
. - ,
. - -
, .

( 13.3)
.
(, c
). : x,
. ,
.
13. ____________317

. , ,
, , ,
.
.

13.3.

type pstack=^stck;
stck=record
dat: chr;
prev: pstack;
end;
var f:text;
ptop, : pstck;
correct :booiean;
1,2:integer;
begin
assign(f,'skobki.txt'); reset(f);
correct: =true;
while (not seekeof(f)) and (correct) do
begin
new(p);
rad(f,p^.dt); { }
cse p^.data of
'(', '{', '[': begin { }
P^.rev:=to; to:=;
end;
')', '}', '] '. { }
if to=nil then correct:=false { }
else begin
cse pto^.dt of

(': c1:=1;
'{': c1:=2;
'[': c1:=3;
end;
cse ^.data of
): c2:=1;
'}': c2:=2;
']': 2:=3;
end;
if 1<>2 then correct:=flse { }
else begin
:=top^.rev; { }
318_________________________________ II.

dispose(ptop);
ptop:=p
end;
end;
end;
end;
if ptop<>nil then correct:=false;) { }
if correct then writeln(' .')
else writeln(' ');
readln
end.

. , -
, .
, :

c ;

. ,
(.. . 4.2.3). B

;
-
( , , );
-
. , -
o , , -

, c .
-
. -
, -
.
, . . c -
.

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

, . ,
, Delhi, -
" " Turbo Pscl.
, ,
, , -
, -
- . -
,
,
, , -
.
, ,
- -
Turbo Pscl. , -


- -
, -
. B , ,
. -
!
320___________________________________________________

B , -
: -
"-"
, , . -
. -
""
. -
c -
- -

. ,
: -
, , ()
. - C. . , ,
- , ,
. , ,
, c .
1



Turbo Pascal

1.1. ,
Turbo Pascal
1.1.1.
Turbo Pscl :
;
;
;
.
-
Turbo Pascal. , -
: , ,
,
.
Turbo sl ( 7.0)
DOS: lnd
Pscl ( .) Turbo Pscl ( Turb.).
- , Brlnd Pascal -
. B , , ,
Brlnd Pscl . B , -
, Turbo Pscl.
- Turbo Pscl :
-
322___________________________________________________________ 1

64 (1 = 1024 ). -
, -
tpu- (. .. 7).

1.1.2.
:
turbo [ ())
, :
, -
.
-
, , ,
.
, -
, . -
<F10>, -
<Alt> , -
.
, . -
(...),
. >,
.

o .
-
.

1.1.3.
-
. , File|
New . B -
,
- NONAME00.PAS ("" ). -
,
.
, , -
, 10-15
, <F2>.

Turbo Pascal___________323

( 8 - ), pas -
.
File | Change
dir... .
, ( ..\
" ").
c
bak. , , "-
" , .
B , , -
File I Save as....

1.1.4.
Edit.
- -
, c (,
).
.
:
Undo , ;
Redo ;
Show clipboard (
Turbo Pscl,
);
Cut, , Paste - , ;
Clear - ( ).
-
- -
(.. 1.1).

1.1.

<Shift>+<>,<>,<> ,< >


-
<Ctrl>+<Del> Edit l

324______________________________________________________ 1

1.1 ()

<Ctrl>+<lns> Edit | Copy



(Shift>+<Del> Edit I Cut

<Shift>+<Is> Edit Paste


<Ctrl>+<L> Search I Search Again


Find ()

<F2> File | Save



<F3> File | Open

- o
( ), ,
.
-
<Entr>. <Esc>.
-
"", -
<Ctrl>+<Brek>.
c , -
Window|Close <Alt>+<F3>.
, -
.

, File|Open
<F3>. *.s -
Turbo Pscl ( s). -
*.*.

1.1.5. c
Turbo Pscl . ,
c . -
p .
- .
Turbo Pascal_____ 325

.
. B . 1.2
.

1.2.

<Alt>+<> -
c
<Alt>+<0> Window | List...
<Alt>+<F3> Window I Close
<Alt>+<F5> Debug User screen
c

<Alt>+<> File | Exit Turbo Pscl

<F6> Window | Next


<Shift>+<F6> Window I Previous

<F5> Window | Zoom

<Ctrl>+<F5> Window |Size move


-
<Esc> <Ctr1>+<>.
File | Exit (<Alt>+<>).

1.2.
,

Turbo Pscl.
-, , -
, -
. , -
, , -
<t> <F1>. <Ctrl>+<F1>
, -
, .
-

.
326________________________________________________________ 1

- -
a (.. 1.3).
1.3.

<F1 > Help | Contents



<F1>,<F1> Help | Using help

<Shift>+<F1> Help | Index

<Alt>+<F1> Help | Previous Topic

<Ctrl>+<F1 > Help | Topic Search


(,
)

1.3. ,


- < F9> <lt>+<F9>. B
, c -

(. . 1.4).
.
o -
. -
, .
,
.
, . ., , -
.


, -
- c .
B ,
, , , a
o Error 85: ; expected ( 85: ";").
Turbo Pascal__________327

11.4. ,
,
cex
(<Ctrl>+<F9>). -
( -
, , readln; ), -
. -
, <Alt>+<F5>.
Turbo Pscl ,
. -
, -
c . -
.
Compile | Desti-
nation , <Enter>, Memory Disk.
,
, c .
, .
() . B -
, -
-
(.. 1.4).

1.4.

<F4> Run | Go to cursor


<F7> Run | into


<F8> Run | Step over
( -
)
<Ctrl>+<F2> Run I Program reset
<Ctrl>+<F4> Debug | Evaluate/Modify...
.

<Ctrl>+<F4>

<Ctrl>+<F7> Debug | Add watch... ,


,
( )
328________________________________________________________________1

1.4.1. c
p popa
var a,b: integer; c: real;
begin
readln(, b);
c:=b/a
writeln ('b/ =', );
end.

0 1. B
200, a ap c:=b/a;.
, :
1. Run into ( <F7>).
, e . B
into .
begin . - -
, . . , .
2. <F7>
readln . . ,
<F7> .
:
Edit ( ) Watches ( ).
Edit. -
Edit Output ( ), -
, , -
Edit .
3. eeex (
Watches) :
<Ctrl>+<F7>, -
dd Watch. , ,
<Ctrl>+<F7>. B -
,
;
a <Enter>: a
Watches. B Watches, ,
:
a: 0
c ,
<Ctrl>+<F7> <Enter>.
Turbo Pascal____________329

B :
: 0.0
a: 0

<Ctrl>+<F7>, b* -
<Enter>. Watches b*:
b*: 0
c: 0.0
a: 0

. -
Watches -
.
4. <F7> -
c:=b/;, Error 200: Division by zero (
200: ).
5. <Ctrl>+<F2> (-
Run | Program reset).
- , -
<F7>, <F8>,
.

1.5.
B
(. . 1.4).
,
. , -
,
.
,
(.. l.5).

1.5.

1 Ms-DOS
2 e Reset,
Append, Rename, Erase

e
3 Reset, Rewrite, Append, Rename, Erase, ChDir, MkDir, RmDir
330____________________________________________ 1

1.5 ()

4 , FILES
Config.ss. : Reset,
Rewrite,Append
5 .
, Read l (" "),
, . : Reset, Append,
Rewrite, Rename, Erase, MkDir, RmDir, Read, BlockRead, Write,
BlockWrite

6
12 . , Reset
Append c
FileMode

15 GetDir
16 . RmDir -

17 Rename , -

18 FindFirst FindNext ,

100 , -

101 ( )

102 c

103 BlockRead,
BlockWrite, Eof, Close, FilePos, Filesize, Flush, Read, Write,
Seek

104 Eof,
EoLn, Read, ReadLn, SeekEof, SeekEoln

105 Write
WriteLn

106 (
)

150

151
Turbo Pascal__________331

1.5 ()

152

154

156

157

158 He

159 B

160

161 c

162

200 ( )

201

202

203

204 c

205 c

206 c

207 c .
sgrt(x) l(x)
,
longint . .

210 .

215

216
2

Turbo Pascal

2.1. CRT
2.1.1.
c 0-127
0 NUL 16 DEL 32 BL 48 0 64 @ 80 P 96 ` 112 p

1 SOH 17 DC1 33 ! 49 1 65 81 Q 97 113 q

2 S 18 DC2 34 50 2 66 B 82 R 98 b 114 r
3 X 19 DC3 35 # 51 3 67 C 83 S 99 115 s
4 20 DC4 36 $ 52 4 68 D 84 T 100 d 116 t
5 ENQ 21 NAK 37 % 53 5 69 85 U 101 117 u
6 22 SYN 38 & 54 6 70 F 86 V 102 f 118 v

7 BEL 23 39 55 7 71 G 87 W 103 g 119 w


8 BS 24 CAN 40 ( 56 8 72 H 88 X 104 h 120 x
9 HT 25 41 ) 57 9 73 I 89 105 i 121 y
10 LF 26 SUB 42 * 58 : 74 J 90 Z 106 j 122 z
11 VT 27 ESC 43 + 59 ; 75 91 [ 107 k 123 {
12 FE 28 FS 44 , 60 < 76 L 92 \ 108 l 124 |
13 CR 29 GS 45 - 61 = 77 93 ] 109 m 125 }
14 SO 30 RS 46 . 62 > 78 N 94 ^ 110 n 126 ~

15 SI 31 US 47 / 63 ? 79 O 95 _ 111 o 127
Turbo Pascal_________________________________333

c 128-255

2.1.2. CRTPLUS
( 2.1)
, crt . -
,
. , .

2.1. , crt
unit crtplus;
interface
uses crt,dos;
const Up = #72; Down = #80; Left = #75; Right = #77;
Enter = #13; Esc = #27; PgUp = #73; PgDn = #81;
type massiv=array(1..20] of string(40];( menu)
procedure savescreen; { }
procedure loadscreen; { }
{ c . : , ,
}
procedure frmewindow(1,1,2,2,frcolor,fon: integer);
334________________________________________________ 2

( .
: , ,
- , , c }
function meu(list:mssiv; x,y,num,fon,marker,text:integer):integer;
implementation
vr screen:rray[l..2000] of word bsolute $800:0; {eoa)
bufer :arry[1..2000] of word; { e}
procedure savescreen;
var k:word;
begin
for k:=1 to 2000 do
bufer[k]:=screen[k];
end;
procedure lodscreen; var
k:word;
begin
for k:=1 to 2000 do
screen[k]:=bufer[k];
end;
{
menu , , a
yp. pepa }
procedure setcursorsize(siz:word);
vr
regs: registers;

begin
with regs do
begin
h:=$01;
ch:=hi(csize) ;
l:=lo(csize);
intr ($10, regs)
end
end;
procedure framewindow(x1, 1,2, 2, frcolor, fon: integer);
{1,1,2,y2 - , , frcolor - }
const frchar:rry[1..6] of char ={ )
(#218,#196,#191,#179,#192,#217);( cep)
vr x,y integer; {x,y - }
w,h:integer;{ w - , h - )
lstttr:byte;{ ,
x lstmode)
Turbo Pascal______________________________ ____335

begin
lastattr:=textattr; ( ]
window(1,1,2,2);
textbackground(fon); clrscr;
window(1,1,2+1,y2);
textcolor(frcolor);
gotoxy(1,1); write(frchar[1]);
w:=x2-x1+1;h:=y2-y1+1;( )
for x:=1 to w-2 do write(frchr[2]);{ ]
write(frchr[3)); ;
for :=2 to h-l do begin { }
gotoxy(w,y); write(frhr[4]);
gotoxy(1,y); write(frchar[4]);{}
end;
gotoxy(1,h); write(frhar[5]);
for x:=1 to w-2 do write(frchr[2]); { }
write (frchar[6]);
window(1+1,y1+1,2-1,y2-1); gotoxy(1,1);
textttr:=lstattr;
end;
function menu(list:massiv; x,y,num,fon,marker,text:integer):integer;
var mx,k:integer;
c; chr;
oldtext;word;
begin
oldtext:=textattr;
svescreen; { p}
textcolor(text);
mx:=0;
for k:=1 to num do { }
if length(list[k])>mx then mx:=length(list[k]);
for k:=1 to num do { oea}
while length(list[k])<max do
lit[k]:=list[k]+' ',
framewindow(x,y;+max+3,y+num+l, text, fon);
textbckground(fon];
SetCursorsize($0l00); { pp c }
gotoxy(1,1);
textbackground(marker);
write(' '+list[1]);
textbckground(fon);
for k:=2 to num do begin
writeln; write(' '+list[k]);
end;
336_____________________________________________________ 2

k:=1;
repeat
c:=readkey;
case c of
up, down:
begin
gotoxy(1,k);
textbackground(fon) ; write(' '+list[k});
if c=up then
if k>l then k:=k-1
else k:=num
else
if k<num then k:=k+1
else k:=1;
gotoxy(1,k);
textbackground(marker); write(' '+list[k]);
end;
esc: menu:=0;
enter: menu:=k;
end;
until (c=esc) r (c=enter) ;
SetCursorSize($0C0E); { yp}
windw(l,1,80,25); {e }
loadscrn; { }
txtattr:=oldtext; { }
end;
end.

2.2.
a Turbo Pascal
K , Turbo Pascal c -
, . (
2.2) , MS-DOS -
$33.

2.2.

unit mouse;
interface
uses dos;
Turbo Pascal_______________________________________337

( pa )
procedur initmouse(var success:boolean; {true - p
, fals - a }

var num:integer); ( )
{ yp a }
procedur showcursor;
{ a }
rocedur hidecursor;
{ }
rocedure mousecondition
(var x,y:integer; {e }
var lft, {
(tru - , false - e)}
right:boolean); ( }
{ c x y}
rocedur utcursor (, : integr);
{ c
}
function leftnum
(vr x,y:integer):word; { }
( e )
function rightnum(vr x,y:integer) :word;
( e coe ( ))
function midlenum(vr x,y:intgr) :word;
imlementtion
vr reg: registers;
rocedure initmouse;
bgin
reg.: =0;
intr ($33, reg) ;
if rg.ax=0 thn success:=flse
else success:=tru;
num:=reg.bx;
end;
rocedur showcursor;
begin
reg.: =1;
intr($33, reg);
end;
rocdure hidecursor;
begin
reg .x:=2;
intr($33,reg) ;
end;
338_____________________________________________________ 2

procedure mousecndition;
begin
reg.: =3;
intr($33,reg);
x:=reg.cx; y:=reg.dx;
if reg.bx and 1=0 then left:=flse
else left:=true;
if rg.bx and 2=0 then right:=false
else right:=true;
end;
rocedure utcursor;
begin
with reg do
begin
ax:=4; cx:=x; dx:=;
end;
intr ($33, reg);
end;
{ () }
function getnum(k:integer; ( (0, 1 2))
vr x,y:integer) :word; (
)
begin
reg.x:=5;
reg.bx:=k;
intr ($33, reg) ;
x: =reg.cx;
y:=reg.dx;
getnum:=reg .bx;
end;
function leftnum;
begin
leftnum:=getnum(0,x,y);
end;
function midlenum;
begin
midlenum:=getnum(2,x,y);
end;
function rightnum;
begin
rightnum:=getnum(1,,y);
end;
begin
end.
Turbo Pascal_____________________________________339

2.3. GRAPH-

( 2.3) a pa pa -
(16 256 ). , pa
256- ( 16- ap 256 )
p . -
Vesa256.bgi Svga256.dgi, , -
.
InstallUserDrivr, pp:
var
gd,gm :integer;
bgin
gd:=instlluserdriver('svga256',nil);
initgrah(gd,gm, '' );

2.3. c bmp-

unit ;
{
*********************************************
:
16 ( RLE)
256 ( RLE)
*********************************************
}
interface
const
{ , ShowBitMaplmage}
BmOk = 0; {}
BmUnknownrror = -1; { }
BmpFilNotFound = -2; { }
BmNotsuMode = -3; (
}
BmNotSuFmt = -9; { }
( }
RED = 0; GREEN = 1; BLUE = 2;
{ bmp- BMPFilNme (,).
FirstDcReg ,
16- c ( 256- )
, .
1- 256- , }
340____________________________________________________________ 2

function showBitMalmage(BMPFileNme : String; X, Y : Word;


FirstDcReg: Integer) : Integer;
implementation
uses Dos, Graph;
type
TPaletteEntry = record ( )
B,G,R: Byte; (, , }
Flags: Byte;
end;
TBitmaFilHeader = record ( bm-)
bfTye: Word;
bfSize: Longint;
bfReserved1: Word;
bfRrved2: Word;
bfOffBits: Longint;
biSize: Longint;
biWidth, biHeight : Longint;
biPlanes: Word;
biBitCount: Word;
biComression: Longint;
biSizeImage: Longint;
biXPelsPrMter, biYPlsPerMeter: Longint;
biClrused, biClrImortant: Longint;
end;
TPlArra = (0..255, RED. .BLUE] of Byte;
vr
F : File;
bfh : TBitmFilader; {3aooo bm-}
Pal : rra(0..255] of TPletteEntry; {}
function OenFile(const BitmaNme:string) : Boolen;
begin
OpenFile := False; Assign(F, BitmaName);
($I-} Reset(F, 1); ($I+)
if IoResult <> 0 thn OenFile:=False else OenFile:=True;
end;
function CloseFile: Boolean;
begin
{$I-} Clos(F); ($I+}
if IoResult <> 0 then CloseFil:=False else CloseFile:=True;
end;
function ReadBMPFilHed : Boolean;
begin
ReadBMPFileed := Flse;
Turbo Pascal____________________________________341

{$I-} BlockRead(F, bfh, Size0f(bfh)); {$I+}


if (IoResult <> 0) or (bfh.bfTye <> $4D42) then Exit;
ReadBPFild := True;
end;
function RadPalett(Palsize:Integer) Boolean;
var C : Byte;
begin
RedPltte := False;
{$I-} BlockRed(F, Pl, Palsize*4); {$I+}
if IoResult <> 0 then Exit;
RadPalette:=True;
end;
procedure SetPalette(Plsize: Integer; FirstDacReg: Integer);
vr
Plette : TPlArry;
Rg : Registers;
i : Byt;
begin
if Getlor>255 then xit;
for i := 0 to Palsize-1 do begin
Palette[i, RED] := Pal[i].R SHR 2;
Palette[i, GREEN] := Pal[i].G SHR 2;
Plette[i, BLUE] := Pa1[i].B SHR 2;
end;
Reg.h := $10; { }
R e g. al := $12;
Reg.bx := FirstDacRg; { e }
Reg.cx := PlSize; { }
Reg.dx := Ofs(Paltte);
Reg.es := Seg(Palette);
Intr($10, Reg); { ne }
end;
{ 1-eoo c}
function ShowImge4(Plffset:Integer; X$trt,YStart:Word): Boolen;
var
, C0, C1 : Byte;
Lin4 : [0..1023] of Byt;
col: LongInt;
Width, Height, xt, yt, W2 : Word;
begin
ShwImg4 := Flse;
Seek(F, bfh.bf0ffBits) ;
Width := bfh.biWidth; Height := bfh.biHeight;
342______________________________________________________2

{e , 8}
while (Width mod 8)<>0 do inc(Width);
W2:= (bfh.biwidth-1) div 2;
for yt := Height-1 downto 0 do begin
{$I-} B1ockRead(F, Lin4, Width div 2); {$I+}
if IOResult<>0 then exit;
{ no a}
for xt :=0 to W2 do begin
Px := Lin4[xt]; CO := Px shr 4;
Px := (Px shr 4) + (Px shl 4); C1:=Px shr 4;
PutPixel(XStart+xt*2, Ystart+yt, C0+PalOffset);
PutPixel(Xstart+xt*2+1, Ystart+yt, C1+Pal0ffset);
end
end;
ShowImage4 := True;
end;
{ 256- }
function ShwImage8(PaiOffset:Integer; XStart,YStart:Word): Boolean;
type
TLi8 = record
X, Y : Word;
Data : rry[0..1023) of Byte;
end;
var
Lin8 : ^TLin8;
i: Integer;
1, col: Longint;
Width, Height, xt, yt, sizeP : Word;
begin
Showlmage8 := False;
Width := bfh.biWidth; Height := bfh.biHeight;
{ , 4}
while (Width mod 4)<>0 do inc(Width);
Seek(F, bfh.bfOffBits);
SizeP := sizof(TLin8); GtMem(Lin8, SizeP);
Lin8^.X := bfh.biWidth-1; Lin8^.Y := 0;
for yt := Height-1 downto 0 do begin
{$1-} BlockRead(F, Lin8^.Data, Width); {$I+)
if IOResult <> 0 then exit;
{ no a}
for i:=0 to Width-1 do
PutPixel(Xstart+i, Ystart+yt,Lin8^.Data[i]+PalOffset)
end;
Turbo Pascal__________________________343

FreeMem(Lin8, SizeP); Showlmage8 := True;


end;

function ShowBitMaplmge(BMPFileName : String; X, Y : Word;


FirstDacReg:Integer) : Integer;
var
MaxC: LongInt;
xt, yt : Word;
begin
:= GetMaxColor;
if (<>15)nd(M<>255) then begin
ShowBitMapImage := BmpNotSuppMode; exit;
end;
ShowBitMapImage := BmpFileNotFound;
if not OpenFile(BMPFileName) then Exit;
ShowBitMapImage := BmpNotSuppFmt;
if not ReadBMPFilHd then Exit;
ShowBitMaplmage := BmpNotSuppMode;
case bfh.biBitCount of
4 : begin
ReadPalette(16);
SetPalette(16,FirstDacReg);
ShowImge4(FirstDacReg,X,Y);
ShowBitMapImage := BmpOk;
end;
8 : begin
ReadPalette(256);
SetPalette(256,FirstDacReg);
Showlmge8(FirstDacReg,X,Y);
ShowBitMapImage := BmpOk;
end;
end;
CloseFile;
end;
end.

1. C. A. / C. A. , E. B. . - M.:
, .. . .-. ., 1989. - 256 c.
2. B. E. . -
: . / B. E. , . C. -
, . . ; .. . B. . - M.: . ., 1991. - 400
c.
3. . . . c .. - M.: , 1991. -
192 c.
4. . C. : . -
/ . C. , A. H. , . . . - .: . ..,
"", 1991. - 365 c.
5. H. + = . . c .. - .:
, 1985. - 406 c.
6. . . . c . - M.: , 1989. -
360 c.
7. r B. A. : . . . c
. / B. A. , . K. , K. . . - M.: ,1993. -
255 c.
8. K. .
. c .. / K. , H. .- M.: , 1982. -
150 c.
9. H. . Turbo l 7.0 Delphi.
, / H. . . - .: -
-, 1999. - 416 c.
10. H. . Turbo Pscal . - .: - -
, 2000. - 256 c.
_____________________________________________345

11. C. B. Turbo sl - -
. - M.: , 1992. - 192 c.
12. C. A. Turbo sl: . - : , 2001. - 256 c.
13. . B. B: .
. / . B. , A. . , B. A. ; .
.. . B. . - .: . ., 1993. - 256 c.
14. O. H. . - .:
, 1988. - 224 c.
15. B. . Turbo sl . 7.0. - M.:
, 1996. - 446 c.
16. B. B. -
. 2-e .. - M.: - , 1992. - 448 c.

Оценить