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

一改以前

本次先上代码

 

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

public class Example {

public static void main(String[] arg) {

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

public static boolean cont(String a) {//Legality of judgment
boolean b = true;
char[] to = a.toCharArray();
int i, j;
for (i = 0; i < to.length - 1; i++) {
if (to[i] == '+' || to[i] == '-' || to[i] == '*' || to[i] == '/') {
if (to[i + 1] == '+' || to[i + 1] == '-' || to[i + 1] == '*' || to[i + 1] == '/') {
return false;
}
}
}
return b;
}


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 EliminatePlusSign(a.toString());//Calculate the formula other than parentheses
}


private static double EliminatePlusSign(String a) {//Eliminate the plus sign

char[] b = a.toCharArray();
for (int i = 0; i < a.length(); i++) {//Testing a plus sign
if (b[i] == '+') {
return EliminateMinusSign(a.substring(0, i)) + EliminatePlusSign(a.substring(i + 1));//Detection of minus
}
}

return EliminateMinusSign(a);//The minus sign is detected directly without the plus sign
}
private static double EliminateMinusSign(String a){//Check for subtractions and subdivisions
char[] b=a.toCharArray();
if (b[0]=='-'){
a="0"+a;
b=a.toCharArray();
}
for (int i = 0; i < a.length(); i++) {//Detection of minus
if (b[i] == '-' && (b[i-1]>='0') && b[i-1]<='9') {
return EliminatePlusSign(a.substring(0, i)) - EliminatePlusSign(a.substring(i + 1));
}
}

String[] sum1 = a.split("/");//Eliminate devide

double too = Double.valueOf(CalculateMultiplication(sum1[0]));/*A multiplication sign is detected for each division expression*/
for (int i = 1; i < sum1.length; i++) {
too = too * 1.0 /Double.valueOf(CalculateMultiplication(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  我们的解析就到这

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

不喜勿喷

 

posted @ 2020-05-12 08:00  Mr小明同学  阅读(402)  评论(0编辑  收藏  举报