1

英語形式で一度に各桁の整数を再帰的に出力する

文字列/文字配列に変換せずに数値の桁を取得する方法は?

これは、C++ ではなく、C でコーディングするためのものです。私は初級レベルのコースにいて、中間試験を終えたばかりなので、C に関する私の知識は非常に限られています。クラスで説明していないキーワードや演算子を含めることはできないため、これをできるだけ単純に保つようにしてください。私のコードではなく、助けが必要なのは私のロジックだけだと思う​​ので、これは必要ではないと思います。

上記の 2 つの例を参照してクラスのコードを記述した後、パズルの最後の小さなピースを完成させる方法に困惑しています。ここSOでいくつかの質問と回答が見つかりましたが、それらは関連しているように見えましたが、コードを使用して私が知らない問題を解決しました。うまくいけば、誰かがここで私の論理を手伝ってくれます。

私の課題の目標は次のとおりです。

ユーザー定義の整数を取得し、数字を英語で表示します。例えば:

整数を入力してください: 123 入力しました
: One Two Three

次に、数字の合計を合計する必要があります (数字が 10 未満の場合は、英語で表示されます)。この場合:

個々の桁の合計は次のとおりです。6

最後に、小数点以下 2 桁を使用して数字を平均化する必要があります。この場合:

平均: 2.00

私はこれをすべて完了しました。例外: 私の最初のステップでは、数字を逆順にリストします! 10 の位、100 の位、1000 の位などを読み取ります。例:

整数を入力してください: 123 入力しました
: スリー ツー ワン

割り当てのこの部分の私の条件は、switch ステートメントを 1 つだけ使用することができ、switch ステートメントを使用する必要があることです (つまり、ループが必要です (私は do を使用しました))。配列も使用しない場合があります。しかし、最後に、そして最も重要なこととして、入力番号を逆にすることはできません (これは、この割り当ての最初のバージョンに対する解決策でした)。もしそれができたら、私はここにいないでしょう。

関連するコードの抜粋を次に示します。

#include <stdio.h>

int main(void)

{

    int userinput, digit

    printf("Please input a number:");
    scanf("%d", &userinput);

    printf("You have entered: ");

    if (userinput < 0)
    {
            printf("Negative ");
            userinput = -userinput;
    }

    do
    {
            digit = userinput%10;
            switch (digit)
            {
                    case 0:
                    {
                            printf("Zero ");
                            break;
                    }
                    case 1:
                    {
                            printf("One ");
                            break;
                    }
                    case 2:
                    {
                            printf("Two ");
                            break;
                    }
                    case 3:
                    {
                            printf("Three ");
                            break;
                    }
                    case 4:
                    {
                            printf("Four ");
                            break;
                    }
                    case 5:
                    {
                            printf("Five ");
                            break;
                    }
                    case 6:
                    {
                            printf("Six ");
                            break;
                    }
                    case 7:
                    {
                            printf("Seven ");
                            break;
                    }
                    case 8:
                    {
                            printf("Eight ");
                            break;
                    }
                    case 9:
                    {
                            printf("Nine ");
                            break;
                    }
                    default:
                    {
                            break;
                    }

            }

            userinput = userinput/10;

    } while (userinput > 0);

    printf("\n");
4

3 に答える 3

1

配列を使用できない場合は、再帰を使用します。

void print_textual(int n)
{
    if (n > 9) {
        print_textual(n / 10);
    }

    switch (n % 10) {
    case 0: printf("zero "); break;
    case 1: printf("one "); break;
    case 2: printf("two "); break;
    case 3: printf("three "); break;
    case 4: printf("four "); break;
    case 5: printf("five "); break;
    case 6: printf("six "); break;
    case 7: printf("seven "); break;
    case 8: printf("eight "); break;
    case 9: printf("nine "); break;
    }
}

ちなみに、少なくとも数字の名前に配列を使用できれば、これは本当にはるかに優れています。

void print_textual(int n)
{
    if (n > 9) {
        print_textual(n / 10);
    }

    static const char *names[] = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine"
    };

    printf("%s ", names[n % 10]);
}
于 2013-11-09T17:56:06.147 に答える
0

再帰の使用を避けるために、 にスケーリングされた 10 のべき乗乗数を形成しnます。次に、この乗数を使用して、最上位から最下位の順に桁を決定します。

同じものを使用してdigits_in_english()、桁の合計を出力します。

void digits_in_english(const char *prompt, int n, int *Sum, int *Count) {
  fputs(prompt, stdout);
  *Sum = 0;
  *Count = 0;
  if (n < 0) {
    printf(" Negative");
    n = -n;
  }
  int m = n;
  int pow10 = 1;
  while (m > 9) {
    m /= 10;
    pow10 *= 10;
  }
  do {
    static const char *Edigit[] = { "Zero", "One", "Two", "Three", "Four",
       "Five", "Six", "Seven", "Eight", "Nine" };
    int digit = n / pow10;
    *Sum += digit;
    (*Count)++;

    // OP knows how to put a switch statement here instead of printf()
    printf(" %s", Edigit[digit]);

    n -= digit * pow10;
    pow10 /= 10;
  } while (pow10 > 0);
  fputs("\n", stdout);
}

void Etest(int n) {
  int Count, Sum;

  // Change this to a printf() and scanf()
  printf("Please enter an integer: %d\n", n);

  digits_in_english("You have entered:", n, &Sum, &Count);
  double Average = (double) Sum / Count;

  // Do no care about the resultant Sum, Count
  digits_in_english("The sum of the individual digits is:", Sum, &Sum, &Count);

  printf("The average is: %.2f\n\n", Average);
}

サンプル

Please enter an integer: -2147483647
You have entered: Negative Two One Four Seven Four Eight Three Six Four Seven
The sum of the individual digits is: Four Six
The average is: 4.60

INT_MIN では機能しません。移植可能な方法でこれを行うには少し注意が必要です。

OPは配列の使用を許可されていないようです。文字列が含まれていないことを願っています。

于 2013-11-09T21:38:02.023 に答える
-1

そのため、多くのつまずきを経て、使用を許可された知識のみを使用して、意図したとおりにコードを機能させることができました。完成品は次のとおりです (大きなエラーが表示されない限り)。

    //initializing variables, of course
    int userinput, number1, number2, numbersum;
    int div = 1;
    float number3;

    //displaying instructions to the user
    printf("Please input a number: ");
    scanf("%d", &userinput);

    printf("You have entered: ");

    if (userinput < 0)
    {
            printf("Negative ");
            userinput = -userinput;
    }

    //the variables number1-3 are for the data analysis at the end
    //I am preserving the original input in them so I can mutilate it in the following step
    number1 = userinput;
    number2 = userinput;

    while (div <= userinput)
    {
            div = div*10;
    }

    do
    {
            if (userinput != 0)
            {
                    div = div/10;

                    switch (userinput/div)
                    {
                            case 0:
                            {
                                    printf("Zero ");
                                    break;
                            }
                            case 1:
                            {
                                    printf("One ");
                                    break;
                            }
                            case 2:
                            {
                                    printf("Two ");
                                    break;
                            }
                            case 3:
                            {
                                    printf("Three ");
                                    break;
                            }
                            case 4:
                            {
                                    printf("Four ");
                                    break;
                            }
                            case 5:
                            {
                                    printf("Five ");
                                    break;
                            }
                            case 6:
                            {
                                    printf("Six ");
                                    break;
                            }
                            case 7:
                            {
                                    printf("Seven ");
                                    break;
                            }
                            case 8:
                            {
                                    printf("Eight ");
                                    break;
                            }
                            case 9:
                            {
                                    printf("Nine ");
                                    break;
                            }
                            default:
                            {
                                    break;
                            }

                    }

                    userinput = userinput%div;

            }

            else
            {
                    printf("Zero");
            }

    } while (userinput > 0);

    //line break to make it look pretty
    printf("\n");

    //boring math to determine the sum of the digits
    //assuming all are positive due to know contrary instructions
    //set equal to zero since this variable refers to itself in the following function
    numbersum = 0;

    while (number1 > 0)
    {
            numbersum = numbersum + (number1 % 10);
            number1 = number1 / 10;
    }

    //nested switch in if statement to print english if digits less than or equal to 10
    if (numbersum <= 10)
    {
            switch (numbersum)
            {
                    case 0:
                    {
                            printf("The sum of the individual integers is: Zero");
                            break;
                    }
                    case 1:
                    {
                            printf("The sum of the individual integers is: One");
                            break;
                    }
                    case 2:
                    {
                            printf("The sum of the individual integers is: Two");
                            break;
                    }
                    case 3:
                    {
                            printf("The sum of the individual integers is: Three");
                            break;
                    }
                    case 4:
                    {
                            printf("The sum of the individual integers is: Four");
                                    break;
                    }
                    case 5:
                            {
                            printf("The sum of the individual integers is: Five");
                            break;
                    }
                    case 6:
                    {
                            printf("The sum of the individual integers is: Six");
                            break;
                    }
                    case 7:
                    {
                            printf("The sum of the individual integers is: Seven");
                            break;
                    }
                    case 8:
                    {
                            printf("The sum of the individual integers is: Eight");
                            break;
                    }
                    case 9:
                    {
                            printf("The sum of the individual integers is: Nine");
                            break;
                    }
                    case 10:
                    {
                            printf("The sum of the individual integers is: Ten");
                    }
                    default:
                    {
                            break;
                    }

            }

            printf("\n");
    }

    //else if greater than 10, just print the decimal number
    else
    {
            printf("The sum of the individual digits in the integer is: %d\n", numbersum);
    }

    if (numbersum == 0)
    {
            printf("The average is of zero is not a number.\n");
    }

    else
    {

            //initializing a variable here because it's totally irrelevant to the above functions
            //and this feels cleaner because of it. I'm not sure if this is bad etiquette
            int i;

            //picks out the number of digits in the input and effectively sets i to that number
            for (i = 0; number2 > 0; i++)
            {
                    number2 = number2/10;
            }

            //this is necessary for turning number3 into an actual floating point, not an int stored as float
            number3 = numbersum;

            //math to determine average (sum of digits divided by number of digits)
            number3 = number3 / i;

            printf("The average is: %.2f\n", number3);

    }

    return 0;

それは大きくて、おそらくずさんなところがありますが(私が新人なので)、うまく機能し、それが今の私にとって本当に重要なことのすべてです.

助けてくれてありがとう、みんな。

于 2013-11-14T10:46:21.827 に答える