Question

I made this program that evaluates a postfix expression. It works fine if only single digit numbers are used.

My problem is how do I push multiple-digit numbers if input has spaces?

ex. input: 23+34*- output is -7

but if I input: 23 5 + output is only 3(which is the digit before the space) it should have an output of 28

my codes:

public class Node2
{
    public long num;
    Node2 next;
    Node2(long el, Node2 nx){
        num = el;
        next = nx;
    }
}


class stackOps2
    {
        Node2 top;
        stackOps2(){
            top = null;
        }

        public void push(double el){
            top = new Node2(el,top);
        }

        public double pop(){
            double temp = top.num;
            top = top.next;
            return temp;
        }

        public boolean isEmpty(){
            return top == null;
        }
    }




public class ITP {

static stackOps2 so = new stackOps2();
public static final String operator = "+-*/^";




    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the infix:");
           String s = input.next();
               String output;
    InToPost theTrans = new InToPost(s);
    output = theTrans.doTrans();
    System.out.println("Postfix is " + output + '\n');
 System.out.println(output+" is evaluated as: "+evaluate(output));
  }

    public static double evaluate(String value)throws NumberFormatException{

            for(int i=0;i<value.length();i++){
                char val = value.charAt(i);
                          if(Character.isDigit(value.charAt(i))){
                    String v = ""+val;
                    so.push(Integer.parseInt(v));
                }
                else if(isOperator(val)){
                    double rand1=so.pop();
                    double rand2=so.pop();
                    double answer ;
                    switch(val){
                        case '+': answer = rand2 + rand1;break;
                        case '-': answer = rand2 - rand1;break;
                        case '*': answer = rand2 * rand1;break;
                            case '^': answer = Math.pow(rand2, rand1);break;
                            default : answer = rand2 / rand1;break;
                    }
                    so.push(answer);
                    }
                    else if(so.isEmpty()){
                        throw new NumberFormatException("Stack is empty");
                    }
                } 
                return so.pop();
            }

            public static boolean isOperator(char ch){
                String s = ""+ch;
                return operator.contains(s);
            }

}
Was it helpful?

Solution

This is a small, self-contained example that does all the string parsing and evaluation. The only difference from your example is that it accepts the whole string at once instead of using a Scanner. Note the use of Integer.parseInt -- that's missing in your example. I think you can easily extend this for your needs.

@SuppressWarnings({"rawtypes", "unchecked"})
public static void main(String[] args) {
  final String in = "5 9 + 2 * 6 5 * +";
  final Deque<Object> s = new LinkedList();
  for (String t : in.split(" ")) {
    if (t.equals("+")) s.push((Integer)s.pop() + (Integer)s.pop());
    else if (t.equals("*")) s.push((Integer)s.pop() * (Integer)s.pop());
    else s.push(Integer.parseInt(t));
  }
  System.out.println(s.pop());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top