CS246 [Winter 2012] Assignment 3.

V0.1

In this assignment, you are to write C++ programs that convert from one number base to another.  For example, ff in base 16 could be converted to 255 in base 10.  Note: each base, 16 and 10 in these examples, will be written in base 10.  We will only consider bases 2 through 16 inclusive.  You are to write three programs:

strnum – converts a string s in a given base to a number n (in base 10)

numstr – converts a number n (in base 10) to a string s in a given base

strstr – converts a string s1 in a given base to another string s2 in another given base

Input to programs.  Programs strnum and numstr read their input (strings and  bases) from the standard input stream cin.  Program strstr reads its input (strings and bases) from its shell arguments.

Examples of using strnum:

$ ./strnum

[strnum] Give str, base: 10 2

10 base 2 = 2

[strnum] Give str, base: f 16

f base 16 = 15

[strnum] Give str, base: ffff 16

ffff base 16 = 65535

[strnum] Give str, base: 10 14

10 base 14 = 14

[strnum] Give str, base: 0 0         Two zeros means to halt.

Examples of using numstr:

$ ./numstr

[numstr] Give num, base: 10 2

10 base 2 = 1010

[numstr] Give num, base: 255 16

255 base 16 = ff

[numstr] Give num, base: 64 8

64 base 8 = 100

[numstr] Give num, base: 0 0     Two zeros means to halt.

Examples of using strstr.

$ ./strstr ff 16 2

ff base 16 = 11111111 base 2

$ ./strstr 377 8 16

377 base 8 = ff base 16

Use of assertions. Generally document assumptions, when it is straightforward, using assert statements.

Write you own code. You are not to use predefined or library C++ code that that may solve much of the conversions you are to write.

You can use assertions to catch errors and/or you can use error messages (your choice).  Any error messages from your program should go to the standard error stream cerr.

Stopping conditions.  The programs strnum and numstr halt when their input is two zeros: 0 0

Program structure.  The structure of your programs will illustrate the reuse of functions and the use of header files.  Your C++ program is to consist of the following files.

snmain.cpp – main program for strnum.  Uses function in sn.* to do the conversion

                strnum – executable (sample solution code given to you)

nsmain.cpp – main program for numstr. Uses function in ns.* to do the conversion

                numstr – executable (sample code solution given to you)

ssmain.cpp – main program for strstr.  Uses functions in sn.* and ns.* to do the conversion (see below).  It is to use strnum to convert its input string to an internal number (type unsigned) then uses numstr to convert that number to the target string.

                strstr – executable (sample code solution given to you)

sn.h – header for function that converts a string to a number (given to you)

sn.cpp – implements function described in sn.h

ns.h – header for function that converts a number to a string (given to you)

ns.cpp – implements function described in ns.h

Provided files.  Files provided to you can be found in:  ~holt/cp-for-students/cs/246/2012/asgn03

                You can use the shell script mkstuff to compile you files.