10652 - Block 2   

Description

In function.h we define a class called Block. Several constructors and member functions are implemented. Your task is to complete the implementation of the following two functions:

1. void clockwise90();
This function rotates the pattern by 90 degrees clockwise. For example,

@OOX
OOXX
XXXX
XXXX

becomes

XXO@
XXOO
XXXO
XXXX

 

2. Block& halfsize();
This function enlarges the pattern to make it twice as half and half as tall. For example,

AABB
AABB
CCDD
CCDD

becomes

AB
CD

 

To execute halfsize() conveniently, the input is always designed as the form of

AABB
AABB
CCDD
CCDD

You need to implement the two functions in function.cpp.

 

sample code of main.cpp

#include "function.h"
#include <iostream>

int main()
{
        Block b{4, "OOXX" "OOXX" "XXOO" "XXOO"}; 
 
        b.clockwise90(); 
        b.halfsize();
        std::cout << b;

}

code of function.h

#ifndef _BLOCKCLASS_
#define _BLOCKCLASS_
#include <iostream>
#include <algorithm>
#include <utility>
class Block {
private:
    int size;
    char** pattern; // array of pointers to buf
    char* buf;      // 2D pattern stored in 1D raster scan order
public:
    Block(): size{0}, pattern{nullptr}, buf{nullptr} {}

    Block(int sz, const char* pat):
        size{sz}, pattern{new char* [size]}, buf{new char[size*size]}
    {
        // std::cout << "custom constructor\n";
        for (int i=0; i<size*size; i++) buf[i]=pat[i];
        for (int i=0; i<size; i++) {
            pattern[i] = (char*) &buf[i*size];
        }
    }

    Block(const Block &b):
        size{b.size}, pattern{new char* [size]}, buf{new char[size*size]}
    {
        // std::cout << "copy constructor\n";
        for (int i=0; i<size*size; i++) buf[i]=b.buf[i];
        for (int i=0; i<size; i++) {
            pattern[i] = (char*) &buf[i*size];
        }
    }

    Block& operator=(Block& c) {
        Block b{c};
        // std::cout << "copy assignment\n";
        std::swap(buf, b.buf);
        std::swap(pattern, b.pattern);
        size = b.size;
        return *this;
    }

    // rvalue reference
    Block(Block&& b): size{b.size}, pattern{b.pattern}, buf{b.buf}
    {
        // std::cout << "move constructor\n";
        b.size = 0;
        b.pattern = nullptr;
        b.buf = nullptr;
    }

    // rvalue reference
    Block& operator=(Block&& b) {
        // std::cout << "move assignment\n";
        if (this != &b) {
            delete [] buf;
            delete [] pattern;

            buf = b.buf;
            pattern = b.pattern;
            size = b.size;

            b.buf = nullptr;
            b.pattern = nullptr;
            b.size = 0;
        }
        return *this;
    }

    ~Block()
    {
        // std::cout << "destructor\n";
        delete [] buf;
        delete [] pattern;
    }

    friend std::ostream& operator<<(std::ostream& os, Block& b) {
        for (int i=0; i<b.size; i++) {
            for (int j=0; j<b.size; j++) {
                os << b.pattern[i][j];
            }
            os << std::endl;
        }
        return os;
    }

    // the task is to implement the following two functions
    void clockwise90();
    Block& halfsize();

};
#endif

Input

The input will be given in main.cpp

Output

The output will be generated by main.cpp

Sample Input  Download

Sample Output  Download

Partial Judge Code

10652.cpp

Partial Judge Header

10652.h

Tags




Discuss