📄 zformulaparser.java
字号:
//System.out.println("op1 - push h to stack");
tokenStack.push(h);
} else if ((RPNField.TOK_OPEN_BRACKET == hh._type) || (getFuncNum(hh._func) > getFuncNum(h._func))) {
//System.out.println("op2 - push h to stack");
tokenStack.push(h);
} else {
//System.out.println("op3");
while (!tokenStack.empty() && (RPNField.TOK_OPEN_BRACKET != hh._type) &&
(getFuncNum(hh._func) < getFuncNum(h._func))) {
//System.out.println("adding to RPN: "+((RPNField)tokenStack.peek())._strValue);
_RPNExpression.addElement(tokenStack.pop());
if (tokenStack.empty()) {
break;
}
hh = (RPNField) tokenStack.peek();
}
//System.out.println("pushing h to stack");
tokenStack.push(h);
}
;
break;
case RPNField.TOK_NUMBER:
//System.out.println("num - adding to RPN");
_RPNExpression.addElement(h);
break;
case RPNField.TOK_PARAMETER:
//System.out.println("param - adding to RPN");
_RPNExpression.addElement(h);
break;
case RPNField.TOK_OPEN_BRACKET:
//System.out.println("open - push to stack");
tokenStack.push(h);
break;
case RPNField.TOK_CLOSE_BRACKET:
//System.out.println("found closing bracket");
while (((RPNField) tokenStack.peek())._type != RPNField.TOK_OPEN_BRACKET) {
//System.out.println("adding to RPN"+((RPNField)tokenStack.peek())._strValue);
_RPNExpression.addElement(tokenStack.pop());
//System.out.println("next="+((RPNField)tokenStack.peek())._strValue);
}
//System.out.println("popping (");
tokenStack.pop(); // '(' rauswerfen
break;
}
}
while (!tokenStack.empty()) {
_RPNExpression.addElement(tokenStack.pop());
}
}
/**
*/
protected Function determineFunctionForToken(String token) {
if (null == token) {
return null;
}
for (int i = 0; i < _functions.length; i++) {
if (token.equalsIgnoreCase(_functions[i].getName())) {
return _functions[i];
}
}
return null;
}
/**
*
*/
protected void tokenize() {
boolean type = true;
RPNField rf = null;
do {
rf = getNextMorphem();
if (null == rf) {
_error = ERROR_FORMEVAL;
System.err.println("error: tokenize");
break;
}
if (RPNField.TOK_NUMBER == rf._type) {
_tokenExpression.addElement(rf);
type = false;
} else if (RPNField.TOK_PARAMETER == rf._type) {
_tokenExpression.addElement(rf);
type = false;
} else if (RPNField.TOK_OPEN_BRACKET == rf._type) {
_tokenExpression.addElement(rf);
type = true;
} else if (RPNField.TOK_CLOSE_BRACKET == rf._type) {
_tokenExpression.addElement(rf);
type = false;
} else if (RPNField.TOK_OPERATOR == rf._type) {
if (type && (rf._strValue.equals("-"))) {
//System.out.println("negation!");
rf._strValue = "~";
rf._func = determineFunctionForToken("~");
}
_tokenExpression.addElement(rf);
type = true;
} else if (RPNField.TOK_UNUSED == rf._type) {
_error = ERROR_FORMEVAL;
System.err.println("error: tokenize");
break;
}
;
} while (expression.length() > 0);
}
/**
*
* @param text
*
* @return
*/
Object toObject(String text) {
int i;
ZCellID tl = null;
ZCellID br = null;
String t1 = null;
String t2 = null;
if ((i = text.indexOf(':')) > 0) {
t1 = text.substring(0, i);
t2 = text.substring(i + 1);
} else {
t1 = text;
}
tl = toCellID(t1);
if (t2 != null) {
br = toCellID(t2);
} else {
if ((tl.row == -1) || (tl.col == -1)) {
return null;
}
br = new ZCellID(-1, -1);
}
if ((tl.row != -1) && (tl.col != -1) && (br.row == -1) && (br.col == -1)) {
return new Double(calculator.getValue(tl.row ,tl.col));
} else if ((tl.row != -1) && (tl.col != -1) && (br.row != -1) && (br.col != -1)) {
return new _Cells(tl.row, tl.col, br.row, br.col);
} else if ((tl.row == -1) && (tl.col != -1) && (br.row == -1) && (br.col != -1)) {
return new _Cols(tl.col, br.col);
} else if ((tl.row != -1) && (tl.col == -1) && (br.row != -1) && (br.col == -1)) {
return new _Rows(tl.row, br.row);
} else {
return null;
}
}
/**
*
* @param text
*
* @return
*/
private ZCellID toCellID(String text) {
ZCellID cid = new ZCellID(-1, -1);
String x = "";
for (int i = text.length() - 1; i >= 0; i--) {
if (Character.isDigit(text.charAt(i))) {
x = text.charAt(i) + x;
} else {
break;
}
}
if (x.length() > 0) {
cid.row = (Integer.valueOf(x)).intValue();
}
x = text.substring(0, text.length() - x.length());
if (x.length() > 0) {
cid.col = ZDefaultSheet.intId(x);
}
return cid;
}
/**
*
* @param text
*
* @return
*/
private String trimToCellsStringID(String text) {
String result = "";
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if ((ch == ':') || Character.isDigit(ch) || Character.isLetter(ch)) {
result += ch;
} else {
break;
}
}
return result;
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class _Rows {
int top;
int bottom;
_Rows(int t, int b) {
top = t;
bottom = b;
}
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class _Cols {
int left;
int right;
_Cols(int l, int r) {
left = l;
right = r;
}
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class _Cells {
String id;
int left;
int top;
int right;
int bottom;
Vector values ;
_Cells(int t, int l, int b, int r) {
left = l;
top = t;
right = r;
bottom = b;
}
public double sum()
{
double val =0.0f;
for(int t = top;t<=bottom;t ++)
for(int l = left;l<= right;l++)
val += calculator.getValue(t,l) ;
return val;
}
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class _Cell {
int row;
int col;
_Cell(int r, int c) {
row = r;
col = c;
}
}
/**
*
*/
protected static abstract class Function {
/**
*
* @return
*/
public abstract String getName();
/**
*
* @param stack
*
* @return
*/
public abstract double calculate(Stack stack) throws Exception;
}
protected class RPNField {
public static final int TOK_UNUSED = 0;
public static final int TOK_NUMBER = 1;
public static final int TOK_OPERATOR = 2;
public static final int TOK_OPEN_BRACKET = 3;
public static final int TOK_CLOSE_BRACKET = 4;
public static final int TOK_PARAMETER = 5;
public int _type = TOK_UNUSED; // Type of this element
public Function _func = null; // function for this RPN element
public String _strValue = null;
public double _doubleValue; // numeric value
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -