可以用合并成一个数组的两个操作数进行计算吗?

huangapple go评论69阅读模式
英文:

Is it possible to Calculate with Operands in Order out of two Arrays Merged to one?

问题

我尝试使用用户输入构建一个计算器,并希望按顺序使用操作数计算所有内容。我将用户输入保存在两个String ArrayList中:
-> postNumbers 用于所有数字
-> postOperands 用于所有操作数

我尝试使用不同的for循环和switch/case来计算它们,但是当输入两个加号或斜杠时,它就无法正常工作。是否有一种方法可以将这两个ArrayList合并成一个,然后像这样进行计算:

列表 N:10 5 3 2

列表 M:10+5*3-2

列表 O:+ * -

然后最终将其放入一个变量中?

英文:

I try to build a calculator with user input and want to calculate everything with Operands in Order. I saved the user input in two String ArrayLists:
-> postNumbers for all numbers
-> postOperands for all operands

I tried to calculate them with different for loops and switch/cases but it didnt worked right as soon as two pluses or slashes were inputed.
Is there a way to put the two ArrayLists into one alternativly and calcute it then like:

List N: 10 5 3 2

List M: 10+5*3-2

List O:   + * -

And then finally put it into a variable?

答案1

得分: 1

首先,我相信你的意思是postOperators,而不是postOperands
一个操作数是数值。

你可以创建一个for循环来迭代数字的List,然后将它们编译成一个result

此外,你应该查看抽象语法树结构。

/** @param postNumbers 必须是可变的 */
double evaluate(List<String> postNumbers, List<String> postOperators) {
    double result = Double.parseDouble(postNumbers.remove(0));
    double value;
    char operator;
    for (int index = 0; index < postNumbers.size(); index++) {
        value = Double.parseDouble(postNumbers.get(index));
        operator = postOperators.get(index).charAt(0);
        result = switch (operator) {
            case '*' -> result * value;
            case '/' -> result / value;
            case '+' -> result + value;
            case '-' -> result - value;
            default -> throw new IllegalArgumentException();
        };
    }
    return result;
}

示例输入

List<String> postNumbers = new ArrayList<>(Arrays.asList("10", "5", "3", "2"));
List<String> postOperators = List.of("+", "*", "-");
double result = evaluate(postNumbers, postOperators);

输出

43.0
英文:

First, I believe you mean postOperators, as opposed to postOperands.
An operand is the value.

You can create a for-loop to iterate the List of numbers, and compile them into a result.

Additionally, you should review the Abstract Syntax Tree structure.

/** @param postNumbers must be mutable */
double evaluate(List<String> postNumbers, List<String> postOperators) {
    double result = Double.parseDouble(postNumbers.remove(0));
    double value;
    char operator;
    for (int index = 0; index < postNumbers.size(); index++) {
        value = Double.parseDouble(postNumbers.get(index));
        operator = postOperators.get(index).charAt(0);
        result = switch (operator) {
            case '*' -> result * value;
            case '/' -> result / value;
            case '+' -> result + value;
            case '-' -> result - value;
            default -> throw new IllegalArgumentException();
        };
    }
    return result;
}

Example input

List<String> postNumbers = new ArrayList<>(Arrays.asList("10", "5", "3", "2"));
List<String> postOperators = List.of("+", "*", "-");
double result = evaluate(postNumbers, postOperators);

Output

43.0

答案2

得分: -2

以下是您问题的答案:

import java.util.*;
public class Calculator {
    public static void main(String[] args) {
        List<Double> numbers = Arrays.asList(10.0, 5.0, 3.0, 2.0);
        List<Character> operands = Arrays.asList('+', '*', '-');

        Stack<Double> stack = new Stack<>();
        int operandIndex = 0;

        for (Double number : numbers) {
            stack.push(number);

            if (operandIndex < operands.size()) {
                char operand = operands.get(operandIndex);
                double result = 0;

                switch (operand) {
                    case '+':
                        result = stack.pop() + stack.pop();
                        break;
                    case '-':
                        result = -stack.pop() + stack.pop();
                        break;
                    case '*':
                        result = stack.pop() * stack.pop();
                        break;
                    case '/':
                        result = 1 / stack.pop() * stack.pop();
                        break;
                }

                stack.push(result);
                operandIndex++;
            }
        }

        System.out.println(stack.pop());
    }
}
英文:

Here is an answer to your question:

import java.util.*;
public class Calculator {
public static void main(String[] args) {
List&lt;Double&gt; numbers = Arrays.asList(10.0, 5.0, 3.0, 2.0);
List&lt;Character&gt; operands = Arrays.asList(&#39;+&#39;, &#39;*&#39;, &#39;-&#39;);
Stack&lt;Double&gt; stack = new Stack&lt;&gt;();
int operandIndex = 0;
for (Double number : numbers) {
stack.push(number);
if (operandIndex &lt; operands.size()) {
char operand = operands.get(operandIndex);
double result = 0;
switch (operand) {
case &#39;+&#39;:
result = stack.pop() + stack.pop();
break;
case &#39;-&#39;:
result = -stack.pop() + stack.pop();
break;
case &#39;*&#39;:
result = stack.pop() * stack.pop();
break;
case &#39;/&#39;:
result = 1 / stack.pop() * stack.pop();
break;
}
stack.push(result);
operandIndex++;
}
}
System.out.println(stack.pop());
}
}

huangapple
  • 本文由 发表于 2023年5月24日 21:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定