废话不多说,直接贴代码,复制就能用
import java.util.Stack;
/**
* 算术运算类
* by lcf
* 2022/6/27
*
*/
public class SuanShu {
/***
* 表达式计算支持加减乘除
* @param expstr 表达式,比如 1+1=2
* @return 计算结果
*/
public static double expressionEvaluation(String expstr) {
Stack s1 = new Stack();
String[] hou = new String[30];
String[] hao = new String[30];
int i = 0;
int j = 0;
boolean fu = false;
while(true) {
while(i < expstr.length()) {
String shu = "";
char ch = expstr.charAt(i);
switch(ch) {
case '(':
shu = shu + ch;
hao[j] = shu;
++i;
++j;
continue;
case ')':
shu = shu + ch;
hao[j] = shu;
++i;
++j;
continue;
case '*':
case '/':
shu = shu + ch;
hao[j] = shu;
++i;
++j;
continue;
case '+':
shu = shu + ch;
hao[j] = shu;
++i;
++j;
continue;
case '-':
if (i > 0 && expstr.charAt(i - 1) == '(') {
fu = true;
++i;
continue;
}
if (i == 0) {
fu = true;
++i;
} else {
shu = shu + ch;
hao[j] = shu;
++i;
++j;
}
continue;
case 'C':
shu = shu + ch;
hao[j] = shu;
++i;
++j;
continue;
case 'K':
shu = shu + ch;
hao[j] = shu;
++i;
++j;
continue;
}
while(ch >= '0' && ch <= '9' || ch == '.') {
shu = shu + ch;
++i;
if (i < expstr.length()) {
ch = expstr.charAt(i);
} else {
ch = '=';
}
}
if (fu) {
shu = '-' + shu;
fu = false;
}
hao[j] = shu;
++j;
}
int t = 0;
int o = 0;
while(true) {
while(t < j) {
if (hao[t].equals("+")) {
while(!s1.empty() && !s1.peek().equals("(")) {
hou[o] = (String)s1.pop();
++o;
}
s1.push(hao[t]);
++t;
} else if (hao[t].equals("-")) {
while(!s1.empty() && !s1.peek().equals("(")) {
hou[o] = (String)s1.pop();
++o;
}
s1.push(hao[t]);
++t;
} else if (hao[t].equals("*")) {
while(!s1.empty() && !s1.peek().equals("(") && !s1.peek().equals("+") && !s1.peek().equals("-")) {
hou[o] = (String)s1.pop();
++o;
}
s1.push(hao[t]);
++t;
} else if (hao[t].equals("/")) {
while(!s1.empty() && !s1.peek().equals("(") && !s1.peek().equals("+") && !s1.peek().equals("-")) {
hou[o] = (String)s1.pop();
++o;
}
s1.push(hao[t]);
++t;
} else if (hao[t].equals("C")) {
s1.push(hao[t]);
++t;
} else if (hao[t].equals("K")) {
s1.push(hao[t]);
++t;
} else if (hao[t].equals("(")) {
s1.push(hao[t]);
++t;
} else if (!hao[t].equals(")")) {
hou[o] = hao[t];
++o;
++t;
} else {
for(String out = (String)s1.pop(); !out.equals("("); out = (String)s1.pop()) {
hou[o] = out;
++o;
}
++t;
}
}
while(!s1.isEmpty()) {
hou[o] = (String)s1.pop();
++o;
}
Stack s2 = new Stack();
int m = 0;
while(m < o) {
double a;
double b;
double c;
String p;
String q;
String x;
if (hou[m].equals("+")) {
p = (String)s2.pop();
q = (String)s2.pop();
a = Double.parseDouble(p);
b = Double.parseDouble(q);
c = b + a;
x = Double.toString(c);
s2.push(x);
++m;
} else if (hou[m].equals("-")) {
p = (String)s2.pop();
q = (String)s2.pop();
a = Double.parseDouble(p);
b = Double.parseDouble(q);
c = b - a;
x = Double.toString(c);
s2.push(x);
++m;
} else if (hou[m].equals("*")) {
p = (String)s2.pop();
q = (String)s2.pop();
a = Double.parseDouble(p);
b = Double.parseDouble(q);
c = b * a;
x = Double.toString(c);
s2.push(x);
++m;
} else if (hou[m].equals("/")) {
p = (String)s2.pop();
q = (String)s2.pop();
a = Double.parseDouble(p);
b = Double.parseDouble(q);
c = b / a;
x = Double.toString(c);
s2.push(x);
++m;
} else if (hou[m].equals("C")) {
p = (String)s2.pop();
a = Double.parseDouble(p);
c = a * a;
x = Double.toString(c);
s2.push(x);
++m;
} else if (hou[m].equals("K")) {
p = (String)s2.pop();
a = Double.parseDouble(p);
c = Math.sqrt(a);
x = Double.toString(c);
s2.push(x);
++m;
} else {
s2.push(hou[m]);
++m;
}
}
if (s2.empty()) {
return 0.0D;
}
String y = (String)s2.pop();
return Double.parseDouble(y);
}
}
}
}
Stack类计算器函数java实现,能够进行函数表达式运算,自动解析算数表达式,支持加减乘除