Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <vector>
  4. #include <variant>
  5. #include <string>
  6. #include <functional>
  7. #include <cmath>
  8. #include <map>
  9.  
  10. //--DEFINITIONS--//
  11.  
  12. enum class factortype { num ,           // FACTYPE: indicate if the factor is numerical
  13.                         litteral };     //          or is unknown ( symbolical )
  14.  
  15. struct factor                           // FACTOR: rappresentation of an object that is part of a monomial expression
  16. {                                       //         a monomial expression like ⅔x will be rappresented as:
  17.     std::variant                        //         [ num = 2 ; power = 1 ] [ num = 3 ; power = -1 ] [ letter = x ; power = 1 ]
  18.     <                                   //
  19.         std::string,                    //
  20.         double                          //
  21.     >                                   //
  22.     value;                              //
  23.     factortype type;                    //
  24.     double power;                       //
  25. };                                      //
  26.  
  27. struct monom                            // MONOM:   rappresentation of an object that is an entire monomial expression
  28. {                                       //          contains a vector of numeral factors and a vector of litteral factors
  29.     std::vector<factor> numeral;        //          sign is just stored as boolean
  30.     std::vector<factor> litteral;       //
  31.     bool sign;                          //
  32. };                                      //
  33.  
  34. enum class tokentype {      monomial ,  // TYPES:   monomial expression
  35.                             operation , //          an operator ( such as * - + / ^ ! )
  36.                             section ,   //          a section delimitated by "(" and ")"
  37.                             oparen ,    //          an opened parentesis
  38.                             cparen ,    //          a closed parentesis
  39.                             obraket ,   //          an opened braket for delimiting multichar name for variables
  40.                             cbraket ,   //          a closed braket for delimiting multichar name for variables
  41.                             function ,  //          a function name and the start of the arguments token
  42.                             comma,      //          the comma separeting function arguments
  43.                             endarg  };  //          the end of the argument token list
  44.  
  45. enum class operation {      sum,        // OP:      sum between two real numbers with a positive or negative sign and maybe unknow
  46.                             minus,      //          change sign of the following token
  47.                             mul,        //          moltiplication of two tokens
  48.                             div,        //          division of two tokens
  49.                             pow,        //          power of a token
  50.                             fact,       //          factorial of a token
  51.                             modul    }; //          rest of an integer division
  52.  
  53.  
  54. struct token                            // TOKEN:   rappresentation of a self semantic unit of an expression, just as
  55. {                                       //          '2x' or '+' or whatelse
  56.     std::variant                        //          sequence of tokens can be evaluated to get a result
  57.     <                                   //
  58.         monom,                          //
  59.         operation,                      //
  60.         std::vector<token>,             //
  61.         std::string                     //
  62.     >                                   //
  63.     data;                               //
  64.     tokentype type;                     //
  65. };                                      //
  66.  
  67. //--CODE--//
  68.  
  69. struct algebra
  70. {
  71.     using func = std::function<token(token)>;
  72.  
  73.     std::map<std::string,func> functions;
  74.     static std::map<std::string,func> static_functions;
  75.  
  76.     token parse(const std::string & source);
  77. };
  78.  
  79. token algebra::parse(const std::string & source)
  80. {
  81.     token       result;
  82.     token       tmp_token;
  83.     monom       tmp_monomial;
  84.     std::string tmp_function_name;
  85.  
  86.     for ( int i = 0 ; i < source.size() ; i++ )
  87.     {
  88.         if ( source[i] == '+' || source[i] == '-' || source[i] == '*' || source[i] == '/' || source[i] == '^' || source[i] == '!' || source[i] == '%' )
  89.         {
  90.             switch ( source[i] )
  91.             {
  92.                 case ( '+' ):
  93.                 tmp_token.data = operation::sum;
  94.                 break;
  95.  
  96.                 case ( '-' ):
  97.                 tmp_token.data = operation::minus;
  98.                 break;
  99.  
  100.                 case ( '*' ):
  101.                 tmp_token.data = operation::mul;
  102.                 break;
  103.  
  104.                 case ( '/' ):
  105.                 tmp_token.data = operation::div;
  106.                 break;
  107.  
  108.                 case ( '^' ):
  109.                 tmp_token.data = operation::pow;
  110.                 break;
  111.  
  112.                 case ( '!' ):
  113.                 tmp_token.data = operation::fact;
  114.                 break;
  115.  
  116.                 case ( '%' ):
  117.                 tmp_token.data = operation::modul;
  118.                 break;
  119.             }
  120.             tmp_token.type = tokentype::operation;
  121.             std::get<std::vector<token>>(result.data).push_back(tmp_token);
  122.             continue;
  123.         }
  124.     }
  125.  
  126.     return result;
  127. }
  128.  
  129. //---MAIN---//
  130.  
  131. int main()
  132. {
  133.     algebra a;
  134.     auto x = a.parse("");
  135.     std::cout << std::get<std::vector<token>>(x.data).size();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement