public class Counter {
 /**用递归算法,把括号内的公式算出然后递归

  * @param args

  */ public static void calculator (String str){

  //System.out.println(str);

  //装载到StringBuffer提升效率

  StringBuffer cstr= new StringBuffer(str);

  //用正则把算式内的剩除符号和数字分组,

  Pattern p=Pattern.compile("\\-?[0-9]+(\\.[0-9]+)?|[*/]");

  if(cstr.length()==0)System.out.println("请输入计算式");

  //找出最后一个括号的坐标

  int start=cstr.lastIndexOf("(");

  //叛断公式是否还有括号

  if(start!=-1){

  int end=cstr.indexOf(")",start);

  StringBuffer spstr=new StringBuffer( cstr.subSequence(start+1, end));

  //删去截去的括号内的基本公式

       cstr=cstr.delete(start, end+1);

       //正则匹配

       Matcher m =p.matcher(spstr);

      List numlist=new ArrayList();

      //把基本公式的值和乘除符号加入到给组

      while(m.find()){

       numlist.add(m.group());

      }

      //调用计算方法并返回结果

      StringBuffer str1=count(numlist);

      //把结果INSERT到指定位置

      cstr=cstr.insert(start, str1);

  }else{

   //最后一次无括号计算基本公式并打印最终结果

   Matcher m =p.matcher(cstr);

   List numlist=new ArrayList();

   while(m.find()){

      numlist.add(m.group());

   }

   StringBuffer str1=count(numlist);

   System.out.println(str1);

   return;

  }

     //递归数组

 // System.out.println(cstr);

  calculator(new String(cstr));

 }
/**

 * 

 * @param list  接收基本公式数组 

 * @return 返回计算结果字符串

 */

 private static StringBuffer count(List list){

 // System.out.println(list);

  double num = 0;

  for (int i = 0; i < list.size(); i++) {

   if(list.get(i).equals("*")||list.get(i).equals("/")){

   Double  d=list.get(i).equals("*")?Double.parseDouble((String) list.get(i-1))*Double.parseDouble((String) list.get(i+1)):Double.parseDouble((String) list.get(i-1))/Double.parseDouble((String) list.get(i+1));

    list.add(i-1, d+"");list.remove(i);list.remove(i);list.remove(i);

    i=i-1; 

   

   }

  }

  

 for (int i = 0; i < list.size(); i++) {

   num+=Double.parseDouble((String) list.get(i));

  }  return  new StringBuffer(num+"");

 

 }

 

 

 public static void main(String[] args) {

  

  calculator("3+2+((5-6)*(-3+3/2)/2)");
 }
}