今天小编就为大家分享一篇关于Java计算器核心算法代码实现,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
在进行一个表达式的计算时,先将表达式分割成数字和字符串然后利用出入栈将分割后的表达式进行中缀转后缀,再将后缀表达式进行计算得到结果(思想在上一篇写过)现在贴下java语言的代码实现。(学习java时间不长所以可能会有很多不足的地方,我会改进也欢迎大神可以给我一些意见和建议~谢谢啦)
我将这部分分成三个方法完成功能,并在getresult方法调用(getresult方法被主方法调用)private string getresult(string str) {
//分割
string[] str = segment(str);
//中缀转后缀
string newstr = inftosuf(str);
//后缀计算
string result = suftores(newstr);
return suftores(result);
} 1.字符串分割,为避免在textview上显示带空格删除时不方便而且显示屏就那么大占地方,录入时的字符串中没有空格然后就手动分割了private static string[] segment(string str) {
string[] exp = new string[str.length()+1];
//找最近的索引并截取字符串
int l = str.length();
for(int i = 0;i < l+1;i++) {
int index;
int[] ind = new int[6];
ind[0] = str.indexof('+');
ind[1] = str.indexof('-');
ind[2] = str.indexof('*');
ind[3] = str.indexof('/');
ind[4] = str.indexof('(');
ind[5] = str.indexof(')');
if(ind[1] == 0) {
arrays.sort(ind);
int t;
for(t = 0;t <6;t++) {
if(ind[t] >= 0)
break;
}
int r = ind[t+1];
exp[i] = str.substring(0,r);
i++;
exp[i] = str.substring(r,r+1);
str = str.substring(r+1);
}else if(((ind[1]-ind[4]) == 1) && (ind[4]==0)) {
arrays.sort(ind);
int t ;
for(t = 0;t <6;t++) {
if(ind[t] >= 0)
break;
}
int r = ind[t+1];
exp[i] = str.substring(0,1);
i++;
exp[i] = str.substring(1,r+2);
i++;
exp[i] = str.substring(r+2,r+3);
str = str.substring(r+3);
}else {
arrays.sort(ind);
int t;
for(t = 0;t <6;t++) {
if(ind[t] >= 0)
break;
}
if(t==6)
break;
index = ind[t];
if(index!=0) {
exp[i] = str.substring(0,index);
i++;
}
exp[i] = str.substring(index,index+1);
str = str.substring(index+1);
}
}
int j = 0;
int k = 0;
for(; exp[j]!=null ;j++){}
if(!exp[j-1].equals(")")) {
exp[j]=str;
str = "";
k = j;
}else {
k = j-1;
}
string[] expp = new string[k+1];
for(int t = 0; t < k+1;t++) {
expp[t] = exp[t];
}
return expp;
//system.out.println("分割的字符串:");
} 2.中缀转后缀private static string inftosuf(string[] exp) {
string newstrs = "";
//初始化栈
stack<string> stack = new stack<>();
/*
判断并放入后缀表达式中:
for循环遍历整个str进行判断
循环结束若栈不为空全部出栈
*/
int l = exp.length;
for(int i = 0; i < l; i++) {
if ((stack.empty()) && (exp[i].equals("+") || exp[i].equals("-") || exp[i].equals("*") || exp[i].equals("/"))) {
stack.push(exp[i]);
} else if (exp[i].equals("(")) {
stack.push(exp[i]);
} else if (exp[i].equals("*") || exp[i].equals("/")) {
while (stack.peek().equals("*") || stack.peek().equals("/")) {
newstrs = newstrs.concat(stack.pop()+" ");
if(stack.isempty()) {
break;
}
}
stack.push(exp[i]);
} else if (exp[i].equals("+") || exp[i].equals("-")) {
while (!(stack.isempty())&&((stack.peek()).equals("*") || (stack.peek()).equals("/") || (stack.peek()).equals("+") || (stack.peek()).equals("-"))) {
newstrs = newstrs.concat(stack.pop()+" ");
if(stack.isempty()) {
break;
}
}
stack.push(exp[i]);
} else if (exp[i].equals(")")) {
int t = stack.search("(");
for (int k = 1; k < t; k++) {
newstrs = newstrs.concat(stack.pop()+" ");
}
string tstr = stack.pop();
} else {
newstrs = newstrs.concat(exp[i]+ " ");
}
}
while (!stack.empty()) {
if (!stack.peek().equals("(") || !stack.peek().equals(")")) {
newstrs = newstrs.concat(stack.pop()+" ");
} else if (stack.peek().equals("(") || stack.peek().equals(")")) {
string tstr = stack.pop();
}
}
// system.out.println("后缀:"+newstrs);
return newstrs;
} 3.后缀的计算private static string suftores(string sufstr) {
string[] exp = sufstr.split(" ");
stack<string> stack = new stack<>();
string res = "";
for(int i = 0;i < exp.length; i++) {
if(!exp[i].equals("+") && !exp[i].equals("-") && !exp[i].equals("*") && !exp[i].equals("/")){
stack.push(exp[i]);
}else if(exp[i].equals("+")) {
bigdecimal b2 = new bigdecimal(stack.pop());
bigdecimal b1 = new bigdecimal(stack.pop());
bigdecimal b3 = b1.add(b2);
stack.push(b3.tostring());
}else if(exp[i].equals("-")) {
bigdecimal b2 = new bigdecimal(stack.pop());
bigdecimal b1 = new bigdecimal(stack.pop());
bigdecimal b3 = b1.subtract(b2);
stack.push(b3.tostring());
}else if(exp[i].equals("*")) {
bigdecimal b2 = new bigdecimal(stack.pop());
bigdecimal b1 = new bigdecimal(stack.pop());
bigdecimal b3 = new bigdecimal(0);
if(b1.compareto(bigdecimal.zero)== 0|| b2.compareto(bigdecimal.zero) == 0) {
b3 = bigdecimal.zero;
}else {
b3 = b1.multiply(b2);
}
stack.push(b3.tostring());
}else if(exp[i].equals("/")){
bigdecimal b2 = new bigdecimal(stack.pop());
bigdecimal b1 = new bigdecimal(stack.pop());
bigdecimal b3 = new bigdecimal(0);
double d1 = b1.doublevalue();
double d2 = b2.doublevalue();
if(d1%d2 == 0){
b3 = (b1.divide(b2));
stack.push(b3.tostring());
}else {
b3 = b1.divide(b2,10, roundingmode.half_up);
stack.push(b3.tostring());
}
}
}
res = stack.pop();
boolean flag = false;
for (int m = 0; m < res.length() - 1;m++) {
if(res.charat(m) == '.'){
flag = true;
}
}
if(flag) {
for(int m = res.length()-1;m >= 0;m--) {
if(res.charat(m) == '0'){
}else {
res = res.substring(0,m+1);
break;
}
}
if(res.charat(res.length()-1) == '.') {
res = res.substring(0,res.length()-1);
}
}
return res;
} 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/sdr_zd/article/details/52135924
|