<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 閱讀(1134) 評論(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());
     }
    }


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


    網站導航:
     
    主站蜘蛛池模板: 四虎永久免费地址在线观看| 最近高清中文字幕无吗免费看| 国产小视频在线观看免费| 亚洲精品国产啊女成拍色拍| 日韩免费无码一区二区三区| 久久久久久a亚洲欧洲AV| 无码少妇精品一区二区免费动态| 国产亚洲一区二区手机在线观看| 国产午夜不卡AV免费| 亚洲一区二区三区自拍公司| 光棍天堂免费手机观看在线观看| 久久精品国产亚洲麻豆| 久操视频免费观看| 亚洲欧洲日产国码二区首页| 99在线精品视频观看免费| 亚洲人成图片网站| 国产免费久久精品| 一区二区视频免费观看| 久久伊人久久亚洲综合| 久久国产高潮流白浆免费观看| 亚洲伊人久久大香线蕉啊| 毛片网站免费在线观看| 老司机亚洲精品影院在线观看| 亚洲国产精品毛片av不卡在线| 一级成人a做片免费| 久久久久无码精品亚洲日韩| 97国产免费全部免费观看| 亚洲精品无码国产片| 亚洲国产一成久久精品国产成人综合| 国产精品免费久久久久影院| 亚洲综合久久久久久中文字幕| 丁香花免费完整高清观看| 老妇激情毛片免费| 亚洲精品人成在线观看| 最近中文字幕无吗免费高清| xvideos永久免费入口| 久久亚洲精精品中文字幕| 波多野结衣久久高清免费| 国产免费伦精品一区二区三区| 亚洲黄色免费电影| 亚洲国产成人乱码精品女人久久久不卡|