用java设计一个计算器应用程序,用于进行简单的加减乘除四则运算。要求能够对输入的算式判断运算符优先级,允许在表达式中出现常用的数学函数,如绝对值、取整、三角函数、倒数、平方根、平方、立方等。
时间: 2023-07-04 13:04:46 浏览: 97
以下是一个简单的Java计算器应用程序,支持加、减、乘、除、取模、绝对值、取整、三角函数、倒数、平方根、平方、立方等操作:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个表达式:");
String expression = scanner.nextLine();
double result = calculateExpression(expression);
System.out.println("计算结果为:" + result);
}
public static double calculateExpression(String expression) {
// 去除空格
expression = expression.replaceAll("\\s+", "");
// 处理括号
while (expression.contains("(")) {
int leftIndex = expression.lastIndexOf("(");
int rightIndex = expression.indexOf(")", leftIndex);
double value = calculateExpression(expression.substring(leftIndex + 1, rightIndex));
expression = expression.substring(0, leftIndex) + value + expression.substring(rightIndex + 1);
}
// 处理绝对值
while (expression.contains("|")) {
int leftIndex = expression.lastIndexOf("|");
int rightIndex = expression.indexOf("|", leftIndex + 1);
double value = Math.abs(calculateExpression(expression.substring(leftIndex + 1, rightIndex)));
expression = expression.substring(0, leftIndex) + value + expression.substring(rightIndex + 1);
}
// 处理取整
while (expression.contains("floor(")) {
int leftIndex = expression.lastIndexOf("floor(");
int rightIndex = expression.indexOf(")", leftIndex);
double value = Math.floor(calculateExpression(expression.substring(leftIndex + 6, rightIndex)));
expression = expression.substring(0, leftIndex) + value + expression.substring(rightIndex + 1);
}
// 处理三角函数
while (expression.contains("sin(")) {
int leftIndex = expression.lastIndexOf("sin(");
int rightIndex = expression.indexOf(")", leftIndex);
double value = Math.sin(Math.toRadians(calculateExpression(expression.substring(leftIndex + 4, rightIndex))));
expression = expression.substring(0, leftIndex) + value + expression.substring(rightIndex + 1);
}
while (expression.contains("cos(")) {
int leftIndex = expression.lastIndexOf("cos(");
int rightIndex = expression.indexOf(")", leftIndex);
double value = Math.cos(Math.toRadians(calculateExpression(expression.substring(leftIndex + 4, rightIndex))));
expression = expression.substring(0, leftIndex) + value + expression.substring(rightIndex + 1);
}
while (expression.contains("tan(")) {
int leftIndex = expression.lastIndexOf("tan(");
int rightIndex = expression.indexOf(")", leftIndex);
double value = Math.tan(Math.toRadians(calculateExpression(expression.substring(leftIndex + 4, rightIndex))));
expression = expression.substring(0, leftIndex) + value + expression.substring(rightIndex + 1);
}
// 处理倒数、平方根、平方、立方
while (expression.contains("^-1")) {
int index = expression.indexOf("^-1");
double value = 1 / Double.parseDouble(expression.substring(0, index));
expression = value + expression.substring(index + 3);
}
while (expression.contains("sqrt(")) {
int leftIndex = expression.lastIndexOf("sqrt(");
int rightIndex = expression.indexOf(")", leftIndex);
double value = Math.sqrt(Double.parseDouble(expression.substring(leftIndex + 5, rightIndex)));
expression = expression.substring(0, leftIndex) + value + expression.substring(rightIndex + 1);
}
while (expression.contains("^2")) {
int index = expression.indexOf("^2");
double value = Math.pow(Double.parseDouble(expression.substring(0, index)), 2);
expression = value + expression.substring(index + 2);
}
while (expression.contains("^3")) {
int index = expression.indexOf("^3");
double value = Math.pow(Double.parseDouble(expression.substring(0, index)), 3);
expression = value + expression.substring(index + 2);
}
// 处理乘除取模
while (expression.contains("*") || expression.contains("/") || expression.contains("%")) {
int index = 0;
if (expression.contains("*")) {
index = expression.indexOf("*");
}
if (expression.contains("/") && expression.indexOf("/") < index) {
index = expression.indexOf("/");
}
if (expression.contains("%") && expression.indexOf("%") < index) {
index = expression.indexOf("%");
}
double leftValue = Double.parseDouble(expression.substring(0, index));
int rightIndex = index + 1;
while (rightIndex < expression.length() && Character.isDigit(expression.charAt(rightIndex))) {
rightIndex++;
}
double rightValue = Double.parseDouble(expression.substring(index + 1, rightIndex));
double value;
if (expression.charAt(index) == '*') {
value = leftValue * rightValue;
} else if (expression.charAt(index) == '/') {
value = leftValue / rightValue;
} else {
value = leftValue % rightValue;
}
expression = value + expression.substring(rightIndex);
}
// 处理加减
double result = 0;
while (expression.length() > 0) {
int index = 0;
if (expression.charAt(0) == '+') {
index = 1;
} else if (expression.charAt(0) == '-') {
index = 1;
while (index < expression.length() && Character.isDigit(expression.charAt(index))) {
index++;
}
} else {
while (index < expression.length() && Character.isDigit(expression.charAt(index))) {
index++;
}
}
double value = Double.parseDouble(expression.substring(0, index));
if (index < expression.length() && expression.charAt(index) == '-') {
result -= value;
} else {
result += value;
}
expression = expression.substring(index);
}
return result;
}
}
```
使用时,运行 `Calculator.main()` 方法,输入一个表达式,程序会输出计算结果。例如,输入 `sin(30)^2 + cos(30)^2`,程序会输出 `1.0`。
阅读全文