C++代写:COMP1201BaseConvertor


完成任意进制数字 Base 转换器。
![Base
Convertor](https://upload.wikimedia.org/wikipedia/commons/thumb/7/75/Binary_counter.gif/220px-
Binary_counter.gif)

Requirement

A grade school teacher wants to develop a program to generate values to a
series of numbers in different base systems. The teacher is limiting the bases
to be between 2 and 9, inclusive. Each line of input will have a base value
and a sequence of characters representing a nonnegative number in that base.
However, each number (sequence of characters) has been placed “backwards”.
That is the rightmost digit is first in the sequence, the second digit from
the right is encountered next, and so on.
The program reads the digits one a time. As each digit is read, the program
should translate that digit into its corresponding decimal value, and then
multiply it by the appropriate power of the base. There is an arbitrary number
of blanks between the base and the first digit in the sequence.
Suppose that the teacher wants the students to convert the binary number 1011.
The program would require this example input line:
2 1101
The first number, 2, represents the base. Base 2 is designated as binary. The
1101 represents a character sequence of binary digits of the number to
convert. IT IS BACKWARDS! This means that the first 1 is in the one’s position
or 2 to the 0 power. The second 1 is in the two’s position or 2 to the 1
power. The 0 is in the four’s position or 2 to the second power. And lastly,
the rightmost 1 is in the eight’s position or 2 to the third power. Remember
the number that was to be converted was 1011.
The value of this number is 1 1 + 1 2 + 0 4 + 1 8 which equals 11. Please
see the sample output on how values are to be displayed.
Ask your instructor to give another example in a different base.
However, the teacher’s grade school students have entered the data values.
This data CANNOT be trusted as being valid data.
Your program needs to check for valid bases and if the base is invalid write
out a message that says,
Invalid base given, throwing away the rest of the line.
To throw away a line of input use cin.ignore( ). Your instructor will talk
about this in class. In addition, your program needs to check for valid digits
in the given base. For example, if the base is 2, any digit equal to 2 or
higher is NOT legitimate. Your error message should say (i.e. input line is :
2 234510)
For the given base 2, the number is NOT valid!
You will need to throw away the rest of the line here as well.
Lastly, your program is supposed to add up the values of all valid numbers
given and print this value as the final part of the output. If there are NO
valid inputted numbers given, then the sum is zero (0).
Again, please see the sample output on how values are to be displayed.
The following additional requirements must be followed:

A

Your main function should adhere to the following pseudocode:
— Pseudocode —-
Set totalSum to zero
Read until a valid base is read or end of file
while not at end of file
write “For the given base “, base
Assign to numberValue the translated number from the input
if numberValue is not valid
Write “ the number is NOT valid!”
else
Accumulate numberValue int totalSum
Write numberValue appropriately labelled
Read until another valid base is read or end of file
Write totalSum appropriately labelled
—|—

B

The above pseudocode suggests some functions. Keep these functions single-
minded, performing a focused task that can be well-named. You should design
pseudocode for each of these functions. All function bodies, including the
body of main, should be NO more than 30 lines long, including braces, blank
lines and comments.

C

You must NOT use arrays. You will lose ALL points if you do.

D

You must follow the programming standards.

E

You must follow the formatting of the sample I/O below.

F

You must thoroughly test your program.

G

You must use the following functions and you MUST add the documentation for
the parameters for functions that have parameters.
//———————————————————————
// This function reads bases until a valid base is read or eof occurs.
// If an invalid base is read, an error message is displayed and the
// rest of the line is ignored and another attempt to read a base value
// will be attempted.
// -1 is returned if eof occurs otherwise a valid base value is
// returned.
//———————————————————————
int ReadUntilValidBaseRead( )
//———————————————————————
// This function reads in a sequence of characters that represent
// a number in the given base. A valid sequence is given in a
// “backwards” format such that the rightmost digit is given first,
// the second to the rightmost digit is next, etc.
// This function returns the value of this sequence of characters if
// it is a valid sequence. If it is not valid it returns -1.
// params: TODO
//———————————————————————
int ReadNumbersReturningValue( int base )
//———————————————————————
// This function returns the numeric value of the character digit that
// is stored in chDigit.
// params: TODO
//———————————————————————
int DecimalValueOf( char chDigit )
//———————————————————————
// This function returns true if chDigit is a valid digit in the given
// base, it returns false otherwise.
// params: TODO
//———————————————————————
bool IsValid( char chDigit, int base )
—|—

H

To get credit for the assignment, your solution must minimally work on Test
Case # 1 below.
Sample I/O
Below are two sample runs.
They do NOT cover all cases.
Test Case # 1
Input for Run 1:
2 1101
3 1212
5 66
2 1111
The output for Test Run 1:
2 1101
For the given base 2, the decimal value of the input string is 11.
3 1212
For the given base 3, the decimal value of the input string is 70.
5 66
For the given base 5, the number is NOT valid!
2 1111
For the given base 2, the decimal value of the input string is 15.
The total sum of all valid values is 96.
Test Case # 2
Input for Test Run 2:
1 hello
10 this is bad too
-1 as well as this
Output for Run 2:
1 hello
Invalid base given, throwing away the rest of the line.
10 this is bad too
Invalid base given, throwing away the rest of the line.
-1 as well as this
Invalid base given, throwing away the rest of the line.
The total sum of all valid values is 0


文章作者: SafePoker
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 SafePoker !
  目录