#include #include #include "apmatrix.h" class Quilt { public: Quilt(istream & inFile, int rowsOfBlocks, int colsOfBlocks); // constructor, given number of blocks in each row and column apmatrix QuiltToMat(); // returns a matrix with the entire quilt stored in it private: apmatrix myBlock; // stores pattern for one block int myRowsOfBlocks; // number of rows of blocks in the quilt int myColsOfBlocks; // number of columns of blocks in the quilt void PlaceBlock(int startRow, int startCol, apmatrix & qmat); void PlaceFlipped(int startRow, int startCol, apmatrix & qmat); }; Quilt::Quilt(istream & inFile, int rowsOfBlocks, int colsOfBlocks) : myBlock(0,0), myRowsOfBlocks(rowsOfBlocks), myColsOfBlocks(colsOfBlocks) // pre: inFile is open, rowsOfBlocks > 0, colsOfBlocks > 0 // post: myRowsOfBlocks and myColsOfBlocks are initialized to // the number of rows and columns of blocks that make up // the quilt; myBlock has been resized and initialized to the // block pattern from the stream inFile { int j,k,r,c; inFile >> r >> c; myBlock.resize(r,c); // make room for pattern for(j=0; j < r; j++) { for(k=0; k < c; k++) { inFile >> myBlock[j][k]; // non-blank chars, >> is ok } } } void Quilt::PlaceBlock(int startRow, int startCol, apmatrix & qmat) // pre: startRow >= 0; startCol >= 0; // startRow + myBlock.numrows() <= qmat.numrows(); // startCol + myBlock.numcols() <= qmat.numcols(); // post: myBlock has been copied into the matrix // qmat with its upper-left corner at the position // startRow, startCol { int r,c; for(r=0; r < myBlock.numrows(); r++) { for(c=0; c < myBlock.numcols(); c++) { qmat[startRow + r][startCol + c] = myBlock[r][c]; } } } void Quilt::PlaceFlipped(int startRow, int startCol, apmatrix & qmat) // pre: startRow >= 0; startCol >= 0; // startRow + myBlock.numrows() <= qmat.numrows(); // startCol + myBlock.numcols() <= qmat.numcols(); // post: a flipped version ofmyBlock has been copied into the // matrix qmat with its upper-left corner at the position // startRow, startCol { int r,c; for(r=0; r < myBlock.numrows(); r++) { for(c=0; c < myBlock.numcols(); c++) { qmat[startRow + r][startCol + c] = myBlock[myBlock.numrows() - r - 1][c]; } } } apmatrix Quilt::QuiltToMat() { apmatrix qmat(myRowsOfBlocks * myBlock.numrows(), myColsOfBlocks * myBlock.numcols()); int j, row, col; int totalBlocks = myRowsOfBlocks*myColsOfBlocks; for(j=0; j < totalBlocks; j++) { col = (j % myColsOfBlocks) * myBlock.numcols(); row = (j / myColsOfBlocks) * myBlock.numrows(); if (j % 2 == 0) { PlaceBlock(row,col,qmat); } else { PlaceFlipped(row,col,qmat); } } return qmat; } void print(const apmatrix & mat) { int j,k; for(j=0; j < mat.numrows(); j++) { for(k=0; k < mat.numcols(); k++) { cout << mat[j][k]; } cout << endl; } } int main() { ifstream input("c:\\ets\\exam\\quilt.dat"); Quilt q(input,4,5); print(q.QuiltToMat()); return 0; }