算术表达式系列之后缀表达式求值

  1 package data.struct.algorithm;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 
  7 class Stackfix2 {
  8     private int maxSize;
  9     private int stackArr[];
 10     private int top;
 11 
 12     public Stackfix2(int maxSize) {
 13         this.maxSize = maxSize;
 14         stackArr = new int[maxSize];
 15         top = -1;
 16     }
 17 
 18     // 进栈
 19     public void push(int value) {
 20         stackArr[++top] = value;
 21     }
 22 
 23     // 出栈
 24     public int pop() {
 25         return stackArr[top--];
 26     }
 27 
 28     // 显示栈顶元素
 29     public int peek() {
 30         return stackArr[top];
 31     }
 32 
 33     // 判断栈是否为空
 34     public boolean isEmpty() {
 35         return top == -1;
 36     }
 37 
 38     public boolean isFull() {
 39         return top == maxSize - 1;
 40     }
 41 }
 42 
 43 class Postfix {
 44     private Stackfix2 theStackfix2;
 45     private String input;
 46 
 47     public Postfix(String in) {
 48         input = in;
 49     }
 50 
 51     public int doCalculate() {
 52         int num1;
 53         int num2;
 54         int inresult;
 55         char ch;
 56         int j;
 57         theStackfix2 = new Stackfix2(20);
 58         for (j = 0; j < input.length(); j++) {
 59             ch = input.charAt(j);
 60             // 如果字符为整数,则进行类型强制转换,并压入栈中
 61             if (ch >= '0' && ch <= '9') {
 62                 theStackfix2.push((int) (ch - '0'));
 63             } else {
 64                 // 测试用例最最关键的位置,考虑计算过程中的操作数的位置
 65                 // 先弹出栈的元素是第二个操作数,后弹出栈的是第一个操作数
 66                 num2 = theStackfix2.pop();
 67                 num1 = theStackfix2.pop();
 68                 switch (ch) {
 69                 case '+':
 70                     inresult = num1 + num2;
 71                     break;
 72                 case '-':
 73                     inresult = num1 - num2;
 74                     break;
 75                 case '*':
 76                     inresult = num1 * num2;
 77                     break;
 78                 case '/':
 79                     inresult = num1 / num2;
 80                     break;
 81                 default:
 82                     inresult = 0;
 83                     break;
 84                 }
 85                 theStackfix2.push(inresult);
 86             }
 87         }
 88         return theStackfix2.pop();
 89     }
 90 }
 91 
 92 public class PostfixValue {
 93 
 94     /**
 95      * @param args
 96      * @throws IOException
 97      */
 98     public static void main(String[] args) throws IOException {
 99 
100         while (true) {
101             System.out.println("Enter a Postfix:");
102             System.out.flush();
103             String input;
104             int result;
105             input = getString();
106             Postfix parse = new Postfix(input);
107             result = parse.doCalculate();
108             System.out.println("Postfix求值的结果是:" + result);
109         }
110     }
111 
112     public static String getString() throws IOException {
113         InputStreamReader isr = new InputStreamReader(System.in);
114         BufferedReader bufr = new BufferedReader(isr);
115         return bufr.readLine();
116     }
117 
118 }

 

posted @ 2016-04-10 17:11  菜鸟奋斗史  阅读(373)  评论(0编辑  收藏  举报