Java数据结构和算法中文第二版.pdf 代码
StackX.java package com.ch4.postfix; public class StackX { private int maxSize ; private int[] stackArray ; private int top ; public StackX(int size){ maxSize = size ; stackArray = new int[size] ; top = -1 ; } public void push(int elem){ stackArray[++top] = elem ; } public int pop(){ return stackArray[top--] ; } public int peek(){ return stackArray[top] ; } public boolean isEmpty(){ return (top == -1) ; } public int peekIndex(int index){ return stackArray[index] ; } public int size(){ return top +1 ; } public void displayStack(String info){ System.out.print(info) ; System.out.print("Stack (bottom -->top): ") ; for (int j = 0; j < size(); j++){ System.out.print(peekIndex(j)) ; System.out.print(' '); } System.out.println() ; } } Postfix.java package com.ch4.postfix; public class Postfix { private StackX theStack ; private String input ; public Postfix(String S){ input = S ; } public int doParse(){ theStack = new StackX(20) ; char ch ; int i ; int num1 , num2 ,interAns ; for (i = 0; i < input.length(); i++){ ch = input.charAt(i) ; theStack.displayStack("" + ch + "") ; if ((ch >= '0') && (ch <='9')){ theStack.push((int)(ch -'0')) ; }else{ num2 = theStack.pop() ; num1 = theStack.pop() ; switch(ch){ case '+' : interAns = num1 + num2 ; break ; case '-' : interAns = num1 - num2 ; break ; case '*' : interAns = num1 * num2 ; break ; case '/' : interAns = num1 / num2 ; break ; default: interAns = 0; break ; } theStack.push(interAns) ; } } interAns = theStack.pop() ; return interAns ; } } PostfixApp.java package com.ch4.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import com.ch4.infix.Infix; import com.ch4.postfix.Postfix; public class PostfixApp { /** * @param args */ public static void main(String[] args) throws IOException{ String input , output ; while(true ){ System.out.print("Enter infix: ") ; System.out.flush() ; input = getString() ; if (input.equals("")){ break ; } Infix theTrans = new Infix(input) ; output = theTrans.doTrans() ; Postfix postfix = new Postfix(output) ; System.out.println("src String : " + input) ; System.out.println("post src String : " + output) ; System.out.println("Postfix is :" + postfix.doParse()) ; ; } } public static String getString() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in) ; BufferedReader br = new BufferedReader(isr) ; String s = br.readLine() ; return s ; } }
只对个位数计算,如 :1* 2 -3+6/2
运行结果:
Enter infix: 1 + 2*3 + (9-5) /2
For 1 Stack (bottom -->top):
For + Stack (bottom -->top):
For 2 Stack (bottom -->top): +
For * Stack (bottom -->top): +
For 3 Stack (bottom -->top): + *
For + Stack (bottom -->top): + *
For ( Stack (bottom -->top): +
For 9 Stack (bottom -->top): + (
For - Stack (bottom -->top): + (
For 5 Stack (bottom -->top): + ( -
For ) Stack (bottom -->top): + ( -
For / Stack (bottom -->top): +
For 2 Stack (bottom -->top): + /
While Stack (bottom -->top): + /
While Stack (bottom -->top): +
End Stack (bottom -->top):
src String : 1 + 2*3 + (9-5) /2
post src String : 123*+95-2/+
1Stack (bottom -->top):
2Stack (bottom -->top): 1
3Stack (bottom -->top): 1 2
*Stack (bottom -->top): 1 2 3
+Stack (bottom -->top): 1 6
9Stack (bottom -->top): 7
5Stack (bottom -->top): 7 9
-Stack (bottom -->top): 7 9 5
2Stack (bottom -->top): 7 4
/Stack (bottom -->top): 7 4 2
+Stack (bottom -->top): 7 2
Postfix is :9