<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 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    java處理字符串的四則運算

    Posted on 2012-12-11 16:24 yuhaibo736 閱讀(1137) 評論(0)  編輯  收藏


    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());
     }
    }


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 99视频免费播放| 久久免费视频网站| 免费高清资源黄网站在线观看| 亚洲精品色午夜无码专区日韩| 国产精品免费久久久久电影网| 亚洲精品高清在线| 国产成人无码精品久久久免费| 亚洲?V乱码久久精品蜜桃| 日日躁狠狠躁狠狠爱免费视频| 亚洲国产精品成人久久蜜臀| 人碰人碰人成人免费视频| 亚洲午夜无码AV毛片久久| 国产在线精品一区免费香蕉| 亚洲av永久无码精品漫画 | 久久国产精品免费一区| 亚洲精品无码永久在线观看你懂的| 91视频免费观看| 亚洲高清美女一区二区三区| 一二三四影视在线看片免费| 亚洲精品乱码久久久久久V| vvvv99日韩精品亚洲| 91在线免费观看| 91亚洲精品麻豆| 亚洲?V无码成人精品区日韩| 成人无码a级毛片免费| 亚洲最大黄色网站| 日韩免费视频播放| 中文字幕视频免费在线观看| 亚洲福利视频网站| 国产成人免费手机在线观看视频| 国产精品玖玖美女张开腿让男人桶爽免费看 | a毛片视频免费观看影院| 亚洲精品国产手机| 国产精品自在自线免费观看| 国产在线观看免费av站| 久久国产亚洲高清观看| 免费无码又爽又刺激毛片| 两个人看的www免费高清 | 国产午夜无码视频免费网站| 日本道免费精品一区二区| 亚洲男人的天堂久久精品|