Esempio n. 1
0
bool CCalculator::SetFunction(const std::string & fnId, const std::string & varId)
{
	if (IsVarExist(fnId) || IsFunctionExist(fnId) || !IsVarExist(varId) || !IsNameCorrect(fnId))
	{
		return false;
	}

	SFunctionData fnInfo;
	fnInfo.firstOperand = varId;
	m_functions.insert(make_pair(fnId, fnInfo));
	CalculateFunctionValue(fnId);
	return true;
}
Esempio n. 2
0
bool CCalculator::SetVar(const std::string & var)
{
	if (!IsNameCorrect(var) || IsFunctionExist(var) || IsVarExist(var))
	{
		return false;
	}

	m_variables.insert({ var, std::numeric_limits<double>::quiet_NaN() });
	return true;
}
Esempio n. 3
0
void		ParseEngine::RegisterVar(IParseVar *	parseVar)
{
	if(IsVarExist(parseVar->GetVarName()))
	{
		ErrorOutput("%s已存在, ParseEngine变量注册失败!", parseVar->GetVarName());
		return;
	}

	m_ParseVars.Add(parseVar);
}
Esempio n. 4
0
double CCalculator::GetValue(const std::string & var) const
{
	if (IsFunctionExist(var))
	{
		return m_functions.at(var).value;;
	}

	if (IsVarExist(var))
	{
		return m_variables.at(var);
	}

	return std::numeric_limits<double>::quiet_NaN();
}
Esempio n. 5
0
bool CCalculator::LetVarValue(const std::string & firstVar, const std::string & secondValue)
{
	if (secondValue.empty())
	{
		return false;
	}

	if (!IsVarExist(firstVar))
	{
		if (!SetVar(firstVar))
		{
			return false;
		}
	}

	m_variables[firstVar] = GetValue(secondValue);
	if (m_variables[firstVar] != m_variables[firstVar])
	{
		char *end = nullptr;
		auto value = strtod(secondValue.c_str(), &end);
		if (*end == '\0')
		{
			m_variables[firstVar] = std::atof(secondValue.c_str());
		}
		else
		{
			return false;
		}
	}

	for (auto func : m_functions)
	{
		if (func.second.firstOperand == firstVar || func.second.secondOperand == firstVar)
		{
			CalculateFunctionValue(func.first);
		}
	}

	return true;
}
Esempio n. 6
0
bool CCalculator::SetFunction(const std::string & fnId, const std::string & firstOperand, 
	Operator fnOperator, const std::string & secondOperand)
{
	if (IsVarExist(fnId)
		|| IsFunctionExist(fnId)
		|| !IsNameCorrect(fnId) 
		|| (!IsNameCorrect(firstOperand))
		|| (fnOperator == Operator::None)
		|| (!IsNameCorrect(secondOperand)))
	{
		return false;
	}

	SFunctionData fnInfo;
	fnInfo.firstOperand = firstOperand;
	fnInfo.secondOperand = secondOperand;
	fnInfo.operatorType = fnOperator;

	m_functions.insert({ fnId, fnInfo });
	CalculateFunctionValue(fnId);
	return true;
}