[̲] [] [] [ ]

 

5.4.

 

, , . , , , ' , . - .

, , . . , - . - : , , , , , . , , , . , , , .

, . , ,

, ̳ ISO ASCII.

, , Ch ( character - ) m Ch = m. m < 255, . , Ch, , , . , <- b b, b ( ). , b, b, , - b, b b . , : , Ch, - .

Ch. , .

Ch - ,

 

'A', 'B', 'C', ..., 'Z' Î Ch, (5.18)

 

'0', '1', '2', ..., '9' Î Ch, (5.19)

, ( ), ..

, , Ch,

chr: N -> Ch,

dom(chr) - Nm. m = 255. , chr .

 

5.5. , , , Ch.

 

Liter

I: ;

I<-0 255

(chr(I))

.

 

5.9. 31- . . 5.5 0 32.

 

Ch = m,

ord: Ch -> Nm,

- chr:

 

chr(ord(S)) = S, S Î Ch; (5.20)

ord(chr(I)) = I, I Î Nm. (5.21)

 

(5.20) (5.21) Ch , Nm,

 

S w = ord(S) w ord() (S, Î Ch); (5.22)

 

w (w Î {=, <>, <, <=, >, >=}). ord, Ch

- succ

 

succ(S) = ord(S) < m chr(ord(S)+1)

;

- pred

 

pred(S) = ord(S) > 0 chr(ord(S)-)(1)

.

 

succ(S) , S, pred(S) - .

(5.18) (5.19)

 

succ('A') = 'B', succ('B') = 'C', ..., succ('Y') = 'Z'; (5.18')

succ('0') = '1', succ('1') = '2', ..., succ('8') = '9'. (5.19')

 

5.6. (5.18') .

 

ABC

I: ;

I<-ord('A') ord('Z')

(chr(I))

.

 

Ch, ( - 256) , (5.18) (5.19) chr. Ch succ pred,

{=, <>, <, <=, >, >=}, ord.

Ch ' AB. , , .

 

s<-s0 s1

(s)

,

s - ,

 

I<-ord(s0 ) ord(s1 )

(chr(I))

.

5.6

 

S: ;

S<-' ' 'Z'

(S)

.

 

5.7. s1, s2,... ³, s1 '/' s2, s3,... '/'. s1,..., sn - , '/' (n ). s1,..., sn.

 

Znak

C: ; N: ;

N <- 0;

(C);

C='/' ;

C='!' N <- N+1

;

(N)

.

 

5.8.

 

-17+3-5+1501;

 

, , .

(5.19'),

 

Inout

Minus= '-'; Tz= ';';

N0= '0'; N9= '9';

 

Rez, Zn, M, K0: ;

S: ;

(S);

Rez <- 0; K0 <- ord(N0);

S<>Tz

S=Minus Zn <- -1

Zn <- 1 ;

(S); M <- 0;

N0<=S & S<=N9

M <- 10*M+ord(S)-K0;

(S)

; { S= '+' V '-' V ';' }

Rez <- Rez+Zn*M

; { S= ';' }

(' = ', Rez)

.

 

5.5.

 

. Ch chr

 

Ch = { chr(k): k Î N255}.

 

', , 'A', '&'.

char:

 

var , Q, R: char.

, Q R ' .

, , , .

:

 

ord(c) - c

;

 

chr(ord(c)-1), ord(c) > 0,

pred(c) = <

chr(255), ord(c) = 0;

 

chr(ord(c)+1), ord(c) < 255,

succ(c) = <

chr(0), ord(c) = 255;

 

: pred succ 0 255, Overflow checking.

 

upcase(c) - ( ), , := 'a'; := upcase() => = 'A'.

 

5P. '!' ( 5.7).

 

program Znak;

var : char; N: byte;

begin

N:= 0; writeln(' ');

while true do begin

read(C);

if C='/' then break;

if C='!' then N:= N+1

end;

writeln;

write(N, ' !')

end.

 

6P. ( 5.8).

 

program Inout;

const Minus= '-'; Tz= ';';

N0= '0'; N9= '9';

var Rez, Zn, M, K0: integer;

S: char;

begin

writeln(' '); read(S);

Rez:= 0; K0:= ord(N0);

while S<>Tz do begin

if S=Minus then Zn:= -1

else Zn:= 1;

read(S); M:= 0;

while (N0<=S) and (S<=N9) do begin

M:= 10*M+ord(S)-K0;

read(S)

end;

Rez:= Rez+Zn*M

end;

writeln; write(' = ', Rez)

end.

 

ѳ. ', , 'A', '&'. 8 ( 16). \ , , \0 0 ( \x0, x , 16).

char:

 

char p, q, r;

p, q r ' .

char , char , . . chr ord . :

 

- , ,

 

isalnum(c) () ;

 

isalpha(c) ();

 

isdigit(c) ;

 

islower(c) ();

 

isupper(c) ();

 

- ()

 

tolower(c) ( );

 

toupper(c) ( );

 

: ctype.h.

 

scanf printf %c. : getchar putchar.

 

c = getchar(); - ;

putchar(c); - .

 

5C.

 

#include <stdio.h>

 

/* Znak */

main()

{

char c;

unsigned n;

 

n = 0; printf( );

while (1)

{

c=getchar();

if (c=='/') break;

if (c=='!') n++;

}

printf(\n%u !\n, n);

}

 

6C.

 

#include <stdio.h>

 

#define Minus -

#define Tz ;

#define N0 0

#define N9 9

 

/* Inout */

main()

{

char s;

int rez, zn, m;

 

printf( );

s = getchar();

rez = 0;

while (s!=Tz)

{

if (s==Minus) zn = -1;

else zn = 1;

s = getchar(); m = 0;

while (N0<=s && s<=N9)

{

m = 10*m+(s-N0);

s = getchar();

}

rez += zn*m;

}

printf(\n = %d, rez);

}

 

 

5.6. ,

 

. ,

 

= (c1, c2,..., cn ) (5.23)

 

, :

- ord

ord(ck) = k-1, k=1,2,..., n, (5.24)

 

- succ

succ(ck ) = ck+1, k=1,2,..., n-1 (5.25)

 

pred

pred(ck ) = ck-1, k=2,3,..., n; (5.26)

 

" r, s Î :

r w s = ord(r) w ord(s), (5.27)

 

w Î {=, <>, >, >=, <, <=}.

(5.23) (5.24)-(5.26) (5.27) , - .

' ck -, , , , ck - .

, (5.23) , , c1, c2,..., cn, (5.24)-(5.27), - .

(5.23); a, b Î <b; - , ; - .

 

<-a b

 

 

<- a;

;

=b ;

<- succ()

.

 

5.9. .

 

D_W

= (, , , , , , );

Day: ;

Day<-

(Day)

.

 

(IF), . - ; - , ; 1, 2,..., k - ; 1, 2,..., k, Q - .

 

-----------------

: ;

1 1

: ;

2 2 (Cs)

....

: ;

k k

Q

_

L----------------

 

. Q . _ .

 

:

.

1) ,

.

2) i, i<=k.

i

, i.

, (Cs).

, i

, Q (Cs) .

Q , ,

(Cs).

L---------------------------------------------------------------

 

i i, j,..., n,

 

....

, ,..., : ;

i j n i

....

.

 

,

 

Operator= (min, plu, mul, dvd)

r Operator.

 

r

plu: z <- +;

min: z <- x-y;

mul: z <- *;

dvd: z <- /

.

 

:

5.5. (Cs)

 

=

1 1

=

2 2

................

=

k k

Q

.

 

,

 

F Q

 

 

F

: ;

: Q

 

 

F

: ;

Q

,

 

 

F

 

 

F

: ;

:

F

:

.

 

.

succ; a, b Î <b. 0 ,

 

0 = (a, succ(a), ..., b),

 

 

0 = a..b;

,

 

_ = ..;

, .

0 , T. f - , 0 - f0,

 

f0 ()= f() Î 0 f()

" Î 0.

 

, , f0 f.

.

Zm, Nm

 

= 0..m;

 

- , , .

, ,

 

----------------

ABRZNChT ...T + IF + Lw

1 k

L---------------

^ ^ ^ ^

----------------------------

----------------

---- L---------

---------------- -------------------------------------

----------v---------- ---------v---------

--v-- ----- ----- ----- --v--- ------ ------

B N Z R Ch ...

L---- L---- L---- L---- L----- L--1-- L--k--

L-------------------- L------------------

L-------------------------------------------------------

 

m , . - , , , AB.

 

5.10. [1920,2099] , , 1 1920 - .

, , 1 - , 7. 0, ; , - .. , 6, . , [1,356], 1 7.

1 - , , . , 1 - '. 8 ( 0) - , 9 ( 1) , 10 ( 2) - ..

365 1 ( 365 7 1). 366 2. Y [1920,2099] 1 1920 , Y 1920 KV, :

 

KV = Y=1920 0 ((Y - 1) - 1920) div 4 + 1;

 

 

Day_Week

= (, , , , , , );

̳= (, , , , , , , , , , , );

= 1920..2099;

Day: 1..366; Disp, i: 0..6;

Num, N_D: 1..31;

Mon, : ̳;

Year: ;

D_W: ;

KV: ;

{ }

(' г= '); (Year);

(' ̳= '); (on);

on { on }

, , , : N_D <- 30;

: Year mod 4=0 N_D <- 29

N_D <- 28

N_D <- 31

;

(' = '); (Num);

Num>N_D (' ! ')

Num <= N_D;

{ }

Day <- Num;

on>

<- pred(on)

, , , : Day <- Day + 30;

: Year mod 4=0 Day <- Day + 29

Day <- Day + 28

Day <- Day + 31

;

;

{ 1 1920 }

Year = 1920 KV <- 0

KV <- ((Year - 1) - 1920) div 4 + 1 ;

Disp <- ((Year-1920)+KV+(Day-1)) mod 7;

{ }

D_W <- ;

i<-1 Disp

D_W= D_W <-

D_W <- succ(D_W)

;

(D_W)

.

 

5.7. ,

 

5.7.1.

 

.

 

type = (c1, c2,..., cn );

 

- ' , ci - , i= 1,...,n.

,

 

type Operator= (plus, minus, multi, divide);

Week= (mon, tue, wed, thu, fri, sat, sun);

var Day: Week; Op1, Op2, Op3: Operator;

 

' , - 256 .

. {<, <=, =, <>, >, >=}, ord, succ pred. T , ord. T. ,

 

week(1)=tue,

operator(2)=multi.

 

(Cs) :

 

case of

: begin Pas( ) end;

1 1

: begin Pas( ) end;

2 2

....

: begin Pas( ) end;

k k

else begin Pas(Q) end

end.

 

7P. ( 5.9).

 

program Pech;

type Week=(Mon, Tue, Wed, Thu, Fri, Sat, Sun);

var D: Week;

begin

for D:=Mon to Sun do begin

write(ord(D),'-');

case D of

Mon: writeln('');

Tue: writeln('');

Wed: writeln('');

Thu: writeln('');

Fri: writeln('''');

Sat: writeln('');

Sun: writeln('')

end

end

end.

 

ѳ :

 

typedef enum {c1, c2,..., cn} T;

 

, , (main).

,

 

typedef enum {plus, minus, multi, divide} operator;

typedef enum {mon, tue, wed, thu, fri, sat, sun} week;

...

week day;

operator op1, op2, op3;

 

ѳ , ord . , 1, 0. ѳ .

,

 

typedef enum {plus=1, minus=2, multi=4, divide=8} operator;

typedef enum {jan=1, feb, mar, apr, may, jun, jul, aug, sep,

oct, nov, dec} month;

 

month jan=1, feb=2 .

(Cs) :

 

switch ()

{

case : C( ) break;

1 1

case : C( ) break;

2 2

...

case : C( ) break;

k k

case else C(Q )

}.

 

break , . C(Pi) break, C(Pi+1).

 

7C. ( 5.9).

 

#include <stdio.h>

 

/* Pech */

 

typedef enum {mon, tue, wed, thu, fri, sat, sun} week;

 

main()

{

week d;

 

for (d=mon; d<=sun; d++)

{

printf("%d-",d);

switch (d)

{

case mon: printf("\n"); break;

case tue: printf("\n"); break;

case wed: printf("\n"); break;

case thu: printf("\n"); break;

case fri: printf("'\n"); break;

case sat: printf("\n"); break;

case sun: printf("\n"); break;

}

}

}

 

5.7.2.

 

- , , , , , . - , a, b - , <b. 0

 

type 0 = a..b;

 

0 , .

:

 

type NAT = 0..maxint;

LET = 'a'..'z';

NUM = '0'..'9';

WORKDAY = Mon..Fri;

WEEKEND = Sat..Sun;

 

.

5.10. , , , . Range checking $R+. .

:

         ;

         ' ' ;

         .

8P. ( 5.10).

{$R+}

program Day_Week;

type Week = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

Month = 1..12;

Years = 1920..2099;

var Day: 1..366; Disp,i: 0..6;

Num, N_D: 1..31;

Mn, M: Month;

Year: Years;

KV: word;

D_W: Week;

begin

{ }

write(' г [1920..2099]=? '); readln(Year);

write(' ̳ [1..12]=? '); readln(Mn);

case Mn of

4, 6, 9, 11: N_D:= 30;

2: if Year mod 4=0 then N_D:= 29 else N_D:= 28;

else N_D:= 31

end;

repeat

write(' [1..',N_D,']=?'); readln(Num);

if Num>N_D then writeln(' ')

until Num<=N_D;

{ }

Day:= Num;

for M:=1 to Mn-1 do

case M of

4, 6, 9, 11: Day:=Day+30;

2: if Year mod 4=0 then Day:=Day+29 else Day:=Day+28;

else Day:=Day+31

end;

{ }

if Year = 1920 then KV := 0

else KV := ((Year - 1) - 1920) div 4 + 1;

Disp := ((Year-1920)+KV+(Day-1)) mod 7; { c }

D_W:= Thu; { 1 1920 - }

for i:=1 to Disp do

if D_W=Sun then D_W:= Mon { }

else D_W:= succ(D_W);

case D_W of

Mon: writeln('');

Tue: writeln('');

Wed: writeln('');

Thu: writeln('');

Fri: writeln('''');

Sat: writeln('');

Sun: writeln('')

end

end.

 

ѳ .

8C. ( 5.10).

#include <stdio.h>

 

typedef enum {mon,tue,wed,thu,fri,sat,sun} weekday;

 

/* Day_Week */

main()

{

weekday wd;

int y,m,d,disp,kv,day,nd,i;

 

do

{

printf("г [1920..2099]=? ");

scanf("%d",&y);

}

while (y < 1920 || y > 2099);

do

{

printf("̳ [1..12]=? ");

scanf("%d",&m);

}

while (m < 1 || m > 12);

switch (m)

{

case 2: nd = (y % 4 == 0 ? 29 : 28); break;

case 4:

case 6:

case 9:

case 11: nd = 30; break;

default: nd = 31;

}

do

 

{

printf(" [1..%d]=? ",nd);

scanf("%d",&d);

}

while (d < 1 || d > nd);

kv = (y == 1920 ? 0 : ((y - 1) - 1920) / 4 + 1);

day = d;

for (i = 1; i < m; i++)

switch(i)

{

case 2: day += (y % 4 == 0 ? 29 : 28); break;

case 4:

case 6:

case 9:

case 11: day += 30; break;

default: day += 31;

}

disp = ((y - 1920) + kv + (day - 1)) % 7;

wd = thu;

for (i = 1; i <= disp; i++)

if (wd == sun)

wd = mon;

else

wd++;

switch (wd)

{

case mon: printf("monday\n"); break;

case tue: printf("tuesday\n"); break;

case wed: printf("wednesday\n"); break;

case thu: printf("thursday\n"); break;

case fri: printf("friday\n"); break;

case sat: printf("saturday\n"); break;

case sun: printf("sunday\n"); break;

}

}

 

[̲] [] [] [ ]