void EXParser::defineVariable(const std::string name, const double value) { variables.erase(std::remove_if(variables.begin(), variables.end(), first_equal<std::string>(name)),variables.end()); variables.push_back(EXVariable(name, value)); }
void EXParser::evaluateFormula(std::string &_expression, const int index, EXFormulaSet::iterator foiter) { EXVariableSet formulaVariables; std::vector<std::string> formulaVariableNames, formulaVariableValues; std::string formulaParameters = isolateParenthesisExpression(_expression, index), formulaExpression = foiter->second, token; int formulaParamLen = formulaParameters.length(); priorityLoop(formulaParameters); splitString(formulaVariableNames, foiter->third); splitString(formulaVariableValues, formulaParameters); if(formulaVariableNames.size()!=formulaVariableValues.size()){ throw EXError("formula_parameter_error", ErrorIndex::FORMULA_PARAMETER_ERROR); } for(int i=0; i<formulaVariableNames.size(); i++){ formulaVariables.push_back(EXVariable(formulaVariableNames.at(i), atof(formulaVariableValues.at(i).c_str()))); } findAndReplaceVariables(formulaExpression, formulaVariables); _expression.replace(index-foiter->first.length(), foiter->first.length()+formulaParamLen+2, formulaExpression); }
bool EXParser::evaluateFormula(std::string &_expression, const int index, EXFormulaSet::iterator foiter) { EXVariableSet formulaVariables; std::vector<std::string> formulaVariableNames, formulaVariableValues; std::string formulaParameters = isolateParenthesisExpression(_expression, index), formulaExpression = foiter->second, token; int formulaParamLen = formulaParameters.length(); priorityLoop(formulaParameters); splitString(formulaVariableNames, foiter->third); splitString(formulaVariableValues, formulaParameters); if(formulaVariableNames.size()!=formulaVariableValues.size()){ mErrorStr = "formula parameter error"; return false; } for(unsigned int i=0; i<formulaVariableNames.size(); i++){ formulaVariables.push_back(EXVariable(formulaVariableNames.at(i), atof(formulaVariableValues.at(i).c_str()))); } findAndReplaceVariables(formulaExpression, formulaVariables); _expression.replace(index-foiter->first.length(), foiter->first.length()+formulaParamLen+2, formulaExpression); return true; }