AnimatLab  2
Test
StdPostFixEval.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 
9 namespace StdUtils
10 {
11 
19 {
20  m_dblSolution = 0.0;
21 }
22 
30 {
31 
32 }
33 
43 {return m_dblSolution;}
44 
54 {return m_strEquation;}
55 
64 void CStdPostFixEval::Equation(std::string strVal)
65 {
66  //First remove any formatting blanks.
67  strVal = Std_Replace(strVal, " ", "");
68 
69  SavePostFixInArray(strVal);
70  m_strEquation = strVal;
71 }
72 
81 void CStdPostFixEval::AddVariable(std::string strVarName)
82 {
83  CStdVariable *lpVar = FindVariable(strVarName);
84  if(lpVar)
85  THROW_TEXT_ERROR(Std_Err_lVariableExists, Std_Err_strVariableExists, strVarName);
86 
87  lpVar = new CStdVariable;
88  lpVar->m_strVariable = strVarName;
89  m_aryVariables.Add(lpVar);
90 }
91 
101 void CStdPostFixEval::SetVariable(std::string strVarName, double dblVal)
102 {
103  CStdVariable *lpVar = FindVariable(strVarName);
104  if(!lpVar)
105  THROW_TEXT_ERROR(Std_Err_lVariableNotExists, Std_Err_strVariableNotExists, strVarName);
106 
107  lpVar->m_dblValue = dblVal;
108 }
109 
121 {
122  int iSize = m_aryVariables.GetSize(), i;
123  CStdVariable *lpVar=NULL;
124 
125  for(i=0; i<iSize; i++)
126  {
127  lpVar = m_aryVariables[i];
128  if(lpVar->m_strVariable == strVarName) return lpVar;
129  }
130 
131  return NULL;
132 }
133 
142 void CStdPostFixEval::FillInVariables(CStdArray<std::string> &aryPostFix)
143 {
144  int iSize=aryPostFix.GetSize(), i;
145  std::string strTemp;
146  CStdVariable *lpVar=NULL;
147 
148  for(i=0; i<iSize; i++)
149  {
150  lpVar = FindVariable(aryPostFix[i]);
151  if(lpVar)
152  {
153  strTemp = str( boost::format("%f") % (float) lpVar->m_dblValue );
154  aryPostFix[i] = strTemp;
155  }
156  }
157 }
158 
168 {
169  CStdArray<std::string> aryPostFix;
170  double dblVal, dblLeft, dblRight;
171  long lSize, i;
172  std::string strTemp;
173 
174  aryPostFix = m_aryPostFix;
175  FillInVariables(aryPostFix);
176 
177  lSize = aryPostFix.GetSize();
178  for(i=0; i<lSize; i++)
179  {
180  strTemp = aryPostFix[i];
181  if(strTemp == "^")
182  {
183  GetParams(dblLeft, dblRight, 2);
184  dblVal = pow(dblLeft, dblRight);
185  m_aryStack.Push(dblVal);
186  }
187  else if(strTemp == "*")
188  {
189  GetParams(dblLeft, dblRight, 2);
190  dblVal = dblLeft * dblRight;
191  m_aryStack.Push(dblVal);
192  }
193  else if(strTemp == "/")
194  {
195  GetParams(dblLeft, dblRight, 2);
196  if(!dblRight) THROW_ERROR(Std_Err_lDivByZero, Std_Err_strDivByZero);
197  dblVal = dblLeft / dblRight;
198  m_aryStack.Push(dblVal);
199  }
200  else if(strTemp == "%")
201  {
202  GetParams(dblLeft, dblRight, 2);
203  if(!dblRight) THROW_ERROR(Std_Err_lDivByZero, Std_Err_strDivByZero);
204  dblVal = ((long) dblLeft) % ((long) dblRight);
205  m_aryStack.Push(dblVal);
206  }
207  else if(strTemp == "+")
208  {
209  GetParams(dblLeft, dblRight, 2);
210  dblVal = dblLeft + dblRight;
211  m_aryStack.Push(dblVal);
212  }
213  else if(strTemp == "-")
214  {
215  GetParams(dblLeft, dblRight, 2);
216  dblVal = dblLeft - dblRight;
217  m_aryStack.Push(dblVal);
218  }
219  else if(strTemp == "cos")
220  {
221  GetParams(dblLeft, dblRight, 1);
222  dblVal = cos(dblRight);
223  m_aryStack.Push(dblVal);
224  }
225  else if(strTemp == "sin")
226  {
227  GetParams(dblLeft, dblRight, 1);
228  dblVal = sin(dblRight);
229  m_aryStack.Push(dblVal);
230  }
231  else if(strTemp == "tan")
232  {
233  GetParams(dblLeft, dblRight, 1);
234  dblVal = tan(dblRight);
235  m_aryStack.Push(dblVal);
236  }
237  else if(strTemp == "acos")
238  {
239  GetParams(dblLeft, dblRight, 1);
240  dblVal = acos(dblRight);
241  m_aryStack.Push(dblVal);
242  }
243  else if(strTemp == "asin")
244  {
245  GetParams(dblLeft, dblRight, 1);
246  dblVal = asin(dblRight);
247  m_aryStack.Push(dblVal);
248  }
249  else if(strTemp == "atan")
250  {
251  GetParams(dblLeft, dblRight, 1);
252  dblVal = atan(dblRight);
253  m_aryStack.Push(dblVal);
254  }
255  else if(strTemp == "sqrt")
256  {
257  GetParams(dblLeft, dblRight, 1);
258  if(!dblRight) THROW_ERROR(Std_Err_lSqrtNegNumber, Std_Err_strSqrtNegNumber);
259  dblVal = sqrt(dblRight);
260  m_aryStack.Push(dblVal);
261  }
262  else if(strTemp == "exp")
263  {
264  GetParams(dblLeft, dblRight, 1);
265  dblVal = exp(dblRight);
266  m_aryStack.Push(dblVal);
267  }
268  else if(strTemp == "rnd")
269  {
270  dblVal = Std_DRand(0, 1);
271  m_aryStack.Push(dblVal);
272  }
273  else
274  {
275  //If it is else then it must be a number
276  dblVal = atof(strTemp.c_str());
277  m_aryStack.Push(dblVal);
278  }
279  }
280 
281  //If there is more than one entry in the stack then something is wrong
282  if(m_aryStack.GetSize() != 1)
283  THROW_ERROR(Std_Err_lToManyParamsLeft, Std_Err_strToManyParamsLeft);
284  dblVal = m_aryStack.Pop();
285 
286  return dblVal;
287 }
288 
297 void CStdPostFixEval::SavePostFixInArray(std::string &strEqu)
298 {
299  long lSize = strEqu.length();
300  int i;
301  std::string strTemp;
302 
303  m_aryPostFix.RemoveAll();
304 
305  for(i=0; i<lSize; i++)
306  {
307  if(strEqu[i] == ',')
308  {
309  m_aryPostFix.Add(strTemp);
310  strTemp = "";
311  }
312  else
313  strTemp+=strEqu[i];
314 
315  }
316 
317  if(strTemp.length() > 0)
318  m_aryPostFix.Add(strTemp);
319 
320 }
321 
332 void CStdPostFixEval::GetParams(double &dblLeft, double &dblRight, int iNumParams)
333 {
334  dblLeft = 0.0;
335  dblRight = 0.0;
336 
337  if(m_aryStack.GetSize() < iNumParams)
338  THROW_PARAM_ERROR(Std_Err_lInvalidNumParams, Std_Err_strInvalidNumParams, "NumParams", iNumParams);
339 
340  dblRight = m_aryStack.Pop();
341 
342  if(iNumParams>1)
343  dblLeft = m_aryStack.Pop();
344  else
345  dblLeft = 0;
346 
347 }
348 
349 } //StdUtils
void AddVariable(std::string strVarName)
Adds a variable.
A variable that is used within the CStdPostFixEval class.
Definition: StdVariable.h:17
void SavePostFixInArray(std::string &strEqu)
Saves a post fix in array.
CStdStack< double > m_aryStack
Stack of equation parts.
std::string m_strVariable
The variable name.
Definition: StdVariable.h:21
std::string Equation()
Gets the post-fix equation.
void FillInVariables(CStdArray< std::string > &aryPostFix)
Fill in variables.
std::string m_strEquation
The post-fix equation.
double Solution()
Gets the solution.
double m_dblValue
The variable value.
Definition: StdVariable.h:24
CStdVariable * FindVariable(std::string strVarName)
Searches for the a variable with the specified name.
double m_dblSolution
The double solution value.
virtual ~CStdPostFixEval()
Destructor.
CStdArray< std::string > m_aryPostFix
internal array used to store the equation parts.
void GetParams(double &dblLeft, double &dblRight, int iNumParams)
Gets the parameters.
Namespace for the standard utility objects.
Definition: MarkupSTL.cpp:19
double Std_DRand(double low, double high)
Generates a random number between two values.
double Solve()
Solves the equation using the defined variable values.
void SetVariable(std::string strVarName, double dblVal)
Sets the value of a variable.
CStdPostFixEval()
Default constructor.
std::string Std_Replace(std::string strVal, std::string strFind, std::string strReplace)
Replaces a substring with another string.
CStdPtrArray< CStdVariable > m_aryVariables
Array of variables.