百行以内实现复杂数学表达式计算

kuoying 2020-05-12

一改以前

本次先上代码

package good;
//Evaluate complex expressions
import java.util.Scanner;

public class Example {

  public static void main(String[] arg) {//test

    Scanner in = new Scanner(System.in);
//    String one = in.next();
    try {
      System.out.println("(1.5+1)+(1+1)=" + RemoveBrackets(new StringBuffer("(1.5+1)+(1+1)")));
    }
    catch (Exception e){
      System.out.println("Please check if the expression is valid if there is an error");
    }
  }

  public static double RemoveBrackets(StringBuffer a) {//Bracket removal method

    while (a.toString().indexOf(‘(‘) != -1) {
      int i = a.toString().indexOf(‘(‘);
      int j = GetBracketPosition(a.toString(), i + 1);
      String to = a.toString().substring(i + 1, j);
      a.replace(i, j + 1, String.valueOf(RemoveBrackets(new StringBuffer(to))));
    }
    return OnlyMultiplication(a.toString());//计算除乘法以外的算式
  }


  //A method for calculating addition, subtraction, multiplication, and division without parentheses
  private static double OnlyMultiplication(String a) {//To eliminate the formula other than multiplication

    char[] b = a.toCharArray();
    for (int i = 0; i < a.length(); i++) {/*I‘m going to check the addition and subtraction because I‘m going to multiply and divide and then I‘m going to add and subtract*/

      if (b[i] == ‘+‘) {
        return OnlyMultiplication(a.substring(0, i)) + OnlyMultiplication(a.substring(i + 1));//Prevent double parenthesis
      }

      if (b[i] == ‘-‘) {
        return OnlyMultiplication(a.substring(0, i)) - OnlyMultiplication(a.substring(i + 1));
      }
    }//So let‘s go over here and do the subtraction and the subtraction
    //That means there‘s no plus or minus sign
    //A separate multiplication or division
    String[] sum1 = a.split("/");//The second half of this method is to remove the division sign

    double too = Double.valueOf(CalculateMultiplication(sum1[0]));
    for (int i = 1; i < sum1.length; i++) {
      too = too * 1.0 / Double.valueOf(sum1[i]);
    }
    return too;
  }

  //So the only way to multiply is to multiply
  private static double CalculateMultiplication(String aaa) {//Remove the multiplication sign
    double to = 1.0;
    if (aaa.indexOf(‘*‘) == -1) {//Without a multiplication sign
      return Double.valueOf(aaa);
    }
    String[] too = aaa.split("\\*");

    for (int i = 0; i < too.length; i++) {
      to = to * Double.valueOf(too[i]);
    }
    return to;
  }

  private static int GetBracketPosition(String a, int b) {//Find the corresponding close parenthesis position
    int count1 = 1;
    int count2 = 0;
    char[] aa = a.toCharArray();
    for (int i = b; i < a.length(); i++) {
      if (aa[i] == ‘(‘) {//Prevent double parenthesis
        count1++;
      }
      if (aa[i] == ‘)‘) {
        count2++;
        if (count2 == count1) {
          return i;
        }
      }
    }
    return 0;
  }
}

下面是运行结果

百行以内实现复杂数学表达式计算

OK  代码可以先看一下     相信肯定有人能够不靠解析   直接看懂

下面我来具体的一行一行的解释我的代码

先说一下具体思路

我们先考虑括号的问题     因为括号内容优先计算

当我们吧所有括号里面的内容处理好之后     我们用计算的结果替换掉括号以及里面的表达式

我们下面会将那种不带括号的表达式是怎样计算的

当然  必须要提示的一点    不排除会有双层括号情况的出现   所以我们要对拆分出来的括号里面分表达式在进行一次括号检测     当检测到没有括号的时候   我们才调用无括号表达式计算方法   计算表达式的最终结果

OK  下面我们进入无括号表达式计算方法的解释环节

现在强调一下  我们已经完成括号处理  

我们的表达式里只有加减乘除的简单运算

我们就以加号和减号为分界线    

这样就成功吧式子拆成了要么是乘除运算  要么是单独是数    这样处理起来是不是就方便的太多太多了

哈哈哈哈   香不香

OK  我们的解析就到这

当然 方法肯定不止一种    比我这个方法好的方法也肯定多了去了       

不喜勿喷

相关推荐

seasongirl / 0评论 2020-06-05