ARTICLE AD BOX
I am creating a math parser using a tokenizing system, turning the user entered expression into Postfix/Reverse Polish Notation. I have set the function to turn a user-entered input into RPN, then I am plugging into a function which takes in that expression. I want to plug in a number where x is, and that has been set up.
I am wondering if there is a more efficient way to do the next function, which is checking if the iterator position of the RPN expression is an operator, then putting the two different numbers into a function called operators, and then removing those two numbers, and instead swapping those numbers with the operated result from the expression that I passed in the operations function.
This is the current code, where the linspace is a vector which holds a bunch of randomly generated values:
vector<string> calculated_formula(vector<string> rpn_expression, vector<double> linspace){ vector<double> calculated_expression; vector<string> copy_of_rpn_expression = rpn_expression; for (int i = 0; i < linspace.size(); i++) { vector<string> copy_of_rpn_expression = rpn_expression; for (int j = 0; j < rpn_expression.size(); j++) { if (copy_of_rpn_expression[j] == 'x') { copy_of_rpn_expression[j] = linspace[i]; } double operated_results; if (rpn_expression[j].size() == 1) { operated_results = operations(stod(rpn_expression[j-2]), stod(rpn_expression[j-1]), rpn_expression[j]); copy_of_rpn_expression.erase(rpn_expression[j-2]); copy_of_rpn_expression.erase(rpn_expression[j-1]); copy_of_rpn_expression.erase(rpn_expression[j]); copy_of_rpn_expression.insert(rpn_expression[j], operated_results); } } } }