Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- template < typename type >
- class matrix;
- template < typename type >
- class line {
- private:
- friend class matrix < type >;
- matrix < type > &parent;
- size_t X;
- line(matrix < type > &_parent, size_t _X): parent(_parent), X(_X) {}
- line(line &&_line): parent(_line.parent), X(_line.X) {}
- public:
- line(const line&) = delete;
- line& operator = (const line&) = delete;
- type& operator[](size_t Y) {
- return parent.data[X][Y];
- }
- const type& operator[](size_t Y) const {
- return parent.data[X][Y];
- }
- ~line() = default;
- };
- template < typename type >
- class matrix {
- private:
- typedef matrix < type > matrix_t;
- typedef line < type > line_t;
- typedef function < type (const type&, const type&) > bopf;
- typedef function < void (type&) > medft;
- typedef function < type (const type&) > mtrft;
- type **data = nullptr;
- size_t X;
- size_t Y;
- public:
- bool exist() const {
- return data;
- }
- void destroy() {
- if (!exist()) {
- return;
- }
- for (size_t i = 0; i < X; ++i){
- delete[] data[i];
- }
- delete[] data;
- data = nullptr;
- }
- void resize(size_t a, size_t b) {
- destroy();
- X = a;
- Y = b;
- data = new type*[X];
- for (size_t i = 0; i < X; ++i){
- data[i] = new type[Y];
- }
- }
- matrix() = default;
- matrix(size_t _X, size_t _Y){
- resize(_X, _Y);
- };
- matrix(const matrix_t &m){
- resize(m.X, m.Y);
- for (int k = 0; k < X; k++)
- for (int i = 0; i < Y; i++)
- data[k][i] = m.data[k][i];
- };
- matrix(matrix_t &&m): data(m.data), X(m.X), Y(m.Y) {
- m.data = nullptr;
- }
- ~matrix(){
- destroy();
- };
- void read(istream &is = cin) {
- if (!exist()){
- return;
- }
- for (size_t i = 0; i < X; ++i){
- for (size_t j = 0; j < Y; ++j){
- is >> data[i][j];
- }
- }
- }
- void print(ostream &os = cout, size_t wal = 5) const {
- if (!exist()){
- return;
- }
- for (size_t i = 0; i < X; ++i){
- for (size_t j = 0; j < Y; ++j){
- os << setw(wal) << data[i][j] << " ";
- }
- os << "\n";
- }
- };
- size_t getX() const {
- return X;
- }
- size_t getY() const {
- return Y;
- }
- void genMatrix()
- {
- for (int i = 0; i < X; i++)
- for (int j = 0; j < Y; j++)
- data[i][j] = 0;
- };
- static matrix_t mbop(const matrix_t &a, const matrix_t &b, bopf func) {
- if (!a.exist() || !b.exist() || a.getX() != b.getX() || a.getY() != b.getY()) {
- return matrix_t();
- }
- const size_t &X = a.X, &Y = a.Y;
- matrix_t r(X, Y);
- for (size_t i = 0; i < X; ++i){
- for (size_t j = 0; j < Y; ++j){
- r.data[i][j] = func(a.data[i][j], b.data[i][j]);
- }
- }
- return r;
- }
- static void med(matrix_t &m, medft func) {
- const size_t &X = m.X, &Y = m.Y;
- for (size_t i = 0; i < X; ++i) {
- for (size_t j = 0; j < Y; ++j) {
- func(m.data[i][j]);
- }
- }
- }
- matrix_t operator + (const matrix &m) const {
- return mbop(*this, m, plus < type >());
- }
- matrix_t operator - (const matrix &m) const {
- return mbop(*this, m, minus < type >());
- }
- line_t operator [](size_t x){
- return line_t(*this, x);
- };
- };
- int main(){
- matrix < int > a(2, 3), b(2, 3);
- auto rf = [](int &v){ cin >> v; };
- auto medptr = matrix < int > :: med;
- medptr(a, rf);
- medptr(b, rf);
- matrix < int > c = a + b;
- c.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement