<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 41, comments - 15, trackbacks - 0, articles - 1
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理


    package com.cbd.dk.test;

    import java.util.ListIterator;
    import java.util.Stack;

    public class Test {

     private String src;

     public Test(String src) {
      this.src = src;
     }
     public double getResult() {
      String postfix = getPostfix();
      Stack<String> stk = new Stack<String>();
      //System.out.println(postfix);
      String parts[] = postfix.split(" +");
      double result=0;
      for(int i=0; i<parts.length; i++){
       char tmp = parts[i].charAt(0);
       if(!isOperator(tmp)){
        stk.push(parts[i]);
       }else{
        double a = Double.parseDouble(stk.pop());
        double b = Double.parseDouble(stk.pop());
        result = calculate(b,a,tmp);
        stk.push(String.valueOf(result));
       }
      }
      return result;
     }


     /**
      * test if the character is an operator,such +,-,*,/
      * @param op  the character to test
      * @return  true if op is an operator otherwise false
      */
     private boolean isOperator(char op){
      return (op=='+'||op=='-'||op=='*'||op=='/');
     }


     /**
      * calculate an expression such (a op b)
      * @param a  number 1
      * @param b  number 2
      * @param op the operator
      * @return  (double)(a op b)
      */
     public double calculate(double a, double b, char op) {
      switch (op) {
       case '+':
        return a + b;
       case '-':
        return a - b;
       case '*':
        return a * b;
       case '/':
        return a / b;
      }
      return -1;
     }


     /**
      * convert the suffix to postfix
      * @return the postfix as a string
      */
     private String getPostfix() {
      Stack<String> stk = new Stack<String>();
      String postfix = new String();
      char op;
      int i = 0;
      while (i < src.length()) {
       if (Character.isDigit(src.charAt(i))||src.charAt(i)=='.') {
        postfix += " ";
        do {
         postfix += src.charAt(i++);
        } while ((i < src.length()) && (Character.isDigit(src.charAt(i))));
        postfix += " ";
       }else {
        switch (op = src.charAt(i++)) {
         case '(':
          stk.push("(");
          break;
         case ')':
          while (stk.peek() != "(") {
           String tmp = stk.pop();
           postfix += tmp;
           if(tmp.length()==1 && isOperator(tmp.charAt(0)))
            postfix += " ";
          }
          stk.pop();
          postfix += " ";
          break;
         case '+':
         case '-':
          while ((!stk.empty()) && (stk.peek() != "(")) {
           postfix += stk.pop()+" ";
          }
          stk.push(new Character(op).toString());
          break;
         case '*':
         case '/':
          while ((!stk.empty()) && ((stk.peek() == "*") || (stk.peek() == "/"))) {
           postfix += stk.pop()+" ";
          }
          stk.push(new Character(op).toString());
          break;
        }
       }
      }
      ListIterator it = stk.listIterator(stk.size());
      while (it.hasPrevious())
       postfix += it.previous() + " ";
      return postfix.trim().replaceAll(" +\\.",".");
     }

     /**
      * main function
      * @param args
      */
     public static void main(String args[]) {
      //System.out.println(new Test("((1.5+6.000)*9+9.36)*(8-8*8+8*7)").getResult());
      System.out.println(new Test("(2+(3*2)/2)").getResult());
     }
    }


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久乐国产综合亚洲精品| 亚洲Av无码专区国产乱码DVD| 亚洲AV无码久久久久网站蜜桃| 日韩精品免费在线视频| 久久精品国产亚洲av麻| 精品视频一区二区三区免费| 亚洲AV日韩AV高潮无码专区| 麻豆成人久久精品二区三区免费| 亚洲老熟女@TubeumTV| 国产99视频精品免费观看7| 亚洲成av人片在线天堂无| 国产成人免费ā片在线观看| 免费高清A级毛片在线播放| 国产午夜亚洲不卡| 免费国产成人18在线观看| 亚洲精品美女久久久久| 午夜免费福利网站| 在线播放免费人成视频网站| 精品亚洲成α人无码成α在线观看| 3344在线看片免费| 亚洲欧洲在线播放| 免费国产人做人视频在线观看| 美女巨胸喷奶水视频www免费| 亚洲AV无码乱码国产麻豆| 1000部无遮挡拍拍拍免费视频观看| 亚洲日日做天天做日日谢| 夜色阁亚洲一区二区三区| 一个人免费视频在线观看www| 亚洲激情视频网站| 免费**毛片在线播放直播| 成人影片一区免费观看| 亚洲看片无码在线视频| 亚洲综合久久夜AV | 亚洲三级在线免费观看| 在线观看国产一区亚洲bd| 国产精品久久久亚洲| 成人超污免费网站在线看| A毛片毛片看免费| 亚洲日韩亚洲另类激情文学| 亚洲熟妇av一区二区三区| 老司机在线免费视频|