我为我的作业用 C 语言编写了一个程序,该程序应该接收文本并通过 LED 将其转换为摩尔斯电码。要知道,我已将 LED 闪烁的预定长度替换为:“它是 __。” 这就像现在一样有效,我会得到充分的信任,但我的问题是是否有更好的方法来做到这一点?我知道必须有,而不是使用所有那些'if'语句。但我是 C 的初学者。(这个代码的唯一缺点是它很长,你不能输入空格。)它目前的工作方式是它接受一个字符串并将其分解为单个字母,然后在“for”中循环检查这些单独的字母是否有相应的摩尔斯电码。让我知道你的想法。
// 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;
}