Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //basic mathematical parser v1.0
- //author jakub molinski
- //feel free to use, modify,
- //copy and sell this code
- #include <iostream>
- #include <iomanip>
- #include <sstream>
- #include <cstdlib>
- #include <conio.h>
- #include <string>
- #include <math.h>
- using namespace std;
- // list of functions
- string parser(string);
- string addition(string);
- string subtraction(string);
- string multiplication(string);
- string division(string);
- string exponentiation(string);
- string sqroot(string);
- // main function
- int main()
- {
- cout<<setprecision(50);
- // cout << parser("M_E+M_PI+10+10+M_PI2+M_E+M_PI+M_E");
- cout << parser("M_E+M_PI2") << endl;
- cout << addition("10+15.5") << endl;
- cout << subtraction("10-15.5") << endl;
- cout << multiplication("10*15.5") << endl;
- cout << division("10/15.5") << endl;
- cout << exponentiation("10^3") << endl;
- cout << sqroot("sqrt(81)") << endl;
- _getch();
- return 0;
- }
- //parser, main body
- string parser(string text)
- {
- // initiation, variables
- string text_fixed = text;
- int e;
- int pi;
- int pi2;
- // sources
- const string M_E_str = "2.71828182846";
- const string M_PI_str = "3.14159265358";
- const string M_PI2_str = "6.28318530717";
- // first change, inserting constant values
- e = text_fixed.find("M_E");
- while(e != string::npos)
- {
- text_fixed.replace(e, 3, M_E_str);
- e = text_fixed.find("M_E");
- }
- pi = text_fixed.find("M_PI");
- while(pi != string::npos)
- {
- text_fixed.replace(pi, 4, M_PI_str);
- pi = text_fixed.find("M_PI");
- }
- pi2 = text_fixed.find("M_PI2");
- while(pi2 != string::npos)
- {
- text_fixed.replace(pi2, 5, M_PI2_str);
- pi2 = text_fixed.find("M_PI2");
- }
- //
- // returning modified text
- return text_fixed;
- }
- //basic addition
- string addition(string to_add)
- {
- // variables
- int ch_place = to_add.find("+");
- string leftside_str = "";
- string rigthside_str = "";
- double leftside;
- double rigthside;
- double result;
- string result_str;
- // finding the left and the right side of "+" character
- for(int i = 0; i < ch_place; i++)
- {
- leftside_str += to_add[i];
- }
- for(int i = ++ch_place; i < to_add.length(); i++)
- {
- rigthside_str+= to_add[i];
- }
- // converting string into double
- leftside = atof(leftside_str.c_str());
- rigthside = atof(rigthside_str.c_str());
- // calculating the result
- result = leftside + rigthside;
- // converting double into string
- ostringstream strs;
- strs << result;
- result_str = strs.str();
- // returning calculated value
- return result_str;
- }
- //basic substraction
- string subtraction(string to_sub)
- {
- // variables
- int ch_place = to_sub.find("-");
- string leftside_str = "";
- string rigthside_str = "";
- double leftside;
- double rigthside;
- double result;
- string result_str;
- // finding the left and the right side of "-" character
- for(int i = 0; i < ch_place; i++)
- {
- leftside_str += to_sub[i];
- }
- for(int i = ++ch_place; i < to_sub.length(); i++)
- {
- rigthside_str+= to_sub[i];
- }
- // converting string into double
- leftside = atof(leftside_str.c_str());
- rigthside = atof(rigthside_str.c_str());
- // calculating the result
- result = leftside - rigthside;
- // converting double into string
- ostringstream strs;
- strs << result;
- result_str = strs.str();
- // returning calculated value
- return result_str;
- }
- //basic multiplication
- string multiplication(string to_mul)
- {
- // variables
- int ch_place = to_mul.find("*");
- string leftside_str = "";
- string rigthside_str = "";
- double leftside;
- double rigthside;
- double result;
- string result_str;
- // finding the left and the right side of "*" character
- for(int i = 0; i < ch_place; i++)
- {
- leftside_str += to_mul[i];
- }
- for(int i = ++ch_place; i < to_mul.length(); i++)
- {
- rigthside_str+= to_mul[i];
- }
- // converting string into double
- leftside = atof(leftside_str.c_str());
- rigthside = atof(rigthside_str.c_str());
- // calculating the result
- result = leftside * rigthside;
- // converting double into string
- ostringstream strs;
- strs << result;
- result_str = strs.str();
- // returning calculated value
- return result_str;
- }
- //basic division
- string division(string to_div)
- {
- // variables
- int ch_place = to_div.find("/");
- string leftside_str = "";
- string rigthside_str = "";
- double leftside;
- double rigthside;
- double result;
- string result_str;
- // finding the left and the right side of "/" character
- for(int i = 0; i < ch_place; i++)
- {
- leftside_str += to_div[i];
- }
- for(int i = ++ch_place; i < to_div.length(); i++)
- {
- rigthside_str+= to_div[i];
- }
- // converting string into double
- leftside = atof(leftside_str.c_str());
- rigthside = atof(rigthside_str.c_str());
- // calculating the result
- result = leftside / rigthside;
- // converting double into string
- ostringstream strs;
- strs << result;
- result_str = strs.str();
- // returning calculated value
- return result_str;
- }
- //basic exponentiation
- string exponentiation(string to_exp)
- {
- // variables
- int ch_place = to_exp.find("^");
- string leftside_str = "";
- string rigthside_str = "";
- double leftside;
- double rigthside;
- double result;
- string result_str;
- // finding the left and the right side of "^" character
- for(int i = 0; i < ch_place; i++)
- {
- leftside_str += to_exp[i];
- }
- for(int i = ++ch_place; i < to_exp.length(); i++)
- {
- rigthside_str+= to_exp[i];
- }
- // converting string into double
- leftside = atof(leftside_str.c_str());
- rigthside = atof(rigthside_str.c_str());
- // calculating the result
- result = pow(leftside,rigthside);
- // converting double into string
- ostringstream strs;
- strs << result;
- result_str = strs.str();
- // returning calculated value
- return result_str;
- }
- //basic square root
- string sqroot(string to_root)
- {
- // variables
- double root;
- double result;
- string result_str;
- // extracting the number
- to_root.erase(0, 5);
- to_root.erase((to_root.length()-1), 1);
- // converting string into double
- root = atof(to_root.c_str());
- // calculating the result
- result = pow(root,0.5);
- // converting double into string
- ostringstream strs;
- strs << result;
- result_str = strs.str();
- // returning calculated value
- return result_str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement