Glenpl

parserv2

Nov 14th, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.74 KB | None | 0 0
  1. //basic mathematical parser v2.0
  2. //author jakub molinski
  3. //feel free to use, modify,
  4. //copy and sell this code
  5.  
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <cstdlib>
  10. #include <conio.h>
  11. #include <string>
  12. #include <math.h>
  13.  
  14. using namespace std;
  15.  
  16. // list of functions
  17. string parser(string);
  18. string switcher(string);
  19. string addition(string);
  20. string subtraction(string);
  21. string multiplication(string);
  22. string division(string);
  23. string exponentiation(string);
  24. string sqroot(string);
  25.  
  26. // main function
  27. int main()
  28. {
  29.     cout << setprecision(50);
  30.     //  cout << parser("M_E+M_PI+10+10+M_PI2+M_E+M_PI+M_E");
  31.     cout << endl << "Wynik: " << parser("(((5+5)+10)+(5*5))") << endl;
  32.  
  33.     // subfunction test
  34.     /*
  35.     cout << addition("10+15.5") << endl;
  36.     cout << subtraction("10-15.5") << endl;
  37.     cout << multiplication("10*15.5") << endl;
  38.     cout << division("10/15.5") << endl;
  39.     cout << exponentiation("10^3") << endl;
  40.     cout << sqroot("sqrt(81)") << endl;
  41.     */
  42.  
  43.     _getch();
  44.     return 0;
  45. }
  46.  
  47. //parser, main body
  48. string parser(string text)
  49. {
  50.  
  51.     // initiation, variables
  52.     string text_fixed = text;
  53.     int e;
  54.     int pi;
  55.     int pi2;
  56.     int bracket;
  57.     string operation;
  58.     int operation_length = 0;
  59.     bool done;
  60.     int operation_start = 0;
  61.     int operation_end = 0;
  62.  
  63.     // sources
  64.     const string M_E_str = "2.71828182846";
  65.     const string M_PI_str = "3.14159265358";
  66.     const string M_PI2_str = "6.28318530717";
  67.  
  68.     // first change, inserting constant values
  69.     e = text_fixed.find("M_E");
  70.     while (e != string::npos)
  71.     {
  72.         text_fixed.replace(e, 3, M_E_str);
  73.         e = text_fixed.find("M_E");
  74.     }
  75.     pi = text_fixed.find("M_PI");
  76.     while (pi != string::npos)
  77.     {
  78.         text_fixed.replace(pi, 4, M_PI_str);
  79.         pi = text_fixed.find("M_PI");
  80.     }
  81.     pi2 = text_fixed.find("M_PI2");
  82.     while (pi2 != string::npos)
  83.     {
  84.         text_fixed.replace(pi2, 5, M_PI2_str);
  85.         pi2 = text_fixed.find("M_PI2");
  86.     }
  87.  
  88.     // cutting into smaller operations, calculating them
  89.     bracket = text_fixed.find("(");
  90.     operation_start = bracket;
  91.  
  92.     while (
  93.         (text_fixed.find("(") != string::npos) ||
  94.         (text_fixed.find(")") != string::npos) ||
  95.         (text_fixed.find("+") != string::npos) ||
  96.         (text_fixed.find("-") != string::npos) ||
  97.         (text_fixed.find("*") != string::npos) ||
  98.         (text_fixed.find("/") != string::npos) ||
  99.         (text_fixed.find("^") != string::npos) ||
  100.         (text_fixed.find("sqrt") != string::npos))
  101.     {
  102.         done = false;
  103.         while (done == false)
  104.         {
  105.             if (bracket != string::npos)
  106.             {
  107.                 for (int i = (bracket + 1);; i++)
  108.                 {
  109.                     if (text_fixed[i] == ')')
  110.                     {
  111.                         if (operation[0] == 's')
  112.                         {
  113.                             operation_length++;
  114.                             operation += ')';
  115.                         }
  116.                         done = true;
  117.                         operation_end = ++i;
  118.                         cout << "Text fixed1: " << text_fixed << endl;
  119.                         cout << "Operacja1: " << operation << endl;
  120.  
  121.                         // calculate the operation and replace it with its result
  122.                         text_fixed.replace(operation_start, (operation_length + 2), switcher(operation));
  123.                         bracket = text_fixed.find("(");
  124.                         operation_start = bracket;
  125.                         operation_length = 0;
  126.                         operation = "";
  127.                         cout << "Text fixed2: " << text_fixed << endl;
  128.                         break;
  129.                     }
  130.                     if ((text_fixed[i] == '(') && (text_fixed[i - 1] != 't'))
  131.                     {
  132.                         bracket = i;
  133.                         operation_start = bracket;
  134.                         operation_length = 0;
  135.                         operation = "";
  136.                         cout << "Text fixed3: " << text_fixed << endl;
  137.                         cout << "Operacja3: " << operation << endl;
  138.                         break;
  139.                     }
  140.                     operation_length++;
  141.                     operation += text_fixed[i];
  142.                 }
  143.             }
  144.             else
  145.             {
  146.                 break;
  147.             }
  148.         }
  149.     }
  150.  
  151.     // returning modified text
  152.     return text_fixed;
  153. }
  154.  
  155. //sends operation to appropriate calculating function
  156. string switcher(string operation)
  157. {
  158.     if (operation.find("+") != string::npos)
  159.     {
  160.         return addition(operation);
  161.     }
  162.     if (operation.find("-") != string::npos)
  163.     {
  164.         return subtraction(operation);
  165.     }
  166.     if (operation.find("*") != string::npos)
  167.     {
  168.         return multiplication(operation);
  169.     }
  170.     if (operation.find("/") != string::npos)
  171.     {
  172.         return division(operation);
  173.     }
  174.     if (operation.find("^") != string::npos)
  175.     {
  176.         return exponentiation(operation);
  177.     }
  178.     if (operation.find("sqrt") != string::npos)
  179.     {
  180.         return sqroot(operation);
  181.     }
  182.     else
  183.     {
  184.         cout << "\n\n\n BLAD SWITCHERA!!! \n\n\n";
  185.         exit(0);
  186.     }
  187. }
  188.  
  189. //basic addition
  190. string addition(string to_add)
  191. {
  192.     // variables
  193.     int ch_place = to_add.find("+");
  194.     string leftside_str = "";
  195.     string rigthside_str = "";
  196.     double leftside;
  197.     double rigthside;
  198.     double result;
  199.     string result_str;
  200.  
  201.     // finding the left and the right side of "+" character
  202.     for (int i = 0; i < ch_place; i++)
  203.     {
  204.         leftside_str += to_add[i];
  205.     }
  206.     for (int i = ++ch_place; i < to_add.length(); i++)
  207.     {
  208.         rigthside_str += to_add[i];
  209.     }
  210.  
  211.     // converting string into double
  212.     leftside = atof(leftside_str.c_str());
  213.     rigthside = atof(rigthside_str.c_str());
  214.  
  215.     // calculating the result
  216.     result = leftside + rigthside;
  217.  
  218.     // converting double into string
  219.     ostringstream strs;
  220.     strs << result;
  221.     result_str = strs.str();
  222.  
  223.     // returning calculated value
  224.     return result_str;
  225. }
  226.  
  227. //basic substraction
  228. string subtraction(string to_sub)
  229. {
  230.     // variables
  231.     int ch_place = to_sub.find("-");
  232.     string leftside_str = "";
  233.     string rigthside_str = "";
  234.     double leftside;
  235.     double rigthside;
  236.     double result;
  237.     string result_str;
  238.  
  239.     // finding the left and the right side of "-" character
  240.     for (int i = 0; i < ch_place; i++)
  241.     {
  242.         leftside_str += to_sub[i];
  243.     }
  244.     for (int i = ++ch_place; i < to_sub.length(); i++)
  245.     {
  246.         rigthside_str += to_sub[i];
  247.     }
  248.  
  249.     // converting string into double
  250.     leftside = atof(leftside_str.c_str());
  251.     rigthside = atof(rigthside_str.c_str());
  252.  
  253.     // calculating the result
  254.     result = leftside - rigthside;
  255.  
  256.     // converting double into string
  257.     ostringstream strs;
  258.     strs << result;
  259.     result_str = strs.str();
  260.  
  261.     // returning calculated value
  262.     return result_str;
  263. }
  264.  
  265. //basic multiplication
  266. string multiplication(string to_mul)
  267. {
  268.     // variables
  269.     int ch_place = to_mul.find("*");
  270.     string leftside_str = "";
  271.     string rigthside_str = "";
  272.     double leftside;
  273.     double rigthside;
  274.     double result;
  275.     string result_str;
  276.  
  277.     // finding the left and the right side of "*" character
  278.     for (int i = 0; i < ch_place; i++)
  279.     {
  280.         leftside_str += to_mul[i];
  281.     }
  282.     for (int i = ++ch_place; i < to_mul.length(); i++)
  283.     {
  284.         rigthside_str += to_mul[i];
  285.     }
  286.  
  287.     // converting string into double
  288.     leftside = atof(leftside_str.c_str());
  289.     rigthside = atof(rigthside_str.c_str());
  290.  
  291.     // calculating the result
  292.     result = leftside * rigthside;
  293.  
  294.     // converting double into string
  295.     ostringstream strs;
  296.     strs << result;
  297.     result_str = strs.str();
  298.  
  299.     // returning calculated value
  300.     return result_str;
  301. }
  302.  
  303. //basic division
  304. string division(string to_div)
  305. {
  306.     // variables
  307.     int ch_place = to_div.find("/");
  308.     string leftside_str = "";
  309.     string rigthside_str = "";
  310.     double leftside;
  311.     double rigthside;
  312.     double result;
  313.     string result_str;
  314.  
  315.     // finding the left and the right side of "/" character
  316.     for (int i = 0; i < ch_place; i++)
  317.     {
  318.         leftside_str += to_div[i];
  319.     }
  320.     for (int i = ++ch_place; i < to_div.length(); i++)
  321.     {
  322.         rigthside_str += to_div[i];
  323.     }
  324.  
  325.     // converting string into double
  326.     leftside = atof(leftside_str.c_str());
  327.     rigthside = atof(rigthside_str.c_str());
  328.  
  329.     // calculating the result
  330.     result = leftside / rigthside;
  331.  
  332.     // converting double into string
  333.     ostringstream strs;
  334.     strs << result;
  335.     result_str = strs.str();
  336.  
  337.     // returning calculated value
  338.     return result_str;
  339. }
  340.  
  341. //basic exponentiation
  342. string exponentiation(string to_exp)
  343. {
  344.     // variables
  345.     int ch_place = to_exp.find("^");
  346.     string leftside_str = "";
  347.     string rigthside_str = "";
  348.     double leftside;
  349.     double rigthside;
  350.     double result;
  351.     string result_str;
  352.  
  353.     // finding the left and the right side of "^" character
  354.     for (int i = 0; i < ch_place; i++)
  355.     {
  356.         leftside_str += to_exp[i];
  357.     }
  358.     for (int i = ++ch_place; i < to_exp.length(); i++)
  359.     {
  360.         rigthside_str += to_exp[i];
  361.     }
  362.  
  363.     // converting string into double
  364.     leftside = atof(leftside_str.c_str());
  365.     rigthside = atof(rigthside_str.c_str());
  366.  
  367.     // calculating the result
  368.     result = pow(leftside, rigthside);
  369.  
  370.     // converting double into string
  371.     ostringstream strs;
  372.     strs << result;
  373.     result_str = strs.str();
  374.  
  375.     // returning calculated value
  376.     return result_str;
  377. }
  378.  
  379. //basic square root
  380. string sqroot(string to_root)
  381. {
  382.     // variables
  383.     double root;
  384.     double result;
  385.     string result_str;
  386.  
  387.     // extracting the number
  388.     to_root.erase(0, 5);
  389.     to_root.erase((to_root.length() - 1), 1);
  390.  
  391.     // converting string into double
  392.     root = atof(to_root.c_str());
  393.  
  394.     // calculating the result
  395.     result = pow(root, 0.5);
  396.  
  397.     // converting double into string
  398.     ostringstream strs;
  399.     strs << result;
  400.     result_str = strs.str();
  401.  
  402.     // returning calculated value
  403.     return result_str;
  404. }
Advertisement
Add Comment
Please, Sign In to add comment