目前,我正在寻找一个Java Simple Calculator,因为我需要在我的应用程序中使用它。
如果我在Windows中,我可以使用
启动Runtime.getRuntime().exec("calc");
但是,在Linux中没有可移植的方法。因此,我需要提供一个与Windows'
非常相似的计算器我遇到了http://leepoint.net/notes-java/examples/components/calculator/calc.html
但是,它没有浮点。
您是否遇到过与Windows类似的免费Java计算器?
答案 0 :(得分:3)
我会说,作为一般规则,调用外部(本机)应用程序非常难看,应该避免,除非你真的,真的需要这样做。即使有一个优秀且无处不在的Linux(和OSX等)计算器,您的应用程序也会更好地提供自己的基于Java的计算器,而不依赖于现有的外部计算器,并担心例如执行路径问题。
有了这个,Sourceforge上有很多Java计算器。这里有三个让你入门:
http://sourceforge.net/projects/jcalcadvance/
答案 1 :(得分:0)
我使用以下源代码:
http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/gui/portfolio/Calc.java?revision=1.1&view=markup http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/gui/portfolio/CalcLogic.java?revision=1.1&view=markup
答案 2 :(得分:0)
代码未完成,但它可以帮助您
字符串计算器:
import java.util.ArrayList; import java.util.Stack;
/ ** * * @author Milad Hasanpour * /
公共决赛班数学{
private String[] Split = {"0"};
private double a = 0, b = 1, answer;
private double[] Numbers = {0};
public String print ,data;
public String Maths(String data) {
this.data=data;
}
public String getAnswer(String data) {
print = " {\n";
return findBracketPos(data);
}
private String findBracketPos(String data) {
int pos = 0;
if (-1 != (pos = data.indexOf("("))) {
String subexp = extractFromBraces(data, pos);
print += " " + data + "\n";
data = data.replace("(" + subexp + ")", findBracketPos(subexp));
}
print += " " + data + "\n";
return spliter(data, "\\+");
}
private static String extractFromBraces(String data, int pos) {//bracket
int braceDepth = 1;
String subexp = "";
for (int i = pos + 1; i < data.length(); i++) {
switch (data.charAt(i)) {
case '(':
braceDepth++;
subexp += "(";
break;
case ')':
if (--braceDepth != 0) {
subexp += ")";
}
break;
default:
if (braceDepth > 0) {
subexp += data.charAt(i);
}
}
if (braceDepth == 0 && !subexp.equals("")) {
return subexp;
}
}
return "Failure!";
}
private String spliter(String Datas, String Parameter) {
String[] Data = Datas.split(Parameter);
ArrayList<String> list = new ArrayList<>();
for (String data : Data) {
try {
list.add(Solve(data, ParameterFinder(data)));
} catch (Exception e) {
switch (Parameter) {
case "\\+":
list.add(spliter(data, "-"));
break;
case "-":
list.add(spliter(data, "/"));
break;
case "/":
list.add(spliter(data, "\\*"));
break;
case "\\*":
list.add(spliter(data, "%"));
break;
case "%":
list.add(spliter(data, "\\^"));
break;
case "\\^":
list.add(Solve(data, "\\^"));
break;
}
}
}
String add = "";
int l = 0;
while (l < list.size() - 1) {
add += list.get(l) + Parameter.replaceAll("\\\\", "");
l++;
}
add += list.get(list.size() - 1);
return Solve(add, Parameter);
}
private String Solve(String data, String Parameter) {
if (isNumeric(data)) {
return data;
}
Numbers = castS_D(Split = data.split(Parameter));
try {
a = Math.toRadians(Double.valueOf(Split[1]));
} catch (NumberFormatException e) {
a = 1;
}
try {
b = Double.valueOf(Split[0]);
} catch (NumberFormatException e) {
b = 1;
}
switch (Parameter) {
case "\\+":
answer = 0;
for (double Num : Numbers) {
answer += Num;
}
break;
case "-":
if (Numbers.length == 1) {
return Numbers[0] + "";
}
int i = 1;
if ((answer = Numbers[0]) == 0) {
answer = Numbers[1];
i = 2;
}
for (int j = i; j < Numbers.length; j++) {
answer -= Numbers[i];
}
break;
case "\\*":
answer = 1;
for (double Num : Numbers) {
answer *= Num;
}
break;
case "/":
answer = Numbers[0];
for (int j = 1; j < Numbers.length; j++) {
if (Numbers.length > 1) {
answer /= Numbers[j];
}
}
break;
case "%":
answer = 100;
for (double Num : Numbers) {
answer = Num * (answer / 100);
}
break;
case "\\^":
answer = Numbers[Numbers.length - 1];
for (int j = Numbers.length - 2; j > -1; j--) {
if (Numbers.length > 1) {
answer = Math.pow(Numbers[j], answer);
}
}
break;
case "v":
answer = b * Math.sqrt(Numbers[1]);
break;
case "pi":
answer = Math.PI;
break;
case "ei":
answer = Math.E;
break;
case "sin":
answer = b * Math.sin(a);
break;
case "cos":
answer = b * Math.cos(a);
break;
case "tan":
answer = b * Math.tan(a);
break;
case "cot":
answer = b * (1 / Math.tan(a));
break;
case "sinh":
answer = b * Math.sinh(a);
break;
case "cosh":
answer = b * Math.cosh(a);
break;
case "tanh":
answer = b * Math.tanh(a);
break;
case "coth":
answer = b * (1 / Math.tanh(a));
break;
case "asin":
answer = b * Math.asin(a);
break;
case "atan":
answer = b * Math.atan(a);
break;
case "acos":
answer = b;
break;
case "acot":
answer = b * (1 / Math.atan(a));
break;
case "sec":
answer = b * (Math.sqrt(Math.pow(Math.tan(a) + 1, 2)));
break;
case "exp":
answer = b * Math.exp(Math.toDegrees(a));
break;
case "abs":
answer = b * Math.abs(Math.toDegrees(a));
break;
case "atann":
answer = Math.atan2(Math.toRadians(b), a);
break;
case "cbrt":
answer = b * Math.cbrt(a);
break;
case "ceil":
answer = b * Math.ceil(a);
break;
case "hypot":
answer = Math.hypot(b, Math.toDegrees(a));
break;
case "rad":
answer = b * a;
break;
case "deg":
answer = b * Math.toDegrees(Math.toDegrees(a));
break;
case "rou":
answer = b * Math.round(Math.toDegrees(a));
break;
case "ran":
answer = b * Math.random() * Math.toDegrees(a);
break;
case "ln":
answer = b * Math.log(Math.toDegrees(a));
break;
case "log":
answer = b * Math.log10(Math.toDegrees(a));
break;
case "~":
answer = b * Math.pow(10, Math.toDegrees(a));
break;
}
return answer + "";
}
private double[] castS_D(String[] Split) {
int i = 0;
if (Split[0].isEmpty() || Split[0].contains("�")) {
i = 1;
}
Numbers = new double[Split.length];
for (int j = i; j < Split.length; j++) {
Numbers[j] = Double.valueOf(Split[j]);
}
return Numbers;
}
private String ParameterFinder(String Data) {
String[] data = {"+", "-", "*", "/", "^", "v", "sin", "cos", "tan", "cot", "sinh", "cosh", "tanh", "coth", "asin", "atan", "sec", "?", "int", "%", "exp", "abs", "atann", "cbrt", "hypot", "rad", "deg", "rou", "ran", "ln", "log", "plot", "pi", "ei", "~"};
String find = "";
for (String data1 : data) {
if (Data.contains(data[0]) || Data.contains(data[2]) || Data.contains(data[4])) {
find = "\\" + data1;
}
if (Data.contains(data1)) {
find = data1;
}
}
return find;
}
private boolean isNumeric(String minData) {
try {
Double D = Double.valueOf(minData);
} catch (NumberFormatException e) {
return false;
}
return true;
}
}