?? calculator.java
字號:
package com.corba.mnq.tool;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Calculator {
public Calculator() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param exp
* e.g: 2+3*4 or 5<<1<<2 or 5<<(1<<2) support
* (),*,/,%,+,-,<<,>>,&,^,|
* @param isN
* whether is floating calculating or number calculating.
* @return
*/
synchronized public static String calc(String exp, boolean isN) {
String res = "";
exp = exp.replaceAll("\\s", "");
String type_dcl = "\\([^\\(\\)]*\\)";
Pattern pType = Pattern.compile(type_dcl, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(exp);
List s_index = new Vector();
List e_index = new Vector();
List subcontent = new Vector();
while (mType.find()) {
String parenthesis = mType.group();
String num = handleParenthesis(parenthesis, isN);
s_index.add(new Integer(mType.start()));
e_index.add(new Integer(mType.end()));
subcontent.add(num);
}
boolean not_finished = false;
String exp_no_par = "";
int first = 0;
for (int i = 0; i < s_index.size(); i++) {
exp_no_par += exp.substring(first, ((Integer) s_index.get(i))
.intValue())
+ (String) subcontent.get(i);
first = ((Integer) e_index.get(i)).intValue();
not_finished = true;
}
exp_no_par += exp.substring(first);
if (not_finished) {
exp_no_par = calc(exp_no_par, isN);
}
res = handleParenthesis(exp_no_par, isN);
return res;
}
private static String handleParenthesis(String parenthesis, boolean isN) {
if (parenthesis.indexOf("(") != -1)
parenthesis = parenthesis.substring(1, parenthesis.length() - 1);
parenthesis = handleMulDiv(parenthesis, isN);
parenthesis = handlePlusMinus(parenthesis, isN);
parenthesis = handleLR(parenthesis, isN);
parenthesis = handleAnd(parenthesis, isN);
parenthesis = handleXor(parenthesis, isN);
parenthesis = handleOr(parenthesis, isN);
return parenthesis;
}
private static String handleMulDiv(String md, boolean isN) {
// group 1 -- left, 2 -- op, 3 -- right
String type_dcl;
if (isN)
type_dcl = "(\\d+)([*/%])(\\d+)";
else
type_dcl = "(\\d+\\.?\\d*)([*/%])(\\d+\\.?\\d*)";
Pattern pType = Pattern.compile(type_dcl, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(md);
boolean cont = false;
while (mType.find()) {
String op = mType.group(2);
String num = "";
if (isN) {
int left = 0;
int right = 0;
try {
left = Integer.parseInt(mType.group(1));
right = Integer.parseInt(mType.group(3));
} catch (Exception e) {
e.printStackTrace();
}
if (op.equals("*")) {
num = Long.toString(left * right);
} else if (op.equals("/")) {
num = Long.toString(left / right);
} else {
num = Long.toString(left % right);
}
} else {
double left = 0;
double right = 0;
try {
left = Double.parseDouble(mType.group(1));
right = Double.parseDouble(mType.group(3));
} catch (Exception e) {
e.printStackTrace();
}
if (op.equals("*")) {
num = Double.toString(left * right);
} else if (op.equals("/")) {
num = Double.toString(left / right);
} else {
num = Double.toString(left % right);
}
}
md = mType.replaceFirst(num);
cont = true;
break;
}
if (cont)
md = handleMulDiv(md, isN);
return md;
}
private static String handlePlusMinus(String pm, boolean isN) {
String ori;
do {
ori = pm;
pm = pm.replaceAll("--", "+");
pm = pm.replaceAll("\\+\\+", "+");
pm = pm.replaceAll("\\+-", "-");
pm = pm.replaceAll("-\\+", "-");
} while (!ori.equals(pm));
// group 1 -- left, 2 -- op, 3 -- right
// String type_dcl = "((?>[-\\+])?\\d+)([-\\+])(\\d+)";
String type_dcl;
if (isN)
type_dcl = "(-?\\d+)([-\\+])(\\d+)";
else
type_dcl = "(-?\\d+\\.?\\d*)([-\\+])(\\d+\\.?\\d*)";
Pattern pType = Pattern.compile(type_dcl, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(ori);
boolean cont = false;
while (mType.find()) {
String op = mType.group(2);
String num = "";
if (isN) {
int left = 0;
int right = 0;
try {
left = Integer.parseInt(mType.group(1));
right = Integer.parseInt(mType.group(3));
} catch (Exception e) {
e.printStackTrace();
}
if (op.equals("+")) {
num = Long.toString(left + right);
} else {
num = Long.toString(left - right);
}
} else {
double left = 0;
double right = 0;
try {
left = Double.parseDouble(mType.group(1));
right = Double.parseDouble(mType.group(3));
} catch (Exception e) {
e.printStackTrace();
}
if (op.equals("+")) {
num = Double.toString(left + right);
} else {
num = Double.toString(left - right);
}
}
pm = mType.replaceFirst(num);
cont = true;
break;
}
if (cont)
pm = handlePlusMinus(pm, isN);
return pm;
}
private static String handleLR(String lr, boolean isN) {
// group 1 -- left, 2 -- op, 3 -- right
// String type_dcl = "((?>[-\\+])?\\d+)([-\\+])(\\d+)";
String type_dcl;
if (isN)
type_dcl = "(-?\\d+)((?>\\>\\>)|(?>\\<\\<))(\\d+)";
else
return lr;
Pattern pType = Pattern.compile(type_dcl, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(lr);
boolean cont = false;
while (mType.find()) {
String op = mType.group(2);
String num = "";
if (isN) {
int left = 0;
int right = 0;
try {
left = Integer.parseInt(mType.group(1));
right = Integer.parseInt(mType.group(3));
} catch (Exception e) {
e.printStackTrace();
}
if (op.equals(">>")) {
num = Long.toString(left >> right);
} else {
num = Long.toString(left << right);
}
}
lr = mType.replaceFirst(num);
cont = true;
break;
}
if (cont)
lr = handleLR(lr, isN);
return lr;
}
private static String handleAnd(String and, boolean isN) {
// group 1 -- left, 2 -- right
// String type_dcl = "((?>[-\\+])?\\d+)([-\\+])(\\d+)";
String type_dcl;
if (isN)
type_dcl = "(-?\\d+)\\&(\\d+)";
else
return and;
Pattern pType = Pattern.compile(type_dcl, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(and);
boolean cont = false;
while (mType.find()) {
String num = "";
if (isN) {
int left = 0;
int right = 0;
try {
left = Integer.parseInt(mType.group(1));
right = Integer.parseInt(mType.group(2));
} catch (Exception e) {
e.printStackTrace();
}
num = Long.toString(left & right);
}
and = mType.replaceFirst(num);
cont = true;
break;
}
if (cont)
and = handleLR(and, isN);
return and;
}
private static String handleXor(String xor, boolean isN) {
// group 1 -- left, 2 -- right
// String type_dcl = "((?>[-\\+])?\\d+)([-\\+])(\\d+)";
String type_dcl;
if (isN)
type_dcl = "(-?\\d+)\\^(\\d+)";
else
return xor;
Pattern pType = Pattern.compile(type_dcl, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(xor);
boolean cont = false;
while (mType.find()) {
String num = "";
if (isN) {
int left = 0;
int right = 0;
try {
left = Integer.parseInt(mType.group(1));
right = Integer.parseInt(mType.group(2));
} catch (Exception e) {
e.printStackTrace();
}
num = Long.toString(left ^ right);
}
xor = mType.replaceFirst(num);
cont = true;
break;
}
if (cont)
xor = handleLR(xor, isN);
return xor;
}
private static String handleOr(String or, boolean isN) {
// group 1 -- left, 2 -- right
// String type_dcl = "((?>[-\\+])?\\d+)([-\\+])(\\d+)";
String type_dcl;
if (isN)
type_dcl = "(-?\\d+)\\|(\\d+)";
else
return or;
Pattern pType = Pattern.compile(type_dcl, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(or);
boolean cont = false;
while (mType.find()) {
String num = "";
if (isN) {
int left = 0;
int right = 0;
try {
left = Integer.parseInt(mType.group(1));
right = Integer.parseInt(mType.group(2));
} catch (Exception e) {
e.printStackTrace();
}
num = Long.toString(left | right);
}
or = mType.replaceFirst(num);
cont = true;
break;
}
if (cont)
or = handleLR(or, isN);
return or;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -