javaは逆ポーランド式の転化を実現し、結果を計算します。


1.中綴り表現を逆ポーランド式に変換し、1+2を12+:
    strigbufferを定義してサフィックスを保存し、スタックスタックスタックを定義します。
 

  loop:          ,          a:
      step1:  a       append stringbuffer .
     step2:  a    “(”  a  
      step3:  a    ,       ,       ,                  append stringbuffer ,          ,         ,           stringbuffer .
     step4:  a    ,       a                   a  a  ,         stringbuffer ,               a  ,               +-*/     
end loop;
    最後にstingbufferをStringしてstackの要素をつづり合わせてサフィックス表現を得ます。
2.逆ポーランド的な考え方を計算する
  
      stack:
            12+:
  loop:             b:
  1.  a    ,   
  2.  a    ,               a    ,        
  end loop;
  3,最後にスタックの要素を入力すると演算結果になります。
コードを付ける:
まず簡単なスタックを定義します。すべてのコードの中で異常な判断が行われていません。
スタック:

/     
class MyStack<T> {
	private Entry<T> top = new Entry<T>(null, null);
	private int size;

	public MyStack() {
		top.next = null;
		top.element = null;
	}

	//   
	public MyStack<T> push(T t) {
		Entry<T> node = new Entry<T>(top.next, t);
		top.next = node;
		size++;
		return this;
	}

	//   
	public T pop() {
		if (size == 0) {
			return null;
		}
		Entry<T> t = top.next;
		Entry<T> foo = top.next.next;
		top.next = foo;
		foo = null;
		size--;
		return t.element;
	}
//      
	public T top() {
		return top.next.element;
	}

	public int size() {
		return size;
	}
}

//     
class Entry<T> {
	T element;
	Entry<T> next;

	Entry(Entry<T> next, T e) {
		this.element = e;
		this.next = next;
	}
}

//アルゴリズム実現タイプRpn

//                  
//              
public class Rpn {

	//                      ,                    string 
	//         ,       ,   float,double     
	public int countRpn(String src) {
		MyStack<String> stack = new MyStack<String>();
		for (int i = 0; i < src.length(); i++) {
			String foo = src.charAt(i) + "";
			if (!isOperator(foo)) {
				stack.push(foo);
			} else {//      
				//           ,          
				String a = stack.pop();
				String b = stack.pop();
				int temp = count(b, a, foo);// zhu yi chao zuo shu nei xing
				stack.push(temp + "");

			}
		}
		return Integer.parseInt(stack.pop());//                
	}

	//       
	public boolean isOperator(String e) {
		if (null == e || "".equals(e)) {
			return false;
		}
		return "+".equals(e) || "*".equals(e) || "-".equals(e) || "/".equals(e);
	}

	//       
	public boolean isLeft(String s) {
		return "(".equals(s);
	}
   //      
	public boolean isRight(String s) {
		return ")".equals(s);
	}

	//      (e)        (a,b)
	public int count(String a, String b, String e) {
		int temp1 = Integer.parseInt(a);
		int temp2 = Integer.parseInt(b);
		if ("+".equals(e)) {
			return temp1 + temp2;
		} else if ("-".equals(e)) {
			return temp1 - temp2;
		} else if ("*".equals(e)) {
			return temp1 * temp2;
		} else {
			return temp1 / temp2;
		}
	}

	//               (    )
	public String reverse2Rpn(String src) {
		MyStack<String> stack = new MyStack<String>();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < src.length(); i++) {
			String temp = src.charAt(i) + "";
			if (!isOperator(temp) && !isRight(temp) && !isLeft(temp)) {
				//    ,+-    
				sb.append(temp);
			} else if (isOperator(temp)) {
				//       
				if (stack.size() == 0) {//   zhan    zhan
					stack.push(temp);
				} else if ("+".equals(temp) || "-".equals(temp)) {
					if (isLeft(stack.top())) {//        ,     
						stack.push(temp);//        
					} else {//      ,                        
						String top = stack.top();
						boolean next = true;
						while (("*".equals(top) || "/".equals(top)) && next) {
							top = stack.pop();
							sb.append(top);//       
							next = stack.size() == 0 ? false : true;
						}
					   stack.push(temp);//
					}
					

				} else {//     :"*" "/"     
					stack.push(temp);// +-            ,      

				}
			} else if (isLeft(temp)) {//          
				stack.push(temp);

			} else if (isRight(temp)) {//       ,      ,              
				String top = stack.pop();
				boolean next = true;
				while (!isLeft(top) && next) {
					sb.append(top);
					top = stack.pop();
					next = stack.size() == 0 ? false : true;
				}
			}
		}
		while (stack.size() > 0) {
			sb.append(stack.pop());
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		Rpn rpn = new Rpn();
		String src = rpn.reverse2Rpn("((1+2)*2)*4)+9*(1+2)");
		System.out.println("      :"+src);
		System.out.println("    :"+rpn.countRpn(src));
	}

}