テキストを取り込んで LED を介してモールス符号に変換することになっている宿題用に、C でプログラムを作成しました。知っているように、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;
}