MCX1/4
Math
Department
Mr. Gary. Jaye
Danny Jaye, A.P.S.
1998 Question 1 from APCS A Exam: Part II
Assume that registration codes, each consisting of a sequence of characters, are
implemented using the constants
and class declaration below.
const int MINLEN = 5;
const int MAXLEN = 15;
class Code
{
public:
Code(); // length is 0, capacity is MAXLEN chars
Code(const apstring & s); // code has chars in s, length is s.length()
bool IsValid(); // returns true if code is valid, else false
int Length(); // returns # characters in the code
void AppendCheckSum(); // adds appropriate check sum to the code
// other member functions not shown
private:
int myLength; // # characters in code
apvector<char> myChars; // the characters in the code
};
Part A Write the member function IsValid, whose header is given below. IsValid returns true if a code is valid and returns false otherwise. A code is valid if it satisfies the following criteria.
For example, consider the values of a Code variable c shown below.
| myLength | myChars | result of call c.IsValid() |
|---|---|---|
| 7 | 120H056 | true |
| 5 | ABCDE | true |
| 9 | ABCDE9999 | true |
| 3 | AB1 | false (Length() is too small) |
| 14 | ABCDEF00FEDCBA | false (Length() is too large) |
| 8 | ab?XX123 | false (characters a, b, ? are all invalid) |
In writing IsValid you may call the functions isdigit and isupper
whose headers are shown below. You do
not need to write isdigit or isupper.
bool isdigit(char ch) // postcondition: returns true if and only if ch // is a digit: '0' .. '9' bool isupper(char ch) // postcondition: returns true if and only if ch // is an upper case letter: 'A' .. 'Z' Complete member function IsValid below.
bool Code::IsValid()
// postcondition: returns true if MINLEN <= Length() < MAXLEN - 2
//
and for every k, 0 <= k < Length(),
//
myChars[k] is a digit '0'--'9' or a char 'A' -- 'Z'
Part B
Consider the problem of appending a checksum to the end of a registration code. The checksum is computed as follows:
Write the member function AppendCheckSum, whose header is given below. If a
Code value is valid as specified
in part (a), then AppendCheckSum should calculate the checksum and append the
character '-' followed by the
character for the checksum to the original Code value; otherwise AppendCheckSum
should leave the original Code
value unchanged.
For example, if c is a variable of type Code
Before the call c.AppendCheckSum() |
After the call c.AppendCheckSum() | ||||
| c.Length() | c.myChars | sum of digits | c.Length() | c.myChars | |
| 7 | 120H056 | 14 | 9 | 120H056-4 | |
| 5 | ABCDE | 0 | 7 | ABCDE-0 | |
| 9 | ABCDE9999 | 36 | 11 | ABCDE9999-6 | |
| 3 | AB1 | (code not valid) | 3 | AB1 | |
| 14 | ABCDEF00FEDCBA | (code not valid) | 14 | ABCDEF00FEDCBA | |
| 8 | ab?XX123 | (code not valid) | 8 | ab?XX123 | |
The functions digitToInt and intToDigit convert from a digit
character to the equivalent integer and
from a one-digit integer to the equivalent character, respectively.
int digitToInt(char d) {
// precondition: isdigit(d) == true
// postcondition: returns the value 0 to 9 corresponding
// to character d
char intToDigit(int val) {
// precondition: 0 <= val <= 9
// postcondition: returns the character from
// '0'..'9' corresponding to val
In writing AppendCheckSum, you may call the functions intToDigit, digitToInt, isupper and
isdigit specified above and in part (a) (you do not have to write the bodies of these functions.)
You may also call member function IsValid specified in part (a). Assume that IsValid
works as specified, regardless of what you wrote in part (a).
Complete member function AppendCheckSum below.
void Code::AppendCheckSum() {
// postcondition: if IsValid() then myLength is updated and
//
myChars contains the original characters
followed
//
by a dash and the appropriate checksum;
otherwise
//
myLength and myChars
are unchanged