2

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 :)

user3120023
  • 197
  • 3
  • 6
  • 16
  • 1
    Why is that particular list arrangement the one you want? You are not taking into account order of operation. You probably even need two different lists, one two hold values and one to hold operators. I would use two Stacks – Paul Samsotha Dec 27 '13 at 11:12
  • You need a stack. Write a loop, read in each iteration an input, which may be either a number of an operator. If it's a number, push it on your stack, if it's an operator, pop two elements from your stack, do the operation, and push the result to your stack again. I have coded a solution here https://github.com/gefei/project-euler/blob/master/093.py in the method eval_expr, which is, however, in Python – gefei Dec 27 '13 at 11:14
  • I have updated my answer. (Check the answers below.) , It will work fine , I think so. – Sujith PS Dec 27 '13 at 12:04

3 Answers3

1

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.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
0

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.

Sujith PS
  • 4,776
  • 3
  • 34
  • 61
0

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.

Tomasz Szymulewski
  • 2,003
  • 23
  • 23