Go实现莫斯码

Go实现莫斯码的方式和Java有异曲同工之妙,也是使用Map定义映射关系,只不过在Go里面叫字典。另外,Go里面键-值转换要比Java方便。代码如下:

// Morse Code
Go 
// An highlighted block
package LogicDetail
import (
	"fmt"
	"strings"
)

//定义莫斯码字典用户加密
func morse_dic() map[string]string{
	morse_code :=  make(map[string]string)
	morse_code["A"] = ".-"
	morse_code["a"] = "._"
	morse_code["B"] = "-..."
	morse_code["b"] = "_..."
	morse_code["C"] = "-.-."
	morse_code["c"] = "_._."
	morse_code["D"] = "-.."
	morse_code["d"] = "_.."
	morse_code["E"] = "."
	morse_code["e"] =","
	morse_code["F"] = "..-."
	morse_code["f"] =  ".._."
	morse_code["G"] = "--."
	morse_code["g"] = "__."
	morse_code["H"] = "...."
	morse_code["h"] = ",,,"
	morse_code["I"] = ".."
	morse_code["i"] = ",,"
	morse_code["J"] = ".---"
	morse_code["j"] = ".___"
	morse_code["K"] = "-.-"
	morse_code["k"] = "_._"
	morse_code["L"] = ".-.."
	morse_code["l"] ="._.."
	morse_code["M"] = "--"
	morse_code["m"] = "__"
	morse_code["N"] = "-."
	morse_code["n"] = "_."
	morse_code["O"] = "---"
	morse_code["o"] = "___"
	morse_code["P"] = ".--."
	morse_code["p"] = ".__."
	morse_code["Q"] = "--.-"
	morse_code["q"] = "__._"
	morse_code["R"] = ".-."
	morse_code["r"] = "._."
	morse_code["S"] = "..."
	morse_code["s"] = ",,,,"
	morse_code["T"] = "-"
	morse_code["t"] = "_"
	morse_code["U"] = "..-"
	morse_code["u"] = ".._"
	morse_code["V"] = "...-"
	morse_code["v"] = "..._"
	morse_code["W"] = ".--"
	morse_code["w"] = ".__"
	morse_code["X"] = "-..-"
	morse_code["x"] = "_.._"
	morse_code["Y"] = "-.--"
	morse_code["y"] = "_.__"
	morse_code["Z"] = "--.."
	morse_code["z"] = "__.."
	morse_code["0"] = "-----"
	morse_code["1"] = ".----"
	morse_code["2"] = "..---"
	morse_code["3"] = "...--"
	morse_code["4"] = "....-"
	morse_code["5"] = "....."
	morse_code["6"] = "-...."
	morse_code["7"] = "--..."
	morse_code["8"] = "---.."
	morse_code["9"] = "----."
	morse_code[","] = "--..--"
	morse_code["."] = ".-.-.-"
	morse_code["?"] = "..--.."
	morse_code["!"] = "-.-.--"
	morse_code["'"] = ".----." //单引号
	morse_code["\""] = ".-..-."//引号
	morse_code["="] = "-...-"
	morse_code[":"] = "---..."
	morse_code[";"] = "-.-.-."
	morse_code["("] = "-.--."
	morse_code[")"] = "-.--.-"
	morse_code[" "] = " "
	return morse_code
}
//键值互换,进行莫斯电码解密
func keyReserveValue() map[string]string{
	oriDic := morse_dic() //获取原莫斯字典
	resDic := make(map[string]string) //定义键值反转的莫斯字典
	for k,v := range oriDic{
		resDic[v] = k
	}
	return resDic
}
//莫斯电码转字符串
func MorseToStr(mor string){
	m := strings.Split(mor,"●")
	rd := keyReserveValue()
	fmt.Printf("莫斯译文:")
	var ori string = "" //定义明文
	for b:=0;b< len(m);b++{
		tmp := m[b]
		t,flag := rd[tmp]
		if flag{
			ori += t
		}
	}
	fmt.Println(ori)
}
//字符串转莫斯电码
func StrToMorse(str string){
	//fmt.Printf("------莫斯电码------\n")
	m :=[]byte(str) //将明文转换为byte数组
	var cip string = ""//定义密文字符串
	dic := morse_dic()//获取莫斯字典
	fmt.Printf("莫斯电码:")
	for a:=0;a< len(m);a++{
		tmp := m[a]
		t,flag := dic[string(tmp)]
		if flag{
			//cip = strings.Join([]string{t}, "●")
			cip += t + "●"

		}
	}
	fmt.Println(cip)
}
// Morse Code
Go
// An highlighted block
package main

import (
	"GoAlgorithm/LogicDetail"
	"fmt"
)

func main(){
	fmt.Println("----------莫斯码实现----------")
	//莫斯电码
	morStr := "I will communicate with you tomorrow!"
	fmt.Println("原文:",morStr)
	LogicDetail.StrToMorse(morStr)
	morTranStr := ".--●,,,●._●_●.----.●,,,,● ●_●,,,●,● ●__●._●_●_●,●._.●..--..●"
	fmt.Println("原莫斯码:",morTranStr)
	LogicDetail.MorseToStr(morTranStr)

}

你可能感兴趣的:(Go)