Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //basic mathematical parser v2.2.1
- //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 switcher(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(20);
- // parser tests
- //cout << endl << "Wynik: " << parser("(M_E+M_PI)") << endl << endl;
- //cout << endl << "Wynik: " << parser("((((5+5)+10)+(5*5))-(10*(11-6)))") << endl << endl;
- //cout << endl << "Wynik: " << parser("((50/3)-(10*6))") << endl << endl;
- //cout << endl << "Wynik: " << parser("(((5+5)+10)+(5*M_PI2))") << endl << endl;
- //cout << endl << "Wynik: " << parser("((((((100+90)+80)+70)+60)+50)+1.55)") << endl << endl;
- //cout << endl << "Wynik: " << parser("(5^2)") << endl << endl;
- //cout << endl << "Wynik: " << parser("((sqrt(81))+50)") << endl << endl;
- // subfunction tests
- /*
- 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;
- int bracket;
- string operation;
- int operation_length = 0;
- bool done;
- int operation_start = 0;
- int operation_end = 0;
- // 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");
- }
- // cutting into smaller operations, calculating them
- bracket = text_fixed.find("(");
- operation_start = bracket;
- while (
- (text_fixed.find("(") != string::npos) ||
- (text_fixed.find(")") != string::npos) ||
- (text_fixed.find("+") != string::npos) ||
- (text_fixed.find("-") != string::npos) ||
- (text_fixed.find("*") != string::npos) ||
- (text_fixed.find("/") != string::npos) ||
- (text_fixed.find("^") != string::npos) ||
- (text_fixed.find("sqrt") != string::npos))
- {
- //checks if text_fixed does not look like (45) or something
- if((text_fixed[0] == '(') && (text_fixed[(text_fixed.length())-1] == ')'))
- {
- string text_onlynumbers = text_fixed;
- text_onlynumbers.erase(0,1);
- text_onlynumbers.erase((text_onlynumbers.length())-1,1);
- bool calculated = true;
- for(int i = 0; i < text_onlynumbers.length(); i++)
- {
- if((text_onlynumbers[i] > 57) || (text_onlynumbers[i] < 48 ))
- {
- calculated = false;
- break;
- }
- }
- if(calculated)
- {
- text_fixed.erase(0,1);
- text_fixed.erase((text_fixed.length())-1,1);
- break;
- }
- }
- // main
- done = false;
- while (done == false)
- {
- if (bracket != string::npos)
- {
- for (int i = (bracket+1);; i++)
- {
- if (text_fixed[i] == ')')
- {
- if (operation[0] == 's')
- {
- operation_length++;
- operation += ')';
- }
- done = true;
- operation_end = ++i;
- cout << "Text fixed1: " << text_fixed << endl;
- cout << "Operacja1: " << operation << endl;
- // calculate the operation and replace it with its result
- text_fixed.replace(operation_start, (operation_length + 2), switcher(operation));
- bracket = text_fixed.find("(");
- operation_start = bracket;
- operation_length = 0;
- operation = "";
- break;
- }
- // sqrt exception
- if ((text_fixed[i] == '(') && (text_fixed[i - 1] != 't'))
- {
- bracket = i;
- operation_start = bracket;
- operation_length = 0;
- operation = "";
- break;
- }
- operation_length++;
- operation += text_fixed[i];
- }
- }
- else
- {
- break;
- }
- }
- }
- // returning modified text
- return text_fixed;
- }
- //sends operation to appropriate calculating function
- string switcher(string operation)
- {
- if (operation.find("+") != string::npos)
- {
- return addition(operation);
- }
- if (operation.find("-") != string::npos)
- {
- return subtraction(operation);
- }
- if (operation.find("*") != string::npos)
- {
- return multiplication(operation);
- }
- if (operation.find("/") != string::npos)
- {
- return division(operation);
- }
- if (operation.find("^") != string::npos)
- {
- return exponentiation(operation);
- }
- if (operation.find("sqrt") != string::npos)
- {
- return sqroot(operation);
- }
- else
- {
- cout << "\n\n\n BLAD SWITCHERA!!! \n\n\n";
- exit(0);
- }
- }
- //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