CS 12, Fall 2008
Assignment 1
Due: Friday, October 10, 9:00 pm
Worth: 100 homework points
- Write and test a program called intbase.c to get and display
integer values in various bases.
- Input bases are decimal, octal, and hexadecimal. Output bases are decimal, octal, hexadecimal, and binary.
- Start with a copy of
~mikec/cs12/hw1/intbase.c-skeleton (at CSIL), and mind
the comments/instructions in that file.
- You must implement main, and the six other functions described below, so your program behaves and produces
results that exactly match the behavior and results
produced by our executable solution (
~mikec/cs12/hw1/intbase),
including the menu text and all messages.
- Write the function,
enum choice menu(void), to display a menu for the user, and return
the user's choice as one of the enum choice constants (see the skeleton).
Restriction: you may not use printf in this program, but you may use puts
to print a character string (with a newline).
- Write the function,
int getInteger(void), to get an integer value from the user, and
print the value along with a label that indicates the base that was entered (use exactly
the same labels printed by our solution).
Allow this integer to be entered as a positive or negative decimal,
octal (begins with '0'), or hexadecimal (begins with "0x" or "0X")
constant. Skip leading white space, then read to the end of the integer. Return 0 if the user
enters invalid text.
Restrictions: you may not use scanf, sscanf, fscanf,
atoi, atof, strtol, or any other library function to process
the input. Use getchar to get one character at a time.
Hints:You may use the character functions of <ctype.h>, and you may
model your solution after the simpler atoi implementation given in the K&R text on page 61.
- Write four functions to print the current value in different bases (as messages that match the
ones produced by our solution):
void putDecimal(int value) - to display in base 10.
void putOctal(int value) - to display in base 8.
void putHex(int value) - to display in base 16.
void putBinary(int value) - to display in base 2.
Restrictions: you may not use printf (or related library functions) to print the
value.
Use putchar to print the value one character at a time.
Hints: see itoa and exercise 3-5 on K&R page 64. Our solution includes the
itob function described in the exercise (plus reverse from page
62), and we used this function to easily implement
all four of these required display functions.
Note: show negative values with a minus sign in front of the absolute value
(i.e., not the "twos complement" values, even for the binary display).
- Write
main to present the menu and respond to the users choices until the user chooses
to quit. Let the initial value be 0, in case the user chooses to display a value before
entering one.
We encourage you to add utility functions as appropriate to simplify
your solution, but you must adhere to the restrictions about library functions.
- Compile by "
gcc -Wall -o intbase intbase.c" and fix as necessary to
eliminate all compiler errors and warnings before turning in. Test the executable
file too.
- Complete this Student Information Form.
Then turn in intbase.c
from your Engineering account as follows:
turnin hw1@cs12 intbase.c
Late projects may not be accepted.
Updated 9/24/08 by C. Michael Costanzo