摩尔斯电码(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

相关文章

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当