CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
45
Task/Caesar-cipher/C++/caesar-cipher.cpp
Normal file
45
Task/Caesar-cipher/C++/caesar-cipher.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
class MyTransform {
|
||||
private :
|
||||
int shift ;
|
||||
public :
|
||||
MyTransform( int s ) : shift( s ) { }
|
||||
|
||||
char operator( )( char c ) {
|
||||
if ( isspace( c ) )
|
||||
return ' ' ;
|
||||
else {
|
||||
static std::string letters( "abcdefghijklmnopqrstuvwxyz" ) ;
|
||||
std::string::size_type found = letters.find(tolower( c )) ;
|
||||
int shiftedpos = ( static_cast<int>( found ) + shift ) % 26 ;
|
||||
if ( shiftedpos < 0 ) //in case of decryption possibly
|
||||
shiftedpos = 26 + shiftedpos ;
|
||||
char shifted = letters[shiftedpos] ;
|
||||
return shifted ;
|
||||
}
|
||||
}
|
||||
} ;
|
||||
|
||||
int main( ) {
|
||||
std::string input ;
|
||||
std::cout << "Which text is to be encrypted ?\n" ;
|
||||
getline( std::cin , input ) ;
|
||||
std::cout << "shift ?\n" ;
|
||||
int myshift = 0 ;
|
||||
std::cin >> myshift ;
|
||||
std::cout << "Before encryption:\n" << input << std::endl ;
|
||||
std::transform ( input.begin( ) , input.end( ) , input.begin( ) ,
|
||||
MyTransform( myshift ) ) ;
|
||||
std::cout << "encrypted:\n" ;
|
||||
std::cout << input << std::endl ;
|
||||
myshift *= -1 ; //decrypting again
|
||||
std::transform ( input.begin( ) , input.end( ) , input.begin( ) ,
|
||||
MyTransform( myshift ) ) ;
|
||||
std::cout << "Decrypted again:\n" ;
|
||||
std::cout << input << std::endl ;
|
||||
return 0 ;
|
||||
}
|
||||
23
Task/Caesar-cipher/Common-Lisp/caesar-cipher.lisp
Normal file
23
Task/Caesar-cipher/Common-Lisp/caesar-cipher.lisp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(defun encipher-char (ch key)
|
||||
(let*
|
||||
((c (char-code ch))
|
||||
(la (char-code #\a)) (lz (char-code #\z))
|
||||
(ua (char-code #\A)) (uz (char-code #\Z))
|
||||
(base (cond
|
||||
((and (>= c la) (<= c lz)) la)
|
||||
((and (>= c ua) (<= c uz)) ua)
|
||||
(t nil))))
|
||||
(if base (code-char (+ (mod (+ (- c base) key) 26) base)) ch)))
|
||||
|
||||
(defun caesar-cipher (str key)
|
||||
(map 'string #'(lambda (c) (encipher-char c key)) str))
|
||||
|
||||
(defun caesar-decipher (str key) (caesar-cipher str (- key)))
|
||||
|
||||
(let* ((original-text "The five boxing wizards jump quickly")
|
||||
(key 3)
|
||||
(cipher-text (caesar-cipher original-text key))
|
||||
(recovered-text (caesar-decipher cipher-text key)))
|
||||
(format t " Original: ~a ~%" original-text)
|
||||
(format t "Encrypted: ~a ~%" cipher-text)
|
||||
(format t "Decrypted: ~a ~%" recovered-text))
|
||||
21
Task/Caesar-cipher/D/caesar-cipher-1.d
Normal file
21
Task/Caesar-cipher/D/caesar-cipher-1.d
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import std.stdio, std.traits;
|
||||
|
||||
pure S rot(S)(in S s, in int key) if (isSomeString!S) {
|
||||
auto res = s.dup;
|
||||
|
||||
foreach (i, ref c; res) {
|
||||
if ('a' <= c && c <= 'z')
|
||||
c = ((c - 'a' + key) % 26 + 'a');
|
||||
else if ('A' <= c && c <= 'Z')
|
||||
c = ((c - 'A' + key) % 26 + 'A');
|
||||
}
|
||||
return cast(S) res;
|
||||
}
|
||||
|
||||
void main() {
|
||||
int key = 3;
|
||||
auto txt = "The five boxing wizards jump quickly";
|
||||
writeln("Original: ", txt);
|
||||
writeln("Encrypted: ", txt.rot(key));
|
||||
writeln("Decrypted: ", txt.rot(key).rot(26 - key));
|
||||
}
|
||||
20
Task/Caesar-cipher/D/caesar-cipher-2.d
Normal file
20
Task/Caesar-cipher/D/caesar-cipher-2.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import std.stdio, std.ascii;
|
||||
|
||||
void inplaceRot(char[] txt, in int key) pure nothrow {
|
||||
foreach (ref c; txt) {
|
||||
if (isLower(c))
|
||||
c = (c - 'a' + key) % 26 + 'a';
|
||||
else if (isUpper(c))
|
||||
c = (c - 'A' + key) % 26 + 'A';
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum key = 3;
|
||||
auto txt = "The five boxing wizards jump quickly".dup;
|
||||
writeln("Original: ", txt);
|
||||
txt.inplaceRot(key);
|
||||
writeln("Encrypted: ", txt);
|
||||
txt.inplaceRot(26 - key);
|
||||
writeln("Decrypted: ", txt);
|
||||
}
|
||||
64
Task/Caesar-cipher/Dart/caesar-cipher.dart
Normal file
64
Task/Caesar-cipher/Dart/caesar-cipher.dart
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
class Caesar {
|
||||
int _key;
|
||||
|
||||
Caesar(this._key);
|
||||
|
||||
int _toCharCode(String s) {
|
||||
return s.charCodeAt(0);
|
||||
}
|
||||
|
||||
String _fromCharCode(int ch) {
|
||||
return new String.fromCharCodes([ch]);
|
||||
}
|
||||
|
||||
String _process(String msg, int offset) {
|
||||
StringBuffer sb=new StringBuffer();
|
||||
for(int i=0;i<msg.length;i++) {
|
||||
int ch=msg.charCodeAt(i);
|
||||
if(ch>=_toCharCode('A')&&ch<=_toCharCode('Z')) {
|
||||
sb.add(_fromCharCode(_toCharCode("A")+(ch-_toCharCode("A")+offset)%26));
|
||||
}
|
||||
else if(ch>=_toCharCode('a')&&ch<=_toCharCode('z')) {
|
||||
sb.add(_fromCharCode(_toCharCode("a")+(ch-_toCharCode("a")+offset)%26));
|
||||
} else {
|
||||
sb.add(msg[i]);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String encrypt(String msg) {
|
||||
return _process(msg, _key);
|
||||
}
|
||||
|
||||
String decrypt(String msg) {
|
||||
return _process(msg, 26-_key);
|
||||
}
|
||||
}
|
||||
|
||||
void trip(String msg) {
|
||||
Caesar cipher=new Caesar(10);
|
||||
|
||||
String enc=cipher.encrypt(msg);
|
||||
String dec=cipher.decrypt(enc);
|
||||
print("\"$msg\" encrypts to:");
|
||||
print("\"$enc\" decrypts to:");
|
||||
print("\"$dec\"");
|
||||
Expect.equals(msg,dec);
|
||||
}
|
||||
|
||||
main() {
|
||||
Caesar c2=new Caesar(2);
|
||||
print(c2.encrypt("HI"));
|
||||
Caesar c20=new Caesar(20);
|
||||
print(c20.encrypt("HI"));
|
||||
|
||||
// try a few roundtrips
|
||||
|
||||
trip("");
|
||||
trip("A");
|
||||
trip("z");
|
||||
trip("Caesar cipher");
|
||||
trip(".-:/\"\\!");
|
||||
trip("The Quick Brown Fox Jumps Over The Lazy Dog.");
|
||||
}
|
||||
59
Task/Caesar-cipher/Elena/caesar-cipher.elena
Normal file
59
Task/Caesar-cipher/Elena/caesar-cipher.elena
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#define std'dictionary'*.
|
||||
#define std'basic'*.
|
||||
#define std'patterns'*.
|
||||
#define std'routines'strings'*.
|
||||
|
||||
#define math'* = std'math'*.
|
||||
|
||||
// --- subjects ---
|
||||
#subject cipher_key.
|
||||
|
||||
#symbol Letters = "abcdefghijklmnopqrstuvwxyz".
|
||||
#symbol BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
#symbol TestText = "Pack my box with five dozen liquor jugs.".
|
||||
#symbol Key = 12.
|
||||
|
||||
#symbol EEncrypt : aKey =
|
||||
{
|
||||
eval : aChar
|
||||
[
|
||||
#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.
|
||||
].
|
||||
].
|
||||
]
|
||||
}.
|
||||
|
||||
#symbol Encrypted &literal:aLiteral &cipher_key:aKey
|
||||
= __group(EEncrypt::aKey, Summing::String) start:Scan::aLiteral.
|
||||
|
||||
#symbol Decrypted &literal:aLiteral &cipher_key:aKey
|
||||
= __group(EEncrypt::(26 - aKey), Summing::String) start:Scan::aLiteral.
|
||||
|
||||
#symbol Program =
|
||||
[
|
||||
#var anS := TestText.
|
||||
|
||||
'program'output << "Original text :" << anS << "%n".
|
||||
|
||||
anS := Encrypted &&literal:anS &cipher_key:Key.
|
||||
|
||||
'program'output << "Encrypted text:" << anS << "%n".
|
||||
|
||||
anS := Decrypted &&literal:anS &cipher_key:Key.
|
||||
|
||||
'program'output << "Decrypted text:" << anS << "%n".
|
||||
|
||||
'program'input get.
|
||||
].
|
||||
54
Task/Caesar-cipher/Euphoria/caesar-cipher.euphoria
Normal file
54
Task/Caesar-cipher/Euphoria/caesar-cipher.euphoria
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
--caesar cipher for Rosetta Code wiki
|
||||
--User:Lnettnay
|
||||
|
||||
--usage eui caesar ->default text, key and encode flag
|
||||
--usage eui caesar 'Text with spaces and punctuation!' 5 D
|
||||
--If text has imbedded spaces must use apostophes instead of quotes so all punctuation works
|
||||
--key = integer from 1 to 25, defaults to 13
|
||||
--flag = E (Encode) or D (Decode), defaults to E
|
||||
--no error checking is done on key or flag
|
||||
|
||||
include std/get.e
|
||||
include std/types.e
|
||||
|
||||
|
||||
sequence cmd = command_line()
|
||||
sequence val
|
||||
-- default text for encryption
|
||||
sequence text = "The Quick Brown Fox Jumps Over The Lazy Dog."
|
||||
atom key = 13 -- default to Rot-13
|
||||
sequence flag = "E" -- default to Encrypt
|
||||
atom offset
|
||||
atom num_letters = 26 -- number of characters in alphabet
|
||||
|
||||
--get text
|
||||
if length(cmd) >= 3 then
|
||||
text = cmd[3]
|
||||
end if
|
||||
|
||||
--get key value
|
||||
if length(cmd) >= 4 then
|
||||
val = value(cmd[4])
|
||||
key = val[2]
|
||||
end if
|
||||
|
||||
--get Encrypt/Decrypt flag
|
||||
if length(cmd) = 5 then
|
||||
flag = cmd[5]
|
||||
if compare(flag, "D") = 0 then
|
||||
key = 26 - key
|
||||
end if
|
||||
end if
|
||||
|
||||
for i = 1 to length(text) do
|
||||
if t_alpha(text[i]) then
|
||||
if t_lower(text[i]) then
|
||||
offset = 'a'
|
||||
else
|
||||
offset = 'A'
|
||||
end if
|
||||
text[i] = remainder(text[i] - offset + key, num_letters) + offset
|
||||
end if
|
||||
end for
|
||||
|
||||
printf(1,"%s\n",{text})
|
||||
Loading…
Add table
Add a link
Reference in a new issue