/*
* an implementation of a simple calculator
* @author Biagioni, Edoardo
* @assignment lecture 6
* @date January 30, 2008
*/
import java.lang.ArithmeticException;
public class Calculate {
/* simple test code for arbitrary-precision arithmetic
* @param expression: two operands and an operator
* @return nothing
* @throws ArithmeticException when dividing by zero
*/
public static void main(String[] parameters) {
if (parameters.length == 3) {
ArbitraryPrecisionInterface x =
new ArbitraryPrecision (parameters[0]);
ArbitraryPrecisionInterface y =
new ArbitraryPrecision (parameters[2]);
ArbitraryPrecisionInterface result =
new ArbitraryPrecision (); // zero
if (parameters[1].equals("+")) {
if (parameters[2].equals("1")) {
result = x.oneMore();
} else {
result = x.add(y);
}
} else if (parameters[1].equals("-")) {
if (parameters[2].equals("1")) {
result = x.oneLess();
} else {
result = x.subtract(y);
}
} else if (parameters[1].equals("*")) {
if (parameters[2].equals("2")) {
result = x.twice();
} else {
result = x.multiply(y);
}
} else if (parameters[1].equals("/")) {
if (parameters[2].equals("2")) {
result = x.half();
} else {
result = x.divide(y);
}
} else if (parameters[1].equals("%")) {
result = x.modulo(y);
} else {
System.out.println("unknown operator " + parameters[1]);
}
System.out.println(parameters[0] + " " +
parameters[1] + " " +
parameters[2] + " = " +
result);
} else {
System.out.println("three arguments expected");
}
}
}