📄 expressioncalculator.java
字号:
/*
* Created on 2007-7-12
*/
package willishz.soho.arithmetic;
import java.util.ArrayList;
import java.util.Scanner;
/**
* calculate an expression(end with "=", decimal supported, brackets unsupported).
* @author willishz
*/
public class ExpressionCalculator {
public static final char PLUS = '+';
public static final char SUBTRACT = '-';
public static final char MULTIPLE = '*';
public static final char DIVIDE = '/';
public static final char DECIMAL = '.';
public static final char EQUAL = '=';
public static final String PLUS_STR = "+";
public static final String SUBTRACT_STR = "-";
public static final String MULTIPLE_STR = "*";
public static final String DIVIDE_STR = "/";
/**
* interpret expression string to units.
* @param expression
* @return
*/
public static String interpret(String expression) {
ArrayList explist = new ArrayList();
int flg = 0; // flag to divide expression units
StringBuffer sb = new StringBuffer();
char[] expChars = expression.toCharArray();
// expression string to units
for (int i = 0; i < expChars.length; i++) {
// expression divided by operators
if (expChars[i] == ExpressionCalculator.EQUAL || expChars[i] == ExpressionCalculator.PLUS || expChars[i] == ExpressionCalculator.SUBTRACT || expChars[i] == ExpressionCalculator.MULTIPLE || expChars[i] == ExpressionCalculator.DIVIDE) {
// paste digits of number between each two segments
for (int j = flg; j < i; j++) {
sb.append(expChars[j]);
flg = i + 1;
}
// sucess divide a unit
explist.add(sb.toString());
// expression end with "=", paste last digits of number
if (expChars[i] != ExpressionCalculator.EQUAL) {
explist.add(String.valueOf(expChars[i]));
}
sb = new StringBuffer();
}
}
// calculate this expression units
return ExpressionCalculator.calculate(explist);
}
/**
* calculate all units.
* @param explist
* @return
*/
public static String calculate(ArrayList explist) {
do {
int operatorIndex = -1; // operator location
// count operator, first multiple and divide
if (explist.indexOf(ExpressionCalculator.PLUS_STR) > 0 && explist.indexOf(ExpressionCalculator.DIVIDE_STR) > 0) { // both
operatorIndex = Math.min(explist.indexOf(ExpressionCalculator.PLUS_STR), explist.indexOf(ExpressionCalculator.DIVIDE_STR));
} else if ((explist.indexOf(ExpressionCalculator.PLUS_STR) > 0 && explist.indexOf(ExpressionCalculator.DIVIDE_STR) < 0)) { // multiple only
operatorIndex = explist.indexOf(ExpressionCalculator.PLUS_STR);
} else if ((explist.indexOf(ExpressionCalculator.PLUS_STR) < 0 && explist.indexOf(ExpressionCalculator.DIVIDE_STR) > 0)) { // divide only
operatorIndex = explist.indexOf(ExpressionCalculator.DIVIDE_STR);
}
// finish counting multiple or divide operator
if (operatorIndex > 0) { // do have multiple or divide operator
explist = execute(explist, operatorIndex); // multiple or divide operation
continue; // continue calculate
}
// no multiple or divide operation, count plus and subtract operator
if (explist.indexOf(ExpressionCalculator.PLUS_STR) > 0 && explist.indexOf(ExpressionCalculator.SUBTRACT_STR) > 0) { // both
operatorIndex = Math.min(explist.indexOf(ExpressionCalculator.PLUS_STR), explist.indexOf(ExpressionCalculator.SUBTRACT_STR));
} else if ((explist.indexOf(ExpressionCalculator.PLUS_STR) > 0 && explist.indexOf(ExpressionCalculator.SUBTRACT_STR) < 0)) { // plus only
operatorIndex = explist.indexOf(ExpressionCalculator.PLUS_STR);
} else if ((explist.indexOf(ExpressionCalculator.PLUS_STR) < 0 && explist.indexOf(ExpressionCalculator.SUBTRACT_STR) > 0)) { // substract only
operatorIndex = explist.indexOf(ExpressionCalculator.SUBTRACT_STR);
}
// finish counting plus or subtract operator
if (operatorIndex > 0) { // do have plus or subtract operator
explist = execute(explist, operatorIndex); // plus or subtract operation
continue; // continue calculate
}
// no operators
return explist.get(0).toString(); // return result
} while(true);
}
/**
* calculate two units.
* @param explist
* @return
*/
public static ArrayList execute(ArrayList explist, int operatorIndex) {
double unit1 = Double.valueOf(explist.get(operatorIndex - 1).toString()).doubleValue(); // get factor1
double unit2 = Double.valueOf(explist.get(operatorIndex + 1).toString()).doubleValue(); // get factor2
char operator = explist.get(operatorIndex).toString().charAt(0); // get operator
// estimate operator, do calculate, then rebuild arraylist
switch (operator) {
case ExpressionCalculator.PLUS:
explist.set(operatorIndex - 1, String.valueOf(unit1 + unit2));
explist.remove(operatorIndex + 1);
explist.remove(operatorIndex);
break;
case ExpressionCalculator.SUBTRACT:
explist.set(operatorIndex - 1, String.valueOf(unit1 - unit2));
explist.remove(operatorIndex + 1);
explist.remove(operatorIndex );
break;
case ExpressionCalculator.MULTIPLE:
explist.set(operatorIndex - 1, String.valueOf(unit1 * unit2));
explist.remove(operatorIndex + 1);
explist.remove(operatorIndex);
break;
case ExpressionCalculator.DIVIDE:
explist.set(operatorIndex - 1, String.valueOf(unit1 / unit2));
explist.remove(operatorIndex + 1);
explist.remove(operatorIndex);
break;
}
return explist;
}
public static void main(String[] args) {
System.out.print("请输入一个表达式以 \"=\" 结束(中间不加空格, 支持小数, 不支持括号): ");
Scanner sc = new Scanner(System.in); // from console
System.out.println(ExpressionCalculator.interpret(sc.next()));
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -