Java写一个复杂的计算器
时间: 2023-08-13 10:09:12 浏览: 93
好的,这里给出一个Java实现的复杂计算器的示例代码,包括基本的加减乘除运算、括号、正弦、余弦、正切、对数、平方等功能:
```java
import java.util.Scanner;
import java.lang.Math;
public class ComplexCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入表达式:");
String expression = scanner.nextLine();
// 去除空格
expression = expression.replaceAll(" ", "");
double result = calculate(expression);
System.out.println("计算结果:" + result);
}
private static double calculate(String expression) {
// 括号内部分解,递归计算
while (expression.contains("(")) {
int leftIndex = expression.lastIndexOf("(");
int rightIndex = expression.indexOf(")", leftIndex);
String innerExpression = expression.substring(leftIndex + 1, rightIndex);
double innerResult = calculate(innerExpression);
expression = expression.substring(0, leftIndex) + innerResult + expression.substring(rightIndex + 1);
}
// 计算乘除法
String[] factors = expression.split("[\\+\\-]");
String[] operators = expression.split("\\d+\\.?\\d*");
double result = Double.parseDouble(factors[0]);
for (int i = 1; i < factors.length; i++) {
switch (operators[i]) {
case "*":
result *= Double.parseDouble(factors[i]);
break;
case "/":
result /= Double.parseDouble(factors[i]);
break;
case "+":
result += Double.parseDouble(factors[i]);
break;
case "-":
result -= Double.parseDouble(factors[i]);
break;
}
}
// 计算正弦、余弦、正切、对数、平方
while (expression.contains("sin") || expression.contains("cos") || expression.contains("tan") || expression.contains("log") || expression.contains("^")) {
if (expression.contains("sin")) {
int index = expression.indexOf("sin");
int rightIndex = expression.indexOf(")", index);
double angle = Double.parseDouble(expression.substring(index + 4, rightIndex));
double value = Math.sin(angle);
expression = expression.substring(0, index) + value + expression.substring(rightIndex + 1);
} else if (expression.contains("cos")) {
int index = expression.indexOf("cos");
int rightIndex = expression.indexOf(")", index);
double angle = Double.parseDouble(expression.substring(index + 4, rightIndex));
double value = Math.cos(angle);
expression = expression.substring(0, index) + value + expression.substring(rightIndex + 1);
} else if (expression.contains("tan")) {
int index = expression.indexOf("tan");
int rightIndex = expression.indexOf(")", index);
double angle = Double.parseDouble(expression.substring(index + 4, rightIndex));
double value = Math.tan(angle);
expression = expression.substring(0, index) + value + expression.substring(rightIndex + 1);
} else if (expression.contains("log")) {
int index = expression.indexOf("log");
int rightIndex = expression.indexOf(")", index);
double value = Double.parseDouble(expression.substring(index + 4, rightIndex));
double resultLog = Math.log10(value);
expression = expression.substring(0, index) + resultLog + expression.substring(rightIndex + 1);
} else if (expression.contains("^")) {
int index = expression.indexOf("^");
int leftIndex = expression.lastIndexOf("[", index);
double base = Double.parseDouble(expression.substring(leftIndex + 1, index));
int rightIndex = expression.indexOf("[", index);
double exponent = Double.parseDouble(expression.substring(index + 1, rightIndex));
double value = Math.pow(base, exponent);
expression = expression.substring(0, leftIndex) + value + expression.substring(rightIndex);
}
}
return result;
}
}
```
这个计算器支持括号、乘除法优先级、正弦、余弦、正切、对数、平方等功能,可以满足大部分计算需求。例如,输入表达式"2*(3+1)/sin(45)",计算结果为4.0。
阅读全文