开发者

Free and Open Source (Preferable) Calculator [closed]

开发者 https://www.devze.com 2022-12-29 21:57 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
开发者_开发问答

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question

Currently, I am looking for a Java Simple Calculator, as I need to use in my application.

If I am in Windows, I can just launch using

Runtime.getRuntime().exec("calc");

However, there is no portable way to do so in Linux. Hence, I need to supply a calculator, which is very similar to Windows'

I came across http://leepoint.net/notes-java/examples/components/calculator/calc.html

However, it doesn't have floating point.

Do you come across any free Java calculator similar to Windows'?


I would say as a general rule calling out to an external (native) app is pretty ugly, and should be avoided unless you really, really need to do that. Even if there is a good and ubiquitous Linux (and OSX, etc.) calculator, your application will be better of supplying its own Java-based calculator and not relying on an external one existing and worrying about e.g. execution path issues.

With that, there are plenty of Java calculators on Sourceforge. Here are three to get you started:

http://sourceforge.net/projects/jcalcadvance/

http://sourceforge.net/projects/jscicalc/

http://sourceforge.net/projects/jcalculator/


I use the following source code :

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


code is not completed , but it help you

String Calculator :


import java.util.ArrayList; import java.util.Stack;

/** * * @author Milad Hasanpour */

public final class Maths {

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;
}

}

  • System.out.print(new Math( sin( tan( cot (90)))));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号