Skip to content

Instantly share code, notes, and snippets.

@rusdevops
Last active April 20, 2018 11:58
Show Gist options
  • Save rusdevops/9844abc96ce8d38a40743a09acb4d3e8 to your computer and use it in GitHub Desktop.
Save rusdevops/9844abc96ce8d38a40743a09acb4d3e8 to your computer and use it in GitHub Desktop.
RK II

Задачи

  1. Реализовать класс Complex:
// complex.hpp                                             | // main.cpp                               
                                                           |                                           
class Complex {                                            | #include <complex.hpp>                    
                                                           |                                           
public:                                                    | int main() {                              
                                                           |                                           
  typedef double type;                                     |   Complex c1{};                         
                                                           |   Complex c2{3};                        
  Complex();                                               |   Complex c3{1, 4};                     
                                                           |                                     
  Complex(type a);                                         |   cout << c1 << endl;         
                                                           |   cout << c2 << endl;         
  Complex(type a, type b);                                 |   cout << c3 << endl;         
                                                           |                                     
  Complex(const Complex& c);                               |   Complex c4{c3};                       
                                                           |   Complex c5;                           
  auto swap(Complex& c) -> void;                           |   c5 = c4;                              
                                                           |                                     
  auto operator=(const Complex& c) -> Complex&;            |   cout << c4 << endl;         
                                                           |   cout << c5 << endl;         
  auto operator+(const Complex& c) -> Complex;             |                                     
                                                           |   cout << c2 + c5 << endl;      
  auto operator-(const Complex& c) -> Complex;             |   cout << c2 - c5 << endl;                    
                                                           |   cout << c2 * c5 << endl;                                      
  auto operator*(const Complex& c) -> Complex;             |   cout << c2 / c5 << endl;                                      
                                                           |                                   
  auto operator/(const Complex& c) -> Complex;             |   cout << boolalpha;
                                                           |   cout << (c2 != c5) << endl;                                      
  auto operator!() -> Complex;                             |   cout << (c3 == c5) << endl;                                      
                                                           |                                     
  auto operator==(const Complex& c) -> bool;               |   cout << static_cast<double>(c5) << endl;                                    
                                                           |   cout << c5[0] << "," << c5[1] << endl;                                      
  auto operator!=(const Complex& c) -> bool;               |                                     
                                                           |   cin >> c1;                                      
  explicit operator type();                                |                                                     
                                                           |   ofstream output{ "complex.bin" };                                     
  auto operator[](size_t index) -> type;                   |   output << c1;                                      
                                                           |                                     
  friend                                                   |   ifstream input{ "complex.bin" };                                
  auto operator>>(istream&, Complex& c) -> istream&;       |   input >> c1;                                      
                                                           |                                     
  friend                                                   |   auto c6 = Complex::from_string("(1,2)");  
  auto operator<<(ostream&, const Complex& c) -> ostream&; |   cout << c6 << endl;                                      
                                                           |                                     
  static Complex from_string(const std::string& string);   |   c1.swap(c6);                                      
                                                           |   cout << c1 << endl;                                      
  ~Complex();                                              |                                     
                                                           |   cout << !c1 << endl;                                            
private:                                                   |                                     
  std::pair<type, type>* pair;                             |   cout << c5[2] << endl;                                      
};                                                         | }
                                                                                                                                     
  1. Заполнить отчет
$ git clone https://github.com/<username>/complex
$ cd complex
$ g++ main.cpp complex.cpp -I. -std=c++11 -o complex-example
$ ./complex-example
...

Требования

  • Класс Complex сделать шаблонным
  • Пометить все необходимые методы квалификатором const
  • Использовать списки инициализации, как для полей, так и для конструкторов
  • Избежать дублирование кода

Задачи

  1. Реализовать класс Matrix:
// matrix.hpp                                       | // main.cpp
                                                    |
class Matrix {                                      | #include <matrix.hpp>
                                                    | 
public:                                             | int main() {
                                                    |   
    typedef int type;                               |   Matrix m1{3,5};
                                                    |   Matrix m2{1, 2, 3, 4, 5, 6};
    Matrix();                                       |   Matrix m3{m2};
                                                    |
    Matrix(std::initialer_list<type> list);         |   cout << m1 << endl; 
                                                    |   cout << m2 << endl;
    Matrix(const Matrix&);                          |   cout << m3 << endl;
                                                    |   
    auto swap(Matrix&) -> void;                     |   Matrix m4;
                                                    |   m4 = m3;
    auto operator=(const Matrix& q);                |   cout << m4 << endl;
                                                    |   
    auto empty() -> bool;                           |   m4[0][0] = 8;
                                                    |   cout << m4 << endl;
    auto columns() -> size_t;                       |   
                                                    |   cout << boolalpha 
    auto rows() -> size_t;                          |        << (m2 == m3) 
                                                    |        << (m3 == m4) << endl; 
    auto operator-(const Matrix&) -> Matrix;        |        
                                                    |   cout << m1.rows()      
    auto operator+(const Matrix&) -> Matrix;        |        << m1.columns() << endl; 
                                                    |   
    auto operator[](size_t index) -> type*;         |   Matrix m5{0, 2, 4, 6, 8, 10};    
                                                    |   cout << m3 + m5 << endl; 
    bool operator==(const Matrix&);                 |   cout << m3 - m5 << endl;
                                                    | 
    friend                                          |   Matrix m6{}; 
    auto operator<<(ostream&, Matrix&) -> ostream&; |   cout << m6.empty();
                                                    |
    friend                                          |   ofstream out{"matrix.bin"};
    auto operator>>(istream&, Matrix&) -> istream&; |   out << m3;
                                                    |   
    ~Matrix();                                      |   ifstream in{"matrix.bin"};
                                                    |   in >> m6;
private:                                            |   m3.swap(m6);
    type** ptr;                                     |   cout << m3 << endl;
};                                                  | }
  1. Заполнить отчет
$ git clone https://github.com/<username>/matrix
$ cd matrix
$ g++ main.cpp queue.cpp -I. -std=c++11 -o matrix-example
$ ./matrix-example
...

Требования

  • Класс Matrix сделать шаблонным
  • Пометить все необходимые методы квалификатором const
  • Использовать списки инициализации, как для полей, так и для конструкторов
  • Избежать дублирование кода

Задачи

  1. Реализовать класс Queue:
// queue.hpp                                        | // main.cpp
                                                    |
class Queue {                                       | #include <queue.hpp>
                                                    | 
public:                                             | int main() {
                                                    |   
    typedef int type;                               |   Queue q1;
                                                    |   
    Queue();                                        |   Queue q2 = {1, 2, 3, 4};
                                                    |
    Queue(std::initialer_list<type> list);          |   Queue q3{q2};
                                                    |
    Queue(const Queue& q);                          |   cout << q2 << endl;
                                                    |   cout << q3 << endl;
    auto swap(Queue& q) -> void;                    |
                                                    |   q1.swap(q3);
    auto operator=(const Queue& q);                 |   cout << q1 << endl;
                                                    |
    auto empty() -> bool;                           |   cout << boolalpha
                                                    |        << q3.empty();
    auto size() -> size_t;                          |  
                                                    |   cout << q1.size() << endl;
    auto push(type) -> void;                        |
                                                    |   q3.push(5);
    auto pop() -> void;                             |   cout << q3 << endl;
                                                    |   
    auto front() -> type&;                          |   ofstream output{ "queue.bin" };
                                                    |   output << q3;
    auto back() -> type&;                           |   
                                                    |   Queue q4;
    bool operator==(const Queue& q);                |   ifstream input{ "queue.bin" };
                                                    |   input >> q4;
    friend                                          |   
    auto operator<<(ostream&, Queue&) -> ostream&;  |   cout << (q4 == q3) << endl; 
                                                    |
    friend                                          |   cout << q4.front() << endl;
    auto operator>>(istream&, Queue&) -> istream&;  |   cout << q5.back() << endl;
                                                    |   
    ~Queue();                                       |   q4.pop();
                                                    |   cout << q4 << endl;
private:                                            |
    ForwardList* list;                              |   q4.front() = 6;
};                                                  | }
  1. Заполнить отчет
$ git clone https://github.com/<username>/queue
$ cd queue
$ g++ main.cpp queue.cpp -I. -std=c++11 -o queue-example
$ ./queue-example
...

Требования

  • Класс Queue сделать шаблонным
  • Пометить все необходимые методы квалификатором const
  • Использовать списки инициализации, как для полей, так и для конструкторов
  • Избежать дублирование кода
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment