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

ChackoNorris

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?

Reilas

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare two arrays containing objects in order to calculate what changed?

Sorting two arrays by the order of one of the arrays [Swift 3.0 - Xcode 8]

Select random items out of two arrays and match the items order

PHP: Show the difference in two arrays when elements out of order

Can two loops be merged into one?

Compare two arrays keeping order in mind for one array in Ruby

Combining two int's arrays into one array by order not working

Formatting the arrangement of two arrays in order of one in Numpy Python

Two arrays one with time in, another with time out of something

Creating new matrix out of one column by order of two other columns

Finding median in merged array of two sorted arrays

Merged two dataframe columns with lists in order of lists

Is it possible to use one for loop for two string arrays in Java?

Why does order of operands matter when adding powershell strings and arrays?

Every two columns are merged into one column in

Sorting two arrays that are linked into descending order. One is a string array, one is an int array. c++

Is it possible to order by two fields in a given table,one in ascending and the other in descending order at the same time?

Two arrays into one Dictinary

Two arrays to one chunked

Concating two arrays into one

join two arrays as one

is it possible two make two arrays into one to create an x,y position array?

Trying to "crossover" two arrays using subset of first array and maintaining order of second array where possible

Compare two arrays with not the same order

Combine two arrays in specific order

Merge two arrays in "snake" order

Mix two arrays in the given order

printing out difference of two arrays

Create array out of two arrays