代碼還沒有完成, 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)))));
xcalc是不可接受的?我認爲它在很多Linux發行版上。 – MJB 2010-05-12 18:11:10
投票結束,因爲與編程無關。 – danben 2010-05-12 18:17:34