字符串转莫尔斯电码

本文介绍了如何使用C++中的for循环和if-else条件语句,将输入的字符串转换为ASCII艺术形式并在控制台上显示。
摘要由CSDN通过智能技术生成
#include <bits/stdc++.h>
using namespace std;

string mes(string s) {
	for (int i = 0; i <= s.size(); i++) {
		if (s[i] == ' ')
			cout << " ";
		else if (s[i] == 'a')
			cout << "·-"  << " ";
		else if (s[i] == 'b')
			cout << "-···" << " ";
		else if (s[i] == 'c')
			cout << "-·-·"    << " ";
		else if (s[i] == 'd')
			cout << "-··" << " ";
		else if (s[i] == 'e')
			cout << "·" << " ";
		else if (s[i] == 'f')
			cout << "··-·" << " ";
		else if (s[i] == 'g')
			cout << "--·" << " ";
		else if (s[i] == 'h')
			cout << "····" << " ";
		else if (s[i] == 'i')
			cout << "··" << " ";
		else if (s[i] == 'j')
			cout << "·---" << " ";
		else if (s[i] == 'k')
			cout << "-·-" << " ";
		else if (s[i] == 'l')
			cout << "·-··" << " ";
		else if (s[i] == 'm')
			cout << "--" << " ";
		else if (s[i] == 'n')
			cout << "-·" << " ";
		else if (s[i] == 'o')
			cout << "---" << " ";
		else if (s[i] == 'p')
			cout << "·--·" << " ";
		else if (s[i] == 'q')
			cout << "--·-" << " ";
		else if (s[i] == 'r')
			cout << "·-·" << " ";
		else if (s[i] == 's')
			cout << "···" << " ";
		else if (s[i] == 't')
			cout << "-" << " ";
		else if (s[i] == 'u')
			cout << "··-" << " ";
		else if (s[i] == 'v')
			cout << "···-" << " ";
		else if (s[i] == 'w')
			cout << "·--" << " ";
		else if (s[i] == 'x')
			cout << "-··-" << " ";
		else if (s[i] == 'y')
			cout << "-·--" << " ";
		else if (s[i] == 'z')
			cout << "--··" << " ";
	}
}

int main() {
	string a;
	getline(cin, a);
	mes(a);
	return 0;
}

莫尔斯电码是一种用于发送电报的编码方式,其中字母和数字被编码成长短不同的信号,例如点和划线。如果您想在MATLAB中生成莫尔斯电码,可以按照以下步骤进行: 1. 定义莫尔斯电码的字母和数字对应的编码方式。例如,字母A的莫尔斯电码为“.-”,数字1的莫尔斯电码为“.----”。 2. 将需要换为莫尔斯电码的本分解成单个字符。 3. 将每个字符换为其莫尔斯电码等效项,例如“-”和“.”。 4. 将每个字符的莫尔斯电码等效项连接起来,以形成完整的莫尔斯电码序列。 以下是一个简单的MATLAB代码示例,用于将字符串“HELLO WORLD”换为莫尔斯电码: ```matlab % 定义莫尔斯电码字母和数字对应的编码方式 morseCode = struct('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', '----.'); % 要换的字符串 text = 'HELLO WORLD'; % 将字符串换为大写形式 text = upper(text); % 将每个字符换为莫尔斯电码等效项 morseText = cell(size(text)); for i = 1:length(text) if isfield(morseCode, text(i)) morseText{i} = morseCode.(text(i)); else morseText{i} = ''; end end % 将每个字符的莫尔斯电码等效项连接起来,以形成完整的莫尔斯电码序列 morseSequence = strjoin(morseText, ' '); % 输出结果 disp(morseSequence); ``` 该代码将输出以下结果: ``` .... . .-.. .-.. --- / .-- --- .-. .-.. -.. ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值