莫尔斯电码翻译要多组输入输出
题目描述
莫尔斯电码表示方式为:
a/A .-
b/B -...
c/C -.-.
d/D -..
e/E .
f/F ..-.
g/G --.
h/H ....
i/I ..
j/J .---
k/K -.-
l/L .-..
m/M --
n/N -.
o/O ---
p/P .--.
q/Q --.-
r/R .-.
s/S ...
t/T -
u/U ..-
v/V ...-
w/W .--
x/X -..-
y/Y -.--
z/Z --..
0 .----
1 ..---
2 ...--
3 ....-
4 .....
5 -....
6 --...
7 ---..
8 ----.
9 -----
其中字母部分不区分大小写。任务要求是将输入的字符串 (测试数据中所有的字母字符均为大写字母,无须再做判断,且长度小于等于 100) 转换成莫尔斯电码,每个电码间用 "===" 分割开来
输入描述
每行输入大写字母和数字构成的符号串,长度小于等于 100,没有分隔符
输出描述
对应的莫尔斯电码,每个莫尔斯码都用 "===" 隔开
样例输入
ABCD123456EFG
样例输出
.-===-...===-.-.===-..===..---===...--===....-===.....===-....===--...===.===..-.===--.
https://blog.csdn.net/qq_33793086/article/details/77504600
#include
#include
//首先将摩斯电码的36个符号及其电码(1对应.,0对应-)记录在一个数组内
char a[36][6] = { "10","0111","0101","011","1","1101","001","1111","11","1000","010","1011","00","01","000","1001","0010","101","111","0",
"110","1110","100","0110","0100","0011","00000","10000","11000","11100","11110","11111","01111","00111","00011","00001" };
//此函数实现摩斯电码的查找比对功能
void search(char s[6])
{
for (int i = 0; i < 36; i++)
if (strcmp(s, a[i]) == 0)
{
if (i < 26)
putchar(i + 97);
else
putchar(i + 48 - 26);
break;
}
for (int i = 0; i < 6; i++)
s[i] = 0;
}
//这是主要的函数 主要是实现莫尔斯电码的翻译过程
void translate(char *s)
{
char dest[6];
int index = 0;
for (int i = 0; s[i] != '\0'; i++)
{
if (s[i] == ' ' || s[i] == '/') ?//待翻译的电码需要以空格和除号分隔开来
? {
dest[index] = '\0';
search(dest);
index = 0;
}
else
{
dest[index++] = s[i];
}
}
dest[index] = 0;
search(dest);
}
int main()
{
printf("Please puts the Morse code or the cleartext:");
char *s;
s = new char[10000];
gets_s(s,10000);
if (s[0] != '.'&&s[0] != '-') { //如果要将正常的字符翻译成摩斯电码,直接输入字符串即可,如果要将摩斯电码翻译成字符串 直接输入摩斯电码即可
//程序有自动识别的功能
for (int i = 0; i < strlen(s); i++)
if (s[i] >= '0' && s[i] <= '9')
printf(a[26 + s[i] - 48]);
else if (s[i] >= 'a'&& s[i] <= 'z')
printf(a[s[i] - 97]);
else if (s[i] >= 'A' && s[i] <= 'Z')
printf(a[s[i] - 65]);
else {
printf("?");
break;
}
}
else {
for (int i = 0; s[i] != '\0'; i++)
if (s[i] == '.')
s[i] = '1';
else if (s[i] == '-')
s[i] = '0';
translate(s);
}
return 0;
}