2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -3,9 +3,12 @@
|
|||
[[wp:Morse_code|Morse code]] is one of the simplest and most versatile methods of telecommunication in existence.
|
||||
It has been in use for more than 160 years — longer than any other electronic encoding system.
|
||||
|
||||
The task: Send a string as audible morse code to an audio device
|
||||
(e.g., the PC speaker).
|
||||
|
||||
;Task:
|
||||
Send a string as audible Morse code to an audio device (e.g., the PC speaker).
|
||||
|
||||
|
||||
As the standard Morse code does not contain all possible characters,
|
||||
you may either ignore unknown characters in the file,
|
||||
or indicate them somehow (e.g. with a different pitch).
|
||||
or indicate them somehow (e.g. with a different pitch).
|
||||
<br><br>
|
||||
|
|
|
|||
68
Task/Morse-code/C++/morse-code.cpp
Normal file
68
Task/Morse-code/C++/morse-code.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
Michal Sikorski
|
||||
06/07/2016
|
||||
*/
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <windows.h>
|
||||
#include <string.h>
|
||||
using namespace std;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
string inpt;
|
||||
char ascii[28] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ", lwcAscii[28] = " abcdefghijklmnopqrstuvwxyz";
|
||||
string morse[27] = {" ", ".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--.", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. "};
|
||||
string outpt;
|
||||
getline(cin,inpt);
|
||||
int xx=0;
|
||||
int size = inpt.length();
|
||||
cout<<"Length:"<<size<<endl;
|
||||
|
||||
xx=0;
|
||||
while(xx<inpt.length())
|
||||
{
|
||||
int x=0;
|
||||
bool working = false;
|
||||
while(!working)
|
||||
{
|
||||
if(ascii[x] != inpt[xx]&&lwcAscii[x] != inpt[xx])
|
||||
{
|
||||
x++;
|
||||
}
|
||||
else
|
||||
{
|
||||
working = !working;
|
||||
}
|
||||
}
|
||||
|
||||
cout<<morse[x];
|
||||
outpt = outpt + morse[x];
|
||||
xx++;
|
||||
}
|
||||
|
||||
xx=0;
|
||||
while(xx<outpt.length()+1)
|
||||
{
|
||||
if(outpt[xx] == '.')
|
||||
{
|
||||
Beep(1000,250);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(outpt[xx] == '-')
|
||||
{
|
||||
Beep(1000,500);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(outpt[xx] == ' ')
|
||||
{
|
||||
Sleep(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
xx++;
|
||||
}
|
||||
system("PAUSE");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
58
Task/Morse-code/D/morse-code.d
Normal file
58
Task/Morse-code/D/morse-code.d
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import std.conv;
|
||||
import std.stdio;
|
||||
|
||||
immutable string[char] morsecode;
|
||||
|
||||
static this() {
|
||||
morsecode = [
|
||||
'a': ".-",
|
||||
'b': "-...",
|
||||
'c': "-.-.",
|
||||
'd': "-..",
|
||||
'e': ".",
|
||||
'f': "..-.",
|
||||
'g': "--.",
|
||||
'h': "....",
|
||||
'i': "..",
|
||||
'j': ".---",
|
||||
'k': "-.-",
|
||||
'l': ".-..",
|
||||
'm': "--",
|
||||
'n': "-.",
|
||||
'o': "---",
|
||||
'p': ".--.",
|
||||
'q': "--.-",
|
||||
'r': ".-.",
|
||||
's': "...",
|
||||
't': "-",
|
||||
'u': "..-",
|
||||
'v': "...-",
|
||||
'w': ".--",
|
||||
'x': "-..-",
|
||||
'y': "-.--",
|
||||
'z': "--..",
|
||||
'0': "-----",
|
||||
'1': ".----",
|
||||
'2': "..---",
|
||||
'3': "...--",
|
||||
'4': "....-",
|
||||
'5': ".....",
|
||||
'6': "-....",
|
||||
'7': "--...",
|
||||
'8': "---..",
|
||||
'9': "----."
|
||||
];
|
||||
}
|
||||
|
||||
void main(string[] args) {
|
||||
foreach (arg; args[1..$]) {
|
||||
writeln(arg);
|
||||
foreach (ch; arg) {
|
||||
if (ch in morsecode) {
|
||||
write(morsecode[ch]);
|
||||
}
|
||||
write(' ');
|
||||
}
|
||||
writeln();
|
||||
}
|
||||
}
|
||||
74
Task/Morse-code/Delphi/morse-code.delphi
Normal file
74
Task/Morse-code/Delphi/morse-code.delphi
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
program Morse;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
{$R *.res}
|
||||
|
||||
uses
|
||||
System.Generics.Collections,
|
||||
SysUtils,
|
||||
Windows;
|
||||
|
||||
const
|
||||
Codes: array[0..35, 0..1] of string =
|
||||
(('a', '.- '), ('b', '-... '), ('c', '-.-. '), ('d', '-.. '),
|
||||
('e', '. '), ('f', '..-. '), ('g', '--. '), ('h', '.... '),
|
||||
('i', '.. '), ('j', '.--- '), ('k', '-.- '), ('l', '.-.. '),
|
||||
('m', '-- '), ('n', '-. '), ('o', '--- '), ('p', '.--. '),
|
||||
('q', '--.- '), ('r', '.-. '), ('s', '... '), ('t', '- '),
|
||||
('u', '..- '), ('v', '...- '), ('w', '.-- '), ('x', '-..- '),
|
||||
('y', '-.-- '), ('z', '--.. '), ('0', '-----'), ('1', '.----'),
|
||||
('2', '..---'), ('3', '...--'), ('4', '....-'), ('5', '.....'),
|
||||
('6', '-....'), ('7', '--...'), ('8', '---..'), ('9', '----.'));
|
||||
var
|
||||
Dictionary: TDictionary<String, String>;
|
||||
|
||||
procedure InitCodes;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := 0 to High(Codes) do
|
||||
Dictionary.Add(Codes[i, 0], Codes[i, 1]);
|
||||
end;
|
||||
|
||||
procedure SayMorse(const Word: String);
|
||||
var
|
||||
s: String;
|
||||
begin
|
||||
for s in Word do
|
||||
if s = '.' then
|
||||
Windows.Beep(1000, 250)
|
||||
else if s = '-' then
|
||||
Windows.Beep(1000, 750)
|
||||
else
|
||||
Windows.Beep(1000, 1000);
|
||||
end;
|
||||
|
||||
procedure ParseMorse(const Word: String);
|
||||
var
|
||||
s, Value: String;
|
||||
begin
|
||||
for s in word do
|
||||
if Dictionary.TryGetValue(s, Value) then
|
||||
begin
|
||||
Write(Value + ' ');
|
||||
SayMorse(Value);
|
||||
end;
|
||||
end;
|
||||
|
||||
begin
|
||||
Dictionary := TDictionary<String, String>.Create;
|
||||
try
|
||||
InitCodes;
|
||||
if ParamCount = 0 then
|
||||
ParseMorse('sos')
|
||||
else if ParamCount = 1 then
|
||||
ParseMorse(LowerCase(ParamStr(1)))
|
||||
else
|
||||
Writeln('Usage: Morse.exe anyword');
|
||||
|
||||
Readln;
|
||||
finally
|
||||
Dictionary.Free;
|
||||
end;
|
||||
end.
|
||||
|
|
@ -18,8 +18,7 @@ defmodule Morse do
|
|||
def code(text) do
|
||||
String.upcase(text)
|
||||
|> String.codepoints
|
||||
|> Enum.map(fn c -> Dict.get(@morse, c, " ") end)
|
||||
|> Enum.join(" ")
|
||||
|> Enum.map_join(" ", fn c -> Map.get(@morse, c, " ") end)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
85
Task/Morse-code/Forth/morse-code.fth
Normal file
85
Task/Morse-code/Forth/morse-code.fth
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
HEX
|
||||
\ PC speaker hardware control (requires GIVEIO or DOSBOX for windows operation)
|
||||
042 constant fctrl
|
||||
043 constant tctrl
|
||||
061 constant sctrl
|
||||
0FC constant smask
|
||||
|
||||
\ PC@ is Port char fetch (Intel IN instruction). PC! is port char store (Intel OUT instruction)
|
||||
: speak ( -- ) sctrl pc@ 03 or sctrl pc! ;
|
||||
: silence ( -- ) sctrl pc@ smask and 01 or sctrl pc! ;
|
||||
|
||||
: tone ( freq -- ) \ freq is actually just a divisor value
|
||||
?dup \ check for non-zero input
|
||||
if 0B6 tctrl pc! \ enable PC speaker
|
||||
dup fctrl pc! \ set freq
|
||||
8 rshift fctrl pc!
|
||||
speak
|
||||
else
|
||||
silence
|
||||
then ;
|
||||
|
||||
\ morse demonstration begins here
|
||||
DECIMAL
|
||||
1000 value freq \ arbitrary value that sounded ok
|
||||
90 value adit \ 1 dit will be 90 ms
|
||||
|
||||
: dit_dur adit ms ;
|
||||
: dah_dur adit 3 * ms ;
|
||||
: wordgap adit 5 * ms ;
|
||||
: off_dur adit 2/ ms ;
|
||||
: lettergap dah_dur ;
|
||||
|
||||
: sound ( -- ) freq tone ;
|
||||
|
||||
: MORSE-EMIT ( char -- )
|
||||
dup bl = \ check for space character
|
||||
if
|
||||
wordgap drop \ and delay if detected
|
||||
else
|
||||
pad C! \ write char to buffer
|
||||
pad 1 evaluate \ evaluate 1 character
|
||||
lettergap \ pause for correct sounding morse code
|
||||
then ;
|
||||
|
||||
: TRANSMIT ( ADDR LEN -- )
|
||||
cr \ newline,
|
||||
bounds \ convert loop indices to address ranges
|
||||
do
|
||||
I C@ dup emit \ dup and send char to console
|
||||
morse-emit \ send the morse code
|
||||
loop ;
|
||||
|
||||
NAMESPACE MORSE \ prevent name conflicts with letters and numbers
|
||||
|
||||
MORSE DEFINITIONS \ the following definitions go into MORSE namespace
|
||||
|
||||
: . ( -- ) sound dit_dur silence off_dur ;
|
||||
: - ( -- ) sound dah_dur silence off_dur ;
|
||||
|
||||
\ define morse letters as Forth words. They transmit when executed
|
||||
|
||||
: A . - ; : B - . . . ; : C - . - . ; : D - . . ;
|
||||
: E . ; : F . . - . ; : G - - . ; : H . . . . ;
|
||||
: I . . ; : J . - - - ; : K - . - ; : L . - . . ;
|
||||
: M - - ; : N - . ; : O - - - ; : P . - - . ;
|
||||
: Q - - . - ; : R . - . ; : S . . . ; : T - ;
|
||||
: U . . - ; : V . . . - ; : W . - - ; : X - . . - ;
|
||||
: Y - . - - ; : Z - - . . ;
|
||||
|
||||
: 0 - - - - - ; : 1 . - - - - ;
|
||||
: 2 . . - - - ; : 3 . . . - - ;
|
||||
: 4 . . . . - ; : 5 . . . . . ;
|
||||
: 6 - . . . . ; : 7 - - . . . ;
|
||||
: 8 - - - . . ; : 9 - - - - . ;
|
||||
|
||||
: ' - . . - . ;
|
||||
: \ . - - - . ;
|
||||
: ! . - . - . ;
|
||||
: ? . . - - . . ;
|
||||
: , - - . . - - ;
|
||||
: / _ . . - . ;
|
||||
: . . - . - . - ;
|
||||
|
||||
PREVIOUS DEFINITIONS \ go back to previous namespace
|
||||
: TRANSMIT MORSE TRANSMIT PREVIOUS ;
|
||||
44
Task/Morse-code/Java/morse-code.java
Normal file
44
Task/Morse-code/Java/morse-code.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import java.util.*;
|
||||
|
||||
public class MorseCode {
|
||||
|
||||
final static String[][] code = {
|
||||
{"A", ".- "}, {"B", "-... "}, {"C", "-.-. "}, {"D", "-.. "},
|
||||
{"E", ". "}, {"F", "..-. "}, {"G", "--. "}, {"H", ".... "},
|
||||
{"I", ".. "}, {"J", ".--- "}, {"K", "-.- "}, {"L", ".-.. "},
|
||||
{"M", "-- "}, {"N", "-. "}, {"O", "--- "}, {"P", ".--. "},
|
||||
{"Q", "--.- "}, {"R", ".-. "}, {"S", "... "}, {"T", "- "},
|
||||
{"U", "..- "}, {"V", "...- "}, {"W", ".- - "}, {"X", "-..- "},
|
||||
{"Y", "-.-- "}, {"Z", "--.. "}, {"0", "----- "}, {"1", ".---- "},
|
||||
{"2", "..--- "}, {"3", "...-- "}, {"4", "....- "}, {"5", "..... "},
|
||||
{"6", "-.... "}, {"7", "--... "}, {"8", "---.. "}, {"9", "----. "},
|
||||
{"'", ".----. "}, {":", "---... "}, {",", "--..-- "}, {"-", "-....- "},
|
||||
{"(", "-.--.- "}, {".", ".-.-.- "}, {"?", "..--.. "}, {";", "-.-.-. "},
|
||||
{"/", "-..-. "}, {"-", "..--.- "}, {")", "---.. "}, {"=", "-...- "},
|
||||
{"@", ".--.-. "}, {"\"", ".-..-."}, {"+", ".-.-. "}, {" ", "/"}}; // cheat a little
|
||||
|
||||
final static Map<Character, String> map = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (String[] pair : code)
|
||||
map.put(pair[0].charAt(0), pair[1].trim());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
printMorse("sos");
|
||||
printMorse(" Hello World!");
|
||||
printMorse("Rosetta Code");
|
||||
}
|
||||
|
||||
static void printMorse(String input) {
|
||||
System.out.printf("%s %n", input);
|
||||
|
||||
input = input.trim().replaceAll("[ ]+", " ").toUpperCase();
|
||||
for (char c : input.toCharArray()) {
|
||||
String s = map.get(c);
|
||||
if (s != null)
|
||||
System.out.printf("%s ", s);
|
||||
}
|
||||
System.out.println("\n");
|
||||
}
|
||||
}
|
||||
59
Task/Morse-code/PowerShell/morse-code-1.psh
Normal file
59
Task/Morse-code/PowerShell/morse-code-1.psh
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
function Send-MorseCode
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([string])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipeline=$true,
|
||||
Position=0)]
|
||||
[string]
|
||||
$Message,
|
||||
|
||||
[switch]
|
||||
$ShowCode
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$morseCode = @{
|
||||
a = ".-" ; b = "-..." ; c = "-.-." ; d = "-.."
|
||||
e = "." ; f = "..-." ; g = "--." ; h = "...."
|
||||
i = ".." ; j = ".---" ; k = "-.-" ; l = ".-.."
|
||||
m = "--" ; n = "-." ; o = "---" ; p = ".--."
|
||||
q = "--.-" ; r = ".-." ; s = "..." ; t = "-"
|
||||
u = "..-" ; v = "...-" ; w = ".--" ; x = "-..-"
|
||||
y = "-.--" ; z = "--.." ; 0 = "-----"; 1 = ".----"
|
||||
2 = "..---"; 3 = "...--"; 4 = "....-"; 5 = "....."
|
||||
6 = "-...."; 7 = "--..."; 8 = "---.."; 9 = "----."
|
||||
}
|
||||
}
|
||||
Process
|
||||
{
|
||||
foreach ($word in $Message)
|
||||
{
|
||||
$word.Split(" ",[StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
|
||||
|
||||
foreach ($char in $_.ToCharArray())
|
||||
{
|
||||
if ($char -in $morseCode.Keys)
|
||||
{
|
||||
foreach ($code in ($morseCode."$char").ToCharArray())
|
||||
{
|
||||
if ($code -eq ".") {$duration = 250} else {$duration = 750}
|
||||
|
||||
[System.Console]::Beep(1000, $duration)
|
||||
Start-Sleep -Milliseconds 50
|
||||
}
|
||||
|
||||
if ($ShowCode) {Write-Host ("{0,-6}" -f ("{0,6}" -f $morseCode."$char")) -NoNewLine}
|
||||
}
|
||||
}
|
||||
|
||||
if ($ShowCode) {Write-Host}
|
||||
}
|
||||
|
||||
if ($ShowCode) {Write-Host}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Task/Morse-code/PowerShell/morse-code-2.psh
Normal file
1
Task/Morse-code/PowerShell/morse-code-2.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
Send-MorseCode -Message "S.O.S"
|
||||
1
Task/Morse-code/PowerShell/morse-code-3.psh
Normal file
1
Task/Morse-code/PowerShell/morse-code-3.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
"S.O.S", "Goodbye, cruel world!" | Send-MorseCode -ShowCode
|
||||
Loading…
Add table
Add a link
Reference in a new issue