📄 calutil.java
字号:
/*
* 创建日期 2005-12-25
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package 计算器2;
/**
* @author T~Master
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.math.BigDecimal;
public class CalUtil {
public static final int MAX_NUMBER_LENGTH = 32;
public static final String strOperator1 = "+-*/";
// error message
public static final String Err_MSG[] = { "Unknown error!",
"function's result is not defined.",
"cannot divided by zero.",
"invalid value." };
public static final String Help_VerDialog_Info = "Calculator V0.1\nbe created following Microsoft's calculator\n<html>by <B>syakun</B></html>";
public static final String Help_VerDialog_Title = "Version Information";
public static final String Help_TopicDialog_Info = "Please read Microsoft's calculator help files.";
public static final String Help_TopicDialog_Title = "Topic";
public static boolean isOperator1(String strActionCmd) {
if (strOperator1.indexOf(strActionCmd) < 0)
return false;
else
return true;
}
public static boolean isOperator2(String strActionCmd) {
if (strActionCmd.equals("sqrt") || strActionCmd.equals("%")
|| strActionCmd.equals("+/-") || strActionCmd.equals("1/x"))
return true;
else
return false;
}
public static boolean isMemOp(String strActionCmd) {
if (strActionCmd.equals("MC") || strActionCmd.equals("MR")
|| strActionCmd.equals("MS") || strActionCmd.equals("M+"))
return true;
else
return false;
}
public static String calculate(String op1, String op, String op2) {
BigDecimal Op1 = new BigDecimal(op1);
BigDecimal Op2 = new BigDecimal(op2);
BigDecimal TempResult;
char chrOp = op.charAt(0);
switch (chrOp) {
case '+':
TempResult = Op1.add(Op2);
break;
case '-':
TempResult = Op1.subtract(Op2);
break;
case '*':
TempResult = Op1.multiply(Op2);
break;
case '/':
if (Op1.doubleValue() == 0 && Op2.doubleValue() == 0)
return "ERR1";
else if (Op2.doubleValue() == 0)
return "ERR2";
TempResult = Op1.divide(Op2, MAX_NUMBER_LENGTH * 2,
BigDecimal.ROUND_HALF_DOWN);
break;
case '%':
TempResult = (Op1.divide(new BigDecimal("100"), Op1.scale() + 2))
.multiply(Op2);
break;
default:
return "ERR0";
}
return setResultFormat(TempResult.toString());
}
public static String calculate(String op1, String op) {// sqrt, +/-, 1/x
BigDecimal Op1 = new BigDecimal(op1);
BigDecimal TempResult = null;
if (op.equals("sqrt")) {
if (Op1.doubleValue() < 0)
return "ERR3";
TempResult = new BigDecimal(String.valueOf(Math.sqrt(Op1
.doubleValue())));
} else if (op.equals("+/-")) {
Op1 = Op1.negate();
TempResult = Op1;
} else if (op.equals("1/x")) {
if (Op1.doubleValue() == 0)
return "ERR2";
//**********************
//modified on 2005-11-12 by syakun
//TempResult = (new BigDecimal("1")).divide(Op1, 5);
TempResult = (new BigDecimal("1")).divide(Op1, MAX_NUMBER_LENGTH * 2,BigDecimal.ROUND_HALF_DOWN);
//**********************
} else {
return "ERR0";
}
return setResultFormat(TempResult.toString());
}
public static String setResultFormat(String strResult) {
// clear the "0" at the back of dot(".")
int m = strResult.indexOf(".");
if (m >= 0) {
int tail = strResult.length() - 1;
StringBuffer sb = new StringBuffer(strResult);
while (tail > m && strResult.charAt(tail) == '0') {
sb.setLength(sb.length() - 1);
tail--;
}
strResult = sb.toString();
}
if (strResult.length() > MAX_NUMBER_LENGTH) {
double db = Double.parseDouble(strResult);
strResult = String.valueOf(db);
}
return strResult;
}
public static void show_error_msg(CalTextField txf, String strMsg) {
txf.display(strMsg);
}
public static void show_error_msg(CalTextField txf, int i) {
if (i < 0 || i > Err_MSG.length - 1)
txf.display("error in module show_error_msg");
else
txf.display(Err_MSG[i]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -