Description
Homework #1 CS 3844 Section 0B1
Write a C program that takes one or more numbers on the command line. Rules:
- If no numbers are given, print a usage message and quit.
- Do NOT use sscanf or similar. Process each argument one digit at a time.
- For each number given on the command line, check to see if it is a valid binary, octal, decimal and hex number. For hex numbers, allow both lowercase a-f and uppercase A-F.
- If there are any invalid characters in the argument, ignore it. E.g., a 2 is not valid for binary.
- You must also account for a leading minus sign. Additional error checking is not necessary (overflow for example).
Example usage: ./hw1 0 123 abcd -45 error -1 -4D
Example output:
=== Checking 0              Octal     Decimal    Unsigned         Hex
Base 2:                        0           0           0           0
Base 8:                        0           0           0           0
Base 10:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â Â Â 0
Base 16:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â Â Â 0
Â
=== Checking 123            Octal     Decimal    Unsigned         Hex
Base 8:                      123          83          83          53
Base 10:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 173Â Â Â Â Â Â Â Â Â 123Â Â Â Â Â Â Â Â Â 123Â Â Â Â Â Â Â Â Â Â 7b
Base 16:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 443Â Â Â Â Â Â Â Â Â 291Â Â Â Â Â Â Â Â Â 291Â Â Â Â Â Â Â Â Â 123
Â
=== Checking abcd           Octal     Decimal    Unsigned         Hex
Base 16:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 125715Â Â Â Â Â Â Â 43981Â Â Â Â Â Â Â 43981Â Â Â Â Â Â Â Â abcd
Â
=== Checking -45            Octal     Decimal    Unsigned         Hex
Base 8:              37777777733         -37  4294967259    ffffffdb
Base 10:Â Â Â Â Â Â Â Â Â Â Â Â Â Â 37777777723Â Â Â Â Â Â Â Â Â -45Â Â 4294967251Â Â Â Â ffffffd3
Base 16:Â Â Â Â Â Â Â Â Â Â Â Â Â Â 37777777673Â Â Â Â Â Â Â Â Â -69Â Â 4294967227Â Â Â Â ffffffbb
Â
=== Checking error          Octal     Decimal    Unsigned         Hex
Â
=== Checking -1             Octal     Decimal    Unsigned         Hex
Base 2:              37777777777          -1  4294967295    ffffffff
Base 8:              37777777777          -1  4294967295    ffffffff
Base 10:Â Â Â Â Â Â Â Â Â Â Â Â Â Â 37777777777Â Â Â Â Â Â Â Â Â Â -1Â Â 4294967295Â Â Â Â ffffffff
Base 16:Â Â Â Â Â Â Â Â Â Â Â Â Â Â 37777777777Â Â Â Â Â Â Â Â Â Â -1Â Â 4294967295Â Â Â Â ffffffff
Â
=== Checking -4D            Octal     Decimal    Unsigned         Hex
Base 16:Â Â Â Â Â Â Â Â Â Â Â Â Â Â 37777777663Â Â Â Â Â Â Â Â Â -77Â Â 4294967219Â Â Â Â ffffffb3
The columns must line up exactly as shown, but the number of spaces between columns doesn’t matter.
Hint: in C, printf with %-10s will left justify a string in 10 characters, %10s will right justify it.