SHARE
TWEET

Untitled

a guest May 19th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <forward_list>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. enum Type { T_Cell, T_Plus, T_Minus, T_Mult, T_Div, T_UnMinus, T_Value };
  8.  
  9. template <class T>
  10. class Cell
  11. {
  12.     struct CellStruct
  13.     {
  14.         Type type;
  15.         union
  16.         {
  17.             T value;
  18.             const Cell* cell;
  19.         };
  20.  
  21.         CellStruct(const Cell* c) : type(T_Cell), cell(c) {}
  22.         CellStruct(T v) : type(T_Value), value(v) {}
  23.         CellStruct(Type t) : type(t), value(0) {}
  24.     };
  25.  
  26.     forward_list<CellStruct> operations;
  27.     using iterator = typename forward_list<CellStruct>::const_iterator;
  28.  
  29.     Cell& operator <<(Type op) { operations.emplace_front(op); return *this; }
  30.     Cell& operator <<(T value) { operations.emplace_front(value); return *this; }
  31.     Cell& operator <<(const Cell& value) { operations.emplace_front(&value); return *this; }
  32.     Cell& operator <<(Cell &&value) { operations.splice_after(operations.before_begin(), forward_list<CellStruct>(value.operations)); return *this; }
  33.  
  34.     Cell& Clear() { operations.clear(); return *this; }
  35.  
  36.     template <class U, class V>
  37.     Cell(U &&first, V &&second, Type op)
  38.     {
  39.         *this << forward<V>(second) << forward<U>(first) << op;
  40.     }
  41.  
  42.     T Calc(iterator& it) const
  43.     {
  44.         switch (it->type)
  45.         {
  46.         case T_Cell: return (T)*(it++)->cell;
  47.         case T_Plus: return ++it, Calc(it) + Calc(it);
  48.         case T_Minus: return ++it, Calc(it) - Calc(it);
  49.         case T_Mult: return ++it, Calc(it) * Calc(it);
  50.         case T_Div: return ++it, Calc(it) / Calc(it);
  51.         case T_UnMinus: return -Calc(++it);
  52.         case T_Value: return (it++)->value;
  53.         default: exit(1);
  54.         }
  55.     }
  56. public:
  57.     explicit operator T() const {
  58.         iterator start = operations.cbegin();
  59.         return Calc(start);
  60.     }
  61.  
  62.     Cell() {}
  63.     Cell(T value) { *this << value; }
  64.     template <class U> Cell& operator =(U&& val) { Clear(); return *this << forward<U>(val); }
  65.  
  66.     Cell operator-() const & { Cell ret; return ret << *this << T_UnMinus; }
  67.     Cell operator-() && { return *this << T_UnMinus; }
  68.  
  69.     template <class U, class V> friend auto operator +(U &&first, V &&second)->Cell<T> { return Cell<T>(forward<U>(first), forward<V>(second), T_Plus); }
  70.     template <class U, class V> friend auto operator -(U &&first, V &&second)->Cell<T> { return Cell<T>(forward<U>(first), forward<V>(second), T_Minus); }
  71.     template <class U, class V> friend auto operator *(U &&first, V &&second)->Cell<T> { return Cell<T>(forward<U>(first), forward<V>(second), T_Mult); }
  72.     template <class U, class V> friend auto operator /(U &&first, V &&second)->Cell<T> { return Cell<T>(forward<U>(first), forward<V>(second), T_Div); }
  73.  
  74.  
  75.     template <class U> auto operator +=(U &&other)->Cell<T> { return *this << forward<U>(other) << T_Plus; }
  76.     template <class U> auto operator -=(U &&other)->Cell<T> { return *this << forward<U>(other) << T_Minus; }
  77.     template <class U> auto operator *=(U &&other)->Cell<T> { return *this << forward<U>(other) << T_Mult; }
  78.     template <class U> auto operator /=(U &&other)->Cell<T> { return *this << forward<U>(other) << T_Div; }
  79. };
  80.  
  81.  
  82. template <class T>
  83. class SuperCalc
  84. {
  85.     vector<vector<Cell<T>>> _data;
  86. public:
  87.     SuperCalc(int m, int n) : _data(m,vector<Cell<T>>(n, 0)) {}
  88.  
  89.     Cell<T>& operator() (int i, int j)
  90.     {
  91.         return _data[i][j];
  92.     }
  93. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top