📄 calculatestring.java
字号:
package calculator;
import java.math.BigDecimal;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import java.util.Stack;
//**************************************************字符运算主程序***************************************************
public class CalculateString {
public BigDecimal calculateString(String str) {
char[] strs = str.toCharArray();
Stack<String> stack = new Stack<String>();
for (int i = 0; i < strs.length; i++) {
String stackStr = String.valueOf(strs[i]);
stack.push(stackStr);
if (")".equals(stack.top())) {
String subStr = null;
while (!"(".equals(stack.top())) {
stack.pop();
if (!"(".equals(stack.top())) {
subStr = addEnd(subStr, stack.top());
}
}
String pushStr = CalculateReversePolishExpression(subStr);
stack.pop();
stack.push(pushStr);
}
}
String resultStr = null;
while (stack.count != 0) {
resultStr = CalculateReversePolishExpression(stack.toString());
}
BigDecimal result = null;
if (resultStr != null) {
result = new BigDecimal(resultStr);
} else {
result = BigDecimal.ZERO;
}
return result.setScale(2, BigDecimal.ROUND_HALF_UP);
}
//****************************************************************************************************************
public String[] matcher(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
List<String> list = new ArrayList<String>();
while (matcher.find()) {
list.add(matcher.group());
}
String[] result = new String[list.size()];
return list.toArray(result);
}
//****************************************************************************************************************
public List<String> createReversePolishExpression(String subStr) {
String regex = "\\d+\\.{0,1}\\d*";
String[] numbers = matcher(regex, subStr);
String changeStr = subStr.replaceAll(regex, "0").replaceAll("\\-\\-0",
"-1").replaceAll("\\+\\-0", "+1").replaceAll("\\*\\-0", "*1")
.replaceAll("\\/\\-0", "/1");
char[] chars = changeStr.toCharArray();
int index = 0;
List<String> list = new ArrayList<String>();
for (int i = 0; i < chars.length; i++) {
String str = String.valueOf(chars[i]);
if ("0".equals(str)) {
list.add(numbers[index++]);
} else if ("1".equals(str)) {
list.add("-" + numbers[index++]);
} else {
list.add(str);
}
}
List<String> suffix = new ArrayList<String>();
Stack<String> operator = new Stack<String>();
for (int i = 0; i < list.size(); i++) {
if (!isOperatorType(list.get(i))) {
suffix.add(list.get(i));
} else {
if (operator.count == 0) {
operator.push(list.get(i));
} else {
while (operator.count != 0&&compare(operator.top(), list.get(i)) >= 0) {
String top = operator.top();
operator.pop();
suffix.add(top);
}
operator.push(list.get(i));
}
}
}
while (operator.count != 0) {
suffix.add(operator.top());
operator.pop();
}
return suffix;
}
//****************************************************************************************************************
public String CalculateReversePolishExpression(String subStr) {
List<String> suffix = createReversePolishExpression(subStr);
Stack<Double> stack = new Stack<Double>();
for (int i = 0; i < suffix.size(); i++) {
if (!isOperatorType(suffix.get(i))) {
stack.push(Double.valueOf(suffix.get(i)));
} else {
Double current = stack.top();
stack.pop();
Double previous = null;
if (stack.count != 0) {
previous = stack.top();
stack.pop();
} else {
previous = new Double(0);
}
Double result = calculate(suffix.get(i), previous, current);
stack.push(result);
}
}
return stack.top().toString();
}
//****************************************************************************************************************
public String addEnd(String str, String a) {
StringBuffer buf = new StringBuffer();
buf.append(a);
if (str != null) {
buf.append(str);
}
return buf.toString();
}
//********************************************对符号的操作*************************************************************
public boolean isOperatorType(String str) {
if (str.equals("+") || str.equals("-") || str.equals("*")
|| str.equals("/")) {
return true;
}
return false;
}
public int compare(String op1, String op2) {
Integer iop1 = getOperator(op1);
Integer iop2 = getOperator(op2);
return iop1.compareTo(iop2);
}
public Integer getOperator(String op) {
if ("+".equals(op) || "-".equals(op)) {
return new Integer(0);
}
if ("*".equals(op) || "/".equals(op)) {
return new Integer(1);
}
return null;
}
public Double calculate(String op, Double previous, Double current) {
if ("+".equals(op)) {
return previous + current;
}
if ("-".equals(op)) {
return previous - current;
}
if ("*".equals(op)) {
return previous * current;
}
if ("/".equals(op)) {
return previous / current;
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -