CSCI 1300
Introduction to Computing
Fall 1997
Programming Assignment #5
Due Friday, Oct. 3
Write a binary, decimal, hex converter program as follows:
-
1. Write three functions that read a whole number from the input one
character at a time (using getch()) and store it in a variable of type
int, and then return the value of the integer. The three functions are:
-
- get_decimal() - reads a decimal number.
- get_binary() - reads a binary number.
- get_hex() - reads a hexadecimal number.
All three of these functions should accept (and print back out) only
characters that are valid for that type of number, ignoring all
others, and returning as soon as a carriage return (c == 13) is
entered. For example, get_binary() should accept 0's and 1's but
nothing else, and return the integer when a carriage return is
entered.
-
2. Write three more functions that take an integer as a parameter and
print out the integer.
-
- print_decimal() - prints out the integer as a decimal number.
print_decimal() may use cout to print the number.
- print_binary() - prints out the integer as a binary number.
print_binary() may not call cout or printf to print the number - it
should break the number into the individual binary digits and call print_binary_digit() (provided on
the class web page) to print them out.
- print_hex() - prints out the integer as a hexadecimal number.
print_hex() may not call cout or printf to print the number - it
should break the number into the individual hexadecimal digits and
call print_hex_digit() (provided on
the class web page) to print them out.
-
3. Write a program that has a while loop that prints out a prompt
(like "Command: ") and uses getch() to read what the user types, and
the processes the input as follows:
-
- If the user types 'b', call get_binary() to read in a binary
number, and then call print_decimal(), print_hex(), and print_binary()
to print the number out in all three formats.
- If the user types 'd', call get_decimal() to read in a decimal
number, and then call print_decimal(), print_hex(), and print_binary()
to print the number out in all three formats.
- If the user types 'h', call get_hex() to read in a hexadecimal
number, and then call print_decimal(), print_hex(), and print_binary()
to print the number out in all three formats.
- If the user types '?', print out a short description of the
commands available.
- If the user types 'q', exit the program.
- If the user types anything else, ignore it.
-
4. Bonus part (not worth any extra points, but your output will look
better).
-
- Write print_binary() and print_hex() so that leading zeros are
not printed. Of course, if the number is actually 0 then you will
have to print at least one zero.
-
Some useful functions:
-
- isdigit() - takes a character c as input and returns 1 (TRUE) if
c is a character that represents a decimal digit (0-9) and 0 (FALSE)
if not.
- isxdigit() - takes a character c as input and returns 1 (TRUE) if
c is a character that represents a hexadecimal digit (0-9, a-f, A-F).
- Of course, for binary numbers you can just check if c is '0' or
'1'.
- ctoi() (available on the web page) that
takes a single character c and returns the integer value that it
represents.