设计一个计算器应用程序,用于进行简单的加减乘除四则运算。要求能够对输入的算式判断运算符优先级,允许在表达式中出现常用的数学函数,如绝对值、取整、三角函数、倒数、平方根、平方、立方等。
时间: 2023-11-08 12:37:58 浏览: 80
好的,我可以为您提供一个初步的计划:
1. 创建一个输入框,用于用户输入算式。
2. 创建一个运算符按钮组,包括加、减、乘、除等基本运算符。
3. 创建一个数学函数按钮组,包括绝对值、取整、三角函数、倒数、平方根、平方、立方等常用函数。
4. 创建一个等于按钮,用于计算表达式并输出结果。
5. 创建一个清空按钮,用于清空输入框和结果。
6. 建立计算逻辑,包括识别输入的算式、判断运算符优先级、计算数学函数等。
7. 建立输出逻辑,将计算结果显示在一个输出框中。
8. 进行测试和调试,确保程序能够正确地计算各种算式。
这是一个基本的计划,您可以根据实际情况进行调整和完善。
相关问题
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 = calculate(expression);
System.out.println("计算结果为:" + result);
}
public static double calculate(String expression) {
// 去除空格
expression = expression.replaceAll(" ", "");
// 将字符串转化为字符数组
char[] expChars = expression.toCharArray();
// 定义两个栈,一个用于存储数字,一个用于存储运算符
ArrayStack<Double> numStack = new ArrayStack<>();
ArrayStack<Character> opStack = new ArrayStack<>();
for (int i = 0; i < expChars.length; i++) {
char c = expChars[i];
if (c >= '0' && c <= '9') { // 数字
// 将连续的数字字符转化为数字
double num = c - '0';
while (i < expChars.length - 1 && expChars[i + 1] >= '0' && expChars[i + 1] <= '9') {
num = num * 10 + (expChars[i + 1] - '0');
i++;
}
numStack.push(num);
} else if (c == '(') { // 左括号
opStack.push(c);
} else if (c == ')') { // 右括号
while (opStack.peek() != '(') {
char op = opStack.pop();
double num2 = numStack.pop();
double num1 = numStack.pop();
double res = calculate(num1, num2, op);
numStack.push(res);
}
opStack.pop(); // 弹出左括号
} else if (isOperator(c)) { // 运算符
while (!opStack.isEmpty() && opStack.peek() != '(' && comparePriority(c, opStack.peek()) <= 0) {
char op = opStack.pop();
double num2 = numStack.pop();
double num1 = numStack.pop();
double res = calculate(num1, num2, op);
numStack.push(res);
}
opStack.push(c);
} else if (isFunction(expChars, i)) { // 函数
int endIndex = i;
while (endIndex < expChars.length && expChars[endIndex] != '(') {
endIndex++;
}
String funcName = expression.substring(i, endIndex);
i = endIndex;
opStack.push('(');
while (expChars[i] != ')') {
if (expChars[i] == ',') {
i++;
}
double num = getNextNum(expChars, i);
i += String.valueOf(num).length();
numStack.push(num);
}
double res = calculateFunction(funcName, numStack);
numStack.push(res);
} else {
throw new IllegalArgumentException("表达式非法!");
}
}
while (!opStack.isEmpty()) {
char op = opStack.pop();
double num2 = numStack.pop();
double num1 = numStack.pop();
double res = calculate(num1, num2, op);
numStack.push(res);
}
return numStack.pop();
}
// 判断字符是否为运算符
private static boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
// 判断两个运算符的优先级,如果op1优先级高于op2,返回1;如果op1优先级低于op2,返回-1;如果两者优先级相等,返回0
private static int comparePriority(char op1, char op2) {
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
return 1;
} else if ((op1 == '+' || op1 == '-') && (op2 == '*' || op2 == '/')) {
return -1;
} else {
return 0;
}
}
// 计算两个数的运算结果
private static double calculate(double num1, double num2, char op) {
switch (op) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
throw new IllegalArgumentException("运算符非法!");
}
}
// 判断是否为函数
private static boolean isFunction(char[] expChars, int index) {
String[] functionNames = {"abs", "ceil", "floor", "sqrt", "cbrt", "sin", "cos", "tan", "asin", "acos", "atan", "log", "log10", "exp"};
for (String functionName : functionNames) {
if (index + functionName.length() < expChars.length && new String(expChars, index, functionName.length()).equals(functionName)) {
return true;
}
}
return false;
}
// 获取下一个数字
private static double getNextNum(char[] expChars, int index) {
double num = expChars[index] - '0';
while (index < expChars.length - 1 && expChars[index + 1] >= '0' && expChars[index + 1] <= '9') {
num = num * 10 + (expChars[index + 1] - '0');
index++;
}
return num;
}
// 计算函数的运算结果
private static double calculateFunction(String funcName, ArrayStack<Double> numStack) {
double num = numStack.pop();
switch (funcName) {
case "abs":
return Math.abs(num);
case "ceil":
return Math.ceil(num);
case "floor":
return Math.floor(num);
case "sqrt":
return Math.sqrt(num);
case "cbrt":
return Math.cbrt(num);
case "sin":
return Math.sin(num);
case "cos":
return Math.cos(num);
case "tan":
return Math.tan(num);
case "asin":
return Math.asin(num);
case "acos":
return Math.acos(num);
case "atan":
return Math.atan(num);
case "log":
return Math.log(num);
case "log10":
return Math.log10(num);
case "exp":
return Math.exp(num);
default:
throw new IllegalArgumentException("函数名非法!");
}
}
}
// 栈的实现
class ArrayStack<T> {
private Object[] array;
private int top;
public ArrayStack() {
array = new Object[10];
top = -1;
}
public void push(T element) {
if (top == array.length - 1) {
expandCapacity();
}
array[++top] = element;
}
public T pop() {
if (isEmpty()) {
throw new RuntimeException("栈为空!");
}
return (T) array[top--];
}
public T peek() {
if (isEmpty()) {
throw new RuntimeException("栈为空!");
}
return (T) array[top];
}
public boolean isEmpty() {
return top == -1;
}
private void expandCapacity() {
Object[] newArray = new Object[array.length * 2];
System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
}
}
```
使用示例:
```
请输入算式:(1+2)*3-4/2
计算结果为:7.0
请输入算式:sqrt(2)+sin(1)+log10(100)
计算结果为:3.707107
请输入算式:abs(-3)+floor(3.5)+ceil(3.2)
计算结果为:9.0
```
用java设计一个计算器应用程序,用于进行简单的加减乘除四则运算。要求能够对输入的算式判断运算符优先级,允许在表达式中出现常用的数学函数,如绝对值、取整、三角函数、倒数、平方根、平方、立方等。
以下是一个简单的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`。
阅读全文