Exemplo n.º 1
0
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);
}
Exemplo n.º 2
0
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;
}