本文目录一览:
- 1、c语言莫尔斯码 将字符串HappyNewYear2014译成Morse码(码间一空格,码表见下表)并在解密后输出以验证
- 2、C语言关于字母转摩尔斯代码程序
- 3、用C++编程把英文单词转换为莫尔斯码
- 4、C摩尔斯码程序设计
- 5、c语言 摩斯密码问题 在线等,急
- 6、莫尔斯码的c语言怎么编写啊
c语言莫尔斯码 将字符串HappyNewYear2014译成Morse码(码间一空格,码表见下表)并在解密后输出以验证
#include stdio.h
#include string.h
#include ctype.h
char const * const morse_table[]=
{"._",//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
};
void encodeMorse(char *d,const char *s);
void decodeMorse(char *d,const char *s);
int main(void)
{
char str1[1000],str2[1000];
encodeMorse(str1,"HappyNewYear2014");
puts(str1);
decodeMorse(str2,str1);
puts(str2);
return 0;
}
void encodeMorse(char *d,const char *s)
{
*d='\0';
while(*s)
{
if(isalpha(*s))
{
strcat(d,morse_table[toupper(*s)-'A']);
}
if(isdigit(*s))
{
strcat(d,morse_table[*s-'0'+('Z'-'A')+1]);
}
strcat(d," ");
s++;
}
}
void decodeMorse(char *d,const char *s)
{
char temp[50];
int i;
while(*s)
{
sscanf(s,"%s",temp);
s+=strlen(temp);
while(*s==' ')s++;
for(i=0;i36;++i)
{
if(!strcmp(temp,morse_table[i]))
{
*d++=(i26?('A'+i):('0'+i-26));
}
}
}
*d='\0';
}
morse的编码表由于是手打的缘故,有可能写错的,错了也没关系,自己改下编码,不用修改程序本身
C语言关于字母转摩尔斯代码程序
char a[26][8]定义了26个字母对应的摩尔斯编码字符串,也就是a[0]代表字符‘a'的摩尔斯编码字符串,a[1]代表字符'b'的摩尔斯编码字符串,……等等。因为'a‘字符的ASCII值就是97,所以str[i]-97则计算出了字符str[i]所处的位置索引(0~25之间),而a[str[i]-97]即代表字符str[i]的摩尔斯编码字符串了。其实这个程序还不够严密,如果字母是大写的,会出现错误的!因为大写字符'A'的ASCII值是65。
用C++编程把英文单词转换为莫尔斯码
/*英文转摩尔斯码*/ #includestdio.h #includeiostream using namespace std; int main() { char a[26][6]={{'.','-'},{'-','.','.','.'},{'-','.','-','.'},{'-','.','.'}, {'.'},{'.','.','-','.'},{'-','-','.'},{'.','.','.','.'}, {'.','.'},{'.','-','-','-'},{'-','.','-'},{'.','-','.','.'}, {'-','-'},{'-','.'},{'-','-','-'},{'.','-','-','.'}, {'-','-','.','-'},{'.','-','.'},{'.','.','.'},{'-'}, {'.','.','-'},{'.','.','.','-'},{'.','-','-'},{'-','.','.','-'}, {'-','.','-','-'},{'-','-','.','.'}}; int i,m; char str[100]; char c; gets(str); printf("%s",str); for (i=0;(c=str[i])!='\0';i++) if(c==' ') printf(" "); else printf("%s",a[m=str[i]-97]),printf(" "); printf("\n"); getchar(); return 0; } /*摩尔斯码转英文*/ #includestdio.h #include string.h #include iostream using namespace std; int main() { char a[26][6]={{'.','-'},{'-','.','.','.'},{'-','.','-','.'},{'-','.','.'}, {'.'},{'.','.','-','.'},{'-','-','.'},{'.','.','.','.'}, {'.','.'},{'.','-','-','-'},{'-','.','-'},{'.','-','.','.'}, {'-','-'},{'-','.'},{'-','-','-'},{'.','-','-','.'}, {'-','-','.','-'},{'.','-','.'},{'.','.','.'},{'-'}, {'.','.','-'},{'.','.','.','-'},{'.','-','-'},{'-','.','.','-'}, {'-','.','-','-'},{'-','-','.','.'}}; int b,i,m; char str[100],k; char c,*p,t[6]; gets(str); printf("%s",str); p=str; while(*p!='\0') { i=0; b=0; k=0; while(*p!=' '*p!='\0') { t[i++]=*p; p++; } t[i]='\0'; while(strcmp(a[k++],t)!=0); c=k-1+97; putchar(c); while(*p==' ') { b++;p++;} if(b==3) printf(" "); } getchar(); return 0; }
C摩尔斯码程序设计
输入摩斯电码 翻译出 英文,只能识别英文字母, 字母间隔一个空格,单词间隔三个空格 输入摩斯电码
#include "stdio.h"
#include "conio.h"
#include "string.h"
int morseindex(const char *a);
int main(void)
{
int i, j, space;
char *p;
char buffer[1024];
char a[6];
gets(buffer);
p = buffer;
i = 0;
space = 0;
while(1)
{
if (*p == 32 || *p == '\0')
{
a[i] = '\0';
if (strlen(a) != 0)
{
j = morseindex(a);
if (j = 0)
printf("%c", 'a' + j);
}
i = 0;
space++;
if (space == 3)
printf(" ");
}
else
{
a[i++] = *p;
space = 0;
}
if (*p == '\0')
break;
p++;
}
}
int morseindex(const char *a)
{
int i;
static char morsetable[26][5] = {{".-"}, {"-..."}, {"-.-."}, {"-.."}, {"."}, {"..-."}, {"--."}, {"...."}, {".."}, {".---"}, {"-.-"}, {".-.."}, {"--"}, {"-."}, {"---"}, {".--."}, {"--.-"}, {".-."}, {"..."}, {"-"}, {"..-"}, {"...-"}, {".--"}, {"-..-"}, {"-.--"}, {"--.."}};
for (i = 0; i 26; i++)
{
if (strcmp(a, morsetable[i]) == 0)
{
return i;
}
}
return -1;
}
c语言 摩斯密码问题 在线等,急
az[26[5] 是编码表,你要校对一下,我只是快速拍入,给你示意。
程序第一部分是 英文到莫码输出。英文只考虑了小写。
(大写转小写你可以用 -'A'+'a', 下标为
printf("%s",az[s1[0]-'A']);
for (i=1;iL;i++) printf("|%s",az[s1[i]-'A']);
)
程序第2部分是莫码到英文小写。
#include stdio.h
#include stdlib.h
char az[26][5]={
"*-","-***","-*-*","-**",
"*","**-*","--*","****",
"**","*---","-*-*","*-**",
"--","-*","---","*--*",
"--*-","*-*","***","-",
"**-","***-","*--","-**-",
"-*--","--**"};
char toA(char *s){
int i;
for (i=0;i26;i++) if (strcmp(az[i][0],s)==0) return i+'a';
printf("wrong input code: %s\n",s);
}
int main(){
char s1[80]="moses";
char s2[80];
int i,j,L;
L=strlen(s1);
printf("%s",az[s1[0]-'a']);
for (i=1;iL;i++) printf("|%s",az[s1[i]-'a']);
printf("\n");
// Part2:
printf("please input ****|*|*-**|*-**|---\n");
scanf("%[^|]",s1);
printf("%c",toA(s1));
while (scanf("|%[^|,'\n']",s1)==1) {
printf("%c",toA(s1));
}
}
莫尔斯码的c语言怎么编写啊
定义两个数组,一个是char
*型,放A-Z,0-9的莫尔斯码,一个是char型,放'A'-'Z','0'-'9'.
原文转电码时,对每个字符查找其在第二个数组中的位置i,那么第一个数组中下标为i的就是对应的莫尔斯码.
电码转原文时,在第一个数组中用strcmp查找莫尔斯码,然后第二个数组中对应字符.
比如,放莫尔斯码的数组是char
*morse[36],放原文的是char
str[36],那么字符c转莫尔斯码就可以这样:
char
*Char2Morse(char
c)
{
for(int
i=0;i36;i++)
if(str[i]==c)
return
morse[i];
return
NULL;
}
莫尔斯码转原文就是
char
Morse2Char(char
*m)
{
for(int
i=0;i36;i++)
if(strcmp(morse[i],m)==0)
return
str[i];
return
0;
}