recursion - Reversing line from an input using a recursive function -


In my textbook, this is an example which is similar to reversing one line from an input file:

  zero reverse (ifstream and inFile, int level) {int myInput = inFile.get (); If (myInput! = '\ N' & amp; amp; amp; My input! = EOF) / / It does not understand, line 4 reverse (infile, level); If (myInput! = EOF) cout.put (myInput); }  

The line I did not tell is the comment I made. Because of these input files:

  ABC \ ndef \ 0  

When \ n is equal to my input, it does not line up 4 The conditional statement will be false (my input! = '\ N') false before being false, and the second part (MyInput! = EOF) will come true, make the whole line wrong, and then do not call the reverse function? Thank you.

To understand the rehearsal of a very basic level, find out the execution of execution and execute the sequence of calls You will be helped to understand how it works. I placed each call on Rickers () and included line numbers that are executed. 3: myInput = 'A' 5: Reverse () 3: myInput = 'B' 5: Reverse () 3: My input = 'C' 5: Rackers () 3: myInput = '\ N' & lt; & Lt; & Lt; & Lt; The base status, recursion here prevents 7: cout.put ('\ n') 7: cout.put ('C') 7: cout.put ('B') 7: cout.put ('A')

then this output will be '\ nCBA'.

For logic in the base position, remember:

  (not p) and (not q) -> No (pi or q) (myInput! = '\ N') & amp; Amp; (MyInput! = EOF) - & gt; (! (MyInput == '\ n')) & amp; Amp; (! (MyInput == EOF)) - & gt; ! (MyInput == '\ n') || (myInput == EOF))  

On line 4 if statement states the new lines and the end files Therefore, it prevents recursive when it encounters any situation. This is the reason why the file causes recycling to read only one row.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -