Java设计一个计算器应用程序,用于进行简单的加减乘除四 则运算。要求能够对输入的算式判断运算符优允许在表达式中出现常用的数学函数,如绝对值、取整、三角函数、倒数、平方根、平方、立方等
时间: 2023-06-19 11:10:21 浏览: 170
以下是一个简单的Java计算器应用程序,支持加减乘除四则运算以及常用数学函数的计算:
```java
import java.util.Scanner;
import java.lang.Math;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入算式:");
String expression = scanner.nextLine();
double result = calculate(expression);
System.out.println("计算结果为:" + result);
}
public static double calculate(String expression) {
// 去掉空格
expression = expression.replace(" ", "");
// 判断算式中是否包含数学函数
if (expression.contains("abs(")) {
expression = replaceFunction(expression, "abs");
}
if (expression.contains("ceil(")) {
expression = replaceFunction(expression, "ceil");
}
if (expression.contains("floor(")) {
expression = replaceFunction(expression, "floor");
}
if (expression.contains("sin(")) {
expression = replaceFunction(expression, "sin");
}
if (expression.contains("cos(")) {
expression = replaceFunction(expression, "cos");
}
if (expression.contains("tan(")) {
expression = replaceFunction(expression, "tan");
}
if (expression.contains("cot(")) {
expression = replaceFunction(expression, "cot");
}
if (expression.contains("sqrt(")) {
expression = replaceFunction(expression, "sqrt");
}
if (expression.contains("square(")) {
expression = replaceFunction(expression, "square");
}
if (expression.contains("cube(")) {
expression = replaceFunction(expression, "cube");
}
// 判断算式中是否包含括号
while (expression.contains("(")) {
int left = expression.lastIndexOf("(");
int right = expression.indexOf(")", left);
String subExpression = expression.substring(left + 1, right);
double subResult = calculate(subExpression);
expression = expression.substring(0, left) + subResult + expression.substring(right + 1);
}
// 判断算式中是否只剩下一个数字
if (expression.matches("-?\\d+(\\.\\d+)?")) {
return Double.parseDouble(expression);
}
// 计算乘除法
while (expression.contains("*") || expression.contains("/")) {
int index = expression.contains("*") ? expression.indexOf("*") : expression.indexOf("/");
int left = index - 1;
while (left >= 0 && expression.charAt(left) != '+' && expression.charAt(left) != '-' && expression.charAt(left) != '*' && expression.charAt(left) != '/') {
left--;
}
int right = index + 1;
while (right < expression.length() && expression.charAt(right) != '+' && expression.charAt(right) != '-' && expression.charAt(right) != '*' && expression.charAt(right) != '/') {
right++;
}
double leftNumber = Double.parseDouble(expression.substring(left + 1, index));
double rightNumber = Double.parseDouble(expression.substring(index + 1, right));
double result = expression.charAt(index) == '*' ? leftNumber * rightNumber : leftNumber / rightNumber;
expression = expression.substring(0, left + 1) + result + expression.substring(right);
}
// 计算加减法
double result = 0;
String[] numbers = expression.split("[+\\-]");
String[] operators = expression.split("\\d+(\\.\\d+)?");
for (int i = 0; i < numbers.length; i++) {
double number = Double.parseDouble(numbers[i]);
if (i == 0) {
result = number;
} else {
if (operators[i].equals("+")) {
result += number;
} else {
result -= number;
}
}
}
return result;
}
public static String replaceFunction(String expression, String functionName) {
while (expression.contains(functionName + "(")) {
int left = expression.indexOf(functionName + "(");
int right = expression.indexOf(")", left);
String subExpression = expression.substring(left + functionName.length() + 1, right);
double subResult = 0;
switch (functionName) {
case "abs":
subResult = Math.abs(calculate(subExpression));
break;
case "ceil":
subResult = Math.ceil(calculate(subExpression));
break;
case "floor":
subResult = Math.floor(calculate(subExpression));
break;
case "sin":
subResult = Math.sin(Math.toRadians(calculate(subExpression)));
break;
case "cos":
subResult = Math.cos(Math.toRadians(calculate(subExpression)));
break;
case "tan":
subResult = Math.tan(Math.toRadians(calculate(subExpression)));
break;
case "cot":
subResult = 1 / Math.tan(Math.toRadians(calculate(subExpression)));
break;
case "sqrt":
subResult = Math.sqrt(calculate(subExpression));
break;
case "square":
subResult = Math.pow(calculate(subExpression), 2);
break;
case "cube":
subResult = Math.pow(calculate(subExpression), 3);
break;
}
expression = expression.substring(0, left) + subResult + expression.substring(right + 1);
}
return expression;
}
}
```
使用示例:
```
请输入算式:(1 + 2) * 3 - 4 / 2 + abs(-5) + sin(30)
计算结果为:9.5
```
阅读全文