Lab materials
- Slides: Command line arguments
- Sample calculator program [windows] [linux]
- Code sample: Accepting arguments at runtime
Lab tasks
For this lab, you will have one main task with two extra tasks that add extra functionality and features to the base task.
Lab task [W14-1]: Calculator
For this task, you will build a calculator that works by getting its input from the command line
Requirements
- You will create a simple calculator, that
- can do addition, subtraction, multiplication and division
- supports positive integers as operands
- only does one operation at a time (e.g. 3 + 6)
- Have a precision of 2 places after the comma
- Both the operands and the operator will be given as command line arguments
- E.g. an addition operation would look like this:
- Program is not allowed to ask for any input during its execution
- Your calculator must detect the following errors
- Wrong argument count
- Unknown operator
- Non-numeric operand
- Division by zero
- When an error occurs, you will both present the specific error message and the guide how to use your program
Recommended list of functions
- Display help
- Shows how to use the program
- Argument check
- Checks for number of operands
- For the extra task, checks for --help argument
- Error management
- Displays the specific error message
- Calls the display help function
- Operand check
- Checks if the operand is numeric
- Returns the operand converted to a number (e.g. int)
- Calculation
- Identifies the operation and calculates the answer
- Returns the answer
You may also benefit from creating a function to check for the operator.
Example
To test a completed program, we recommend downloading the sample and seeing how it works.
To see all the test cases for the program, check the next paragraph.
Example 1: Input is OK and the program prints the answer
|
1 2 |
risto@risto-lt3-tux:~$ ./calculator 1 / 4 > 0.25 |
Example 2: Program is executed without arguments. An error message and a how to use guide is provided.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
risto@risto-lt3-tux:~/Desktop$ ./calculator Error 0! Invalid argument count or unknown argument(s) Usage: ./calculator operand operation operand Possible operations: + add - subtract * multiply / divide Note, that due to how shell works, the multiplication operator has to be encased in quotes e.g. 2 "*" 3.5 |
Test cases
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# base case, normal operations ./calculator 1 + 1 ./calculator 7 / 5 # base case, incorrect input ./calculator # wrong argument count ./calculator 1 2 3 4 5 # wrong argument count ./calculator 6 + a # non-numeric operand ./calculator 6 +- 5 # unknown operation ./calculator 6 + 6a # non-numeric operand ./calculator 6 n 6 # unknown operation ./calculator 6 / 0 # division by zero # Extra task, normal operation ./calculator 3.0 / 5.0 ./calculator -3.0 / 5.0 # Extra task, incorrect input ./calculator 5.5.5 / 5 ./calculator -5-5 / 5 ./calculator 5-5 / 5 |
Extra task 1 [W14-2]: Real numbers
Change your program in a way that it would support all real numbers. You must add support for both negative numbers and numbers with decimal places.
Test the following
- -3.3
- 3-3
- -3-3
- 3.3.3
Extra task 2 [W14-3]: Extended functionality
Add the following functionality to your program
- Taking a square root
- Power function (xy)
- Displaying help using the argument --help without causing an error to occur.
All added functions must be present when help is shown.
After the class, you should be able to
- Understand what a command line argument is
- Know other programs that use command line arguments
- Be able to accept command line arguments in C code
- Know that main() function has multiple forms
- Understand what argument count is
- Understand what argument vector is
- Know what is the first argument passed to a program
- Know how argument count changes depending on how many arguments are passed
- Understand that * and [] interchangeable
- Understand what * means in the command prompt and how to pass such characters to programs
- Understand how to pass multiple words as a single argument
- Be able to convert strings into integers, floats