Advertisement
Glenpl

parserv1

Nov 11th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.65 KB | None | 0 0
  1. //basic mathematical parser v1.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 addition(string);
  19. string subtraction(string);
  20. string multiplication(string);
  21. string division(string);
  22. string exponentiation(string);
  23. string sqroot(string);
  24.  
  25. // main function
  26. int main()
  27. {
  28.     cout<<setprecision(50);
  29. //  cout << parser("M_E+M_PI+10+10+M_PI2+M_E+M_PI+M_E");
  30.     cout << parser("M_E+M_PI2") << endl;
  31.  
  32.     cout << addition("10+15.5") << endl;
  33.     cout << subtraction("10-15.5") << endl;
  34.     cout << multiplication("10*15.5") << endl;
  35.     cout << division("10/15.5") << endl;
  36.     cout << exponentiation("10^3") << endl;
  37.     cout << sqroot("sqrt(81)") << endl;
  38.     _getch();
  39.     return 0;
  40. }
  41.  
  42. //parser, main body
  43. string parser(string text)
  44. {
  45.  
  46. // initiation, variables
  47.     string text_fixed = text;
  48.     int e;
  49.     int pi;
  50.     int pi2;
  51.  
  52. // sources
  53.     const string M_E_str = "2.71828182846";
  54.     const string M_PI_str = "3.14159265358";
  55.     const string M_PI2_str = "6.28318530717";
  56.  
  57. // first change, inserting constant values
  58.     e = text_fixed.find("M_E");
  59.     while(e != string::npos)
  60.     {
  61.         text_fixed.replace(e, 3, M_E_str);
  62.         e = text_fixed.find("M_E");
  63.     }
  64.     pi = text_fixed.find("M_PI");
  65.     while(pi != string::npos)
  66.     {
  67.         text_fixed.replace(pi, 4, M_PI_str);
  68.         pi = text_fixed.find("M_PI");
  69.     }
  70.     pi2 = text_fixed.find("M_PI2");
  71.     while(pi2 != string::npos)
  72.     {
  73.         text_fixed.replace(pi2, 5, M_PI2_str);
  74.         pi2 = text_fixed.find("M_PI2");
  75.     }
  76.  
  77. //
  78.  
  79. // returning modified text
  80.     return text_fixed;
  81. }
  82.  
  83. //basic addition
  84. string addition(string to_add)
  85. {
  86. // variables
  87.     int ch_place = to_add.find("+");
  88.     string leftside_str = "";
  89.     string rigthside_str = "";
  90.     double leftside;
  91.     double rigthside;
  92.     double result;
  93.     string result_str;
  94.  
  95. // finding the left and the right side of "+" character
  96.     for(int i = 0; i < ch_place; i++)
  97.     {
  98.         leftside_str += to_add[i];
  99.     }
  100.     for(int i = ++ch_place; i < to_add.length(); i++)
  101.     {
  102.         rigthside_str+= to_add[i];
  103.     }
  104.  
  105. // converting string into double
  106.     leftside = atof(leftside_str.c_str());
  107.     rigthside = atof(rigthside_str.c_str());
  108.  
  109. // calculating the result
  110.     result = leftside + rigthside;
  111.  
  112. // converting double into string
  113.     ostringstream strs;
  114.     strs << result;
  115.     result_str = strs.str();
  116.  
  117. // returning calculated value
  118.     return result_str;
  119. }
  120.  
  121. //basic substraction
  122. string subtraction(string to_sub)
  123. {
  124. // variables
  125.     int ch_place = to_sub.find("-");
  126.     string leftside_str = "";
  127.     string rigthside_str = "";
  128.     double leftside;
  129.     double rigthside;
  130.     double result;
  131.     string result_str;
  132.  
  133. // finding the left and the right side of "-" character
  134.     for(int i = 0; i < ch_place; i++)
  135.     {
  136.         leftside_str += to_sub[i];
  137.     }
  138.     for(int i = ++ch_place; i < to_sub.length(); i++)
  139.     {
  140.         rigthside_str+= to_sub[i];
  141.     }
  142.  
  143. // converting string into double
  144.     leftside = atof(leftside_str.c_str());
  145.     rigthside = atof(rigthside_str.c_str());
  146.  
  147. // calculating the result
  148.     result = leftside - rigthside;
  149.  
  150. // converting double into string
  151.     ostringstream strs;
  152.     strs << result;
  153.     result_str = strs.str();
  154.  
  155. // returning calculated value
  156.     return result_str;
  157. }
  158.  
  159. //basic multiplication
  160. string multiplication(string to_mul)
  161. {
  162. // variables
  163.     int ch_place = to_mul.find("*");
  164.     string leftside_str = "";
  165.     string rigthside_str = "";
  166.     double leftside;
  167.     double rigthside;
  168.     double result;
  169.     string result_str;
  170.  
  171. // finding the left and the right side of "*" character
  172.     for(int i = 0; i < ch_place; i++)
  173.     {
  174.         leftside_str += to_mul[i];
  175.     }
  176.     for(int i = ++ch_place; i < to_mul.length(); i++)
  177.     {
  178.         rigthside_str+= to_mul[i];
  179.     }
  180.  
  181. // converting string into double
  182.     leftside = atof(leftside_str.c_str());
  183.     rigthside = atof(rigthside_str.c_str());
  184.  
  185. // calculating the result
  186.     result = leftside * rigthside;
  187.  
  188. // converting double into string
  189.     ostringstream strs;
  190.     strs << result;
  191.     result_str = strs.str();
  192.  
  193. // returning calculated value
  194.     return result_str;
  195. }
  196.  
  197. //basic division
  198. string division(string to_div)
  199. {
  200. // variables
  201.     int ch_place = to_div.find("/");
  202.     string leftside_str = "";
  203.     string rigthside_str = "";
  204.     double leftside;
  205.     double rigthside;
  206.     double result;
  207.     string result_str;
  208.  
  209. // finding the left and the right side of "/" character
  210.     for(int i = 0; i < ch_place; i++)
  211.     {
  212.         leftside_str += to_div[i];
  213.     }
  214.     for(int i = ++ch_place; i < to_div.length(); i++)
  215.     {
  216.         rigthside_str+= to_div[i];
  217.     }
  218.  
  219. // converting string into double
  220.     leftside = atof(leftside_str.c_str());
  221.     rigthside = atof(rigthside_str.c_str());
  222.  
  223. // calculating the result
  224.     result = leftside / rigthside;
  225.  
  226. // converting double into string
  227.     ostringstream strs;
  228.     strs << result;
  229.     result_str = strs.str();
  230.  
  231. // returning calculated value
  232.     return result_str;
  233. }
  234.  
  235. //basic exponentiation
  236. string exponentiation(string to_exp)
  237. {
  238. // variables
  239.     int ch_place = to_exp.find("^");
  240.     string leftside_str = "";
  241.     string rigthside_str = "";
  242.     double leftside;
  243.     double rigthside;
  244.     double result;
  245.     string result_str;
  246.  
  247. // finding the left and the right side of "^" character
  248.     for(int i = 0; i < ch_place; i++)
  249.     {
  250.         leftside_str += to_exp[i];
  251.     }
  252.     for(int i = ++ch_place; i < to_exp.length(); i++)
  253.     {
  254.         rigthside_str+= to_exp[i];
  255.     }
  256.  
  257. // converting string into double
  258.     leftside = atof(leftside_str.c_str());
  259.     rigthside = atof(rigthside_str.c_str());
  260.  
  261. // calculating the result
  262.     result = pow(leftside,rigthside);
  263.  
  264. // converting double into string
  265.     ostringstream strs;
  266.     strs << result;
  267.     result_str = strs.str();
  268.  
  269. // returning calculated value
  270.     return result_str;
  271. }
  272.  
  273. //basic square root
  274. string sqroot(string to_root)
  275. {
  276. // variables
  277.     double root;
  278.     double result;
  279.     string result_str;
  280.  
  281. // extracting the number
  282.     to_root.erase(0, 5);
  283.     to_root.erase((to_root.length()-1), 1);
  284.  
  285. // converting string into double
  286.     root = atof(to_root.c_str());
  287.  
  288. // calculating the result
  289.     result = pow(root,0.5);
  290.  
  291. // converting double into string
  292.     ostringstream strs;
  293.     strs << result;
  294.     result_str = strs.str();
  295.  
  296. // returning calculated value
  297.     return result_str;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement