示例#1
0
void EXParser::findAndReplaceVariables(std::string &_expression, EXVariableSet &_variables)
{
	for(EXVariableSet::iterator vIter = _variables.begin(); vIter!=_variables.end(); ++vIter){
		size_t j, start_index = 0;
		for(; (j = _expression.find(vIter->first, start_index))!=std::string::npos;){
			bool is_var = true;
			if((int)(j+vIter->first.length())<_expression.length()){
				if(isalpha(_expression.at((int)j+vIter->first.length()))){
					is_var = false;
				}
			}
			if((int)(j-1)>0){
				if(isalpha(_expression.at((int)(j-1)))){
					is_var = false;;
				}
			}
			if(is_var){
				_expression.replace(j, vIter->first.length(), dblToStr(vIter->second));
			}
			else{
				start_index = j+vIter->first.length();
			}
		}
	}
}
示例#2
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);
}
示例#3
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;
}