CSc230
January 2009
Page 1
Assignment 1 – Part 2 [90 Points]
Due 4pm, Monday May 30
C Programming Problem
You are asked to program C functions which perform arithmetic operations on integers in an
arbitrary base number system.
The base can be any value from 2 up to 36. The operations are
compare, addition, subtraction and multiplication.
Problem Specifications
You are provided with code for a C program which reads pairs of base
k
numbers and then,
depending on the operator read previously, calls functions to compare, add, subtract or multiply
them together, printing the result. An example input to the program might be as shown in Figure 1.
#16
+
123AF9
00FF
#2
?
+101 +10

+1010011
110111
*
100
101
Figure 1: Sample Program Input
The input “
#16
” sets the number system base to 16 for subsequent input and calculations and stays
in effect until the base is changed again. A line that requests a calculation begins with a ‘?’, ‘
+
’, ‘

’ or
‘
*
’ operator and is followed by two numbers. Each number can begin with an optional ‘
+
’ or ‘

’ sign
and continues with a sequence of one or more digits. Each digit must be valid for the current
number system base.
The program can handle any base from 2 up to 36. If the base is 36, then the digits of this number
system are written as ‘0’, ‘1’ ... ‘9’, ‘A’, ‘B’, ... ‘Z’. If the base is
k
(2
≤
k
≤
36), then the first
k
characters in that sequence are used as the digits.
Your task is to program the compare,
add
,
subtract
and
multiply
functions. The supplied
code provides only dummy definitions for these three functions. The overall structure has been
done for you. The result of running your program on the input of Figure 1 should be output very
similar to that shown in Figure 2 (except, of course, that it should be your own name and student
number printed in the first line).
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
CSc230
January 2009
Page 2
CSc230, assignment 1 part 2, Sheldon Cooper (V0012345)
base = 16
123AF9
+
FF
=
123BF8
base = 2
101 > 10
1010011

110111
= 10001010
100
*
101
=
10100
End of input
Figure 2: Expected Program Output (using input of Figure 1)
Programming Help
The Representation of a Base
k
Number
A base
k
number is implemented in the supplied code by the type
NumberType
, defined as a C
struct using the following declaration:
typedef struct {
char
negative;
// nonzero => negative, zero => positive
char
digit[MAXDIGITS];
// the sequence of base k digits
} NumberType;
The identifier
MAXDIGITS
is a preprocessor constant. The supplied code defines it as 16, but it
could, in principle, be redefined as any value greater than 1.
After seeing the declaration for
NumberType
, the C compiler will treat it as being the name of a
new datatype which can be used for declaring variables in the program. The first field of the struct,
negative
, holds the value 0 if the number is positive and any nonzero value if it is negative (but
the value 1 will normally be used as that nonzero value). Note that the C datatype
char
is one byte
in size and is commonly used for holding small integers as well as for ASCII character codes.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '11
 JasonD.Corless
 C Programming, Computer Architecture, Numeral system

Click to edit the document details