of 73 /73
Η  Pascal  με παραδείγματα George Hadjikyriacou – 2007
• Author

evaplyta
• Category

## Documents

• view

62

1

Embed Size (px)

### Transcript of Tutorial xatzikyriakou-pascalby example

• Pascal

[email protected]

• 1 32 83 134 195 256 297 348 ... 399 (BGI) 4410 (BGI) 5311 58

61 65

2

• 1

3

• Pascal Compiler

Pascal. CompilerBloodshedDevPascal 1.9.2.

.. Pascal .

Program _ ;

Begin End.

1

HelloWorld!!!.

:

ProgramLesson1_Program1;UsesCrt;

BeginClrscr;Write('HelloWorld!!!');{}

ProgramLesson1_Program1;

Lesson1_Program1

4

• Begin

Clrscr; .

Write('HelloWorld!!!');

('.')

Enter .

End.

1.

;2. {}

compiler.

1. DevPascal2. File NewSourceFile3.

5

• 4. compile ( ) ExecuteCompile

5. ( Totalerrors:0) ExecuteRun. :

6

• . .

Compiler. (

)

7

• 2

8

• ( variable)

, . .

Pascal:

1. integer , 32768 32767 2. LongInt , 2^31 2^31

13. real , 3.4x10 38

3.4x1038

4. char , : 'a','B','+'

5. Boolean True/False6. String ( 255

) : Hello

constant

. .

:

Const_ = ;

1

( )

( ) . :

ProgramLesson2_Program1;

9

• VarNum1,Num2,Sum:Integer;

1 ( 3) .

VarNum1,Num2,Sum:Integer; Var

( variable), ( Num1,Num2,Sum)

(: Integer).

.

Sum:=Num1+Num2; Sum Num1 Num2( ).

Writeln('Sum:',Sum); ,

Sum.

10

• 3. Write , Writeln

.

4. := .

5. :+( ),( ), *( ),/( ),()( )

2

ProgramLesson2_Program2;Varname:String;

11

• Pascal

.

1+ 2+ 3 =

3

12

• 3

13

• H IF THEN

:

If thenBegin End;

IFTHEN .

. :

a 0 0

0 0.

Pascal.

3

H IF THEN ELSE :

If thenBeginEnd

elseBeginEnd;

ELSE( ) IFTHEN IF .

: b 100

Error.

2

15

AND

OR. AND OR

. :

c 100 KAI 0

.

3

• H CASE

IF, .

4

'a': Begin writeln('alpha'); End;

'b': Begin writeln('beta'); End;

End ; End . End .

17

• 1. Pascal .

2. Pascal .

. Case. :

1.EURCYP2.CYPEURSelect>_

18

• 4

19

• .

: For,While.do Repeat.Until.

FOR

:

For:=todoBegin ; End;

1

1 10.

count( integer) 1

10( 10 ),

count 1,2,3,4,5,6,7,8,9,10.

2

10 1.

20

1 count ( downto).

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

While . do

:

while doBegin ; End;

3

0 9.

ProgramLesson4_Program3;vara:integer;begina:=0;

whilea

a:=0;.

a 10 a 1 a 0,1,2,3,4,5,6,7,8,9. a

• RepeatUntil

:

Repeat ; Until ;

4

ProgramLesson4_Program4;vara:integer;begina:=0;

Repeatwriteln(a);a:=a+1;Until(a>9);

3 a:=0;.

1 a a 9

a 0,1,2,3,4,5,6,7,8,9.

a 10( a>9)

.

!

, , .

23

• Pascal() 10

.

24

• 5

25

• Arrays

.

5 myarray

: myarray:array[1..5]ofinteger;.

myarray[1] myarray[2] myarray[3] myarray[4] myarray[5]4 7 1 3 6

myarray myarray[1] 4

myarray[5] 6.

myarray:

.

myarray[2]:=7;

myarray:

.

write(myarray[3]);

26

• .

1

.

ProgramLesson5_Program1;

varmyarray:array[1..5]ofinteger;count1,count2:integer;

begin

forcount2:=1to5dobeginwrite(myarray[count2]);end;

1. Pascal pin1 10 ,

.

2. Pascal pin2 5 ,

27

• ( ).

3. Pascal pin3 5 ,

.

FOR .

28

• 6

29

• procedures

,

. ( functions)

.

1

ProgramLesson6_Program1;

proceduredia(aa,bb,cc,dd:integer);beginwriteln('aa:',aa,'bb:',bb,'cc:',cc,'dd:',dd);end;

:

4. dia(10,22,37,49); 10,22,37,49.

5. 10,22,37,49 dia(aa,bb,cc,dd:integer); aa,

bb,cc,dd6.

. 7. [ ENTER] .

:

2

ProgramLesson6_Program2;

30

• vara,b:integer;

procedureprosthesi(aa,bb:integer);beginwriteln('prosthesi:',aa+bb);end;

procedureaferesi(aa,bb:integer);beginwriteln('aferesi:',aabb);end;

:

1. a b.

2. prosthesi(a,b);

. 3. aferesi(a,b);

.

4. [ ENTER] .

functions

31

• .

3

ProgramLesson6_Program3;vara,b,pro,afe:integer;

functionprosthesi(aa,bb:integer):integer;beginprosthesi:=aa+bb;end;

functionaferesi(aa,bb:integer):integer;beginaferesi:=aabb;end;

:

1. a b.

2. prosthesi(a,b);

pro .

32

• 3. aferesi(a,b);

afe . 4. . 5. [ ENTER] .

1. 1 dia(10,22,37,49); 10 (

).

2. ( function) Pascal

.

.

33

• 7

34

• .

( sequentialfiles).

1

ProgramLesson7_Program1;VarFName,Txt:String;UserFile:Text;

UserFile:Text ( text).

Assign(UserFile,FName); .

( ). FName UserFile .

35

• Rewrite(UserFile); FName ( ).

Append(UserFile); (

!!! ) .

Writeln(UserFile,Txt); ( ).

Close(UserFile); FName( ).

2

ProgramLesson7_Program2;VarUserFile:Text;FileName,TFile:String;

36

• Reset(UserFile); FileName .

UntilEof(UserFile);.(EofEndoffile)

Close(UserFile); FileName( ).

EOF.

( !!! )

3

ProgramLesson7_Program3;

VarUFile:Text;FileName:String;

Assign(UFile,FileName);

37

• Erase(UFile);End.

Erase(UFile); FileName .

(procedure) pascal : , , email,

( Append).

. ,

.

:

Onoma:Epitheto:.email:[email protected]:99123456

38

• 8

39

• Unit Pascal

, crt.

Program

:

Usescrt;

Textcolor(int);

textcolor(int);

. :

textcolor(int);

int 0 15 .

:

0Black1DarkBlue2DarkGreen3DarkCyan4DarkRed5Purple6Brown7LightGrey8DarkGrey9LightBlue10LightGreen11Cyan12LightRed13Pink

40

• 14Yellow15White

textcolor(14); !

41

• 1

programLesson8_Program1;

Usescrt;

delay(200); 200 ms(1000ms=1sec)

clrscr;

textBackground(int)

textcolor(int); background

. :

Background(int);

42

• int 0 7 .

:0Black1DarkBlue2DarkGreen3DarkCyan4DarkRed5Purple6Brown7LightGrey

2

programLesson8_Program2;

Usescrt;

43

• 9

(BGI)

44

• compiler BorlandTurboPascal7( DOS).

BGI BorlandGraphicsInterface. BGI 640 x480(pixels)

USESCRT,GRAPH;

VAR

GD,GM:INTEGER;

BEGIN InitGraph(GD,GM, '..\BGI');

{ }

CloseGraph;END.

Pascal

Line(x,y,xx,yy);

Circle(x,y,size);

Rectangle(x,y,xx,yy);

SetColor(int);

45

• SetBkColor(int);

SetFillStyle(style,color);FloodFill(x,y,stop_color);

Outtextxy(x,y,'TextHere');

Line(x,y,xx,yy);. x,y

( pixel), xx,yy .

1

USESCRT,GRAPH;

VAR

GD,GM:INTEGER;

:

46

• Circle(x,y,size);. x,y

( pixel), size .

2

USESCRT,GRAPH;

VAR

GD,GM:INTEGER;

:

47

• Rectangle(x,y,xx,yy);. x,y

( pixel), ( ) xx,yy

( ).

3

USESCRT,GRAPH;

VAR

GD,GM:INTEGER;

:

48

• SetColor(int);

SetBkColor(int);. int

(015) 8.

4

USESCRT,GRAPH;

VAR

GD,GM:INTEGER;

BEGINInitGraph(GD,GM,'..\BGI');SetColor(14);{xromasxhmatoskitrino}SetBkColor(1);{xromafontoumple}

:

49

• 2 .

SetFillStyle(style,color); style(110) (015

8), FloodFill(x,y,stop_color);

x,y stop_color(015 8).

5

USESCRT,GRAPH;

VAR

GD,GM:INTEGER;

BEGINInitGraph(GD,GM,'..\BGI');cleardevice;

SetColor(4);{kokkino}Circle(100,100,50);SetFillStyle(1,3);FloodFill(101,101,4);

50

• CloseGraph;END.

:

:)

Outtextxy(x,y,'TextHere'); x,y

.

6

USESCRT,GRAPH;

VAR

GD,GM:INTEGER;

51

• :

Pascal ( ). ( ):

.

52

• 10

(BGI)

53

• .

1

PROGRAMLesson9_Program1;

USESCRT,GRAPH;

VAR

GD,GM,i:INTEGER;

InitGraph(GD,GM,'..\BGI');

END.

cleardevice; .

54

• 2

PROGRAMLesson9_Program2;

USESCRT,GRAPH;

VAR

GD,GM,i:INTEGER;

InitGraph(GD,GM,'..\BGI');

fori:=10to350dobegincleardevice;outtextxy(i,240,'=TEST');delay(1000);end;

fori:=350downto10dobegincleardevice;outtextxy(i,240,'TEST=');delay(1000);end;

END.

55

• 3

Screensaver

PROGRAMLesson9_Program3;

USESCRT,GRAPH;

VAR

GD,GM,i:INTEGER;

InitGraph(GD,GM,'..\BGI');

END.

Randomize;

56

• .

random(int); 1 int.

Pascal( screensaver) ,

.

57

• 11

58

• compiler BorlandTurboPascal7( DOS).

Pascal Sound(int), int

Hz(Hertz).

1

programLesson11_Program;

USESCRT;

{dilonoumetissixnotitessestatheres}constC=262;D=294;E=330;F=349;G=392;A=440;B=494;H=524;{dilonoumestatheratindiarkiasemillisecond}MS=20000;

beginSound(C);Delay(MS);NoSound;Sound(D);Delay(MS);NoSound;Sound(E);Delay(MS);NoSound;

59

• Sound(F);Delay(MS);NoSound;Sound(G);Delay(MS);NoSound;Sound(A);Delay(MS);NoSound;Sound(B);Delay(MS);NoSound;Sound(H);Delay(MS);NoSound;end.

60

• 61

• x:=y+z; x:=yz; x:=y*z; x:=y/z;

x:=ydivz; x:=ymodz;

SQR(RealVariable) SQRT(RealVariable) SIN(Realvariable) COS(Realvariable) ARCTAN(Realvariable)

62

• ASCII

63

• 64

• 65

• 2

3

66

4

• varcount1,count2,sum:integer;begin

forcount2:=1to10dobeginsum:=sum+pin1[count2];end;

forcount2:=1to5dobeginifpin2[count2]

min:=pin3[1];

forcount2:=2to5dobeginifpin3[count2]

• beginmikroteros:=bb;end;if(aa=bb)thenbeginmikroteros:=bb;end;end;

7

UsesCrt;

procedureinsert();

VarUserFile:Text;name,surname,mail,phone:String;yn:char;

begin

Assign(UserFile,'C:\Phonebook.txt');{ToarxeioC:\Phonebook.txtprepeinayparxeigianamporeseinagineiAppend!!!}Append(UserFile);

Write('EMail:');

70

Writeln(UserFile,'Onoma:'+name);Writeln(UserFile,'Epitheto:'+surname);Writeln(UserFile,'EMail:'+mail);Writeln(UserFile,'Tilefono:'+phone);Writeln(UserFile,'');Close(UserFile);

if(yn='y')thenbeginclrscr;{Katharismostisothonis}insert();endelsebeginWrite('ByeBye...');exit;{exodosapotoprogramma}end;

end;

9

PROGRAMBGI_HOUSE;

USESCRT,GRAPH;

VAR

GD,GM:INTEGER;

BEGIN

71

10

PROGRAMLesson10_Program;

USESCRT,GRAPH;

VAR

GD,GM,i:INTEGER;