您的位置:首页 > 编程语言 > Java开发

[代码记录]java简单计算器的操作类函数封装

2014-12-28 16:26 387 查看
只是为了记录..

class Utils {
final static String FUNC = "sqrt|cos|sin|tan|sinh|tanh|cosh";

/*
* 验证表达式是否合法
*/
public static boolean sync_expression(String s) {
// 简单起见,此处尚未对表达式进行检验,请严格输入表达式;
return true;
}

/*
* 对表达式的三角函数进行处理
*/
public static String match_expression(String s) {
// String exp = "";
Pattern p = Pattern.compile("((" + FUNC + ")\\([^\\)]*\\))");

Matcher m = p.matcher(s);
java.util.List<String> result = new ArrayList<String>();
while (m.find()) {
result.add(m.group());
}

for (String s1 : result) {
String res = match_expression_2(s1);
System.out.println("match_expression:" + res);
if (res != null) {
if (s1.contains("cosh")) {
s = s.replace(
s1,
String.valueOf(Math.cosh(Double.parseDouble(res)
* Math.PI / 180)));
System.out.println(s);
} else if (s1.contains("cos")) {
s = s.replace(
s1,
String.valueOf(Math.cos(Double.parseDouble(res)
* Math.PI / 180)));
System.out.println(s);
}
if (s1.contains("sinh")) {
s = s.replace(
s1,
String.valueOf(Math.sinh(Double.parseDouble(res)
* Math.PI / 180)));
System.out.println(s);

} else if (s1.contains("sin")) {
s = s.replace(
s1,
String.valueOf(Math.sin(Double.parseDouble(res)
* Math.PI / 180)));
System.out.println(s);
}
if (s1.contains("tanh")) {
s = s.replace(
s1,
String.valueOf(Math.tanh(Double.parseDouble(res)
* Math.PI / 180)));
System.out.println(s);

} else if (s1.contains("tan")) {
s = s.replace(
s1,
String.valueOf(Math.tan(Double.parseDouble(res)
* Math.PI / 180)));
System.out.println(s);
}
if (s1.contains("sqrt")) {
s = s.replace(s1,
String.valueOf(Math.sqrt(Double.parseDouble(res))));
System.out.println(String.valueOf(Math.sqrt(Double
.parseDouble(res))));

}

}
}
return s;
}

// 弧度=角度乘以π后再除以180
// 角度=弧度除以π再乘以180
public static String match_expression_2(String s) {
Pattern p = Pattern.compile("(\\([^\\)]*\\))");

Matcher m = p.matcher(s);
java.util.List<String> result = new ArrayList<String>();
while (m.find()) {
result.add(m.group());
}
for (String s1 : result) {
System.out.println("match_expression_2:" + s1);
return express(s1);
}
return null;
}

public static void clearString(JTextField field) {
field.setText("");
}

/**
* 从光标添加一个字符
*
* @param field
* @param addstr
*/
public static void addString(JTextField field, String addstr) {
if (addstr.length() != 0) {
int position = field.getCaretPosition();
String text = field.getText();
text = text.substring(0, position) + addstr
+ text.substring(position, text.length());
field.setText(text);
field.setCaretPosition(text.length());
}
}

/**
* 从光标删除一个字符
*
* @param field
*/
public static void removeString(JTextField field) {
if (field.getText().toString().length() != 0) {
int position = field.getCaretPosition();
String text = field.getText();
text = text.substring(0, position - 1)
+ text.substring(position, text.length());
field.setText(text);
field.setCaretPosition(text.length());
}
}

/**
* 计算表达式
*
* @param express
* @return
*/
public static String express(String express) {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
try {
return engine.eval(express).toString();
} catch (ScriptException e) {
// 不对异常进行处理..
e.printStackTrace();
System.out.println(e.getMessage());
return "error";
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java