{Pascal - 国立大学法人 上越教育大学 ??じめに この講義の目的は、Pascal言語によるプログラム作成を通して、パーソナル コンピュータの操作法と、最も基本的なソフトウェアとしてのエディターの使 用法を習得し、構造化プログラミングの方法を学ぶことです。

  • Published on
    09-Apr-2018

  • View
    217

  • Download
    3

Embed Size (px)

Transcript

  • Pascal

  • Pascal

    Pascal Free Pascal CPad for Pascal Microsoft WindowsOS

    2010 10

    1 Pascal 21.1 . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 . . . . . . . . . . . . . . . . . . 21.3 . . . . . . . . . . . . . . . . . . . . . . . . 3

    2 42.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.3 . . . . . . . . . . . . . . . . . . . . . . . . . . 62.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    3 73.1 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.2 case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    4 84.1 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84.2 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94.3 repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    5 105.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    A Pascal 12A.1 Pascal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12A.2 Pascal . . . . . . . . . . . . . . . . . . . . . . 12

    B 13

    1

  • 1 Pascal

    1.1

    Hello. Pascal

    1.1. Hello.

    program Lesson1;

    begin

    writeln(Hello.);

    end.

    1 Pascalprogram ;;()

    2 ()begin

    3 Hello.

    4 ()end.

    1.2

    PascalHello.

    ?

    Yes

    No

    ?

    Yes

    No

    PCPad.exe

    2

  • 1.3

    2 a b Pascal

    1.2. 2

    program Lesson2;

    var a,b,wa:integer;

    begin

    write(a= );

    readln(a);

    write(b= );

    readln(b);

    wa:=a+b;

    writeln(a+b= , wa);

    end.

    1 Pascalprogram ;;()

    2 a, b, wa

    3 ()begin

    4 a= writewriteln

    5 a

    8 a+b wa () =:=

    9 a+b= wa

    10 ()end.

    Pascal

    program ;

    const type var procedure function

    begin

    end.

    const =;=;

    1

    1.3. n=10v=2.71 1012name=

    const n=10;

    v=2.71E-12;

    name=;

    Pascal

    3

  • type =;=;

    1.4. 3 vector

    type vector=array[1..3] of real;

    ()

    var , , :;

    , , :;

    1.5. a,bx,y,ransname 30

    var a,b:integer;

    x,y,r:real;

    ans:char;

    name:string[30];

    Pascal BASIC

    procedure (>, , );

    1.6. a b

    procedure exchg(var a,b:integer);

    var c:integer;

    begin

    c:=a; a:=b; b:=c;

    end;

    Pascalsin

    function (>, , ):;

    1.7. a b

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

    begin

    if a>b then max:=a

    else max:=b;

    end;

    BASICFORTRANbegin end.

    2

    2.1

    Pascal 5

    byte 0 255 shortint -128 127 integer -32768 32767 word 0 65535 longint -2147483648 2147483647

    2

    MaxInt=32767

    MaxLongInt=2147483647

    2.1. 2 a, b

    4

  • program Lesson3;

    var a,b,wa,sa,seki,shou,amari:integer;

    begin

    write(a= ); readln(a);

    write(b= ); readln(b);

    wa:=a+b;

    sa:=a-b;

    seki:=a*b;

    shou:=a div b;

    amari:=a mod b;

    writeln(a+b= , wa);

    writeln(a-b= , sa);

    writeln(a*b= , seki);

    writeln(a div b= , shou);

    writeln(a mod b= , amari);

    end.

    2 a, b, wa, seki, shou, amari

    6 a+bwa

    7 a-b sa

    8 ab seki

    9 a div b shou

    10 a mod b amari

    11 a+b = wa

    12 a-b = sa

    13 ab = seki

    14 a div b = shou

    15 a mod b = amari

    2.2

    Pascal

    real 2.9 1039 1.7 1038 () 11

    +/:

    trunc(x) x;round(x) x

    2.2. v x

    program Lesson4;

    var v, x:real;

    h, m:integer;

    begin

    write( );readln(v);

    write( (km) );readln(x);

    h:=trunc(x/v);

    m:=round((x/v - h)*60);

    writeln(h, ,m, );end.

    2 v, x

    3 h, m

    5

  • 5

    6 v

    7 (km)

    8 x

    9 x/v h

    10 x/v-h 60m

    11 h m

    2.3

    a, . . ., zA, . . ., Z0, . . ., 9 1:

    var c1,c2: char;

    1 2:

    var name: string[20];

    2.3. ()

    program Lesson5;

    var name: string[20];

    init: char;

    n: integer;

    begin write(Your first name? );

    readln(name);

    init:=name[1];

    n:=Length(name);

    writeln(Length is , n);

    writeln(Initial is , init);

    end.

    2 name 20

    3 init

    4 n

    5 Your first name?

    6 name

    7 init name 1

    8 n name

    9 Length is n

    10 Initial is init

    2.4

    true()false() 2:

    var c: boolean;

    :

    not, and, or, =, , =,

    2.4. a=b cdtrue false

    (a=b) and (c>=d)

    6

  • 2.5

    100100 a1, a2, . . . , a100Pascalaa[1], a[2], , a[100]

    2.5. a 1 100b 1 100

    var a: array[1..100] of integer;

    b: array[1..100] of real;

    3

    3.1 if

    ifif 2:

    if then if then 1 else 2

    3.1. else 1;

    3.2. 2

    program Lesson6;

    var a,b,d,x1,x2: real;

    begin

    writeln(x^2 + ax + b = 0);

    write(a= ); readln(a);

    write(b= ); readln(b);

    d:=a*a - 4*b;

    if d>=0 then begin

    x1:=(-a - sqrt(d))/2;

    x2:=(-a + sqrt(d))/2;

    writeln(x1:5:3);

    writeln(x2:5:3);

    end

    else writeln();end.

    3.2 case

    1casecase:

    case ofcase, , case: ;case, , case: ;

    end;

    3.3.

    program Lesson7;

    var mon:integer;

    begin

    write( );readln(mon);

    case mon of

    3..5 : write();6..8 : write();9..11 : write();12, 1..2 : write();

    end;

    end.

    7

  • 2 mon

    4

    5 mon

    611 mon 3 5mon 6 8mon 9 11mon 12 1 2

    4

    4.1 for

    forfor 2:

    for := to do ( 1)for := downto do ( 1)

    4.1. 1 n

    program Lesson8;

    var a,n,s: integer;

    begin

    write(n = ); readln(n);

    s:=0;

    for a:=1 to n do s:=s + a;

    writeln(1 + ... + ,n, = ,s);

    end.

    2 a, n, s

    4 n= n

    5 s 0

    6 a 1 n s s+a

    7 1 + ... + ns

    4.2. 3 ab

  • h,s,t,v:real;

    i,x:integer;

    begin

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

    s:=0;

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

    h:=s/n; (* *)t:=0;

    for i:=1 to n do t:=t+sqr(a[i]-h);

    v:=sqrt(t/n); (* *)writeln(h:5:2, , v:5:2);

    end.

    2 n 10

    3 a 1 10

    4 h, s, t, v

    5 i, x

    7 i 1 n a i a[i]

    8 s 0

    9 i 1 n s s+a[i]s a[1] a[n]

    10 h s/nh a[1] a[n](* *)

    11 t 0

    12 i 1 n t t+(a[i]-h)2t (a[1]-h)2 (a[n]-h)2

    13 v t/nva[1]a[n]

    14 hv

    4.2 while

    while:

    while do

    4.4.

    program Lesson11;

    var a,n,amax: integer;

    begin

    write(2); readln(n);amax:=trunc(sqrt(n));

    if n mod 2=0 then

    begin if n=2 then writeln(n, )else writeln(n, );end

    else begin

    a:=3;

    while (n mod a0) and (aa) then

    writeln(n, )else writeln(n, );

    end;

    end.

    2 a, n, amax

    4 2 n

    9

  • 5 amaxn

    6 n 2 0 (n)

    79 n=2nn

    10 n

    11 a 3

    12 n a 0 aamaxa a+2

    13 n a 0 n>an

    14 n

    4.3 repeat

    repeat:

    repeat until

    4.5.

    program Lesson12;

    var a,b,r: integer;

    begin

    write(a = ); readln(a);

    write(b = ); readln(b);

    if b>a then begin

    r:=a; a:=b; b:=r;

    end;

    repeat

    r:=a mod b; a:=b; b:=r;

    until b=0;

    writeln( ,a);end.

    2 a, b, r

    4 a= a

    5 b= b

    6 b>a

    7 r aa bb r

    911 r a ba bb r b 0

    12 a

    5

    5.1

    :

    procedure (>, , );

    varvar

    10

  • 5.1.

    program Lesson13;

    type complex=array[1..2] of real;

    var x,y,z: complex;

    (* c:=a*b *)

    procedure compmul(var c: complex; a,b: complex);

    begin

    c[1]:=a[1]*b[1]-a[2]*b[2];

    c[2]:=a[1]*b[2]+a[2]*b[1];

    end;

    begin

    write(Re x = ); readln(x[1]);

    write(Im x = ); readln(x[2]);

    write(Re y = ); readln(y[1]);

    write(Im y = ); readln(y[2]);

    compmul(z,x,y);

    writeln(x*y = ,z[1]:4:2, + ,z[2]:4:2,i);

    end.

    2 complex 1 212

    3 x, y, z complex

    59 ab c compmulcab

    11 Re x = x[1]

    12 Im x = x[2]

    13 Re y = y[1]

    14 Im y = y[2]

    15 compmul xy z

    16 xy = z[1] + z[2]i

    5.2

    ():

    function (>, , ):;

    5.2.

    program Lesson14;

    var a,b,d:integer;

    function gcd(a,b:integer):integer;

    var r: integer;

    begin

    if b>a then begin

    r:=a; a:=b; b:=r;

    end;

    repeat

    r:=a mod b;

    a:=b;

    b:=r;

    until b=0;

    gcd:=a;

    end;

    begin (* main *)

    write( = ); readln(a);write( = ); readln(b);

    11

  • d:=gcd(a,b);

    a:=a div d; b:=b div d;

    write(a,/,b);

    end.

    2 a, b, d

    315 gcd 4.5

    14

    17 = a

    18 = b

    19 a b d

    20 a a db b d

    21 a/b

    A Pascal

    A.1 Pascal

    absolute end inline procedure type

    and external interface program unit

    array file interrupt record until

    begin for label repeat uses

    case forward mod set var

    const function nil shl while

    div goto not shr with

    do if of string xor

    downto implementation or then

    else in packed to

    A.2 Pascal

    i,j,n:x:a:c,ch:f:s,st:b:w:write

    writec, s c:n, s:n ni i 10i:n nx x 10x:n nx:n:j x j

    10n

    read(f, v, . . ., v) vreadln(f, v, . . ., v)

    write(f, w, . . ., w)

    writeln(f, w,. . ., w)

    a:=abs(a) a:=sqr(a) 2x:=sin(a) x:=cos(a) x:=exp(a) x:=ln(a) x:=sqrt(a) x:=arctan(a) i:=trunc(x) i:=round(x) b:=odd(i) true falserandomize x:=random 0.0 1.0 i:=random(n) 0 n

    12

  • c:=chr(i) i:=ord(c) s:=copy(st,i,n) st i ns:=concat(s1, . . . , sn) s1 sn i:=length(st) sti:=pos(s,st) st s

    ( 0)ch:=upcase(c) cdelete(st,i,n) st i ninsert(s,st,i) s st istr(w,s) w s

    val(s,a,i) s a (

    i 0s i)

    B 2/10()

    ( 7F)

    1. (A4 5)

    2.

    3.

    4.

    5.

    (a)

    (b)

    (c)

    , Pascal [ 2], , 1992.

    13

Recommended

View more >