/** * Matt Kretchmar * February 28, 2006 * Calc.java * This program implements a simple command-line calculator. * It supports A op B where A,B are floating point operands * and "op" is +, -, *, /, ^. */ class Calc1 { public static void main ( String [] args ) { double operand1 = 0; double operand2 = 0; double result = 0; // wrong number of arguments? if ( args.length != 3 ) { System.out.println("Wrong number of arguments. Expects:"); System.out.println("Calc A op B"); System.exit(1); } // first and third not valid doubles? try { operand1 = Double.parseDouble(args[0]); } catch ( NumberFormatException nfe) { System.out.println("Invalid numeric format for operand A. Expects:"); System.out.println("Calc A op B"); System.exit(1); } try { operand2 = Double.parseDouble(args[2]); } catch ( NumberFormatException nfe) { System.out.println("Invalid numeric format for operand B. Expects:"); System.out.println("Calc A op B"); System.exit(1); } // Attempt operation if ( args[1].equals("+") ) result = operand1 + operand2; else if ( args[1].equals("-") ) result = operand1 + operand2; else if ( args[1].equals("*") ) result = operand1 * operand2; else if ( args[1].equals("/") ) result = operand1 / operand2; else if ( args[1].equals("^") ) result = Math.pow(operand1,operand2); else { System.out.println("Invalid op (+,-,*,/,^). Expects:"); System.out.println("Calc A op B"); System.exit(1); } System.out.println(result); } }