Question

import java.util.Stack;
public class Evaluate 
{
    public static void main(String[] args)
    {
        Stack<String> ops = new Stack<String>();
        Stack<Double> vals = new Stack<Double>();
        while (!StdIn.isEmpty())
        {
            String s = StdIn.readString();
            if  (s.equals("("))         ;
            else if     (s.equals("+"))     ops.push(s) ;
            else if     (s.equals("-"))     ops.push(s) ;
            else if     (s.equals("*"))     ops.push(s) ;
            else if     (s.equals("/"))     ops.push(s) ;
            else if     (s.equals("sqrt"))  ops.push(s) ;
            else if     (s.equals(")"))                 
            {
            String op = ops.pop();
            double v = vals.pop();
            if  (ops.equals("+"))       v = vals.pop() + v;
            else if     (ops.equals("-"))   v = vals.pop() - v;
            else if     (ops.equals("*"))   v = vals.pop() * v;
            else if     (op.equals("/"))    v = vals.pop() / v;
            else if     (op.equals("sqrt"))     v = Math.sqrt(v);
            vals.push(v);

            }
            else vals.push(Double.parseDouble(s));
        }
        StdOut.println(vals.pop());
    }
}

I am trying to get this program (its straight from the book) to work via command prompt. It will compile, and run. However, when I give it input such as ( 5 + 3 ) and then ctrl-c to exit the program it doesn't give me a result of the computation. Maybe, I have missed something simple, maybe I don't fully understand how ctrl-c is supposed to work. Any help will be appreciated.

Was it helpful?

Solution

As a 10-second quickie, try CtrlZ

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top