1

この例で再帰がどのように機能するか混乱しています。「ABC\n」を入力すると、CBAが出力されます。誰かがプロセスをステップスルーできれば、私はそれを大いに感謝します。

  1. main()では、ReverseLine()が呼び出されます
  2. ローカル自動myInputは'ABC\n'を取り込みます

  3. 次に、myInputの'\ n'とEOFをチェックします。ここで、混乱し始めます。

A!='\ n'およびA!= EOFと書かれているので、ReverseLine()が再度呼び出されますが、それではどうでしょうか。

再帰はどのように機能しますか、プロセスを理解したいだけです

ありがとう

    using namespace std;

    void ReverseLine(){
       int myInput;

       myInput = cin.get();

       if (myInput != '\n' && myInput != EOF)
        ReverseLine();

       if (myInput != EOF)
        cout.put(myInput);
    }

    int main(){

       ReverseLine();
       return 0;

    }
4

4 に答える 4

4

おそらくそれを拡張することはあなたが理解するのを助けるでしょう?

void ReverseLine() {
   int myInput = 'a'

   if (myInput != '\n' && myInput != EOF) {
       int myInput = 'b'

       if (myInput != '\n' && myInput != EOF) {
           int myInput = 'c'

           if (myInput != '\n' && myInput != EOF) {
               int myInput = '\n'
               if (myInput != '\n' && myInput != EOF)
                   ReverseLine(); // doesn't get called
               cout.put(myInput);
           }
           if (myInput != EOF)
               cout.put(myInput);
       }

       if (myInput != EOF)
        cout.put(myInput);
   }

   if (myInput != EOF)
    cout.put(myInput);
}
于 2012-10-06T18:51:28.027 に答える
4

ReverseLineを呼び出すと、文字が読み取られます。文字が改行またはEOFでない場合は、改行に遭遇するまで次の文字を読み取るために再度呼び出し(再帰)、その時点で読み取ったばかりの文字を出力し、ReverseLineに戻って読み取った文字を出力します。このように、ReverseLineの最初の呼び出しに戻り、読み取られた最初の文字を出力してから終了します。

于 2012-10-06T18:52:07.320 に答える
2

Basileが言ったように、再帰は理解するのが難しい場合があります。この例は、ローカル変数の概念に依存しています。myInput再帰層の最後に移動し、最も深い再帰呼び出しから最初の呼び出しまでローカル変数の出力を開始します。

「123」と入力したとします。各インデントは、の新しいローカルスコープですReverseInput()

myInput = 1
ReverseLine()
  myInput = 2
  ReverseLine()
    myInput = 3
    ReverseLine()
      myInput = \n
    prints 3
  prints 2
prints 1

これは、逆の方法で物事を行うための一般的なトリックです。

于 2012-10-06T18:55:35.810 に答える
1

とても簡単です。ReverseLine関数は、戻るに出力を出力します。* ABC \ nと入力した場合の、イベントのシーケンスは次のとおりです。

1. First call to ReverseLine.
1.a **A** is typed.
1.b myInput is not equal to **\n or EOF**, so
   2. Second call to ReverseLine
   2.a **B** is typed.
   2.b myInput is not equal to **\n** or **EOF**, so
      3. Third call to ReverseLine
      3.a **C** is typed.
      3.b myInput is not equal to **\n** or **EOF**, so
         4. Fourth call to ReverseLine
         4.a **\n** is typed.
         4.b myInput is equal to **\n**, so
         4.c ReverseLine is **not** called
         4.d myInput is **not** equal to **EOF**, so
         4.e myInput (**\n**) is printed
         4.f Fourth call to ReverseLine returns
      3.c myInput is **not** equal to **EOF**, so
      3.d myInput (**C**) is printed
      3.e Third call to ReverseLine returns
   2.c myInput is **not** equal to **EOF**, so
   2.d myInput (**B**) is printed
   2.e Second call to ReverseLine returns
1.c myInput is **not** equal to **EOF**, so
1.d myInput (**A**) is printed
1.e First call to ReverseLine returns

そして、プログラムは終了します。

于 2012-10-06T19:00:35.727 に答える