Calculate answer from list

249 Views Asked by At

I have to make a calculator in java that is able to work with brackets, plus, minus, divide and multiply, so far I've got it so if the user were to enter:

14 * ( 2 - ( 3 / 2 ) )

then it returns an ArrayList as:

[14.0, 2.0, 3.0, 2.0, /, -, *]

which is what i want

How do i then apply the '/' to the 2 and the 3 to get:

[14.0, 2.0, 1.5, -, *]

Then so on, so the '-' to the 2 and 1.5 to get:

[14.0, 0.5, *] 

this carry's on to end up with a final answer of 7.0

Could anyone advise on how to do this?

Cheers for any help :)

3

There are 3 best solutions below

0
On

You should only push numbers onto the stack. So operators don't go on the stack. This means the list goes:

[14.0, 2.0, 3.0, 2.0]
// applying / by popping two elements and adding the result
[14.0, 2.0, 1.5]
// applying - by popping two elements and adding the result
[14.0, 0.5]
// applying * by popping two elements and adding the result
[7.0]

Note that this means it's up to you to determine when numbers go on the stack and when operators are applied.

0
On

You can use something like this :

List<String> list=new ArrayList<String>();
     list.add("5");
     list.add("6");
     list.add("7");
     list.add("-");
     list.add("+");
     String prev=null;
     String cur=null;

     List newList=new ArrayList(list);
     Iterator<String> iterator=newList.iterator();
     String val;
     int index=0;

     while(iterator.hasNext()){
          val=iterator.next();
         if(val.matches("(-)?\\d+([.]\\d+)?")){
             prev=cur;
             cur=val;                            
         }
         else{
                index=list.indexOf(prev);
                list.remove(prev);
                list.remove(cur);
                list.remove(val);

            if(val.equals("+")){
                list.add(index,(Float.parseFloat(prev)+Float.parseFloat(cur))+"");
            }
            else if(val.equals("-")){
                list.add(index,(Float.parseFloat(prev)-Float.parseFloat(cur))+"");
            }
           //Initialize iterator to point first element
            newList=new ArrayList(list);
            iterator=newList.iterator();
            prev=null;
            cur=null;
         }
     }



    iterator=list.iterator();
     while(iterator.hasNext()){
         String val1=iterator.next();
         System.out.println(" ## "+val1 );
     }

Conditions : - This code will work for binary operators and will not work for unary operators.

0
On

I think you should use an ArrayList<Object> instead of ArrayList<String> and use recursion to calculate the values ​​of expressions in brackets. The algorithm briefly look like this: If element of the list ArrayList<Object> is an instance of the String class, then we treat it like you did it so far (it is either a single number or operator). If element of the list ArrayList<Object> is an instance of the List class then use recursion to calculate the value of the expression, which lies in that list.
So for example, if you have an expression:

14 * ( 2 - ( 3 / 2 ) )

it should returns an ArrayList as:

[14.0, [2.0, [3.0, 2.0, /], -], *]

I hope my answer helped you a little.