UNB/ CS/ David Bremner/ teaching/ java/ BigCalculator.java
import java.math.BigInteger;
import ccj2.ConsoleReader;

public class BigCalculator extends Calculator{
   

    protected Number parseNumber(){
        
        BigInteger retVal= new BigInteger("0");
        BigInteger ten=new BigInteger("10");
        int sign;
        
        if (match("-")){
            sign=-1;
            advance(1);
        } else {
            sign=1;
        }

        while (moreInput() && ( startsWithDigit()  )){
            retVal=retVal.multiply(ten);
            retVal=retVal.add(BigInteger.valueOf(parseDigit()));
            advance(1);
        }
        retVal=retVal.multiply(BigInteger.valueOf(sign));


        return retVal;
    }


    protected Number evalOperator(Number inLeft, char op, Number inRight){

        BigInteger left=new BigInteger(inLeft.toString());
        BigInteger right=new BigInteger(inRight.toString());

        System.out.print(left);
        System.out.print(op);
        System.out.println(right);


        BigInteger result;
        if (op == '+'){
            result=left.add(right);
        } else if (op=='-') {
            result=left.subtract(right);
        } else if (op=='*') {
            result=left.multiply(right);
        } else if (op=='/') {
            result=left.divide(right);
        } else {
            throw new IllegalArgumentException("Illegal Operator "+op);
        }
        return result;
    }

    public static void main(String[] args){

        
        ConsoleReader console=new ConsoleReader(System.in);

        BigCalculator calc=new BigCalculator();
        

        System.out.print("Enter an expression: ");

        
        String line=console.readLine();

        while (line != null){


            System.out.print(line+"=");
            
            Number i=calc.evalString(line);
            System.out.println(i);
            System.out.print("Enter an expression: ");
            line=console.readLine();

        }
    }
    

}