运算规则 a*(b+c) = ab+ac 
可不可以用一次循环就能算出下面的式子啊2*(1*()+6*(5+3))+4*(5*(6*(3+1)+1*(4*(3+1)))+3*(4*(1*(6+4)+6*(3*(3*(6+4)))))) =12 +256+236     +3456    +1456+13445+11445 +13446   +13444+3334466+3334446

解决方案 »

  1.   

    2*(1*()+6*(5+3))+4*(5*(6*(3+1)+1*(4*(3+1)))+3*(4*(1*(6+4)+6*(3*(3*(6+4))))))
    这个是要运算的式子用 a*(b+c) = ab+ac 的规则有 算上面的式子 
    即 1*(3+4) = 13+14     4*(3+5) =34+45上面的式子算出的结果应为
    =12 +256+236     +3456    +1456+13445+11445 +13446   +13444+3334466+3334446
    望大家帮帮忙   刚才我出去了一下 sorry
      

  2.   

    public static String analyseResult(String s){
    String retStr = new String(s);
    int calEnd=0;
    int i=0;
    int j=0;
    String blank = new String(" ");

    loop1: for(i=0;i<retStr.length();i++){
    if(retStr.charAt(i) == ')'){
    if(retStr.charAt(i-1) == '('){
    retStr = retStr.substring(0,i-2)+retStr.substring(i+1,retStr.length());
    //System.out.println("retStr: "+retStr);
    i=i-2;
    continue loop1;
    }

    calEnd=i; //记录下 右括号的位置
    String tempX = new String();   //左边括号外的数
    String tempAItem= new String();  //括号里面的数
    String tempStrItem = new String(); //所有括号里面的数
    String sign = new String();    //左边括号外的符号
    for(j=i-1;j>=0 && retStr.charAt(j)!='(';j--){
    //取第一个可运算的 "(...)" 为 string(j,i)

    try{
    if(Integer.valueOf(String.valueOf(retStr.charAt(j))).intValue() >=0){
    tempAItem= String.valueOf(retStr.charAt(j))+tempAItem;
    if(retStr.charAt(j-1) == '('){
    tempStrItem = tempAItem+blank+tempStrItem;
    //System.out.println("tempStrItem: "+tempStrItem);
    tempAItem = new String();
    //取括号里的书.用空格格开
    }
    }
    }catch(Exception e){
    tempStrItem = tempAItem+blank+tempStrItem;
    //System.out.println("tempStrItem: "+tempStrItem);
    tempAItem = new String();
    }
    }
    sign = retStr.substring(j-1,j);
    if(sign.charAt(0) == '+'){
    //如果左括号外是 + 则直接去掉两边括号 
    retStr = retStr.substring(0,j)+sign+retStr.substring(j+1,calEnd)+retStr.substring(calEnd+1,retStr.length());
    i=i-2;
    }else{//是 * 号
    try{
    for(j=j-2;j>=0 && Integer.valueOf(String.valueOf(retStr.charAt(j))).intValue() >=0;j--){
    //取 左括号前的数 如:  3*(4+5) 取j到 3的位置
    //j-2 是为了越过*号直接读到3
    tempX =String.valueOf(retStr.charAt(j))+tempX;
    }
    j++;
    String tempRetStr = retStr.substring(0,j);
    //把要进行运算的 前面的式子先存起来
    int begin =0;
    //int bla   =0;
    for(int count=0;count<tempStrItem.length();count++){
    if(tempStrItem.charAt(count) == blank.charAt(0)){
    //System.out.println("tempStrItem.length():"+tempStrItem.length()+"count"+count);
    if(count>begin){
    tempRetStr= tempRetStr + tempX+tempStrItem.substring(begin,count)+new String(" ");
    // System.out.println("tempRetStr: "+tempRetStr);
    }
    begin = count+1;
    }
    }
    // System.out.println("calEnd:"+calEnd+" tempRetStr.length(): "+tempRetStr.length());
    i=tempRetStr.length()-1;
    retStr =tempRetStr+retStr.substring(calEnd+1,retStr.length());
    //System.out.println("retStr: "+retStr);
    }catch(Exception e){
    j++;
    String tempRetStr = retStr.substring(0,j);
    //把要进行运算的 前面的式子先存起来
    int begin =0;
    for(int count=0;count<tempStrItem.length();count++){
    if(tempStrItem.charAt(count) == blank.charAt(0)){
    //System.out.println("tempStrItem.length():"+tempStrItem.length()+"count"+count);
    if(count>begin){
    tempRetStr= tempRetStr + tempX+tempStrItem.substring(begin,count)+new String(" ");
    //System.out.println("tempRetStr: "+tempRetStr);
    }
    begin = count+1;
    }
    }
    // System.out.println("calEnd:"+calEnd+" tempRetStr.length(): "+tempRetStr.length());
    i=tempRetStr.length()-1;
    retStr =tempRetStr+retStr.substring(calEnd+1,retStr.length());
    // System.out.println("retStr: "+retStr);
    }
    }
    }
    }
    //输出前把相同的无素去掉,并把多余的符号去掉
    for(int start=0;start<retStr.length();start++){
    if(retStr.charAt(start) == '+'){
    retStr = retStr.substring(0,start)+retStr.substring(start+1,retStr.length());
    }
    /*for(int tempSI = start+1;tempSI<retStr.length() && retStr.charAt(tempSI) != ' ' ; tempSI++){
    if(retStr.charAt(start) == retStr.charAt(tempSI)){
    retStr = retStr.substring(0,tempSI)+retStr.substring(tempSI+1,retStr.length());
    }
    }*/
    }
    return retStr;
    }