2010-05-12 56 views
0

目前,我正在尋找一個Java簡單計算器,因爲我需要在我的應用程序中使用。自由和開源(優先)計算器

如果我在Windows,我可以使用啓動

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

然而,在Linux下這樣做沒有可移植的方法。因此,我需要提供一個計算器,這非常類似於Windows的

我碰到http://leepoint.net/notes-java/examples/components/calculator/calc.html

來到但是,它不具備浮點。

您是否遇到過類似於Windows的免費Java計算器?

+0

xcalc是不可接受的?我認爲它在很多Linux發行版上。 – MJB 2010-05-12 18:11:10

+2

投票結束,因爲與編程無關。 – danben 2010-05-12 18:17:34

回答

3

我會說作爲一個通用規則呼籲外部(本機)的應用程序是非常醜陋的,應該避免,除非你真的,真的需要這樣做。即使有一個好的和無處不在的Linux(和OSX等)計算器,你的應用程序將更好地提供它自己的基於Java的計算器,而不依賴於現有的外部計算器,並且擔心例如執行路徑問題。

因此,Sourceforge上有很多Java計算器。這裏有三個讓你開始:

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

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

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

0

代碼還沒有完成, BU t將其幫助您

字符串計算器:


進口的java.util.ArrayList; import java.util.Stack;

/** * * @author米拉德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; 
} 

}

  • 是System.out.print(新數學( sin(tan(cot(90)))));