// FILE: 00ab1.cpp // PURPOSE: 2000 Exam: A Question 4 and AB Question 1 //--------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp, apstring.h, // apstring.cpp, apmatrix.h, apmatrix.cpp // 2. COMMAND LINE: // g++ (or gxx) 00ab1.cpp apstring.cpp //---------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Read question 4 from 2000 A Exam or question 1 from 2000 AB Exam // and complete parts a, b, and c accordingly // ------------------------------------------------------------ #include #include "apmatrix.h" #include "apstring.h" struct Point { int row; int col; Point(int newRow = 0, int NewCol = 0); }; class Encryptor { public: Encryptor(); apstring EncryptWord(const apstring &word) const; Point GetCoordinates2(char ch) const; // tests GetCoordinates() apstring EncryptTwo2(const apstring &pair) const; // tests EncryptTwo() private: apmatrix myMat; Point GetCoordinates(char ch) const; apstring EncryptTwo(const apstring &pair) const; }; //====================================================== // Part a. Complete GetCoordinates(), as started below. //====================================================== Point Encryptor::GetCoordinates(char ch) const { } //================================================== // Part b. Complete EncryptTwo(), as started below. //================================================== apstring Encryptor::EncryptTwo(const apstring &pair) const { return "??"; } //=================================================== // Part c. Complete EncryptWord(), as started below. //=================================================== apstring Encryptor::EncryptWord(const apstring &word) const { return "####"; } ostream &operator<<(ostream &os, Point &p); void testGetCoordinates(); void testEncryptTwo(); void testEncryptWord(); int main() { apstring title("2000 AB Question 1"); cout << "\n\n\n\n\n\n\n" << setw(39 + title.length()/2) << title << endl << endl; testGetCoordinates(); testEncryptTwo(); testEncryptWord(); cout << "\nPress ..."; cin.get(); cout << endl; return 0; } Encryptor::Encryptor(void) { apstring mat[6] = {"STUVWX", "YZ0123", "456789", "ABCDEF", "GHIJKL", "MNOPQR"}; const int SIZE = sizeof(mat)/sizeof(mat[0]); myMat.resize(SIZE, SIZE); for (int r = 0; r < SIZE; r++) for (int c = 0; c < SIZE; c++) myMat[r][c] = mat[r][c]; } Point Encryptor::GetCoordinates2(char ch) const { // public shell to test GetCoordinates return GetCoordinates(ch); } apstring Encryptor::EncryptTwo2(const apstring &pair) const { // public shell to test EncryptTwo() return EncryptTwo(pair); } Point::Point(int newRow, int newCol) : row(newRow), col(newCol) {} ostream &operator<<(ostream &os, Point &p) { return os << "row = " << p.row << " col = " << p.col; } void testGetCoordinates() { cout << "Part a. Testing GetCoordinates():\n"; Encryptor x; Point p1 = x.GetCoordinates2('P'), p2 = x.GetCoordinates2('8'), p3 = x.GetCoordinates2('M'); cout << "char Point coordinates" << endl; cout << " " << 'P' << '\t' << p1 << endl; cout << " " << '8' << '\t' << p2 << endl; cout << " " << 'M' << '\t' << p3 << endl << endl; } void testEncryptTwo() { cout << "Part b. Testing EncryptTwo():\n"; const int ROWS(2), COLS(8); apstring m[ROWS][COLS] = { {" Letters:", "BR", "NE", "ET", "RE", "TH", "PR", "GG"}, {"Encrypted:"} }; Encryptor x; for (int c = 1; c < COLS; c++) m[1][c] = x.EncryptTwo2(m[0][c]); for (int r = 0; r < ROWS; r++) for (int c = 0; c < COLS; c++) { cout << m[r][c] << " "; if ( c == COLS - 1 ) cout << endl; } cout << endl; } void testEncryptWord() { cout << "Part c. Testing EncryptWord():\n"; const int ROWS(2), COLS(4); apstring m[ROWS][COLS] = { {" Word:", "COMPUTER", "SCIENCE", "STUDENTS"}, {"Encrypted:"} }; Encryptor x; for (int c = 1; c < COLS; c++) m[1][c] = x.EncryptWord(m[0][c]); for (int r = 0; r < ROWS; r++) for (int c = 0; c < COLS; c++) { cout << m[r][c] << " "; if ( c == COLS - 1 ) cout << endl; } cout << endl; }