Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <variant>
- #include <string>
- #include <functional>
- #include <cmath>
- #include <map>
- //--DEFINITIONS--//
- enum class factortype { num , // FACTYPE: indicate if the factor is numerical
- litteral }; // or is unknown ( symbolical )
- struct factor // FACTOR: rappresentation of an object that is part of a monomial expression
- { // a monomial expression like ⅔x will be rappresented as:
- std::variant // [ num = 2 ; power = 1 ] [ num = 3 ; power = -1 ] [ letter = x ; power = 1 ]
- < //
- std::string, //
- double //
- > //
- value; //
- factortype type; //
- double power; //
- }; //
- struct monom // MONOM: rappresentation of an object that is an entire monomial expression
- { // contains a vector of numeral factors and a vector of litteral factors
- std::vector<factor> numeral; // sign is just stored as boolean
- std::vector<factor> litteral; //
- bool sign; //
- }; //
- enum class tokentype { monomial , // TYPES: monomial expression
- operation , // an operator ( such as * - + / ^ ! )
- section , // a section delimitated by "(" and ")"
- oparen , // an opened parentesis
- cparen , // a closed parentesis
- obraket , // an opened braket for delimiting multichar name for variables
- cbraket , // a closed braket for delimiting multichar name for variables
- function , // a function name and the start of the arguments token
- comma, // the comma separeting function arguments
- endarg }; // the end of the argument token list
- enum class operation { sum, // OP: sum between two real numbers with a positive or negative sign and maybe unknow
- minus, // change sign of the following token
- mul, // moltiplication of two tokens
- div, // division of two tokens
- pow, // power of a token
- fact, // factorial of a token
- modul }; // rest of an integer division
- struct token // TOKEN: rappresentation of a self semantic unit of an expression, just as
- { // '2x' or '+' or whatelse
- std::variant // sequence of tokens can be evaluated to get a result
- < //
- monom, //
- operation, //
- std::vector<token>, //
- std::string //
- > //
- data; //
- tokentype type; //
- }; //
- //--CODE--//
- struct algebra
- {
- using func = std::function<token(token)>;
- std::map<std::string,func> functions;
- static std::map<std::string,func> static_functions;
- token parse(const std::string & source);
- };
- token algebra::parse(const std::string & source)
- {
- token result;
- token tmp_token;
- monom tmp_monomial;
- std::string tmp_function_name;
- for ( int i = 0 ; i < source.size() ; i++ )
- {
- if ( source[i] == '+' || source[i] == '-' || source[i] == '*' || source[i] == '/' || source[i] == '^' || source[i] == '!' || source[i] == '%' )
- {
- switch ( source[i] )
- {
- case ( '+' ):
- tmp_token.data = operation::sum;
- break;
- case ( '-' ):
- tmp_token.data = operation::minus;
- break;
- case ( '*' ):
- tmp_token.data = operation::mul;
- break;
- case ( '/' ):
- tmp_token.data = operation::div;
- break;
- case ( '^' ):
- tmp_token.data = operation::pow;
- break;
- case ( '!' ):
- tmp_token.data = operation::fact;
- break;
- case ( '%' ):
- tmp_token.data = operation::modul;
- break;
- }
- tmp_token.type = tokentype::operation;
- std::get<std::vector<token>>(result.data).push_back(tmp_token);
- continue;
- }
- }
- return result;
- }
- //---MAIN---//
- int main()
- {
- algebra a;
- auto x = a.parse("");
- std::cout << std::get<std::vector<token>>(x.data).size();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement