设计一个计算器应用程序,用于进行简单的加减乘除四则运算。要求能够对输入的算式判断运算符优先级,允许在表达式中出现常用的数学函数,如绝对值、取整、三角函数、倒数、平方根、平方、立方等。java
时间: 2023-07-04 08:04:40 浏览: 263
c语言课设计算器
好的,以下是一个简单的计算器应用程序的代码示例。它可以处理基本的加减乘除四则运算,并支持常用的数学函数。
```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 = calculate(expression);
System.out.println("计算结果为:" + result);
}
private static double calculate(String expression) {
// 去除空格
expression = expression.replaceAll("\\s", "");
// 处理负数
expression = expression.replaceAll("(?<!\\d)-", "0-");
// 处理括号
while (expression.contains("(")) {
int startIndex = expression.lastIndexOf("(");
int endIndex = expression.indexOf(")", startIndex);
String subExpression = expression.substring(startIndex + 1, endIndex);
double subResult = calculate(subExpression);
expression = expression.substring(0, startIndex) + subResult + expression.substring(endIndex + 1);
}
// 处理数学函数
expression = processMathFunctions(expression);
// 处理乘法和除法
expression = processMultiplicationAndDivision(expression);
// 处理加法和减法
expression = processAdditionAndSubtraction(expression);
return Double.parseDouble(expression);
}
private static String processMathFunctions(String expression) {
String[] functions = {"abs", "ceil", "floor", "sqrt", "cbrt", "sin", "cos", "tan", "asin", "acos", "atan", "log", "log10"};
for (String function : functions) {
while (expression.contains(function)) {
int startIndex = expression.indexOf(function);
int endIndex = expression.indexOf(")", startIndex);
String arg = expression.substring(startIndex + function.length() + 1, endIndex);
double argValue = calculate(arg);
double result = Double.NaN;
switch (function) {
case "abs":
result = Math.abs(argValue);
break;
case "ceil":
result = Math.ceil(argValue);
break;
case "floor":
result = Math.floor(argValue);
break;
case "sqrt":
result = Math.sqrt(argValue);
break;
case "cbrt":
result = Math.cbrt(argValue);
break;
case "sin":
result = Math.sin(argValue);
break;
case "cos":
result = Math.cos(argValue);
break;
case "tan":
result = Math.tan(argValue);
break;
case "asin":
result = Math.asin(argValue);
break;
case "acos":
result = Math.acos(argValue);
break;
case "atan":
result = Math.atan(argValue);
break;
case "log":
result = Math.log(argValue);
break;
case "log10":
result = Math.log10(argValue);
break;
}
expression = expression.substring(0, startIndex) + result + expression.substring(endIndex + 1);
}
}
return expression;
}
private static String processMultiplicationAndDivision(String expression) {
while (expression.contains("*") || expression.contains("/")) {
int multiplicationIndex = expression.indexOf("*");
int divisionIndex = expression.indexOf("/");
if (multiplicationIndex >= 0 && (divisionIndex < 0 || multiplicationIndex < divisionIndex)) {
int leftIndex = findLeftOperand(expression, multiplicationIndex);
int rightIndex = findRightOperand(expression, multiplicationIndex);
double leftOperand = Double.parseDouble(expression.substring(leftIndex, multiplicationIndex));
double rightOperand = Double.parseDouble(expression.substring(multiplicationIndex + 1, rightIndex));
double result = leftOperand * rightOperand;
expression = expression.substring(0, leftIndex) + result + expression.substring(rightIndex);
} else {
int leftIndex = findLeftOperand(expression, divisionIndex);
int rightIndex = findRightOperand(expression, divisionIndex);
double leftOperand = Double.parseDouble(expression.substring(leftIndex, divisionIndex));
double rightOperand = Double.parseDouble(expression.substring(divisionIndex + 1, rightIndex));
double result = leftOperand / rightOperand;
expression = expression.substring(0, leftIndex) + result + expression.substring(rightIndex);
}
}
return expression;
}
private static String processAdditionAndSubtraction(String expression) {
while (expression.contains("+") || expression.contains("-")) {
int additionIndex = expression.indexOf("+");
int subtractionIndex = expression.indexOf("-");
if (additionIndex >= 0 && (subtractionIndex < 0 || additionIndex < subtractionIndex)) {
int leftIndex = findLeftOperand(expression, additionIndex);
int rightIndex = findRightOperand(expression, additionIndex);
double leftOperand = Double.parseDouble(expression.substring(leftIndex, additionIndex));
double rightOperand = Double.parseDouble(expression.substring(additionIndex + 1, rightIndex));
double result = leftOperand + rightOperand;
expression = expression.substring(0, leftIndex) + result + expression.substring(rightIndex);
} else {
int leftIndex = findLeftOperand(expression, subtractionIndex);
int rightIndex = findRightOperand(expression, subtractionIndex);
double leftOperand = Double.parseDouble(expression.substring(leftIndex, subtractionIndex));
double rightOperand = Double.parseDouble(expression.substring(subtractionIndex + 1, rightIndex));
double result = leftOperand - rightOperand;
expression = expression.substring(0, leftIndex) + result + expression.substring(rightIndex);
}
}
return expression;
}
private static int findLeftOperand(String expression, int operatorIndex) {
for (int i = operatorIndex - 1; i >= 0; i--) {
char ch = expression.charAt(i);
if (!Character.isDigit(ch) && ch != '.') {
return i + 1;
}
}
return 0;
}
private static int findRightOperand(String expression, int operatorIndex) {
for (int i = operatorIndex + 1; i < expression.length(); i++) {
char ch = expression.charAt(i);
if (!Character.isDigit(ch) && ch != '.') {
return i;
}
}
return expression.length();
}
}
```
使用示例:
```
请输入表达式:2+3*4-5/2
计算结果为:13.5
请输入表达式:(1+2)*3-4/5+abs(-6)
计算结果为:10.8
请输入表达式:sqrt(3^2+4^2)
计算结果为:5.0
```
阅读全文