1

テキストを取り込んで 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;

}
4

3 に答える 3

2

大量に使用していて、if's1 つの一致の後に先に進む必要がif-else-if ladder ある場合は、「b」が見つかった場合に他のすべての条件をチェックしないように使用します。

しかし、ここでの最善の解決策はswitch case.

でこのようなことを試してくださいfor-loop

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;
}
于 2015-09-12T04:08:15.120 に答える
1

これを試して:

    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");
       } 
    }
于 2015-09-12T08:51:33.763 に答える