Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,86 @@
>>SOURCE FORMAT IS FREE
PROGRAM-ID. caesar-cipher.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION encrypt
FUNCTION decrypt
.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 plaintext PIC X(50).
01 offset PIC 99.
01 encrypted-str PIC X(50).
PROCEDURE DIVISION.
DISPLAY "Enter a message to encrypt: " NO ADVANCING
ACCEPT plaintext
DISPLAY "Enter the amount to shift by: " NO ADVANCING
ACCEPT offset
MOVE FUNCTION encrypt(offset, plaintext) TO encrypted-str
DISPLAY "Encrypted: " encrypted-str
DISPLAY "Decrypted: " FUNCTION decrypt(offset, encrypted-str)
.
END PROGRAM caesar-cipher.
FUNCTION-ID. encrypt.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 i PIC 9(3).
01 a PIC 9(3).
LINKAGE SECTION.
01 offset PIC 99.
01 str PIC X(50).
01 encrypted-str PIC X(50).
PROCEDURE DIVISION USING offset, str RETURNING encrypted-str.
MOVE str TO encrypted-str
PERFORM VARYING i FROM 1 BY 1 UNTIL i > FUNCTION LENGTH(str)
IF encrypted-str (i:1) IS NOT ALPHABETIC OR encrypted-str (i:1) = SPACE
EXIT PERFORM CYCLE
END-IF
IF encrypted-str (i:1) IS ALPHABETIC-UPPER
MOVE FUNCTION ORD("A") TO a
ELSE
MOVE FUNCTION ORD("a") TO a
END-IF
MOVE FUNCTION CHAR(FUNCTION MOD(FUNCTION ORD(encrypted-str (i:1))
- a + offset, 26) + a)
TO encrypted-str (i:1)
END-PERFORM
.
END FUNCTION encrypt.
FUNCTION-ID. decrypt.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION encrypt
.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 decrypt-offset PIC 99.
LINKAGE SECTION.
01 offset PIC 99.
01 str PIC X(50).
01 decrypted-str PIC X(50).
PROCEDURE DIVISION USING offset, str RETURNING decrypted-str.
SUBTRACT 26 FROM offset GIVING decrypt-offset
MOVE FUNCTION encrypt(decrypt-offset, str) TO decrypted-str
.
END FUNCTION decrypt.

View file

@ -1,59 +1,74 @@
#define std'dictionary'*.
#define std'basic'*.
#define std'patterns'*.
#define std'routines'strings'*.
#define system.
#define system'routines.
#define extensions.
#define extensions'math.
#define math'* = std'math'*.
// --- subjects ---
#subject cipher_key.
// --- Constants ---
#symbol Letters = "abcdefghijklmnopqrstuvwxyz".
#symbol BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
#symbol TestText = "Pack my box with five dozen liquor jugs.".
#symbol Key = 12.
#symbol EEncrypt : aKey =
// --- Encrypt / Decript ---
#class Encrypting
{
eval : aChar
#field theKey.
#field theExtendee.
#constructor new : aKey &extending:anObject
[
#var anIndex := Letters~eliteralop first_occ'find:aChar.
#if (-1) ifless:anIndex
[
$next eval:(Letters @ (aKey + anIndex)~math'eops math'modulus:26).
]
| [
anIndex := BigLetters~eliteralop first_occ'find:aChar.
#if (-1) ifless:anIndex
[
$next eval:(BigLetters @ (aKey + anIndex)~math'eops math'modulus:26).
]
| [
$next eval:aChar.
].
].
theKey := aKey.
theExtendee := anObject.
]
}.
#symbol Encrypted &literal:aLiteral &cipher_key:aKey
= __group(EEncrypt::aKey, Summing::String) start:Scan::aLiteral.
#method eval : aChar
[
#var anIndex := Letters indexOf &index:0 &char:aChar.
#symbol Decrypted &literal:aLiteral &cipher_key:aKey
= __group(EEncrypt::(26 - aKey), Summing::String) start:Scan::aLiteral.
(-1 < anIndex)
? [
theExtendee eval:(Letters @ (modulus:(theKey+anIndex):26))
]
! [
anIndex := BigLetters indexOf &index:0 &char:aChar.
(-1 < anIndex)
? [
theExtendee eval:(BigLetters @ (modulus:(theKey+anIndex):26))
]
! [
theExtendee eval:aChar.
].
].
]
#symbol Program =
#method => theExtendee.
}
// --- Functions ---
#symbol encrypt = (:aText:aKey)
[ Encrypting new:aKey &extending:(Summing new:(String new)) foreach:aText literal ].
#symbol decrypt = (:aText:aKey)
[ Encrypting new:(26 - aKey) &extending:(Summing new:(String new)) foreach:aText literal ].
// --- Program ---
#symbol program =
[
#var anS := TestText.
'program'output << "Original text :" << anS << "%n".
consoleEx writeLine:"Original text :" :anS.
anS := Encrypted &&literal:anS &cipher_key:Key.
anS := encrypt:anS:Key.
'program'output << "Encrypted text:" << anS << "%n".
consoleEx writeLine:"Encrypted text:" :anS.
anS := Decrypted &&literal:anS &cipher_key:Key.
anS := decrypt:anS:Key.
'program'output << "Decrypted text:" << anS << "%n".
consoleEx writeLine:"Decrypted text:" :anS.
'program'input get.
consoleEx readChar.
].

View file

@ -0,0 +1,18 @@
function rot(s::String, key::Integer)
map(s) do c
if 'a' <= c <= 'z'
char( mod(c - 'a' + key, 26) + 'a')
elseif 'A' <= c <= 'Z'
char( mod(c - 'A' + key, 26) + 'A')
else
c
end
end
end
key = 3
txt = "The five boxing wizards jump quickly"
println("Original: ", txt);
println("Encrypted: ", rot(txt, key))
println("Decrypted: ", rot(rot(txt, key), 26 - key))