⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmcalculator.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   The VMCalculator Object aggregates an expression analyzer and
                   a variables map to provide a single class wrapper for the
                   system.

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision:   $ : $Date:   $";
/*****************************************************************************/

#include "VMCalcVariable.h"
#include "VMCalcSubExpression.h"
#include "VMCalculator.h"


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::VMCalculator

       DESCRIPTION:  ctor.

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  none
*/
VMCalculator::VMCalculator( void )
{
  m_oAnalyzer.AtachVariables( &m_oVariables );
}
/* End of function "VMCalculator::VMCalculator"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::~VMCalculator

       DESCRIPTION:  dtor. erases variables map

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  none
*/
VMCalculator::~VMCalculator( void )
{
  IterCMapVariable oIter = m_oVariables.begin();

  while ( m_oVariables.end() != oIter )
  {
    VMCalcValueBase* pVal = (*oIter).second;
    delete pVal;
    ++oIter;
  }
  m_oVariables.erase( m_oVariables.begin(), m_oVariables.end() );	
}
/* End of function "VMCalculator::~VMCalculator"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::SetEquation

       DESCRIPTION:  Assigns an equation to the analyzer

             INPUT:  pchEquation - pointer to string containing the equation 
                     to be solved
            OUTPUT:  none

           RETURNS:  results of equation analysis
*/
int VMCalculator::SetEquation( const char* pchEquation )
{
  return( m_oAnalyzer.ChangeExpression( pchEquation ) );
}
/* End of function "VMCalculator::SetEquation"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::GetResult

       DESCRIPTION:  used to extract the results of processing the equation

             INPUT:  rdOutput - the results will go here
            OUTPUT:  none

           RETURNS:  result code for the equation processing
*/
int VMCalculator::GetResult( double& rdOutput )
{
  return( m_oAnalyzer.Value( rdOutput ) );
}
/* End of function "VMCalculator::GetResult"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::AddSubExpression

       DESCRIPTION:  used to add a sub-equation to the processing tree

             INPUT:  pchVarName - pointer to string containing the name for 
                                  the sub equation
                     pchVarStrValue - pointer to string containg the actual
                                      equation
            OUTPUT:  

           RETURNS:  void
*/
void VMCalculator::AddSubExpression( const char* pchVarName, const char* pchVarStrValue )
{
  VMCalcExpressionAnalyzer* poExpression = new VMCalcExpressionAnalyzer( &m_oVariables );
  poExpression->ChangeExpression( pchVarStrValue );

  VMCalcSubExpression* poVariable = new VMCalcSubExpression;
  poVariable->SetValue( poExpression );

  std::string oVarName     = pchVarName;
  m_oVariables[ oVarName ] = poVariable;
}
/* End of function "VMCalculator::AddSubExpression"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::AddVariable

       DESCRIPTION:  Adds a new variable to the collection

             INPUT:  pchVarName - the variable name
                     pchVarStrValue - the value for the variable
            OUTPUT:  

           RETURNS:  void
*/
void VMCalculator::AddVariable( const char* pchVarName, const char* pchVarStrValue )
{
  RemoveVariable( pchVarName );

  VMCalcVariable* poVariable = new VMCalcVariable;
  poVariable->SetValue( atof( pchVarStrValue ) );

  std::string oVarName   = pchVarName;
  m_oVariables[oVarName] = poVariable;
}
/* End of function "VMCalculator::AddVariable"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::RemoveVariable

       DESCRIPTION:  removes a variable from the collection

             INPUT:  pchName - pointer to string containing the name of the 
                     variable to remove
            OUTPUT:  none

           RETURNS:  void
*/
void VMCalculator::RemoveVariable( const char* pchName )
{
  std::string oKey = pchName;

  IterCMapVariable oIter = m_oVariables.find( oKey );

  if ( m_oVariables.end() != oIter )
  {
    VMCalcValueBase* pValue = (*oIter).second;
    delete pValue;
    m_oVariables.erase( oIter );
  }
}
/* End of function "VMCalculator::RemoveVariable"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::GetVariables

       DESCRIPTION:  returns the variables collection to the caller

             INPUT:  void
            OUTPUT:  none

           RETURNS:  pointer to the internal variables map
*/
CMapVariable* VMCalculator::GetVariables( void )
{
  return( &m_oVariables );
}
/* End of function "VMCalculator::GetVariables"
/*****************************************************************************/



/*****************************************************************************/
/* Check-in history */
/*
 *$Log:  $
*/
/*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -