Download Purpose - ETSU Computing

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science 2210
Lab Assignment 2
Due Date: ___________
June 2017
Purpose
The purpose of this assignment is to gain further experience with the STL including the stack class.
The Problem
One way to convert a non-negative decimal (base 10) integer to a new base (greater than 1) is described here.
If nBase is the base, then (integer) divide the number to be converted by nBase to get both an integer quotient
and an integer remainder. Place the remainder in a stack, and repeat the process on the quotient until a
quotient of 0 is achieved. Add (push) 0’s to the stack to get the desired number of “digits” in the result. Then
retrieve and output the values on the stack until it is empty, using the letters A, B, C, D, E, and F for remainders
of more than one digit (i.e., 10, 11, 12, 13, 14, and 15, respectively for this assignment).
For example, the decimal number 123 can be converted to binary (base 2) as follows:
123 / 2
= 61 with a remainder of 1, push 1 onto the stack
61 / 2
= 30 with a remainder of 1, push 1 onto the stack
30 / 2
= 15 with a remainder of 0, push 0 onto the stack
15 / 2
= 7 with a remainder of 1, push 1 onto the stack
7/2
= 3 with a remainder of 1, push 1 onto the stack
3/2
= 1 with a remainder of 1, push 1 onto the stack
1/2
= 0 with a remainder of 1, push 1 onto the stack
The quotient is now 0 so we can quit. The stack contains the remainders 1, 1, 1, 1, 0, 1, 1 in that order. If our
goal is an 8-bit result, we push one more 0 onto the stack to get 8 bits, resulting in 0, 1, 1, 1, 1, 0, 1, 1 being the
values on the stack in that order. Popping the stack and outputting the result until the stack is empty yields a
binary value of
01111011
To convert the same number to hexadecimal (base 16), we do the following:
123 / 16
= 7 with a remainder of 11, push B onto the stack
7 / 16
= 0 with a remainder of 7, push 7 onto the stack
Now, if we want to have a 16-bit (2-byte) result, we have to add 2 leading 0’s by pushing them into the stack,
with a result of the stack containing 0, 0, 7, B in that order. The answer is thus: 007B.
To convert a number in the opposite direction (i.e., to convert a number in base nBase back to decimal (base
10) form), process the number one digit at a time, left to right. Start by setting the result to 0. For each digit
from left to right, multiply the result by nBase and add the next digit (adding 10 for A, 11 for B, and so forth).
A recursive function may be written to do this.
For example, the octal number (base 8) 123 converts to the decimal number 83 as follows
((1 x 8) + 2) x 8) + 3 = 83
In this example, the octal number 123 represents (1 x 82) + (2 x 81) + 3 = 64 + 16 + 3 = 83
Don Bailes
Page 1
D:\148082655.doc
Computer Science 2210
Lab Assignment 2
Due Date: ___________
June 2017
Specifications
Write a menu driven program that will allow you to convert any non-negative base 10 integer (within the range
supported by the 32-bit architecture of the machine) to a specified base between 2 and 16 and to convert any
non-negative number in a base between 2 and 16 back into a decimal number (i.e., base 10). Allow the user to
specify the number to be converted, the base to use, and the number of “digits” desired in the output.
Create static functions named ToDecimal and FromDecimal in your utility class. Use these functions in your
main program to do the required conversions. ToDecimal should return an integer whereas FromDecimal
should return a string. Both returned values should contain the proper representations of the numbers in
question. The parameters for both are the numbers to be converted in either integer or string format as
appropriate to its type.
Use the STL as appropriate to implement this solution. At a minimum, use stacks, vectors, strings, and related
operations where they are beneficial. Avoid brute force if there are existing STL or utility functions to help
make the job easier. Use recursion where appropriate.
When testing this program, be sure to verify that when you convert a decimal number to some other base, then
take the answer, and convert it back, the result is your original number. A program with this characteristic may
still be wrong, but it cannot be right without having this characteristic.
Deliverables
Place the solution in a folder labeled Lab2YourName, and submit a zipped copy of this folder as an email
attachment following the rules laid out earlier for email submissions. Make sure you keep a copy in case of
email issues.
Don Bailes
Page 2
D:\148082655.doc