运算效果
![这里写图片描述](https://img-blog.csdn.net/2018030216531571?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjA2OTg5ODM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
核心类
package cn.xt.calculator;
import java.util.Stack;
public class Calculator {
private Stack<Character> charStack = new Stack<Character>();
private Stack<Double> doubStack = new Stack<Double>();
private String Recount(String value) {
char ch = 0;
String reValue = "", newStr = "";
int length = 0, index = 0, count = 0, i = 0;
if (value.length() > 0) {
char last = value.charAt(value.length() - 1);
if (!(last >= '0' && last <= '9') || last == '(')
value = value.substring(0, value.length() - 1);
}
length = value.length();
while (i < length) {
ch = value.charAt(i);
if (ch == '(') {
count++;
} else if (ch == ')') {
count--;
}
if (ch >= '0' && ch <= '9') {
newStr += ch;
} else {
if (count >= 0)
newStr += ch;
}
++i;
}
while (count > 0) {
newStr += ")";
count--;
}
length = newStr.length();
while (index != length) {
ch = newStr.charAt(index);
if (ch >= '0' && ch <= '9') {
if (index > 0)
if (newStr.charAt(index - 1) == ')')
reValue += "*";
reValue += ch;
if (index < length - 1) {
if (newStr.charAt(index + 1) == '(')
reValue += "*";
}
} else {
if (index > 0)
if (ch == '(' && newStr.charAt(index - 1) == ')')
reValue += "*";
if (index == 0 && ch == '-')
reValue += "0";
switch (ch) {
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '.':
case '^':
reValue += ch;
break;
}
}
++index;
}
if(reValue.charAt(0) == '-'){
reValue = "0" + reValue;
}
return (reValue + "=");
}
private int isSwitch(char ch) {
int number = 0;
switch (ch) {
case '+': number = 0; break;
case '-': number = 1; break;
case '*': number = 2; break;
case '/': number = 3; break;
case '(': number = 4; break;
case ')': number = 5; break;
case '^': number = 6; break;
case '=': number = 7; break;
}
return number;
}
private char Judge(char One, char Two) {
char[][] menu = {
{ '>', '>', '<', '<', '<', '>', '<', '>' },
{ '>', '>', '<', '<', '<', '>', '<', '>' },
{ '>', '>', '>', '>', '<', '>', '<', '>' },
{ '>', '>', '>', '>', '<', '>', '<', '>' },
{ '<', '<', '<', '<', '<', 'K', '<', 'E' },
{ '<', '<', '<', '<', '<', '<', '<', 'E' },
{ '>', '>', '>', '>', '<', '>', '>', '>' },
{ '<', '<', '<', '<', '<', '<', '<', '=' }, };
int x = 0, y = 0;
x = isSwitch(One);
y = isSwitch(Two);
return menu[x][y];
}
private String AddStacks(String reValue) {
char nowaday = 0, nextPop = 0, ch = 0;
int length = reValue.length();
double number = 0, temp = 0, decimal = 1, result = 0, One = 0, Two = 0;
boolean flag = false, sflag = false, dflag = false, nflag = false, reckon;
charStack.push('=');
int index = 0;
while (index < length) {
ch = reValue.charAt(index);
while (ch >= '0' && ch <= '9' || ch == '.') {
if (ch != '.') {
number = Double.parseDouble(String.valueOf(ch));
temp = (temp * 10) + number;
flag = true;
if (dflag)
decimal *= 10;
} else
dflag = true;
index++;
ch = reValue.charAt(index);
}
reckon = true;
if (flag == true) {
if (nflag || sflag) {
if (nflag)
nflag = false;
temp = -temp;
}
if (dflag) {
temp /= decimal;
dflag = false;
decimal = 1;
}
doubStack.push(temp);
temp = 0;
flag = false;
}
if (sflag && ch == ')')
sflag = false;
if (ch == '-') {
char chs = reValue.charAt(index - 1);
char chn = reValue.charAt(index + 1);
if (chs == '+' || chs == '-' || chs == '*' || chs == '/'
|| chs == '(') {
if (chn == '(')
sflag = true;
else
nflag = true;
reckon = false;
}
}
while (reckon) {
nowaday = reValue.charAt(index);
nextPop = charStack.pop();
switch (Judge(nextPop, nowaday)) {
case '>':
try {
Two = doubStack.pop();
One = doubStack.pop();
} catch (Exception e) {
return "ERROR";
}
switch (nextPop) {
case '+': result = One + Two; break;
case '-': result = One - Two; break;
case '*': result = One * Two; break;
case '/': result = One / Two; break;
case '^': result = Math.pow(One, Two); break;
}
doubStack.push(result);
break;
case '<':
charStack.push(nextPop);
charStack.push(nowaday);
reckon = false;
break;
case '=':
return (reValue + String.format("%g", doubStack.pop()));
case 'K':
++index;
break;
case 'E':
return ("ERROR");
}
}
++index;
}
return null;
}
public String getEventuate(String expression) {
return AddStacks(Recount(expression.trim()));
}
}
测试类
package cn.xt.calculator;
/**
* 计算器算法
* 知识:数据结构中的栈
* 功能:四则混合运算+括号+小数+开方+纠错处理
*/
public class Test {
public static void main(String[] args) {
Calculator ca = new Calculator();
String value = null;
value = "-123+-999999999";
System.out.println(ca.getEventuate(value));
}
}