quachtridat

math

Sep 28th, 2020
659
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace math {
  2.     struct base_expression_struct;
  3.     using base_expression_t = struct base_expression_struct;
  4.     struct base_expression_struct {};
  5.    
  6.     using number_t = double;
  7.    
  8.     using symbol_t = std::string;
  9.    
  10.     template <typename TName = char, typename TValue = number_t>
  11.     struct variable_struct;
  12.     template <typename TName = char, typename TValue = number_t>
  13.     using variable_t = struct variable_struct<TName, TValue>;
  14.     template <typename TName, typename TValue>
  15.     struct variable_struct : public base_expression_t {
  16.         TName name;
  17.         std::optional<TValue> value;
  18.        
  19.         struct variable_struct<TName, TValue>& operator=(TName other_name) {
  20.             if (this->name != other_name) {
  21.                 this->name = other_name;
  22.                 this->value.reset();
  23.             };
  24.             return *this;
  25.         };
  26.        
  27.         friend ostream& operator<<(ostream& os, const struct variable_struct<TName, TValue>& current_var) {
  28.             return os << current_var.name;
  29.         };
  30.     };
  31.    
  32.     enum operator_enum {
  33.         none,
  34.         plus,
  35.         minus,
  36.         multiply,
  37.         divide
  38.     };
  39.    
  40.     template <typename T = operator_enum>
  41.     struct operator_struct;
  42.     template <typename T = operator_enum>
  43.     using operator_t = struct operator_struct<T>;
  44.     template <typename T>
  45.     struct operator_struct {
  46.         T value;
  47.        
  48.         friend ostream& operator<<(ostream& os, const operator_t<T>& op) {
  49.             return os << op.value;
  50.         };
  51.     };
  52.    
  53.     template <typename TNumber = number_t, typename TVariable = variable_t<>>
  54.     struct term_struct;
  55.     template <typename TNumber = number_t, typename TVariable = variable_t<>>
  56.     using term_t = struct term_struct<TNumber, TVariable>;
  57.     template <typename TNumber, typename TVariable>
  58.     struct term_struct : public base_expression_t {
  59.         TNumber number;
  60.         TVariable variable;
  61.        
  62.         friend ostream& operator<<(ostream& os, const term_t<TNumber, TVariable>& term) {
  63.             return os << term.number << term.variable;
  64.         };
  65.     };
  66.    
  67.     template <typename TOperator = operator_t<>, typename TTerm = term_t<>>
  68.     struct simple_expression_struct;
  69.     template <typename TOperator = operator_t<>, typename TTerm = term_t<>>
  70.     using simple_expression_t = struct simple_expression_struct<TOperator, TTerm>;
  71.     template <typename TOperator, typename TTerm>
  72.     struct simple_expression_struct : public base_expression_t {
  73.         TOperator op;
  74.         TTerm term;
  75.        
  76.         friend ostream& operator<<(ostream& os, const struct simple_expression_struct<TOperator, TTerm>& simple_expression) {
  77.             return os << simple_expression.op << '(' << simple_expression.term << ')';
  78.         };
  79.     };
  80.    
  81.     template <typename TSimpleExpression = simple_expression_t<>, typename TContainer = std::list<TSimpleExpression>>
  82.     using simple_expressions_t = TContainer;
  83.    
  84.     template <typename TFirst = simple_expressions_t<>, typename TOperator = operator_t<>, typename TSecond = simple_expressions_t<>>
  85.     struct expression_struct;
  86.     template <typename TFirst = simple_expressions_t<>, typename TOperator = operator_t<>, typename TSecond = simple_expressions_t<>>
  87.     using expression_t = struct expression_struct<TFirst, TOperator, TSecond>;
  88.     template <typename TFirst, typename TOperator, typename TSecond>
  89.     struct expression_struct {
  90.         TFirst first;
  91.         TOperator op;
  92.         TSecond second;
  93.        
  94.         friend ostream& operator<<(ostream& os, const struct expression_struct<TFirst, TOperator, TSecond>& expression) {
  95.             return os << '(' << expression.first << ')' << expression.op << '(' << expression.second << ')';
  96.         };
  97.     };
  98. };
  99.  
  100. template <typename T>
  101. ostream& operator<<(ostream& os, const std::list<T>& l) {
  102.     os << '[';
  103.     if (!l.empty()) {
  104.         typename std::list<T>::const_iterator it = l.cbegin();
  105.         os << *it;
  106.         for (++it; it != l.cend(); ++it) {
  107.             os << ',' << ' ' << *it;
  108.         };
  109.     };
  110.     os << ']';
  111.     return os;
  112. };
  113.  
  114. template <typename T>
  115. ostream& operator<<(ostream& os, const std::vector<T>& vec) {
  116.     os << '[';
  117.     if (!vec.empty()) {
  118.         typename std::vector<T>::const_iterator it = vec.cbegin();
  119.         os << *it;
  120.         for (++it; it != vec.cend(); ++it) {
  121.             os << ',' << ' ' << *it;
  122.         };
  123.     };
  124.     os << ']';
  125.     return os;
  126. };
RAW Paste Data