用C++和二叉树实现简单的摩斯密码翻译器

摩斯密码由“-”和“.”组成,而大多数摩斯密码翻译器的原理为对需要解密的字符串和标准串进行每个元素的遍历和比较,时间复杂度很高。而建立一个二叉树,以左子树代表“-”,右子树代表“.”可以节省看空间还可以免去不匹配情况的遍历。两全其美。

注:此程序能够解密/加密的范围为(26个字母、0-9自然数、“-”和“.”)

解密输入格式举例:

-.- .-. .-.. .--- -.-. -... (回车结束,每个字符串之间相隔一个空格)

加密输入格式举例:

Ea18  -.87awraWqE    (回车结束,不分大小写,不分空格)

程序:

#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<fstream>
#define max 7
#define MAX_SIZE 1000
using namespace std;
typedef struct tree {
	char Data;
	char Code[max];
	struct tree* lchild;
	struct tree* rchild;
}Bitree;
Bitree* create(int n);
void Enter(char code[max], char s, Bitree* t);
Bitree* Init_tran(Bitree* t);
void Porder(Bitree* t);
void Getcode(Bitree *t);
void Gocode();

int main() {
	Bitree* t;
	t = create(max);
	t = Init_tran(t);
	int command;
	while (1) {
		cout << "\t***********************************\n"
			<< "\t*             菜单                *\n"
			<< "\t*                                 *\n"
			<< "\t*           1:解密                *\n"
			<< "\t*                                 *\n"
			<< "\t*           2:加密                *\n"
			<< "\t*                                 *\n"
			<< "\t*           3:结束                *\n"
			<< "\t*                                 *\n"
			<< "\t***********************************\n"
			<< "\t请输入操作序号:";
		cin >> command;
		switch (command) {
		case 1:Getcode(t); break;
		case 2: Gocode(); break;
		default:return 0;
		}
	}
}
Bitree* create(int n) {
	if (n) {
		Bitree* t;
		t = (Bitree*)malloc(sizeof(Bitree));
		t->Data = '*';
		t->lchild = create(n - 1);
		t->rchild = create(n - 1);
		return t;
	}
	return NULL;
}
void Enter(char code[max], char s, Bitree* t) {
	Bitree* p;
	p = t;
	for (int i = 0; i < strlen(code); i++) {
		if (code[i] == '-') {
			p = p->lchild;
		}
		else {
			p = p->rchild;
		}
	}
	p->Data = s;
	strcpy_s(p->Code, max, code);
}
Bitree* Init_tran(Bitree* t) {
	char a[38][max];
	strcpy_s(a[0],max, ".-");
	strcpy_s(a[1],max, "-...");
	strcpy_s(a[2],max,"-.-.");
	strcpy_s(a[3], max,"-..");
	strcpy_s(a[4], max,".");
	strcpy_s(a[5], max,"..-.");
	strcpy_s(a[6], max,"--.");
	strcpy_s(a[7], max,"....");
	strcpy_s(a[8], max,"..");
	strcpy_s(a[9],max, ".---");
	strcpy_s(a[10], max,"-.-");
	strcpy_s(a[11], max,".-..");
	strcpy_s(a[12], max,"--");
	strcpy_s(a[13],max, "-.");
	strcpy_s(a[14], max,"---");
	strcpy_s(a[15],max, ".--.");
	strcpy_s(a[16], max,"--.-");
	strcpy_s(a[17], max,".-.");
	strcpy_s(a[18],max, "...");
	strcpy_s(a[19], max,"-");
	strcpy_s(a[20],max, "..-");
	strcpy_s(a[21],max, "...-");
	strcpy_s(a[22],max, ".--");
	strcpy_s(a[23],max, "-..-");
	strcpy_s(a[24], max,"-.--");
	strcpy_s(a[25],max, "--..");
	strcpy_s(a[26], max,"-----");
	strcpy_s(a[27],max, ".----");
	strcpy_s(a[28],max, "..---");
	strcpy_s(a[29],max, "...--");
	strcpy_s(a[30],max, "....-");
	strcpy_s(a[31],max, ".....");
	strcpy_s(a[32],max, "-....");
	strcpy_s(a[33], max,"--...");
	strcpy_s(a[34],max, "---..");
	strcpy_s(a[35], max,"----.");
	strcpy_s(a[36], max,"-....-");
	strcpy_s(a[37], max,".-.-.-");

	Enter(a[0], 'A', t);
	Enter(a[1], 'B', t);
	Enter(a[2], 'C', t);
	Enter(a[3], 'D', t);
	Enter(a[4], 'E', t);
	Enter(a[5], 'F', t);
	Enter(a[6], 'G', t);
	Enter(a[7], 'H', t);
	Enter(a[8], 'I', t);
	Enter(a[9], 'J', t);
	Enter(a[10], 'K', t);
	Enter(a[11], 'L', t);
	Enter(a[12], 'M', t);
	Enter(a[13], 'N', t);
	Enter(a[14], 'O', t);
	Enter(a[15], 'P', t);
	Enter(a[16], 'Q', t);
	Enter(a[17], 'R', t);
	Enter(a[18], 'S', t);
	Enter(a[19], 'T', t);
	Enter(a[20], 'U', t);
	Enter(a[21], 'V', t);
	Enter(a[22], 'W', t);
	Enter(a[23], 'X', t);
	Enter(a[24], 'Y', t);
	Enter(a[25], 'Z', t);
	Enter(a[26], '0', t);
	Enter(a[27], '1', t);
	Enter(a[28], '2', t);
	Enter(a[29], '3', t);
	Enter(a[30], '4', t);
	Enter(a[31], '5', t);
	Enter(a[32], '6', t);
	Enter(a[33], '7', t);
	Enter(a[34], '8', t);
	Enter(a[35], '9', t);
	Enter(a[36], '-', t);
	Enter(a[37], '.', t);
	return t;
}
void Getcode(Bitree *t) {
	Bitree* p;
	char code[MAX_SIZE];
	char ch;
	char str[MAX_SIZE];
	int i = 0; int j = 0;
	getchar();
	cin.getline(code, MAX_SIZE);
	do {
		p = t;
		while (code[i] == '-' || code[i] == '.') {
			if (code[i] == '-') {
				p = p->lchild;
			}
			else {
				p = p->rchild;
			}
			i++;
		}
		str[j]=p->Data;
		j++;
		i++;
	} while (code[i] != '\0'&&code[i-1]!='\0');
	str[j] = '\0';
	cout << str << endl;
	
}
void Porder(Bitree* t) {//递归先序
	if (t)
	{
		printf("%c  ", t->Data);
		Porder(t->lchild);
		Porder(t->rchild);
	}
	return;
}

void Gocode() {
	char a[38][max];
	strcpy_s(a[0], max, ".-");
	strcpy_s(a[1], max, "-...");
	strcpy_s(a[2], max, "-.-.");
	strcpy_s(a[3], max, "-..");
	strcpy_s(a[4], max, ".");
	strcpy_s(a[5], max, "..-.");
	strcpy_s(a[6], max, "--.");
	strcpy_s(a[7], max, "....");
	strcpy_s(a[8], max, "..");
	strcpy_s(a[9], max, ".---");
	strcpy_s(a[10], max, "-.-");
	strcpy_s(a[11], max, ".-..");
	strcpy_s(a[12], max, "--");
	strcpy_s(a[13], max, "-.");
	strcpy_s(a[14], max, "---");
	strcpy_s(a[15], max, ".--.");
	strcpy_s(a[16], max, "--.-");
	strcpy_s(a[17], max, ".-.");
	strcpy_s(a[18], max, "...");
	strcpy_s(a[19], max, "-");
	strcpy_s(a[20], max, "..-");
	strcpy_s(a[21], max, "...-");
	strcpy_s(a[22], max, ".--");
	strcpy_s(a[23], max, "-..-");
	strcpy_s(a[24], max, "-.--");
	strcpy_s(a[25], max, "--..");
	strcpy_s(a[26], max, "-----");
	strcpy_s(a[27], max, ".----");
	strcpy_s(a[28], max, "..---");
	strcpy_s(a[29], max, "...--");
	strcpy_s(a[30], max, "....-");
	strcpy_s(a[31], max, ".....");
	strcpy_s(a[32], max, "-....");
	strcpy_s(a[33], max, "--...");
	strcpy_s(a[34], max, "---..");
	strcpy_s(a[35], max, "----.");
	strcpy_s(a[36], max, "-....-");
	strcpy_s(a[37], max, ".-.-.-");
	char str[MAX_SIZE];
	getchar();
	cin.getline(str, MAX_SIZE);
	int i = 0;
	do {
		if (str[i] >= 97 && str[i] <= 122) {
			str[i] -= 32;
		}
		if (str[i] >= 48 && str[i] <= 57) {
			cout << a[str[i] - 22] << "  ";
		}
		else if (str[i] >= 65 && str[i] <= 90) {
			cout << a[str[i] - 65] << "  ";
		}
		else if (str[i] == '-') {
			cout << a[36] << "  ";
		}
		else {
			cout << a[37] << "  ";
		}
		i++;
	} while (str[i] != '\0' && str[i - 1] != '\0');
	cout << endl;
}

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
数据结构关于二叉树的建立遍历以及应用二叉树进行编解码 实验要求 必做部分 1. 小明会按照前序的方式输入一棵二叉树。例如,输入$ACG##H##D##BE#I##F##的话,代表了下面这棵树: 2. 请分别按照前序、中序、后序输出这棵树。 选做部分 背景 在影视剧中,我们经常会看到二战期间情报人员使用电报哒哒哒地发送信息,发送电报所使用的编码叫做尔斯电码(或者叫做密码)。甚至在现代,SOS仍然是国际通用的求救信号之一,其“三短、三长、三短”同样是密码的编码方式。 密码使用若干个“点”和“划”来表示一个字母,字母和字母之间使用短暂的停顿来表示。例如,一种常见的编码方式为: 字母 密码 字母 密码 A .- E . B -... F ..-. C -.-. G --. D -.. H .... 实际上,一个密码本可以使用一棵二叉树来存储: 上图表示,从根节点start开始,遇到一个点(Dot)就访问它的左子树节点,遇到一个划(Dash)就访问它的右子树节点。例如,三个点...代表了S,三个划---代表了O。所以SOS的密码是... --- ...(中间用空格隔开,表示短暂的停顿)。再比如,爱疯手机有一种来电铃声的节奏为“哇哇哇 哇-哇- 哇哇哇”,这其实表示的是…… 现在,小明想在课上偷偷跟你传纸条,但又不想被其他同学看到内容。因此他跟你约定,每次给你传纸条时都使用密码来编写。至于密码本,当然不能使用国际通用的,他会在课前告诉你密码本的内容。然而小明发现,每次写纸条、读纸条都不是很方便,所以他想让你做个程序来自动编码/解码你们的密码。 题目要求 首先,小明输入的那棵二叉树,代表了你们在这堂课上要使用的密码本。例如,输入$ACG##H##D##BE#I##F##的话,代表了下面这棵树: 第一个字母$是什么并不重要,因为它只是代表了根节点,而根节点在我们的斯电码中并不代表一个字符,仅仅代表“start”。 读入密码本后,请记得按照前序、中序、后序输出这棵树。 然后,小明会输入一个数,代表接下来输入的是明文还是斯电码。输入0表示接下来他会输入明文,输入1表示接下来输入的是斯电码,输入-1程序退出。 1. 如果输入的是0,代表接下来要输入的是明文。程序接受一个字符串,根据字符串中每一个字母输出对应的斯电码,用空格隔开。例如如果小明输入“BED”,则程序应该输出“-空格-.空格.-”。如果遇到密码本中没有的字符,则输出“输入有误”。 2. 如果输入的是1,则表示接下来要输入斯电码。小明首先会输入一个数字N,代表有几个电码的输入,例如输入4代表之后会输入4个电码(即这个单词有四个字母)。随后输入空格分割的电码,例如, .. . -- -. 程序需要根据斯电码解读出明文单词并输出,例如上面的电码表示“CAFE”。如果遇到密码本中没有的编码,则输出“输入有误”。 输入输出样例 必做部分: 请输入二叉树: $ACG##H##D##BE#I##F## 前序遍历:$ACGHDBEIF 中序遍历:GCHAD$EIBF 后序遍历:GHCDAIEFB$ 选做部分: 请选择(0为明文,1为电码,-1退出):0 请输入明文:BED 斯电码为:- -. .- 请选择(0为明文,1为电码,-1退出):1 请输入电码个数:4 请输入电码:.. . -- -. 明文为:CAFE 请选择(0为明文,1为电码,-1退出):-1 // 程序结束
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淬炼之火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值