public class IntStack {

    public static void main(String[] args) {
        IntStack m = new IntStack();
        System.out.println(m);
        m.push(1);
        System.out.println(m);
        m.push(3);
        System.out.println(m);
        m.push(6);
        System.out.println(m);
        m.push(2);
        System.out.println(m);
        m.op("/");
        System.out.println(m);
        m.op("*");
        System.out.println(m);
        m.op("-");
        System.out.println(m);
        int tulemus = m.pop();
        System.out.println(m);
        System.out.println(tulemus);
        IntStack acopy = m;
        System.out.println(acopy.equals(m));
        System.out.println(m);
        System.out.println(acopy);
        try {
            acopy = (IntStack) m.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println(acopy.equals(m));
        System.out.println(m);
        System.out.println(acopy);
        m.push(6);
        System.out.println(acopy.equals(m));
        System.out.println(m);
        System.out.println(acopy);
        m.pop();
        System.out.println(acopy.equals(m));
        System.out.println(m);
        System.out.println(acopy);
        String prog = "2 3 + 4 * 10 /";
        if (args.length > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < args.length; i++) {
                sb.append(args[i]);
                sb.append(" ");
            }
            prog = sb.toString();
        }
        System.out.println(prog + "\n "
                + String.valueOf(IntStack.interpret(prog)));
    }

    private int[] sarray;

    private int sp;

    IntStack() {
        sarray = new int[10];
        sp = -1;
    }

    IntStack(int ssize) {
        sarray = new int[ssize];
        sp = -1;
    }

    public static int interpret(String pol) {
        return 0; // TODO!!! Your code here!
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        IntStack tmp = new IntStack(sarray.length);
        if (sp >= 0)
            for (int i = 0; i <= sp; i++)
                tmp.sarray[i] = sarray[i];
        tmp.sp = sp;
        return tmp;
    }

    public boolean stEmpty() {
        return (sp < 0);
    }

    public boolean stFull() {
        return ((sp + 1) >= sarray.length);
    }

    public void push(int a) {
        if (stFull())
            throw new IndexOutOfBoundsException(" stack overflow: " + this);
        sp += 1;
        sarray[sp] = a;
    }

    public int pop() {
        if (stEmpty())
            throw new IndexOutOfBoundsException(" stack underflow");
        int tmp = sarray[sp];
        sp -= 1;
        return tmp;
    }

    public void op(String s) {
        if (sp < 1)
            throw new IndexOutOfBoundsException(" too few elements for " + s);
        int op2 = pop();
        int op1 = pop();
        if (s.equals("+"))
            push(op1 + op2);
        else if (s.equals("-"))
            push(op1 - op2);
        else if (s.equals("*"))
            push(op1 * op2);
        else if (s.equals("/"))
            push(op1 / op2);
        else
            throw new IllegalArgumentException("Invalid operation: " + s);
    }

    public int tos() {
        if (stEmpty())
            throw new IndexOutOfBoundsException(" stack underflow");
        return sarray[sp];
    }

    @Override
    public boolean equals(Object o) {
        if (((IntStack) o).sp != sp)
            return false;
        for (int i = 0; i <= sp; i++)
            if (((IntStack) o).sarray[i] != sarray[i])
                return false;
        return true;
    }

    @Override
    public String toString() {
        if (stEmpty())
            return "empty";
        StringBuffer b = new StringBuffer();
        for (int i = 0; i <= sp; i++)
            b.append(String.valueOf(sarray[i]));
	    b.append(" ");
        return b.toString();
    }

}
