摩尔斯电码(morse)转换英文字符串c语言代码

2023-10-10 09:48

本文主要是介绍摩尔斯电码(morse)转换英文字符串c语言代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        找到这篇文章说明你对摩尔斯电码很赶兴趣,而且你已掌握了摩尔斯电码的基础知识了。想更贴近的感受一下摩尔斯电码的魅力。或你有一个非常棒的关于莫尔斯电码想法而非常激动。如果是这样的话,那你或许会从我的代码中得到帮助,缩短你实现想法征途。如果真的能帮助你的话,那么我也会很高兴。我也是如此的喜欢摩尔斯电码。

        有任何疑问或者有想法想与人一起分享,邮箱我:robert.cysy@gmail.com


我把代码放到了github上了 地址 https://github.com/robert1207/morse_encode.git

可以在Linux下用Makefile 编译,也可以在windows下新建工程添加github上的代码就可以了


下面是代码片段:

#include #include #include #include "morse.h"
#define BUF_LEN 300
int main() {
char *mystr = "abcdefghijklmnopqrstuvwxyz0123456789.:,;?='/!-_\"()$&@";
char mor[BUF_LEN];
char str[BUF_LEN];
char out[BUF_LEN];
memset(out, 0, BUF_LEN);
memset(mor, 0, BUF_LEN);
memset(str, 0, BUF_LEN);
printf("base string:\n%s\n", mystr);
//TO LOWCASE
str2lowcase(mystr, out, BUF_LEN);
//TO MORSE STRING
String2MorseString(out , mor, BUF_LEN);
printf("\nget morse code string:\n%s\n" , mor);
//TO NORMAL STRING
MorseString2String(mor, str, BUF_LEN);
printf("\nget decode string:\n%s\n", str);
return 0;
}

#include "morse.h"
#include #include #include #define NUM_LEN 10
char num[][5] = {
{'-','-','-','-','-'},//0
{'.','-','-','-','-'},//1
{'.','.','-','-','-'},//2
{'.','.','.','-','-'},//3
{'.','.','.','.','-'},//4
{'.','.','.','.','.'},//5
{'-','.','.','.','.'},//6
{'-','-','.','.','.'},//7
{'-','-','-','.','.'},//8
{'-','-','-','-','.'} //9
};
#define MARK_LEN 17
char mark[][8] = {
{'.', '-', '.', '-', '.', '-', '*', '.'},//.	0
{'-', '-', '-', '.', '.', '.', '*', ':'},//:
{'-', '-', '.', '.', '-', '-', '*', ','},//,
{'-', '.', '-', '.', '-', '.', '*', ';'},//;
{'.', '.', '-', '-', '.', '.', '*', '?'},//?
{'-', '.', '.', '.', '-', '*', '*', '='},//=
{'.', '-', '-', '-', '-', '.', '*', '\''},//'
{'-', '.', '.', '-', '.', '*', '*', '/'},///
{'-', '.', '-', '.', '-', '-', '*', '!'},//!
{'-', '.', '.', '.', '.', '-', '*', '-'},//-
{'.', '.', '-', '-', '.', '-', '*', '_'},//_
{'.', '-', '.', '.', '-', '.', '*', '"'},//"
{'-', '.', '-', '-', '.', '*', '*', '('},//(
{'-', '.', '-', '-', '.', '-', '*', ')'},//)
{'.', '.', '.', '-', '.', '.', '-', '$'},//$
{'.', '-', '.', '.', '.', '*', '*', '&'},//&
{'.', '-', '-', '.', '-', '.', '*', '@'} //@	16
};
#define CHARACTER 26
char a2[][4] = {
{'.','-','*','*'},//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
};
Morse_t *new_morse() {
Morse_t *ret;
ret = (Morse_t*)malloc(sizeof(Morse_t));
memset(ret->c, 0, 9);
return ret;
}
/*
*	MARK
*/
bool mark2morse(char n, Morse_t *morse) {
int a = 0;
for (; a < MARK_LEN; a++) {
if (mark[a][7] == n) {
morse->c[0] = mark[a][0];
morse->c[1] = mark[a][1];
morse->c[2] = mark[a][2];
morse->c[3] = mark[a][3];
morse->c[4] = mark[a][4];
morse->c[5] = mark[a][5];
morse->c[6] = mark[a][6];
return true;
}
}
return false;
}
bool morse2mark(Morse_t *morse, char *n) {
int a = 0;
for (; a < MARK_LEN; a++) {
if (mark[a][0] == morse->c[0] &&
mark[a][1] == morse->c[1] &&
mark[a][2] == morse->c[2] &&
mark[a][3] == morse->c[3] &&
mark[a][4] == morse->c[4] &&
mark[a][5] == morse->c[5] &&
mark[a][6] == morse->c[6] ) {
*n = mark[a][7];
return true;
}
}
return false;
}
/*
*	NUMBER
*/
bool num2morse(char n, Morse_t *morse) {
int pos = n - 48;
if (pos <= 9 && pos >= 0) {
morse->c[0] = num[pos][0];
morse->c[1] = num[pos][1];
morse->c[2] = num[pos][2];
morse->c[3] = num[pos][3];
morse->c[4] = num[pos][4];  
return true;
} 
return false;
}
bool morse2num(Morse_t *morse, char *n) {
int i = 0;
for (; i < NUM_LEN; i++) {
if (num[i][0] == morse->c[0] &&
num[i][1] == morse->c[1] &&
num[i][2] == morse->c[2] &&
num[i][3] == morse->c[3] &&
num[i][4] == morse->c[4] ) {
*n = (char)(i + 48);
return true;
}
}
return false;
}
/*
*	CHARACTER
*/
bool str2morse(char m , Morse_t *morse) {
int pos = m - 97;
if (pos >= 0 && pos <= 25) {
morse->c[0] = a2[pos][0];
morse->c[1] = a2[pos][1];
morse->c[2] = a2[pos][2];	
morse->c[3] = a2[pos][3];
return true;
}
return false;
}
bool morse2str(Morse_t *morse, char *ch) {
int i = 0;
for (i = 0; i < CHARACTER; i++) {
if (a2[i][0] == morse->c[0] &&
a2[i][1] == morse->c[1] &&
a2[i][2] == morse->c[2] &&
a2[i][3] == morse->c[3]) { 
*ch =  (char)(i + 97);
return true;
}
}
return false;
}
void MorseString2String(char *morse ,char *string, int buf_len) {
Morse_t *temp = new_morse();
int a = 0;
int b = 0;
int c = 0;
int len = 0;
char ch = '*';
memset(temp->c, '*', 8);
len = strlen(morse);
for ( ; a < len; a ++) {
if (c > buf_len) {
printf("the string buffer is too little\n");
return;
}
if (morse[a] != SEPARATOR && morse[a] != FAKE_SPACE)
temp->c[b++] = morse[a];
else if (morse[a] == SEPARATOR && morse[a-1] != FAKE_SPACE) {//get one charactor
if (true == morse2str(temp, &ch) && b < 5) {
string[c++] = ch;
} else if (true == morse2num(temp, &ch)) {
string[c++] = ch;
} else if (true == morse2mark(temp, &ch)) {
string[c++] = ch;
} else {
printf("has morse that not be decoded !\n");
}
//clean
b = 0;
memset(temp->c, '*' ,8);
} else if (morse[a] == FAKE_SPACE) { //have a space
string[c++] = ' ';
}
} 
}
void String2MorseString(char *string ,char *morse, int buf_len) {
int a = 0;
int b = 0;
int len = strlen(string);
Morse_t * temp = new_morse();
for (; a < len; a ++ ) {
if (buf_len < 8 || b >= buf_len) {
printf("morse buffer is too litte\n");
break;
}
if (string[a] != ' ') {
//if is a num 
if (string[a] >= '0' && string[a] <= '9') {
if (true == num2morse(string[a], temp)) {
morse[b++] = temp->c[0];
morse[b++] = temp->c[1];
morse[b++] = temp->c[2];
morse[b++] = temp->c[3];
morse[b++] = temp->c[4];
} else {
printf("encode on mumber error \n");
return ;
}
}
//if is a character
else if (string[a] >= 97 && string[a] <= 122) {
if (true == str2morse(string[a], temp)) {
morse[b++] = temp->c[0];
if (temp->c[1] != '*')
morse[b++] = temp->c[1];
if (temp->c[2] != '*')
morse[b++] = temp->c[2];
if (temp->c[3] != '*')
morse[b++] = temp->c[3];
} else {
printf("encode on str error \n");
return ;
}
}
//if is a mark
else if (string[a] <= 127) {
if (true == mark2morse(string[a], temp)) {
morse[b++] = temp->c[0]; 
morse[b++] = temp->c[1];
morse[b++] = temp->c[2];
morse[b++] = temp->c[3];
morse[b++] = temp->c[4];
if (temp->c[5] != '*')
morse[b++] = temp->c[5];
if (temp->c[6] != '*')
morse[b++] = temp->c[6];
} else {
printf("encode on mark error \n");
return ;
}
} else {
printf("out of the morse character \n");
return ;
}
//clean
memset(temp->c, 0 , 8);
morse[b++] = SEPARATOR;
} else if (string[a] == ' ') { //have a space and add / to instead
morse[b++] = FAKE_SPACE;
morse[b++] = SEPARATOR;
}
}
}
void str2lowcase(char *str, char *out, int buf_len) {
int len = strlen(str);
int a = 0;
if (len >= buf_len) {
printf("buf is to low\n");
return;
}
for (;a < len; a++) {
if (str[a] >= 'A' && str[a] <= 'Z') {
out[a] = str[a] + 32;
} else {
out[a] = str[a];
}
}
}
#ifndef MORSE_H_
#define MORSE_H_
typedef int bool;
#define false 0
#define true  1
/*
*  FAKE_SPACE IS MARING FOR A SPACE
*/
#define FAKE_SPACE '/'
/*
* THE CHARACTER THAT BETWEEN TWO MORSE STRING
*/
#define SEPARATOR ' '
typedef struct Morse Morse_t;
struct Morse{
char c[9];
};
Morse_t *new_morse();
bool str2morse(char m , Morse_t *morse);
bool morse2str(Morse_t *morse, char *ch);
bool mark2morse(char n, Morse_t *morse);
bool morse2mark(Morse_t *morse, char *n);
bool num2morse(char n, Morse_t *morse);
bool morse2num(Morse_t *morse, char *n);
void MorseString2String(char *morse ,char *string, int buf_len);
void String2MorseString(char *string ,char *morse, int buf_len);
void str2lowcase(char *str, char *out, int buf_len);
#endif /* MORSE_H_ */


这篇关于摩尔斯电码(morse)转换英文字符串c语言代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/179655

相关文章

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

代码随想录冲冲冲 Day39 动态规划Part7

198. 打家劫舍 dp数组的意义是在第i位的时候偷的最大钱数是多少 如果nums的size为0 总价值当然就是0 如果nums的size为1 总价值是nums[0] 遍历顺序就是从小到大遍历 之后是递推公式 对于dp[i]的最大价值来说有两种可能 1.偷第i个 那么最大价值就是dp[i-2]+nums[i] 2.不偷第i个 那么价值就是dp[i-1] 之后取这两个的最大值就是d

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip

D4代码AC集

贪心问题解决的步骤: (局部贪心能导致全局贪心)    1.确定贪心策略    2.验证贪心策略是否正确 排队接水 #include<bits/stdc++.h>using namespace std;int main(){int w,n,a[32000];cin>>w>>n;for(int i=1;i<=n;i++){cin>>a[i];}sort(a+1,a+n+1);int i=1

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

MiniGPT-3D, 首个高效的3D点云大语言模型,仅需一张RTX3090显卡,训练一天时间,已开源

项目主页:https://tangyuan96.github.io/minigpt_3d_project_page/ 代码:https://github.com/TangYuan96/MiniGPT-3D 论文:https://arxiv.org/pdf/2405.01413 MiniGPT-3D在多个任务上取得了SoTA,被ACM MM2024接收,只拥有47.8M的可训练参数,在一张RTX