// CalculatorCL.cpp // // Illustrates use of numerical command-line arguments. // #include #include #include using namespace std; enum ArgStatus {BadOperand1, BadOperator, BadOperand2, AllOK}; void InvocationError(); void ArgumentsError(ArgStatus clStatus); ArgStatus CheckCLArguments(char* argv[]); int Calc(int Operand1, char Operator, int Operand2); void PrintExpression(int Operand1, char Operator, int Operand2, int Result); void main(int argc, char* argv[]) { // Check for correct number of command-line arguments: // (Remember the program name counts as one.) if (argc != 4) { InvocationError(); return; } // Check whether command-line arguments are of correct types: ArgStatus clStatus = CheckCLArguments(argv); if ( clStatus != AllOK) { ArgumentsError(clStatus); return; } // convert command-line arguments to proper types: // (atoi converts ascii string to an integer) int Operand1 = atoi(argv[1]); char Operator = argv[2][0]; int Operand2 = atoi(argv[3]); int Result = Calc(Operand1, Operator, Operand2); PrintExpression(Operand1, Operator, Operand2, Result); } ArgStatus CheckCLArguments(char* argv[]) { bool OperandOK = true, OperatorOK = true; OperatorOK = (argv[2][1] == '\0') && // check operator length ( (argv[2][0] == '+') || // check operator symbol (argv[2][0] == '-') || (argv[2][0] == '*') || (argv[2][0] == '/') ); if (!OperatorOK) return BadOperator; // Check if first operand is a string of digits: for (int Idx = 0; argv[1][Idx] != '\0'; Idx++) { //cout << Idx << " " << argv[1][Idx] << endl; if ( !isdigit(argv[1][Idx]) ) { OperandOK = false; break; } } if (!OperandOK) return BadOperand1; // Check if second operand is a string of digits: for (Idx = 0; argv[3][Idx] != '\0'; Idx++) { //cout << Idx << " " << argv[3][Idx] << endl; if ( !isdigit(argv[3][Idx]) ) { OperandOK = false; break; } } if (!OperandOK) return BadOperand2; // No errors detected if we get this far: return (AllOK); } int Calc(int Operand1, char Operator, int Operand2) { switch (Operator) { case '+': return (Operand1 + Operand2); case '-': return (Operand1 - Operand2); case '*': return (Operand1 * Operand2); case '/': if (Operand2 == 0) return 0; else return (Operand1 / Operand2); default: return 0; } } void PrintExpression(int Operand1, char Operator, int Operand2, int Result) { cout << Operand1 << ' ' << Operator << ' ' << Operand2 << " = " << Result << endl; } void InvocationError() { string Invocation = "Invocation: CalculatorCS \n" "where\n" " must be an integer value\n" " must be one of: '+', '-', '*' or '/'\n\n" "and the expression must be well-formed.\n"; cout << Invocation; } void ArgumentsError(ArgStatus clStatus) { string BadOp1Msg = "The first operand is not an integer.\n" "Please correct the problem.\n\n"; string BadOp2Msg = "The second operand is not an integer.\n" "Please correct the problem.\n\n"; string BadOperatorMsg = "The operand is not + or - or * or /.\n" "Please correct the problem.\n\n"; string AllOKMsg = "The command-line arguments are all proper.\n" "Please correct the problem.\n\n"; string BadStatusMsg = "Unrecognized command-line status.\n" "Please notify program vendor.\n\n"; switch (clStatus) { case BadOperand1: cout << BadOp1Msg; break; case BadOperand2: cout << BadOp2Msg; break; case BadOperator: cout << BadOperatorMsg; break; case AllOK: cout << AllOKMsg; break; default: cout << BadStatusMsg; } }