#include "StdAfx.h"
|
#include "RecipeFormula.h"
|
|
CRecipeFormula::CRecipeFormula(void)
|
{
|
Reset();
|
}
|
|
CRecipeFormula::~CRecipeFormula(void)
|
{
|
}
|
|
|
int CRecipeFormula::GetItemCount() const
|
{
|
return (int)m_vectorFormulaItem.size();
|
}
|
|
void CRecipeFormula::SetItemCount(int nCount)
|
{
|
m_vectorFormulaItem.resize(nCount);
|
}
|
|
const CFormulaItem* CRecipeFormula::GetItem(int nIndex) const
|
{
|
if (nIndex < 0 || nIndex > m_vectorFormulaItem.size()-1) return NULL;
|
|
return &m_vectorFormulaItem[nIndex];
|
}
|
|
void CRecipeFormula::SetItem(int nIndex, const CFormulaItem& formulaItem)
|
{
|
if (nIndex<0 || nIndex>=(int)m_vectorFormulaItem.size()) return;
|
|
m_vectorFormulaItem[nIndex] = formulaItem;
|
}
|
|
void CRecipeFormula::AddItem(const CFormulaItem& formulaItem)
|
{
|
m_vectorFormulaItem.push_back(formulaItem);
|
}
|
|
double CRecipeFormula::CalculateFormula() const
|
{
|
double dReturnValue = 0.0;
|
|
for (int i=0; i<m_vectorFormulaItem.size(); i++)
|
{
|
if (i==0)
|
{
|
dReturnValue = m_vectorFormulaItem[i].dValue;
|
}
|
else
|
{
|
switch(m_vectorFormulaItem[i-1].nOperator)
|
{
|
case OperatorNone:
|
// dReturnValue = m_pItemData[i].dValue;
|
break;
|
|
case OperatorPlus:
|
dReturnValue = dReturnValue + m_vectorFormulaItem[i].dValue;
|
break;
|
|
case OperatorMinus:
|
dReturnValue = dReturnValue - m_vectorFormulaItem[i].dValue;
|
break;
|
|
case OperatorMulti:
|
dReturnValue = dReturnValue * m_vectorFormulaItem[i].dValue;
|
break;
|
|
case OperatorDiv:
|
dReturnValue = dReturnValue / m_vectorFormulaItem[i].dValue;
|
break;
|
|
}
|
}
|
|
}
|
|
return dReturnValue;
|
}
|
|
|
BOOL CRecipeFormula::GetRecipeFormula(const CString& strString, CRecipeFormula& recipeFormula)
|
{
|
recipeFormula.m_vectorFormulaItem.clear();
|
|
int nFirst = 0;
|
int nSecond = 0;
|
|
BOOL bReturn = FALSE;
|
|
while(TRUE)
|
{
|
nFirst = strString.Find(_T('['), nSecond);
|
|
if (nFirst<0) break;
|
|
nSecond = strString.Find(_T(']'), nFirst+1);
|
|
if (nSecond<0) break;
|
|
CString strValue = strString.Mid(nFirst+1, nSecond-nFirst-1);
|
|
CFormulaItem item;
|
item.nOperator = OperatorNone;
|
|
if (nSecond+1 < strString.GetLength())
|
{
|
TCHAR operation = strString.GetAt(nSecond+1);
|
|
switch(operation)
|
{
|
case _T('+'):
|
item.nOperator = OperatorPlus;
|
break;
|
case _T('-'):
|
item.nOperator = OperatorMinus;
|
break;
|
case _T('*'):
|
item.nOperator = OperatorMulti;
|
break;
|
case _T('/'):
|
item.nOperator = OperatorDiv;
|
break;
|
}
|
}
|
|
// formula
|
int nPos1 = strValue.Find(_T('_'));
|
if (nPos1<0)
|
{
|
item.nType = ResultConstant;
|
item.dValue = _tcstod(strValue, NULL);
|
}
|
else
|
{
|
// operation
|
CString strType = strValue.Mid(nPos1+1, strValue.GetLength()-nPos1);
|
|
if (strType.CompareNoCase(_T("C"))==0)
|
{
|
item.nType = ResultCenter;
|
}
|
if (strType.CompareNoCase(_T("F"))==0)
|
{
|
item.nType = ResultFirst;
|
}
|
if (strType.CompareNoCase(_T("S"))==0)
|
{
|
item.nType = ResultSecond;
|
}
|
if (strType.CompareNoCase(_T("L"))==0)
|
{
|
item.nType = ResultLength;
|
}
|
if (strType.CompareNoCase(_T("A"))==0)
|
{
|
item.nType = ResultAngle;
|
}
|
else
|
{
|
bReturn = FALSE;
|
}
|
|
// index
|
int nPos2 = strValue.Find(_T('%'));
|
if (nPos2<0)
|
{
|
bReturn = FALSE;
|
}
|
else
|
{
|
CString strTemp = strValue.Mid(nPos2+1, nPos1-nPos2-1);
|
item.nIndex = _ttoi(strTemp);
|
}
|
}
|
|
recipeFormula.AddItem(item);
|
|
bReturn = TRUE;
|
}
|
|
return bReturn;
|
}
|
|
CString CRecipeFormula::SetFormulaString(const CRecipeFormula& recipeFormula)
|
{
|
CString strString = _T("");
|
|
CString strTemp = _T("");
|
|
for (UINT i=0; i<(UINT)recipeFormula.GetItemCount(); i++)
|
{
|
const CFormulaItem* pItem = recipeFormula.GetItem(i);
|
|
if (pItem==NULL) continue;
|
|
if (pItem->nIndex>=0 && pItem->nType!=ResultConstant)
|
{
|
strTemp.Format(_T("[%c%d"), '%', pItem->nIndex);
|
strString += strTemp;
|
}
|
else
|
{
|
strTemp.Format(_T("["));
|
strString += strTemp;
|
}
|
|
switch(pItem->nType)
|
{
|
case ResultConstant:
|
strTemp.Format(_T("%.3lf]"), pItem->dValue);
|
break;
|
|
case ResultCenter:
|
strTemp.Format(_T("_C]"));
|
break;
|
|
case ResultFirst:
|
strTemp.Format(_T("_F]"));
|
break;
|
|
case ResultSecond:
|
strTemp.Format(_T("_S]"));
|
break;
|
|
case ResultLength:
|
strTemp.Format(_T("_L]"));
|
break;
|
|
case ResultAngle:
|
strTemp.Format(_T("_A]"));
|
break;
|
}
|
strString += strTemp;
|
|
switch(pItem->nOperator)
|
{
|
case OperatorNone:
|
strTemp = _T("");
|
break;
|
|
case OperatorPlus:
|
strTemp = _T("+");
|
break;
|
|
case OperatorMinus:
|
strTemp = _T("-");
|
break;
|
|
case OperatorMulti:
|
strTemp = _T("*");
|
break;
|
|
case OperatorDiv:
|
strTemp = _T("/");
|
break;
|
}
|
strString += strTemp;
|
}
|
|
return strString;
|
}
|
|
CResultFormula::CResultFormula(void)
|
{
|
Reset();
|
}
|
|
CResultFormula::~CResultFormula(void)
|
{
|
}
|