0921每日一练——C++ 摩斯电码转换(学习篇)

设计一段程序,实现莫尔斯电码的转换

如输入:123,打印出转换后的结果为.----…—…—
答:
其实逻辑上不是很复杂,但是涉及到很多字符串的细节操作问题,应用了之前讲到的memset函数,strcpy_s函数等,还学习应用了一些之前学到的c++中关于容器的知识,令我对这块的应用有了进一步的了解。
在这里插入图片描述
具体实现代码如下:

//#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
void input(vector<char> &ch);
void inpup(vector<string> &ch);
void inpus(string &str);
void show1(vector<string> &arr);
void show(vector<char> &arr);

int main()
{
	vector<char> cb;
	vector<string> cd;
	string st;
	int pd;
	while (1)
	{
		cout << "请输入您需要的功能\n1、摩尔斯电码->英语(空格隔开)\n2、摩尔斯电码->英语( / 隔开)\n3、英语->莫尔斯电码:\n";
		cin >> pd;
		if (pd == 1)
			input(cb);
		else if (pd == 2)
		{
			cout << "请输入您要转换的电码(每段用/隔开)" << endl;
			cin >> st;
			inpus(st);
			cout << endl;
		}
		else if (pd == 3)
			inpup(cd);

		else
		{
			cout << "请在1、2、3中选择,谢谢" << endl;
			break;
		}
		cb.clear();
		cd.clear();
	}
	return 0;
}
void input(vector<char> &ch)
{
	cout << "请输入摩尔斯电码(输入 “end”结束 ;每段电码请用空格隔开):\n";
	string str = "";
	int bol = 0;
	string strend = "end";
	while (str != strend)
	{
		cin >> str;
		if (str == ".-")
			ch.push_back('a');
		else if (str == "-...")
			ch.push_back('b');
		else if (str == "-.-.")
			ch.push_back('c');
		else if (str == "-..")
			ch.push_back('d');
		else if (str == ".")
			ch.push_back('e');
		else if (str == "..-.")
			ch.push_back('f');
		else if (str == "--.")
			ch.push_back('g');
		else if (str == "....")
			ch.push_back('h');
		else if (str == "..")
			ch.push_back('i');
		else if (str == ".---")
			ch.push_back('j');
		else if (str == "-.-")
			ch.push_back('k');
		else if (str == ".-..")
			ch.push_back('l');
		else if (str == "--")
			ch.push_back('m');
		else if (str == "-.")
			ch.push_back('n');
		else if (str == "---")
			ch.push_back('o');
		else if (str == ".--.")
			ch.push_back('p');
		else if (str == "--.-")
			ch.push_back('q');
		else if (str == ".-.")
			ch.push_back('r');
		else if (str == "...")
			ch.push_back('s');
		else if (str == "-")
			ch.push_back('t');
		else if (str == "..-")
			ch.push_back('u');
		else if (str == "...-")
			ch.push_back('v');
		else if (str == ".--")
			ch.push_back('w');
		else if (str == "-..-")
			ch.push_back('x');
		else if (str == "-.--")
			ch.push_back('y');
		else if (str == "--..")
			ch.push_back('z');
		else if (str == ".----")
			ch.push_back('1');
		else if (str == "..---")
			ch.push_back('2');
		else if (str == "...--")
			ch.push_back('3');
		else if (str == "....-")
			ch.push_back('4');
		else if (str == ".....")
			ch.push_back('5');
		else if (str == "-....")
			ch.push_back('6');
		else if (str == "--...")
			ch.push_back('7');
		else if (str == "---..")
			ch.push_back('8');
		else if (str == "----.")
			ch.push_back('9');
		else if (str == "-----")
			ch.push_back('0');
		else if (str == "..--..")
			ch.push_back('?');
		else if (str == "-..-.")
			ch.push_back('/');
		else if (str == "-....-")
			ch.push_back('-');
		else if (str == ".-.-.-")
			ch.push_back('.');
		else if (str == "-.--.-")
		{
			if (bol == 0)
			{
				ch.push_back('(');
				bol = 1;
			}
			else
			{
				ch.push_back(')');
				bol = 0;
			}
		}

		if (str == strend)
			show(ch);
	}
}

void inpup(vector<string> &ch)
{
	string temp;
	cout << "请输入需要转换的内容(英语):\n";
	cin >> temp;
	char tem[sizeof(temp)];
	memset(tem, '0', sizeof(tem));//它是对较大的结构体或数组进行清零操作的一种最快方法
								  //memset函数通常用来对一块已经分配地址的内存进行初始化
	strcpy_s(tem, temp.c_str());//保证缓冲区大小
								//c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同
	for (int i = 0; i < temp.length(); i++)
	{
		if (tem[i] == 'a')
			ch.push_back(".-");
		else if (tem[i] == 'b')
			ch.push_back("-...");
		else if (tem[i] == 'c')
			ch.push_back("-.-.");
		else if (tem[i] == 'd')
			ch.push_back("-..");
		else if (tem[i] == 'e')
			ch.push_back(".");
		else if (tem[i] == 'f')
			ch.push_back("..-.");
		else if (tem[i] == 'g')
			ch.push_back("--.");
		else if (tem[i] == 'h')
			ch.push_back("....");
		else if (tem[i] == 'i')
			ch.push_back("..");
		else if (tem[i] == 'j')
			ch.push_back(".---");
		else if (tem[i] == 'k')
			ch.push_back("-.-");
		else if (tem[i] == 'l')
			ch.push_back(".-..");
		else if (tem[i] == 'm')
			ch.push_back("--");
		else if (tem[i] == 'n')
			ch.push_back("-.");
		else if (tem[i] == 'o')
			ch.push_back("---");
		else if (tem[i] == 'p')
			ch.push_back(".--.");
		else if (tem[i] == 'q')
			ch.push_back("--.-");
		else if (tem[i] == 'r')
			ch.push_back(".-.");
		else if (tem[i] == 's')
			ch.push_back("...");
		else if (tem[i] == 't')
			ch.push_back("-");
		else if (tem[i] == 'u')
			ch.push_back("..-");
		else if (tem[i] == 'v')
			ch.push_back("...-");
		else if (tem[i] == 'w')
			ch.push_back(".--");
		else if (tem[i] == 'x')
			ch.push_back("-..-");
		else if (tem[i] == 'y')
			ch.push_back("-.--");
		else if (tem[i] == 'z')
			ch.push_back("--..");
		else if (tem[i] == '1')
			ch.push_back(".----");
		else if (tem[i] == '2')
			ch.push_back("..---");
		else if (tem[i] == '3')
			ch.push_back("...--");
		else if (tem[i] == '4')
			ch.push_back("....-");
		else if (tem[i] == '5')
			ch.push_back(".....");
		else if (tem[i] == '6')
			ch.push_back("-....");
		else if (tem[i] == '7')
			ch.push_back("--...");
		else if (tem[i] == '8')
			ch.push_back("---..");
		else if (tem[i] == '9')
			ch.push_back("----.");
		else if (tem[i] == '0')
			ch.push_back("-----");
		else if (tem[i] == '?')
			ch.push_back("..--..");
		else if (tem[i] == '/')
			ch.push_back("-..-.");
		else if (tem[i] == '-')
			ch.push_back("-....-");
		else if (tem[i] == '.')
			ch.push_back(".-.-.-");
		else if (tem[i] == '(' || tem[i] == ')')
			ch.push_back("-.--.-");
		else
			ch.push_back("  **该处字符无法转换**  ");
	}
	show1(ch);
}

void show1(vector<string> &arr)
{
	//迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型。每种容器类型都定义了自己的迭代器类型,
	//vector<int>::iterator iter; 这条语句定义了一个名为iter的变量,它的数据类型是由vector<int>定义的iterator类型。
	vector<string>::iterator it;
	for (it = arr.begin(); it != arr.end(); it++)
	{
		cout << *it << '/';
	}
	cout << endl;
}

void show(vector<char> &arr)
{
	if (arr.empty())
	{
		cout << "请输入正确电码。\n";
	}
	vector<char>::iterator it;
	for (it = arr.begin(); it<arr.end(); it++)
	{
		cout << *it;
	}
	cout << endl;
}

void inpus(string &str)
{
	unsigned int s1 = 0, s2 = 0, index = 0;
	char x = 0;
	int bol = 0;
	unsigned int end = str.size();
	string temp = "";
	while (index < end)
	{
		s1 = index;
		while (index < end && str[index] != '/')
		{
			index++;
		}
		s2 = index;
		temp = str.substr(s1, s2 - s1);
		if (temp == ".-")
			cout << 'a';
		else if (temp == "-...")
			cout << 'b';
		else if (temp == "-.-.")
			cout << 'c';
		else if (temp == "-..")
			cout << 'd';
		else if (temp == ".")
			cout << 'e';
		else if (temp == "..-.")
			cout << 'f';
		else if (temp == "--.")
			cout << 'g';
		else if (temp == "....")
			cout << 'h';
		else if (temp == "..")
			cout << 'i';
		else if (temp == ".---")
			cout << 'j';
		else if (temp == "-.-")
			cout << 'k';
		else if (temp == ".-..")
			cout << 'l';
		else if (temp == "--")
			cout << 'm';
		else if (temp == "-.")
			cout << 'n';
		else if (temp == "---")
			cout << 'o';
		else if (temp == ".--.")
			cout << 'p';
		else if (temp == "--.-")
			cout << 'q';
		else if (temp == ".-.")
			cout << 'r';
		else if (temp == "...")
			cout << 's';
		else if (temp == "-")
			cout << 't';
		else if (temp == "..-")
			cout << 'u';
		else if (temp == "...-")
			cout << 'v';
		else if (temp == ".--")
			cout << 'w';
		else if (temp == "-..-")
			cout << 'x';
		else if (temp == "-.--")
			cout << 'y';
		else if (temp == "--..")
			cout << 'z';
		else if (temp == ".----")
			cout << '1';
		else if (temp == "..---")
			cout << '2';
		else if (temp == "...--")
			cout << '3';
		else if (temp == "....-")
			cout << '4';
		else if (temp == ".....")
			cout << '5';
		else if (temp == "-....")
			cout << '6';
		else if (temp == "--...")
			cout << '7';
		else if (temp == "---..")
			cout << '8';
		else if (temp == "----.")
			cout << '9';
		else if (temp == "-----")
			cout << '0';
		else if (temp == "..--..")
			cout << '?';
		else if (temp == "-..-.")
			cout << '/';
		else if (temp == "-....-")
			cout << '-';
		else if (temp == ".-.-.-")
			cout << '.';
		else if (temp == "-.--.-")
		{
			if (bol == 0)
			{
				cout << '(';
				bol = 1;
			}
			else
			{
				cout << ')';
				bol = 0;
			}
		}
		else
		{
			cout << "  ** 您此处电码有误!**  ";
		}
		index++;
	}
}
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
摩尔电码(Morse Code)是一种早期的通信方式,通过长短音符组合来代表字母、数字和标点符号。在Python中实现一个摩尔电码转换器,你可以创建两个函数:一个用于将文本转换摩尔电码,另一个用于将摩尔电码转换回文本。这里是一个简单的示例: 1. **编码函数**: 它会接受一个字符串参数,查找每个字符对应的摩尔电码,并连接起来形成最终的摩尔电码字符串。 ```python def encode_morse(text): morse_code_dict = { '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': '----.' } return ' '.join([morse_code_dict[ch] for ch in text.upper()]) ``` 2. **解码函数**: 它会接受一个摩尔电码字符串,将其分割成单独的字符,然后找到每个字符对应的字母并组合起来。 ```python def decode_morse(morse_code): morse_code_dict = {value: key for key, value in morse_code_dict.items()} decoded_text = '' words = morse_code.split(' ') for word in words: decoded_text += morse_code_dict[word] if word in morse_code_dict else ' ' return decoded_text ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值