60 lines
1.4 KiB
Plaintext
60 lines
1.4 KiB
Plaintext
MODULE Caesar;
|
|
IMPORT
|
|
Out;
|
|
CONST
|
|
encode* = 1;
|
|
decode* = -1;
|
|
|
|
VAR
|
|
text,cipher: POINTER TO ARRAY OF CHAR;
|
|
|
|
PROCEDURE Cipher*(txt: ARRAY OF CHAR; key: INTEGER; op: INTEGER; VAR cipher: ARRAY OF CHAR);
|
|
VAR
|
|
i: LONGINT;
|
|
BEGIN
|
|
i := 0;
|
|
WHILE i < LEN(txt) - 1 DO
|
|
IF (txt[i] >= 'A') & (txt[i] <= 'Z') THEN
|
|
cipher[i] := CHR(ORD('A') + ((ORD(txt[i]) - ORD('A') + (key * op))) MOD 26)
|
|
ELSIF (txt[i] >= 'a') & (txt[i] <= 'z') THEN
|
|
cipher[i] := CHR(ORD('a') + ((ORD(txt[i]) - ORD('a') + (key * op))) MOD 26)
|
|
ELSE
|
|
cipher[i] := txt[i]
|
|
END;
|
|
INC(i)
|
|
END;
|
|
cipher[i] := 0X
|
|
END Cipher;
|
|
|
|
BEGIN
|
|
NEW(text,3);NEW(cipher,3);
|
|
COPY("HI",text^);
|
|
Out.String(text^);Out.String(" =e=> ");
|
|
Cipher(text^,2,encode,cipher^);
|
|
Out.String(cipher^);
|
|
|
|
COPY(cipher^,text^);
|
|
Cipher(text^,2,decode,cipher^);
|
|
Out.String(" =d=> ");Out.String(cipher^);Out.Ln;
|
|
|
|
COPY("ZA",text^);
|
|
Out.String(text^);Out.String(" =e=> ");
|
|
Cipher(text^,2,encode,cipher^);
|
|
Out.String(cipher^);
|
|
|
|
COPY(cipher^,text^);
|
|
Cipher(text^,2,decode,cipher^);
|
|
Out.String(" =d=> ");Out.String(cipher^);Out.Ln;
|
|
|
|
NEW(text,37);NEW(cipher,37);
|
|
COPY("The five boxing wizards jump quickly",text^);
|
|
Out.String(text^);Out.String(" =e=> ");
|
|
Cipher(text^,3,encode,cipher^);
|
|
Out.String(cipher^);
|
|
|
|
COPY(cipher^,text^);
|
|
Cipher(text^,3,decode,cipher^);
|
|
Out.String(" =d=> ");Out.String(cipher^);Out.Ln;
|
|
|
|
END Caesar.
|