// FILE: 98a1.cpp // PURPOSE: Work as client of a given class //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: All the files must be in directory in which you compile // 2. COMMAND LINE for g++ or gxx: g++ -Wall 98a1.cpp apstring.cpp //-------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Download 1998 A Exam Question 1, answer Parts A and B, complete // is IsValid() and AppendCheckSum() and run to validate your solutions. //---------------------------------------------------------------- #include #include #include const int MINLEN = 5; const int MAXLEN = 15; class Code { public: Code(); // length is 0, capacity is MAXLEN chars Code(const string & s); // code has chars in s, length is s.length() bool IsValid() const; // returns true if code is valid, else false int Length() const; // returns # characters in the code void AppendCheckSum(); // adds appropriate check sum // other functions not shown void print(ostream & os) const; void printA(bool) const; void printB(void) const; private: string toString(void) const; // do _NOT_ use this in your solutions int myLength; // # characters in code vector myChars; // the characters in the code }; ostream& operator << (ostream&, const Code &); int digitToInt(char); char intToDigit(int); void IsValidDriver(void); void AppendCheckSumDriver(void); ////////////////////////////////////////////////////// // Part A /////////////////////////////////////////////////////// bool Code::IsValid() const { // 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' return false; } ////////////////////////////////////////////////////// // Part B /////////////////////////////////////////////////////// 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 } const Code A[] = { Code("120H056"), Code("ABCDE"), Code("ABCDE9999"), Code("AB1"), Code("ABCDEF00FEDCBA"), Code("ab?XX123") }; const int A_SIZE = sizeof(A)/sizeof(Code); void cls(); void wait(); int main() { IsValidDriver(); AppendCheckSumDriver(); wait(); return 0; } Code::Code() : myLength(0), myChars(MAXLEN) { } Code::Code(const string & s) : myLength(s.length()), myChars(MAXLEN) { int k; for(k=0; k < myLength; k++) { myChars[k] = s[k]; } } int Code::Length() const { return myLength; } void Code::print(ostream & os) const { int k; for(k=0; k < myLength; k++) { os << myChars[k]; } } void Code::printA(bool printValid) const { cout << setw(3) << myLength << " " << setw(18) << setiosflags(ios::left) << toString().c_str(); if ( printValid ) { cout << ( IsValid()? " true " : " false " ) << setiosflags(ios::right); } } void Code::printB(void) const { const string st(toString()); int sum(0); for (int k = 0; k < myLength; k++) if ( isdigit(st[k]) ) sum += digitToInt(st[k]); cout << setw(3) << myLength << " " << setw(16) << setiosflags(ios::left) << st.c_str() << setiosflags(ios::right) << setw(6); if ( IsValid() ) cout << sum; else cout << " NA"; } string Code::toString(void) const { string temp; for (int k = 0; k < myLength; k++) temp += myChars[k]; return temp; } ostream& operator << (ostream& os, const Code & s) { s.print(os); return os; } int digitToInt(char d) { // precondition: isdigit(d) == true // postcondition: returns the value 0 to 9 corresponding // to character d return d - '0'; } char intToDigit(int val) { // precondition: 0 <= val <= 9 // postcondition: returns the character from // '0'..'9' corresponding to val return '0' + val; } void IsValidDriver(void) { string title("1998 APCS A Exam: Question 1"); cls(); cout << setw(39 + title.length()/2) << title.c_str() << endl << endl; cout << "Part A: Testing IsValid():\n\n"; cout << "myLength myChars result of call to IsValid()\n"; for (int k = 0; k < A_SIZE; k++) { A[k].printA(true); cout << endl; } } void AppendCheckSumDriver(void) { cout << "\nPart B: Testing AppendCheckSum():\n\n"; cout << "--- BEFORE CALL TO AppendCheckSum() --- " << "AFTER CALL TO AppendCheckSum()\n"; cout << "myLength myChars sum of digits myLength myChars\n"; for (int k = 0; k < A_SIZE; k++) { Code temp = A[k]; temp.AppendCheckSum(); A[k].printB(); cout << " "; temp.printA(false); cout << endl; } } void wait (void) { cout << "\nPress ..."; cin.ignore(10, '\n'); } void cls(void) { cout << string(55, '\n'); }