C 有什么更好的方法可以做到这一点?莫尔斯电码程序
我为我的家庭作业写了一个用C语言编写的程序,它应该接收文本并通过LED将其翻译成摩尔斯电码。例如,我已经用“这是一个_______________________?我知道肯定有,而不是使用所有那些“如果”语句。但我是C语言的初学者(这个代码的唯一缺点是它很长,不能输入空格)。它目前的工作方式是,它接收字符串并将其分解为单个字母,然后在“for”循环中检查这些单个字母对应的莫尔斯电码。让我知道你的想法C 有什么更好的方法可以做到这一点?莫尔斯电码程序,c,morse-code,C,Morse Code,我为我的家庭作业写了一个用C语言编写的程序,它应该接收文本并通过LED将其翻译成摩尔斯电码。例如,我已经用“这是一个_______________________?我知道肯定有,而不是使用所有那些“如果”语句。但我是C语言的初学者(这个代码的唯一缺点是它很长,不能输入空格)。它目前的工作方式是,它接收字符串并将其分解为单个字母,然后在“for”循环中检查这些单个字母对应的莫尔斯电码。让我知道你的想法 // MorseCode_Attempt1.cpp : Defines the entry po
// MorseCode_Attempt1.cpp : Defines the entry point for the console application.
//
include<stdio.h>
include<conio.h>
include<string.h>
void main() {
char str[500];
printf("Enter a string you want in Morse Code with underscores as spaces: ");
scanf("%s", &str);
int i;
int stringLength = strlen(str);
for (i = 0; i < stringLength; i++) {
printf("\n[%c]\n", str[i]);
if (str[i] == 'a') {
printf("\nIt is an a.\n");
}
if (str[i] == 'b') {
printf("\nIt is an b.\n");
}
if (str[i] == 'c') {
printf("\nIt is an e.\n");
}
if (str[i] == 'd') {
printf("\nIt is an d.\n");
}
if (str[i] == 'e') {
printf("\nIt is an e.\n");
}
if (str[i] == 'f') {
printf("\nIt is an f.\n");
}
if (str[i] == 'g') {
printf("\nIt is an g.\n");
}
if (str[i] == 'h') {
printf("\nIt is an h.\n");
}
if (str[i] == 'i') {
printf("\nIt is an i.\n");
}
if (str[i] == 'j') {
printf("\nIt is an j.\n");
}
if (str[i] == 'k') {
printf("\nIt is an k.\n");
}
if (str[i] == 'l') {
printf("\nIt is an l.\n");
}
if (str[i] == 'm') {
printf("\nIt is an m.\n");
}
if (str[i] == 'n') {
printf("\nIt is an n.\n");
}
if (str[i] == 'o') {
printf("\nIt is an o.\n");
}
if (str[i] == 'p') {
printf("\nIt is an p.\n");
}
if (str[i] == 'q') {
printf("\nIt is an q.\n");
}
if (str[i] == 'r') {
printf("\nIt is an r.\n");
}
if (str[i] == 's') {
printf("\nIt is an s.\n");
}
if (str[i] == 't') {
printf("\nIt is an t.\n");
}
if (str[i] == 'u') {
printf("\nIt is an u.\n");
}
if (str[i] == 'v') {
printf("\nIt is an v.\n");
}
if (str[i] == 'w') {
printf("\nIt is an w.\n");
}
if (str[i] == 'x') {
printf("\nIt is an x.\n");
}
if (str[i] == 'y') {
printf("\nIt is an y.\n");
}
if (str[i] == 'z') {
printf("\nIt is an z.\n");
}
if (str[i] == '_') {
printf("\nIt is a SPACE.\n");
}
}
return 0;
}
//MorseCode_Attempt1.cpp:定义控制台应用程序的入口点。
//
包括
包括
包括
void main(){
char-str[500];
printf(“在莫尔斯电码中输入所需字符串,下划线为空格:”;
scanf(“%s”和&str);
int i;
int stringLength=strlen(str);
对于(i=0;i
当您使用大量的if's
时,在一次匹配后,您必须继续使用,这样,如果找到“b”,则不会检查所有其他条件
但最好的解决方案是一个新的解决方案
在for循环中尝试类似的操作
switch (str[i])
{
case 'a':
printf("\nIt is an a.\n");
break;
case 'b':
printf("\nIt is a b.\n");
break;
/* etc. etc. etc.*/
default:
//default will work when the input to the switch->here str[i] does not match any case.
printf("\nNot a character or space!");
break;
}
试试这个:
for (...)
{
char c = str[i];
unsigned Num;
/* If c is a letter map it to 0..25 */
Num = (unsigned)(toupper(c)-'A');
if (Num<26) //This matches only with letters, as Num is unsigned.
{
//Each letter is represented by max four symbols
static const char MorseCodes[26][5]=
{
"._", //Morsecode for 'a'
"_...", //Morsecode for 'b'
...
"__..", //Morsecode for 'z'
};
printf("You have entered the letter %c. The morse code is %s\n", Num+'A', MorseCodes[Num]);
}
else
if (c=='_')
{
printf ("Space\n");
}
else
{
printf ("What You Are Doing?\n");
}
}
(…)的
{
char c=str[i];
无符号数;
/*如果c是字母,则将其映射到0..25*/
Num=(无符号)(toupper(c)-'A');
如果(Num给出了您的问题,那么下面的代码将引导您走向正确的方向
请注意,函数:out()、函数:delay()、#define ON和#define OFF取决于您的环境
include<stdio.h>
//include<conio.h> -- dont include header files that are not used
include<string.h>
// adjust DELAY_UNIT as necessary
#define DELAY_UNIT (1000)
#define DOT_TIME (1)
#define DASH_TIME (3)
#define INTER_WORD_TIME (7)
#define INTER_CHAR_TIME (3)
#define DOT ('.')
#define DASH ('-')
// OP needs to define function 'out()', LED, ON, OFF and DELAY_UNIT
// to suit the environment
// prototypes
char *findIndex( struct morseStruct *morse, char ch);
void MorseToLed( char *sequencePtr);
void delay( long unsigned int );
/*
* If the duration of a dot is taken to be one unit
* then that of a dash is three units.
*
* The space between the components of one character is one unit,
*
* between characters is three units and between words seven units.
*
* To indicate that a mistake has been made and for the receiver to delete the last word,
* send ........ (eight dots).
*/
// see <http://morsecode.scphillips.com/morse2.html> for source list
// of morse code char formats
struct morseStruct
{
char searchChar;
char *morseSequence;
};
static const struct morseStruct *alphanumeric[] =
{
{'A', ",-" };
{'B', "-..." };
{'C', "-.-." };
... // add other alphameric definitions here
{0, NULL};
};
static const struct morseStruct *punctuation[] =
{
{'.', ".-.-.-" }; // period
{',', "--..--" }; // comma
{':', "---..." }; // colon
{'?', "..--.." }; // question mark
{'\'', ".----."}; // apostrophe (note: must escape certain characters)
{'-', "-....-" }; // hyphen
{'/', "-..-." }; // right slash
{'\"', ".-..-."}; // quotation mark
{'@', ".--.-." }; // at sign
{'=', "-...-" }; // equals sign
{0, NULL};
};
static const struct morseStruct *prosigns[] =
{
... // add prosigns here
{0, NULL};
};
int main( void )
{
char str[500];
printf("Enter a string you want in Morse Code with underscores as spaces: ");
if( NULL == fgets(str, sizeof( str ), stdin );
{ // then fgets failed
perror( "fgets for user input string failed");
exit( EXIT_FAILURE );
}
// implied else, fgets successful
//eliminate trailing newline, if any
if( char *pNewline = strstr( str, "\n")
{
pNewline = '\n';
}
size_t stringLength = strlen(str);
int i;
for (i = 0; i < stringLength; i++)
{
printf("\n[%c]\n", str[i]);
// handle special cases
if '_' == str[i] )
{ // then between words
//out(LED, OFF); // should already be OFF
delay( INTER_WORD_TIME*DELAY_UNIT );
continue;
}
int sequencePtr;
if( NULL == (sequencePtr = findIndex( alphaNumeric, str[i] ) ) )
{// then not alphanumeric value
if( NULL == (sequencePtr = findIndex( punctuation, str[i] ) ) )
{ // then not punctuation value
if( NULL == (sequencePtr = findIndex( prosigns, str[i] ) ) )
{ // then not prosign value
printf( "char: %0X is not recognized\n", str[i]);
continue;
}
}
}
MorseToLed (sequencePtr);
} // end for
return 0;
} // end function: main
char *findIndex( struct morseStruct *morse, char ch)
{
char *pSequence = NULL;
int i = 0;
while( NULL != morse[i].morseSequence )
{
if( ch == morse[i].searchChar )
{ // then found desire morse code sequence
pSequence = morse[i].morseSequence;
break;
}
i++;
}
return( pSequence );
} // end function: findIndex
void MorseToLed( char *sequencePtr)
{
size_t i = strlen( sequencePtr );
for( size_t j = 0; j<i; j++)
{
if( DOT == sequencePtr[j] )
{
out(LED, ON);
delay( DOT_TIME * DELAY_UNIT );
out(LED, OFF);
}
else if( DASH == sequencePtr[j] )
{
out(LED, ON);
delay( DASH_TIME * DELAY_UNIT );
out(LED, OFF);
}
delay( DELAY_UNIT ); // delay between components of one char
} // end for
delay( INTER_CHAR_TIME * DELAY_UNIT ); // delay between characters
} // end function: MorseToLed
void delay( long unsigned int delayUnits )
{
?????
} // end function: delay
包括
//包括--不包括未使用的头文件
包括
//根据需要调整延迟单元
#定义延迟单位(1000)
#定义点时间(1)
#定义破折号时间(3)
#定义字间时间(7)
#定义字符间时间(3)
#定义点('.'))
#定义破折号(“-”)
//OP需要定义函数“out()”、LED、ON、OFF和延迟单元
//适应环境
//原型
char*findIndex(struct morseStruct*morse,char ch);
无效MorseToLed(字符*序列PTR);
无效延迟(长无符号整数);
/*
*如果点的持续时间为一个单位
*那么短跑的速度是三个单位。
*
*一个字符的组成部分之间的间距为一个单位,
*
*字符之间是三个单位,单词之间是七个单位。
*
*为了表明已经犯了错误,并让接收者删除最后一个单词,
*发送……(八点)。
*/
//有关源列表,请参阅
//莫尔斯电码字符格式的研究
结构morseStruct
{
字符搜索字符;
字符*莫尔斯序列;
};
静态const struct morseStruct*字母数字[]=
{
{'A',“,-”};
{'B',“-…”};
{'C',“-.-.-”};
…//在此处添加其他字母数字定义
{0,NULL};
};
静态const struct morseStruct*标点符号[]=
{
{.',“-.-.-”};//句点
{',',“--…”--“};//逗号
{':',“--…”;//冒号
{'?',“.--…”;//问号
{'\'',.---.'.};//撇号(注意:必须转义某些字符)
{'-',“-..-”};//连字符
{'/',“-…-。”};//右斜杠
{'\',“-…-”};//引号
{'@',“--.-”};//符号处
{'=',“-…-”};//等号
{0,NULL};
};
静态const struct morseStruct*prosigns[]=
{
…//在此处添加prosign
{0,NULL};
};
内部主(空)
{
char-str[500];
printf(“在莫尔斯电码中输入所需字符串,下划线为空格:”;
if(NULL==fgets(str,sizeof(str),stdin);
{//fgets失败
perror(“用户输入字符串的FGET失败”);
退出(退出失败);
}
//否则,