gpt4 book ai didi

java - 用于求解表达式的简单 Java 计算器

转载 作者:行者123 更新时间:2023-12-01 06:13:29 25 4
gpt4 key购买 nike

我正在开发一个可以解决表达式问题的计算器。我试图弄清楚如何让它按照 PEMDAS 顺序进行计算。我有一个 for 循环来遍历数组列表和一个调用执行数学运算的类的开关。我尝试过 if 语句,但无法弄清楚。

如何更改此设置以确保表达式以正确的顺序求解?这是我到目前为止所拥有的:

/*
Peter Harmazinski
Simple Calculator

This program solves expressions
*/

import java.util.*;

public class SimpleCalculator2 {
static SimpleMath math = new SimpleMath();

public static void main(String[] args) {
Scanner console = new Scanner(System.in);
boolean again = true;
double number1 = 0.0;
double number2 = 0.0;
double answer = 0.0;
double results = 0.0;
String delims = "[ ]+";

getIntroduction();

while (again) {
System.out.println("Please enter your expression.");
String input = console.nextLine();
System.out.println("This is the user's input: " + input);

//Parses string into array list
List<String> list = new ArrayList<String>(Arrays.asList(input.split(delims)));
System.out.println("list: " + list);

results = doMath(list, number1, number2);
getResults(results);
}
console.close();
}

public static void getIntroduction() {
System.out.println("This is a simple calculator that solves expressions.");
}

//Traverses array list to identify operators and does math for surrounding numbers
//then answer is inserted in i-1 element and the elements i and i+1 are deleted

public static double doMath(List<String> list, double number1, double number2) {
double answer = 0.0;
double results = 0.0;
while (list.size() > 1) {
for (int i = 0; i < list.size(); i++) {
switch (list.get(i)) {
case "*" :
number1 = Double.parseDouble(list.get(i - 1));
number2 = Double.parseDouble(list.get(i + 1));
answer = math.multiply(number1, number2);
System.out.println(answer);
list.add(i - 1, Double.toString(answer));
list.subList(i, i + 3).clear();
System.out.println(list);
break;
case "/" :
number1 = Double.parseDouble(list.get(i - 1));
number2 = Double.parseDouble(list.get(i + 1));
answer = math.divide(number1, number2);
System.out.println(answer);
list.add(i - 1, Double.toString(answer));
list.subList(i, i + 3).clear();
System.out.println(list);
break;
case "+" :
number1 = Double.parseDouble(list.get(i - 1));
number2 = Double.parseDouble(list.get(i + 1));
answer = math.add(number1, number2);
System.out.println(answer);
list.add(i - 1, Double.toString(answer));
list.subList(i, i + 3).clear();
System.out.println(list);
break;
case "-" :
number1 = Double.parseDouble(list.get(i - 1));
number2 = Double.parseDouble(list.get(i + 1));
answer = math.subtract(number1, number2);
System.out.println(answer);
list.add(i - 1, Double.toString(answer));
list.subList(i, i + 3).clear();
System.out.println(list);
break;
}
}
}
return answer;
}

public static void getResults(double results) {
System.out.println("Results are: " + results);
}
}

最佳答案

我认为实现此目的的一个相当标准的算法是 Dijkstra 的调车场算法,然后进行后缀评估。您可以在这里阅读它,伪代码在这里,但您可能需要对基本数据结构有一些了解:http://en.wikipedia.org/wiki/Shunting-yard_algorithm

如果您不了解堆栈、队列和后缀表示法,您可以编写一个更慢、更简单但更困惑的实现。我也用 sin、cos、trig、log 尝试过一次,代码结果可以正常工作,但我不会再尝试一次。

基本上,这个想法是找到仅用 1 个运算符的最高优先级表达式,对其进行评估,然后用它替换它。这是一些伪代码:

input = [user input]
while expression still contains (, ), +, -, *, or /:
toEvaluate = highest priority expression with 1 operator (e.g. 1+2, or 2*3)
calculate the decimal value of toEvaluate
modify input so that you replace toEvaluate with its decimal value

请注意,在您的 doMath() 实现中,在 for 循环中,您只要看到所有运算符就对其进行评估。例如,考虑

1+2*3

您将首先看到 +,计算 1+2,然后将结果乘以 3。相反,您需要首先遍历整个列表,找到最高优先级运算符,对其进行评估,然后再次从头开始。

关于java - 用于求解表达式的简单 Java 计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com