2017-06-04 10 views
-2

テキスト(Javaコードを含む)をファイルから読み込み、再帰を使用して正しくインデントする必要がある場合の問題をどのように解決するかを知りたいと思います。元の出力には のタブがありません。 enter image description here再帰を使用した識別

これはコードですが、私はテキストファイルからの読み込みにそれを適応させる必要があります:

void indent(int m, int n) 
{ 
    System.out.println(m); // Forward Printing 

    if (m < n) 
    { 
    indent(m + 1, n); 
    System.out.println(m); // Backward Printing 
    } 
} 
+0

「astyle」、「indent」または「ide」を使用します。ほとんどの(すべて)、そのことができる。 –

+0

@Elliottは再帰する必要があります – Mesutluka1019

+0

何を試しましたか([mcve]を見せてください)?どこにこだわっていますか、それで何が問題なのですか? –

答えて

0

さて、アルゴリズムは

function indent(theText, indentCharCount) { 
    for each character of the text... 
     if it is an end-of-line character... 
     concatenate spaces to return string using indentCharCount 
     else if it is a '{' character... 
     scan ahead through characters to find matching '}' character 
     recursively call indent() function passing... 
      characters between { and } for 'theText' param 
      indentCharCount+2 for 'indentCharCount' param 
     concatenate return value of indent() to return string 
     set loop index so that the next character will be the matched '}' 
     else (it's some other character) 
     concatenate character to return string 
    return the concatenated string 
} 
...のようにすることができます目標です

私はJavaですべてのコードを書きたいとは思わなかった。あなたが宿題をしているのであれば、私はむしろ何かを学ぶだろう!しかし、それは私があなたの質問に合っていると考える基本的な再帰アルゴリズムです。

+1

'theText'が' Reader'のインスタンスである場合、再帰呼び出しが返ってくるとReaderは閉じ括弧の後に来る文字になるので、先読みする必要はありません。 – SpiderPig

+0

非常に助かりました!私はそれを実装しようとします。 – Mesutluka1019

関連する問題