Tutorial xatzikyriakou-pascalby example

of 73 /73
Η  Pascal  με παραδείγματα George Hadjikyriacou – 2007 [email protected]

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!!!');{}

    Readln;End.

    ProgramLesson1_Program1;

    Lesson1_Program1

    4

  • Begin

    Clrscr; .

    Write('HelloWorld!!!');

    ('.')

    Readln; Readln

    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;

    BeginWrite('Inputnumber1:');Readln(Num1);Write('Inputnumber2:');Readln(Num2);Sum:=Num1+Num2;Writeln('Sum:',Sum);Readln;End.

    1 ( 3) .

    VarNum1,Num2,Sum:Integer; Var

    ( variable), ( Num1,Num2,Sum)

    (: Integer).

    Readln(Num1); Readln( _ )

    .

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

    Writeln('Sum:',Sum); ,

    Sum.

    10

  • 3. Write , Writeln

    .

    4. := .

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

    2

    ProgramLesson2_Program2;Varname:String;

    BeginWrite('WhatisyourName?:');Readln(name);Writeln('Hello,',name,'areyouOK?');Readln;End.

    11

  • Pascal

    .

    1+ 2+ 3 =

    3

    12

  • 3

    13

  • H IF THEN

    :

    If thenBegin End;

    IFTHEN .

    . :

    a 0 0

    0 0.

    Pascal.

    3

    ProgramLesson3_Program1;Vara:Integer;BeginWrite('Dosearithmo:');Readln(a);if(a>0)thenBeginWrite('Thetikos');End;if(a

  • Write('Arnitikos');End;if(a=0)thenBeginWrite('Edoses0');End;Readln;End.

    H IF THEN ELSE :

    If thenBeginEnd

    elseBeginEnd;

    ELSE( ) IFTHEN IF .

    : b 100

    Error.

    2

    ProgramLesson3_Program2;Varb:Integer;BeginWrite('Dosearithmo:');Readln(b);if(b>=100)then

    15

  • BeginWrite('OK');EndelseBeginWriteln('Error');End;Readln;End.

    AND

    OR. AND OR

    . :

    c 100 KAI 0

    .

    3

    ProgramLesson3_Program3;Varc:Integer;BeginWrite('Dosearithmo:');Readln(c);if(c>=0)and(c

  • H CASE

    IF, .

    4

    ProgramLesson3_Program4;Varc:char;BeginWrite('Dosea,borc:');Readln(c);casecof

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

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

    'c': Begin writeln('gamma'); End;End;Readln;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.

    ProgramLesson4_Program1;varcount:integer;beginforcount:=1to10dobeginwriteln(count);end;Readln;End.

    count( integer) 1

    10( 10 ),

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

    2

    10 1.

    20

  • ProgramLesson4_Program2;varcount:integer;beginforcount:=10downto1dobeginwriteln(count);end;Readln;End.

    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

  • Readln;End.

    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);

    Readln;End.

    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;

    readln(myarray[2]);

    myarray:

    .

    write(myarray[3]);

    26

  • .

    1

    .

    ProgramLesson5_Program1;

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

    begin

    forcount1:=1to5dobeginreadln(myarray[count1]);end;

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

    readln;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;

    begindia(10,22,37,49);readln;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;

    beginWrite('Doseton1oakereo:');readln(a);Write('Doseton2oakereo:');readln(b);prosthesi(a,b);aferesi(a,b);readln;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;

    beginWrite('Doseton1oakereo:');readln(a);Write('Doseton2oakereo:');readln(b);pro:=prosthesi(a,b);afe:=aferesi(a,b);writeln('Prosthesh:',pro,'Aferesh:',afe);readln;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;

    BeginWrite('Doseonomaarxeiou(px.C:\test.txt):');readln(FName);Assign(UserFile,FName);Rewrite(UserFile);Writeln('Grapsetokimeno:');Readln(Txt);Writeln(UserFile,Txt);Close(UserFile);End.

    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;

    BeginWrite('Doseonomaarxeiou(px.C:\test.txt):');readln(FileName);Assign(UserFile,FileName);Reset(UserFile);RepeatReadln(UserFile,TFile);Writeln(TFile);UntilEof(UserFile);Close(UserFile);Readln;End.

    36

  • Reset(UserFile); FileName .

    RepeatReadln(UserFile,TFile);Writeln(TFile);UntilEof(UserFile);

    Readln(UserFile,TFile); Writeln(TFile);

    UntilEof(UserFile);.(EofEndoffile)

    Close(UserFile); FileName( ).

    EOF.

    ( !!! )

    3

    ProgramLesson7_Program3;

    VarUFile:Text;FileName:String;

    BeginWrite('Doseonomaarxeiou(px.C:\test.txt):');readln(FileName);

    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;

    vari:integer;beginfori:=0to15dobegintextcolor(i);delay(200);clrscr;writeln('HelloWorld!!!');end;readln;end.

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

    clrscr;

    textBackground(int)

    textcolor(int); background

    . :

    Background(int);

    42

  • int 0 7 .

    :0Black1DarkBlue2DarkGreen3DarkCyan4DarkRed5Purple6Brown7LightGrey

    2

    programLesson8_Program2;

    Usescrt;

    vari:integer;beginfori:=0to7dobegintextBackground(i);delay(100);writeln('HelloWorld!!!');end;readln;end.

    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;

    BEGINInitGraph(GD,GM,'..\BGI');line(0,0,100,100);readln;CloseGraph;END.

    :

    46

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

    ( pixel), size .

    2

    USESCRT,GRAPH;

    VAR

    GD,GM:INTEGER;

    BEGINInitGraph(GD,GM,'..\BGI');circle(100,100,50);readln;CloseGraph;END.

    :

    47

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

    ( pixel), ( ) xx,yy

    ( ).

    3

    USESCRT,GRAPH;

    VAR

    GD,GM:INTEGER;

    BEGINInitGraph(GD,GM,'..\BGI');rectangle(50,50,100,100);readln;CloseGraph;END.

    :

    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}

    rectangle(50,50,100,100);readln;CloseGraph;END.

    :

    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);

    readln;

    50

  • CloseGraph;END.

    :

    :)

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

    .

    6

    USESCRT,GRAPH;

    VAR

    GD,GM:INTEGER;

    BEGINInitGraph(GD,GM,'..\BGI');outtextxy(50,50,'GEORGE');readln;CloseGraph;END.

    51

  • :

    Pascal ( ). ( ):

    .

    52

  • 10

    (BGI)

    53

  • .

    1

    PROGRAMLesson9_Program1;

    USESCRT,GRAPH;

    VAR

    GD,GM,i:INTEGER;

    BEGINwrite('Press[Enter]toStart...');readln;

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

    fori:=10to350dobegincleardevice;outtextxy(i,240,'=TEST');delay(1000);end;outtextxy(0,0,'Press[ENTER]toExit...');readln;CloseGraph;

    END.

    cleardevice; .

    54

  • 2

    PROGRAMLesson9_Program2;

    USESCRT,GRAPH;

    VAR

    GD,GM,i:INTEGER;

    BEGINwrite('Press[Enter]toStart...');readln;

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

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

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

    outtextxy(0,0,'Press[ENTER]toExit...');readln;CloseGraph;

    END.

    55

  • 3

    Screensaver

    PROGRAMLesson9_Program3;

    USESCRT,GRAPH;

    VAR

    GD,GM,i:INTEGER;

    BEGINRandomize;write('Press[Enter]toStart...');readln;

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

    fori:=0to500dobeginsetcolor(random(15));line(random(640),random(480),random(640),random(480));delay(5000);end;cleardevice;setcolor(15);outtextxy(0,0,'Press[ENTER]toExit...');readln;CloseGraph;

    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

    ProgramMathima2_Askisi1;Varnum1,num2,num3,sum:integer;Varmesos:real;Beginwrite('inputnumber1:');readln(num1);write('inputnumber2:');readln(num2);write('inputnumber3:');readln(num3);mesos:=(num1+num2+num3)/3;writeln('mesosoros:',mesos);readln;End.

    3

    ProgramMathima3_Askisi1;vara,b:integer;Beginwrite('dose1ari8mo:');readln(a);write('dose2ari8mo:');readln(b);If(a>b)thenbeginwrite(a);end;if(a=b)thenbeginwrite('isoi!');endelsebeginwrite(b);end;readln;end.

    ProgramMathima3_Askisi2;Varsel:integer;Vara,met:real;Begin

    66

  • Writeln('1.EURCYP');Writeln('2.CYPEUR');Write('Select>');Readln(sel);caseselof1:Beginwrite('doseEUR:');readln(a);met:=a*0.58;writeln('CYP:',met)End;2:Beginwriteln('doseCYP:');readln(a);met:=a*1.74;writeln('EUR:',met);End;End;Readln;End.

    4

    ProgramMathima4_Askisi1;varcount:integer;vara,sum:integer;Begincount:=1;sum:=0;whilecount

  • varcount1,count2,sum:integer;begin

    forcount1:=1to10dobeginreadln(pin1[count1]);end;

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

    writeln('sum:',sum);readln;end.

    ProgramMathima5_Askisi2;varpin2:array[1..5]ofinteger;varcount1,count2,count3:integer;begin

    forcount1:=1to5dobeginreadln(pin2[count1]);end;

    forcount2:=1to5dobeginifpin2[count2]

  • beginreadln(pin3[count1]);end;

    min:=pin3[1];

    forcount2:=2to5dobeginifpin3[count2]

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

    beginWrite('Doseton1oakereo:');readln(a);Write('Doseton2oakereo:');readln(b);mik:=mikroteros(a,b);writeln('mikroteros:',mik);readln;end.

    7

    ProgramLesson7_Askisi;

    UsesCrt;

    procedureinsert();

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

    begin

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

    Write('Onoma:');Readln(name);

    Write('Epitheto:');Readln(surname);

    Write('EMail:');

    70

  • Readln(mail);

    Write('Tilefono:');Readln(phone);

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

    Write('Theletenasinexisete(y/n):');Readln(yn);

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

    end;

    begininsert();readln;end.

    9

    PROGRAMBGI_HOUSE;

    USESCRT,GRAPH;

    VAR

    GD,GM:INTEGER;

    BEGIN

    71

  • InitGraph(GD,GM,'..\BGI');setcolor(9);line(300,0,200,200);line(300,0,400,200);rectangle(200,200,400,400);setcolor(3);rectangle(220,220,270,270);rectangle(330,220,380,270);setcolor(3);rectangle(270,320,330,400);setcolor(14);outtextxy(170,420,'BGIHOUSEBYGEORGEHADJIKYRIACOU');setcolor(7);outtextxy(220,440,'PRESS[ENTER]TOEXIT');

    readln;CloseGraph;END.

    10

    PROGRAMLesson10_Program;

    USESCRT,GRAPH;

    VAR

    GD,GM,i:INTEGER;

    BEGINwrite('Press[Enter]toStart...');readln;

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

    fori:=50to500dobegincircle(random(640),random(480),random(100));delay(5000);end;cleardevice;setcolor(15);outtextxy(0,0,'Press[ENTER]toExit...');readln;CloseGraph;

    72

  • END.

    73