摩尔斯电码
#include <stdio.h>#include <string.h>
//首先将摩斯电码的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;
}
相关阅读
C/C++ 学习笔记:istringstream、ostringstream、string
0、C++的输入输出分为三种:(1)基于控制台的I/O(2)基于文件的I/O(3)基于字符串的I/O 1、头文件[cpp] view plaincopyprint? #incl
VC++6.0的安装并不简单,所以我总结这篇博客让大家少费一些周折。网上找了好多下载地址都安装失败,最后安装成功的压缩包我放到网盘
fill()函数参数:fill(first,last,val); // first 为容器的首迭代器,last为容器的末迭代器,val为将要替换的值。 举例: int a[200]; f
1.定义namespace 名称空间名{变量类型 变量;函数返回值类型 函数原型;} 在名称空间作用域内变量、函数原型被称为实体。实体在名
win32 API &winsock & c++架构实现仿QQ聊天室
由于时间紧迫,我决定学习opengl的同时学习winsock,看了两个星期的windows socket编程,发现动手比看书效果好啊,于是,我决定写一个QQ聊