2012-01-29 12 views
1

スタックデータ構造の使用:入力ファイルがバランスされていない場合、アンバランスの原因とファイル内ローカライズの詳細が提供されます。柔軟性の理由から、テキストファイルからバランスの取れたペアのシンボルを読み取る。私は最後の要件とのトラブルを抱えているシンボルペアのJavaプログラム

()、{}、[]、/ * * /::シンボルの次のペアを考慮して、プログラムをテスト/ * */

私も」することができますファイル内のローカリゼーションの詳細をどのように印刷するのか把握しているようですね。エラーが発生したテキストファイルの行番号

テキストファイルは次のようになります。

(()(() 
{}}}{}{ 
[[[]][][] 
((}})){{] 
() 
[] 
{} 
[]{} 
()()()[] 
*/ /* 
(a+b) = c 

コード:

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 


public class P1 { 

    private boolean match = true; 

    // The stack 
    private java.util.Stack<Character> matchStack = new java.util.Stack<Character>(); 

    // What to do with a match 
    public boolean ismatch() { 
     return match && matchStack.isEmpty(); 
    } 

    // Finding a match 
    public void add(char c) { 
     Character k = leftSide(c); 

     if (k == null) 
      ; 
     else if (k.charValue() == c) 
      matchStack.push(k); 
     else { 
      if (matchStack.isEmpty() || !matchStack.pop().equals(k)) 
       match = false; 
     } 
    } 

    // Add string values 
    public void add(String s) { 
     for (int i = 0; i < s.length(); i++) 
      add(s.charAt(i)); 
    } 

    // The various symbol pairs 
    protected static Character leftSide(char c) { 
     switch (c) { 
     case '(': 
     case ')': 
      return new Character('('); 
     case '[': 
     case ']': 
      return new Character('['); 
     case '{': 
     case '}': 
      return new Character('{'); 
     default: 
      return null; 
     } 
    } 

    // Main method. Welcome message, read the test file, build the array, print 
    // results. 
    public static void main(String args[]) { 

     List<String[]> arrays = new ArrayList<String[]>(); 

     // Welcome message 
     System.out 
       .println("Project #1\n" 
         + "Welcome! The following program ensures both elements of various paired symbols are present.\n" 
         + "Test Data will appear below: \n" 
         + "-------------------------------"); 

     // Read the file 
     try { 
      BufferedReader in = new BufferedReader(new FileReader(
        "testfile.txt")); 
      String str; 

      // Keep reading while there is still more data 
      while ((str = in.readLine()) != null) { 

       // Line by line read & add to array 
       String arr[] = str.split(" "); 
       if (arr.length > 0) 
        arrays.add(arr); 

       // Let the user know the match status (i.e. print the results) 
       P1 mp = new P1(); 
       mp.add(str); 

       System.out.print(mp.ismatch() ? "\nSuccessful Match:\n" 
         : "\nThis match is not complete:\n"); 
       System.out.println(str); 
      } 
      in.close(); 
      // Catch exceptions 
     } catch (FileNotFoundException e) { 
      System.out 
        .println("We're sorry, we are unable to find that file: \n" 
          + e.getMessage()); 
     } catch (IOException e) { 
      System.out 
        .println("We're sorry, we are unable to read that file: \n" 
          + e.getMessage()); 
     } 

    } 

    } 

答えて

0

これは、このようなLocationがクラスであるMap<String, Stack<Location>>、などのスタックのマップを使用することになり実装するための簡単な方法int(行番号と文字番号)が2つあるものを作成します。それはあなたの位置情報です。このマップのキー(String)は、あなたのペアの左側(オープナー)部分です。あなたが開幕戦を行うたびに、マップ内の適切なStackを探し、そのオープナーのために新しいLocationを押します。近くに出くわすたびにオープナーを見て、オープナーを使ってマップ内の正しいStackを探してから一度ポップしてください。私があなたの鍵にStringを使用している理由は、あなたのオープナーのすべてがCharacter、すなわちあなたの/*オープナーで表現できるわけではないので、Stringが必要です。 leftSide(char)(これは今度はleftSide(String)になります)の文字列をオンにすることはできませんので、if-elseを使用するか、より近いオープナーマッピングを作成するためにマップ(Map<String, String>)を使用する必要があります。

ファイルの末尾に達すると、オブジェクトはLocationに残ります。Stackオブジェクトはクローズしていないオープナーにする必要があります。

+0

答えはあまりにも重要ではありませんが、実際には[JDK 7](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html)でストリングをオンにすることができます。 )。 – Mac