Code example for Stack

Methods: peek, pop, push, size

0
        tvOp.setText(String.valueOf(lastOp));
    } 
 
    private void doLastOp() { 
        isRestart = true;
        if (lastOp == '\0' || stack.size() == 1) {
            return; 
        } 
 
        String valTwo = stack.pop();
        String valOne = stack.pop();
        switch (lastOp) {
            case '+': 
                stack.push(new BigDecimal(valOne).add(new BigDecimal(valTwo)).toPlainString());
                break; 
            case '-': 
                stack.push(new BigDecimal(valOne).subtract(new BigDecimal(valTwo)).toPlainString());
                break; 
            case '*': 
                stack.push(new BigDecimal(valOne).multiply(new BigDecimal(valTwo)).toPlainString());
                break; 
            case '/': 
                BigDecimal d2 = new BigDecimal(valTwo);
                if (d2.intValue() == 0) {
                    stack.push("0.0");
                } else { 
                    stack.push(new BigDecimal(valOne).divide(d2, 2, BigDecimal.ROUND_HALF_UP).toPlainString());
                } 
                break; 
            default: 
                break; 
        } 
        setDisplay(stack.peek());
        if (isInEquals) {
            stack.push(valTwo);
        } 
    } 
 
    private void doPercentChar() { 
        if (stack.size() == 0)
            return; 
        setDisplay(new BigDecimal(result).divide(Utils.HUNDRED).multiply(new BigDecimal(stack.peek())).toPlainString());
        tvOp.setText("");
    } 
 
    private void doEqualsChar() { 
        if (lastOp == '\0') {
            return; 
        } 
        if (!isInEquals) {
            isInEquals = true;
            stack.push(result);
        } 
        doLastOp(); 
        tvOp.setText("");
    }