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; }
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; }
void ParseEngine::RegisterVar(IParseVar * parseVar) { if(IsVarExist(parseVar->GetVarName())) { ErrorOutput("%s已存在, ParseEngine变量注册失败!", parseVar->GetVarName()); return; } m_ParseVars.Add(parseVar); }
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(); }
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; }
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; }