本文目錄一覽:
- 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;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/242220.html