小知识 学习 信息 资料
触屏版

close ◇ 读取数据,请稍候 Loading...

.: Welcome to flymote.com [flymot.com] :.



  »

◆ 其它资料:.

MORSE Coder

2005-05-25 17:11:25   李茂祥      阅读: 951   【本类别其它资料】 【资料库首页
上一篇 收藏 打印 收藏夹 下一篇

〖MORSE Coder〗
<?php
//
// Morse Convertor v1.0 by Edwin Groothuis (edwin@mavetju.org)
//
// If you didn't get this file via http://www.mavetju.org, please
// check for the availability of newer versions.
//
// See LICENSE for distribution issues. If this file isn't in
// the distribution, please inform me about it.
//
// Feel free to use this yourself.
//

$lettertomorse=array();
$lettertomorse["a"]=".-";
$lettertomorse["b"]="-...";
$lettertomorse["c"]="-.-.";
$lettertomorse["d"]="-..";
$lettertomorse["e"]=".";
$lettertomorse["f"]="..-.";
$lettertomorse["g"]="--.";
$lettertomorse["h"]="....";
$lettertomorse["i"]="..";
$lettertomorse["j"]=".---";
$lettertomorse["k"]=".-.";
$lettertomorse["l"]=".-..";
$lettertomorse["m"]="--";
$lettertomorse["n"]="-.";
$lettertomorse["o"]="---";
$lettertomorse["p"]=".--.";
$lettertomorse["q"]="--.-";
$lettertomorse["r"]=".-.";
$lettertomorse["s"]="...";
$lettertomorse["t"]="-";
$lettertomorse["u"]="..-";
$lettertomorse["v"]="...-";
$lettertomorse["w"]=".--";
$lettertomorse["x"]="-..-";
$lettertomorse["y"]="-.--";
$lettertomorse["z"]="--..";
$lettertomorse["1"]=".----";
$lettertomorse["2"]="..---";
$lettertomorse["3"]="...--";
$lettertomorse["4"]="....-";
$lettertomorse["5"]=".....";
$lettertomorse["6"]="-....";
$lettertomorse["7"]="--...";
$lettertomorse["8"]="---..";
$lettertomorse["9"]="----.";
$lettertomorse["0"]="-----";
$lettertomorse[" "]=" ";
$lettertomorse["."]=".-.-.-";
$lettertomorse[","]="--..--";
$lettertomorse["EOM"]=".-.-.";

$morsetoletter=array();
reset($lettertomorse);
while (list($letter,$code)=each($lettertomorse)) {
$morsetoletter[$code]=$letter;
}

function morse_encode($txt) {
global $lettertomorse;

$line="";
for ($i=0;$i<strlen($txt);$i++) {
$letter=substr($txt,$i,1);

// ignore unknown characters
if ($lettertomorse[$letter]=="") continue;

$line.=$lettertomorse[$letter]." ";
}
return $line;
}

function morse_decode($string) {
global $morsetoletter;

$line="";
$letters=array();
$letters=explode(" ",$string);
foreach ($letters as $letter) {
// ignore unknown characters
if ($letter=="") $line.=" ";
if ($morsetoletter[$letter]=="") continue;

$line.=$morsetoletter[$letter];
}
return $line;
}
?>

<h1>Morse transletter</h1>

<form method="post" action="<?php echo $PHP_SELF ?>">
Real life text: <input type="text" name="txt" value="<?php echo $txt?>" size=50>
<br>
Morse text: <input type="text" name="morse" value="<?php echo $morse?>" size=50>
<input type="submit" value="Convert">
<input type="reset">
<br>
<hr>

<?php

if ($morse!="") {
echo morse_decode($morse),"<br>\n";
}

if ($txt!="") {
echo morse_encode($txt),"<br>\n";
}

if ($txt!="") {
echo "<hr>\n<pre>";

$line1="";
$line2="";

for ($i=0;$i<strlen($txt);$i++) {
$letter=substr($txt,$i,1);

// ignore unknown characters
if ($lettertomorse[$letter]=="") continue;

$line1.=$lettertomorse[$letter]." ";
$line2.=$letter;
for ($j=0;$j<strlen($lettertomorse[$letter]);$j++)
$line2.=" ";
$size+=strlen($lettertomorse[$letter])+1;

if ($size>70) {
echo "$line1\n";
echo "$line2\n";
echo "\n";
$line1="";
$line2="";
$size=0;
}
}

echo "$line1\n";
echo "$line2\n";
echo "\n";
echo "\n</pre>\n";
}
?>
责任编辑:

【最近的搜索】:

上一篇 收藏 打印 收藏夹 下一篇
CopyRight(c) 2007 - 2020 All Rights Reserved  【赣ICP备12001042号】
触屏版 | Archiver 20200922 05:15 | 简介 | 帮助 | 留言 | 关于 | 360网站安全检测平台