jp.ne.ytp.util
クラス ReversePolish

java.lang.Object
  |
  +--jp.ne.ytp.util.ReversePolish

public class ReversePolish
extends Object

いわゆる逆ポーランド記法(後置記法)を実現します。 渡された計算式を、指定の演算子とその優先順位に従って逆ポーランド記法に変換します。 演算子や優先順位を利用する側で全て決定できるため、 当クラスの利用は四則演算に限定されません。 「カッコ()」の優先順位も外部で指定する必要がありますが、 一般的な利用をする場合、開きカッコは最高位、 閉じカッコは最低位として設定してください。
次のコードは四則演算を実現する簡単な例です。

      ArrayList before = new ArrayList();
      before.add("1");
      before.add("+");
      before.add("2");
      before.add("*");
      before.add("a");
      
      HashMap operators = new HashMap();
      operators.put(ReversePolish.OPENPBRACKET, new Integer(50));
      operators.put(ReversePolish.OPERAND, new Integer(40));
      operators.put("*", new Integer(30));
      operators.put("/", new Integer(30));
      operators.put("+", new Integer(20));
      operators.put("-", new Integer(20));
      operators.put(ReversePolish.CLOSEBRACKET, new Integer(0));
      
      ReversePolish rp = new ReversePolish(operators);
      List reversed = rp.parse2Polish(before);
      
      System.out.print("Reversed:");
      for (int i = 0; i < reversed.size(); i++) {
          System.out.print(reversed.get(i));
      }
      System.out.println("");
  
この結果は、Reversed:12a*+ と表示されます。

バージョン:
$Id: ReversePolish.java,v 1.2 2003/02/19 18:38:04 YT0050 Exp $
作成者:
YTP

フィールドの概要
static String CLOSEBRACKET
          閉じカッコ")"です。
static String OPENPBRACKET
          開きカッコ"("です。
static String OPERAND
          オペランドを意味します。
 
コンストラクタの概要
ReversePolish()
          デフォルトコンストラクタです。
ReversePolish(Map operatorPriority)
          operatorPriorityで指定された演算子と優先順位を持つインスタンスを生成します。
 
メソッドの概要
 List parse2Polish(List tokens)
          tokensに設定された演算式を逆ポーランド記法に変換します。
 void setOperator(Map operatorPriority)
          演算子と優先順位を設定します。
 
クラス java.lang.Object から継承したメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

フィールドの詳細

OPERAND

public static final String OPERAND
オペランドを意味します。優先順位を指定する際に使います。

関連項目:
定数フィールド値

OPENPBRACKET

public static final String OPENPBRACKET
開きカッコ"("です。優先順位を指定する際に使います。

関連項目:
定数フィールド値

CLOSEBRACKET

public static final String CLOSEBRACKET
閉じカッコ")"です。優先順位を指定する際に使います。

関連項目:
定数フィールド値
コンストラクタの詳細

ReversePolish

public ReversePolish()
デフォルトコンストラクタです。 このコンストラクタで生成されたインスタンスでは、 setOperator(Map)メソッドを必ず呼び出して下さい。

関連項目:
setOperator(Map)

ReversePolish

public ReversePolish(Map operatorPriority)
              throws IllegalArgumentException
operatorPriorityで指定された演算子と優先順位を持つインスタンスを生成します。 operatorPriorityには、キーには演算子を表すStringを、 値には優先順位を表すIntegerクラスインスを、それぞれ設定してください。

パラメータ:
operatorPriority - 演算子とその優先順位を設定したMap
例外:
IllegalArgumentException - operatorPriorityがnullの場合
メソッドの詳細

setOperator

public void setOperator(Map operatorPriority)
                 throws IllegalArgumentException
演算子と優先順位を設定します。 operatorPriorityには、キーに演算子を表すStringを、 値に優先順位を表すIntegerクラスインスを、それぞれ設定してください。

パラメータ:
operatorPriority - 演算子とその優先順位を設定したMap
例外:
IllegalArgumentException - operatorPriorityがnullの場合

parse2Polish

public List parse2Polish(List tokens)
tokensに設定された演算式を逆ポーランド記法に変換します。 tokensの各要素はtoString()メソッドにて、 オペランドまたは演算子を返すようにしておく必要があります。 またequals()メソッドを実装し、 オブジェクト同士の比較が正しくできるようにもしておいてください。 (要素としてStringクラスを使うのが一般的です)。 逆ポーランド記法に変換したオペランドまたは演算子を、 Stringクラスの要素としてリストに一つずつ格納し返します。

パラメータ:
tokens - オペランドまたは演算子を要素に持つリスト
戻り値:
逆ポーランド記法に変換したトークン(Stringクラス)を持つリスト


Copyright© 2003, Your Technology Partner(YTP). All rights reserved.