📄 calculatorjava.java
字号:
package DBListen.inServer;
import java.util.Hashtable;
import java.util.Vector;
public class CalculatorJava {
private int nextElement;
private char[] elements = null;
private Hashtable key_value = null;
private Vector stack = null;
private boolean isOver = false;
private String errMsg = null;
private Double cussVariable = null;
private String Exp = "";
private String excMsg = null;
private String[] keyWords="<,>,=,<=,>=,<>,&,|,IIF,TRUE,FALSE".split(",");
private String keyWords2 = "<>=:?&|";
private String keyWord3 = "+-*/<>&|=";
private String keyWord4 = "+-*/<>=&|IIF:1.0IIF:0.0TRUEFALSE";
/**构造 CalculatorJava 实例
* */
public CalculatorJava() {
}
/**
* 构造 CalculatorJava 实例 并设置公式
* */
public CalculatorJava(String src) {
this.Exp = src.trim();
}
/**
* 设置公式,计算公式的结果
* 如果没有设置公式,将报告"公式没有设置"的错误
* @since;
* */
public void setExpressions(String Exp) {
this.Exp = Exp.trim();
}
//初始化
private void initializes() {
this.elements = this.Exp.toCharArray();
nextElement = 0;
this.stack = new Vector();
this.errMsg = "";
excMsg = "";
this.nextElement = 0;
this.isOver = false;
}
// 析构方法
private void to() {
this.elements = null;
this.key_value = null;
this.stack = null;
this.Exp = "";
this.cussVariable = null;
}
/**
* 计算公式并把结果返回,如果产生错误会返回:Float.MAX_VALUE.错误信息通过 showmsg()方法返回
*
*/
public double calculatorRun() {
double rv = 0;
if (this.Exp.length() < 1) {
this.isOver = true;
this.errMsg = "错误:公式未经初始化 "+errMsg;
} else{
this.initializes();
}
try {
while (!isover()) {
this.calculatorEngine();
}
} catch (Exception e) {
e.printStackTrace();
this.errMsg = "发生意想不到的错误,请检查公式 "+errMsg;
}
if(this.cussVariable!=null&&this.cussVariable.isNaN()){
this.errMsg +=this.excMsg;
}
if (!(this.errMsg.length()>0))
rv = this.endValue();
else
rv = Double.MAX_VALUE;
this.to();
return rv;
}
// 返回最终结果
private double endValue() {
double rv = 0;
rv = this.cussVariable.doubleValue();
return rv;
}
// 定义变量
/**
* 定义变量并赋值,如果calculatorRun()发现了没有定义的变量将产生"变量未定义"的错误.
* */
public void addVariable(String key, double v) {
if (this.key_value == null) {
this.key_value = new Hashtable();
}
this.key_value.put(key, new Double(v));
}
/**返回错误信息
* */
public String showmsg() {
return this.errMsg;
}
private boolean isover() throws Exception {
if(this.isOver||this.nextElement >= this.elements.length){
if(this.cussVariable!=null){
boolean isnone = false;
if (this.stack.size() > 0) {
while (!isnone && this.stack.size() > 0) {
String[] stackEle = this.outStack();
stackEle[2] = this.cussVariable.toString();
this.cussVariable = this.clrStack(stackEle);
}
}
}
this.isOver = true;
}
return this.isOver;
}
private void calculatorEngine() {
char tempCh = this.elements[this.nextElement];
/*
* 1.处理字符 包括:变量,IIF ? :
* */
if (Character.isLetterOrDigit(tempCh)) {
if (Character.isDigit(tempCh)) {
this.addDigit();
return;
} else if (Character.isLetter(tempCh)) {
this.transferVariable();
return;
}
} else if (tempCh == '(') {
if (this.cussVariable != null) {
this.isOver = true;
this.errMsg = "\""+ this.elements[this.nextElement<this.elements.length?this.nextElement:this.nextElement-1]+"\" 位置" + this.nextElement + " 语法错误 "+errMsg;
return;
}
this.addDelimit();
return;
} else if (tempCh == ')') {
if (this.cussVariable == null) {
this.isOver = true;
this.errMsg = "\""+ this.elements[this.nextElement<this.elements.length?this.nextElement:this.nextElement-1]+"\" 位置" + this.nextElement + " 语法错误 "+errMsg;
return;
}
try {
this.reDelimit();
} catch (Exception e) {
this.isOver = true;
this.errMsg = "错误:请检查公式 "+errMsg;
}
return;
} else if (tempCh == '+') {
this.addition();
return;
} else if (tempCh == '-') {
this.subtration();
return;
} else if (tempCh == '*') {
this.multiplication();
return;
} else if (tempCh == '/') {
this.division();
return;
} else if (tempCh == ':') {
this.IIFStat();
return;
} else {
isOver = true;
this.errMsg ="\""+ this.elements[this.nextElement<this.elements.length?this.nextElement:this.nextElement-1]+"\" 非法字符 " + this.nextElement + " "+errMsg;
}
}
// '(' 定界符
private void addDelimit() {
String[] stackEle = new String[3];
stackEle[0] = null;
stackEle[1] = "(";
stackEle[2] = null;
this.inStack(stackEle);
this.nextElement++;
}
// ')' 定界符结束
private void reDelimit() throws Exception {
if (this.stack.size() > 0) {
while (true) {
String[] stackEle = this.outStack();
if (stackEle[1] == "(")
break;
else {
stackEle[2] = this.cussVariable.toString();
this.cussVariable = this.clrStack(stackEle);
}
}
} else{
this.outStack();
}
this.nextElement++;
}
// '+' 加法
private void addition() {
String[] stackElement = new String[3];
if (this.cussVariable == null) {
this.isOver = true;
this.errMsg = "\""+ this.elements[this.nextElement<this.elements.length?this.nextElement:this.nextElement-1]+"\" 位置" + this.nextElement + " 语法错误 "+errMsg;
return;
}
try {
this.adjustLevel("+");
} catch (Exception e) {
this.isOver = true;
return;
}
stackElement[0] = this.cussVariable.toString();
stackElement[1] = "+";
stackElement[2] = null;
this.inStack(stackElement);
this.cussVariable = null;
this.nextElement++;
}
// '-' 减法
private void subtration() {
String[] stackElement = new String[3];
if (this.cussVariable == null) {
this.isOver = true;
this.errMsg = "\""+ this.elements[this.nextElement<this.elements.length?this.nextElement:this.nextElement-1]+"\" 位置" + this.nextElement + " 语法错误 "+errMsg;
return;
}
try {
this.adjustLevel("-");
} catch (Exception e) {
this.isOver = true;
return;
}
stackElement[0] = this.cussVariable.toString();
stackElement[1] = "-";
stackElement[2] = null;
this.inStack(stackElement);
this.cussVariable = null;
this.nextElement++;
}
// '*' 乘法
private void multiplication() {
String[] stackElement = new String[3];
if (this.cussVariable == null) {
this.isOver = true;
this.errMsg = "\""+ this.elements[this.nextElement<this.elements.length?this.nextElement:this.nextElement-1]+"\" 位置" + this.nextElement + " 语法错误 "+errMsg;
return;
}
try {
this.adjustLevel("*");
} catch (Exception e) {
this.isOver = true;
return;
}
stackElement[0] = this.cussVariable.toString();
stackElement[1] = "*";
stackElement[2] = null;
this.inStack(stackElement);
this.cussVariable = null;
this.nextElement++;
}
// '/' 除法
private void division() {
String[] stackElement = new String[3];
if (this.cussVariable == null) {
this.isOver = true;
this.errMsg = "\""+ this.elements[this.nextElement<this.elements.length?this.nextElement:this.nextElement-1]+"\" 位置" + this.nextElement + " 语法错误 "+errMsg;
return;
}
try {
this.adjustLevel("/");
} catch (Exception e) {
this.isOver = true;
return;
}
stackElement[0] = this.cussVariable.toString();
stackElement[1] = "/";
stackElement[2] = null;
this.inStack(stackElement);
this.cussVariable = null;
this.nextElement++;
}
// 关系运算
public void connOperation(String ch){
String[] stackElement = new String[3];
if("IIF".equals(ch)){
stackElement[0] = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -