import lista.Pila;
import lista.EmptyPilaException;

class RegOp {
    protected String [] ops ={"+","-","*","/"};
    protected Pila datos;
    
    public RegOp() {
        datos = new Pila();
    }
    
    public boolean esOperacion(String s) {
        boolean es = false;
        for(int i = 0 ; i<ops.length; i++) 
            if (s.equals(ops[i]))
                es = true;
        return es;
    }
    
    public void tomaDigito(String s) throws RegOpException {
        try {
            datos.push(new Double(s));
        } catch (NumberFormatException e) {
            throw new RegOpException("Numero mal formado");
        }
    }    
    
    public void opera(String s) throws RegOpException {
        try {	
            double y = ((Double)datos.pop()).doubleValue();
            double x = ((Double)datos.pop()).doubleValue();
            if (s.equals("+"))
                datos.push(new Double(x+y));
            else if (s.equals("-"))
                datos.push(new Double(x-y));
            else if (s.equals("*"))
                datos.push(new Double(x*y));
            else if (s.equals("/"))
                datos.push(new Double(x/y));
        } catch (NumberFormatException e) {
            throw new RegOpException("Numero mal formado");
        } catch (EmptyPilaException e) {
            throw new RegOpException("Expresion mal construida");
        }
    }
    
    public double resultado() throws RegOpException {
        try {
            return ((Double)datos.pop()).doubleValue();
        } catch (NumberFormatException e) {
            throw new RegOpException("Numero mal formado");
        } catch (EmptyPilaException e) {
            throw new RegOpException("Expresionmal construida");
        }
    }       
}