Assignment Instructions

  1. Edit, compile, and run the following program on the UH UNIX shell:
    1. The point of this program is to learn how to use commandline input and how to use an array of pointers to functions.
    2. Write a calculator for positive, single-digit intergers for operators +, -, ., / (add, subtract, multiply, divide) that takes input from the commandline. (Note that an asterisk ('*') is used on the commandline as a wildcard, so we have to substitute a period (.) instead.)
    3. Note: Do NOT use scanf() or getchar() for input for this program. The input for this program is ONLY from the commandline. For an example of commandline input, read and run this program: command.c
    4. Check to make sure the user entered 4 commandline arguments. If not, print error message and end program.
      The number of commandline arguments is stored as an integer in variable argc.
    5. Check to make sure the user entered two integers as the 2nd and 4th commandline arguments. If not, print error message and end program. (Note that the 0th commandline argument is: ./a.out, ./program, ./asmt14, or whatever the name is for your executable file.)
      The first integer is stored as a character in array element argv[1][0].
      The second integer is stored as a character in array element argv[3][0].
      See examples of how to access characters in an array of pointers to strings here: arraypointer.c and command.c
    6. Check to make sure the user entered a +, -, ., / (add, subtract, multiply, divide) operator as the 3rd commandline argument. If not, print error message and end program.
      The operator is stored as a character in array element argv[2][0].
    7. Convert characters to integers by subtracting character zero ('0') from the digits entered on the commandline. The first digit is the first character in the second commandline argument. The second digit is the first character in the fourth commandline argument. For example:
      int number1 = argv[1][0] - '0';
      int number2 = argv[3][0] - '0';
    8. Subtract the plus character ('+') (the ASCII code is the decimal number 43) from the operator entered on the commandline. This will be the index of the array of functions. For example:
      int index = argv[2][0] - '+';
      You can check out an ASCII chart for the characters '+', '-', '.', and '/' to see that '+' - '+' will be 0, '-' - '+' will be 2, '.' - '+' will be 3, and '/' - '+' will be 4.
    9. Declare an array of functions, so that the 0th element is the function for adding, the 1st element is NULL, the 2nd element is the function for subtracting, the 3rd element is the function for multiplying, the 4th element is the function for dividing.
    10. Here are two examples of an array of functions: functionpointer.c and menu.c
    11. Here is an example of an array of functions. You may have different names for your array name and function names.
      int (*array[5])(int, int)={add, NULL, sub, mul, div};
    12. Output the result, using the array of functions and corresponding index to call each function, and the two digits (number1 and number2) as the function call arguments.
    13. Except for the error checking, you DO NOT NEED to use if-statements (or a switch statement) in your code. The array of pointers to functions is a shortcut to replace if-statements.
    14. The function definitions are short and simple. For example, here is a function definition for adding two integers together:
      int add(int x, int y){
           return x + y;
      }
    15. Email your makefile and C program to the instructor, or show it to the instructor in class.
  2. For the submission guidelines and the grading guidelines, see the syllabus.

Example I/O

./a.out                                                                                             
ERROR: You have to enter 4 commandline arguments for the calculator.

./a.out 2                                                                                   
ERROR: You have to enter 4 commandline arguments for the calculator.

./a.out 2 +                                                                                 
ERROR: You have to enter 4 commandline arguments for the calculator.

./a.out B + 3                                                                              
ERROR: The second commandline argument should be an integer 0-9.

./a.out 9 ! 3                                                                               
ERROR: The third commandline argument should be a +, -, ., or / operator.

./a.out 9 + W                                                                              
ERROR: The fourth commandline argument should be an integer 0-9.

./a.out 9 + 5                                                                               
9 + 5 = 14

./a.out 9 - 5                                                                               
9 - 5 = 4

./a.out 9 . 5                                                                               
9 . 5 = 45

./a.out 8 / 2                                                                               
8 / 2 = 4

Program Outline

/*Commandline Input Calculator: using an array of functions to replace if-statements*/

//four function prototypes

int main(int argc, char *argv[]){
    
    //error checking

    //convert characters to integers 0-9

    //convert symbol input to integer 0-4                                                                        
    
    /*declare an array of functions, so that 
    the 0th element is for adding,
    the 1st element is NULL,
    the 2nd element is for subtracting,
    the 3rd element is for multiplying, 
    the 4th element is for dividing.
    */
        
    //output the result
    
    return 0;
} 

//four function definitions