📄 vmcalcexpressionanalyzer.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Implementation of the Expression Analyzer class
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 <math.h>
#include <ctype.h>
#include "VMCalcExpressionAnalyzer.h"
// Error code var
//
int giResultCode;
void VMCalcExpressionAnalyzer::PruneTree( PNODE pxRoot )
{
if ( NULL == pxRoot )
return;
if ( pxRoot->m_pxLeft != NULL )
PruneTree( pxRoot->m_pxLeft );
if ( pxRoot->m_pxRight != NULL )
PruneTree( pxRoot->m_pxRight );
if ( pxRoot->m_ucOperation == '`' )
delete pxRoot->m_oValueString;
delete pxRoot;
}
VMCalcExpressionAnalyzer::VMCalcExpressionAnalyzer( void )
{
m_xExpressionTree = NULL;
m_oEquation = "";
m_iPosition = 0;
m_poVariables = NULL;
}
VMCalcExpressionAnalyzer::VMCalcExpressionAnalyzer( CMapVariable* pVarsMap )
{
m_xExpressionTree = NULL;
m_oEquation = "";
m_iPosition = 0;
m_poVariables = pVarsMap;
}
VMCalcExpressionAnalyzer::~VMCalcExpressionAnalyzer( void )
{
PruneTree( m_xExpressionTree );
}
int VMCalcExpressionAnalyzer::UpdateValue( void )
{
if ( m_oEquation.empty() )
return( 0 );
PruneTree( m_xExpressionTree );
m_xExpressionTree = NULL;
m_iPosition = 0;
m_xExpressionTree = FactorSumDiff();
if ( m_oEquation[ m_iPosition ] != '\0' )
{
PruneTree( m_xExpressionTree );
m_xExpressionTree = NULL;
}
if ( NULL == m_xExpressionTree )
return( m_iPosition );
else
return( -1 );
}
VMCalcExpressionAnalyzer::PNODE VMCalcExpressionAnalyzer::FactorSumDiff( void )
{
SkipSpaces();
PNODE pxNewNode;
PNODE pxLHS = FactorMultDiv();
PNODE pxRHS;
if ( NULL == pxLHS )
return( NULL );
SkipSpaces();
while ( ( m_oEquation[ m_iPosition ] == '-' )
|| ( m_oEquation[ m_iPosition ] == '+' ) )
{
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_ucOperation = m_oEquation[ m_iPosition ];
m_iPosition++;
pxRHS = FactorMultDiv();
pxNewNode->m_pxRight = pxRHS;
if ( NULL == pxRHS )
{
PruneTree( pxNewNode );
return( NULL );
}
pxLHS = pxNewNode;
}
return( pxLHS );
}
VMCalcExpressionAnalyzer::PNODE VMCalcExpressionAnalyzer::FactorMultDiv( void )
{
SkipSpaces();
PNODE pxNewNode;
PNODE pxLHS = Compute();
PNODE pxRHS;
if ( NULL == pxLHS )
return( NULL );
SkipSpaces();
while ( ( m_oEquation[ m_iPosition ] == '*' )
|| ( m_oEquation[ m_iPosition ] == '/' ) )
{
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_ucOperation = m_oEquation[ m_iPosition ];
m_iPosition++;
pxRHS = Compute();
pxNewNode->m_pxRight = pxRHS;
if ( NULL == pxRHS )
{
PruneTree( pxNewNode );
return( NULL );
}
pxLHS = pxNewNode;
}
return( pxLHS );
}
VMCalcExpressionAnalyzer::PNODE VMCalcExpressionAnalyzer::Compute( void )
{
SkipSpaces();
PNODE pxNewNode = NULL;
PNODE pxLHS = PerformLogicalOperation();
PNODE pxRHS;
if ( NULL == pxLHS )
return( NULL );
SkipSpaces();
while ( m_oEquation[ m_iPosition ] == '^' )
{
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_ucOperation = m_oEquation[ m_iPosition ];
m_iPosition++;
pxRHS = PerformLogicalOperation();
pxNewNode->m_pxRight = pxRHS;
if ( NULL == pxRHS )
{
PruneTree( pxNewNode );
return( NULL );
}
pxLHS = pxNewNode;
}
return( pxLHS );
}
VMCalcExpressionAnalyzer::PNODE VMCalcExpressionAnalyzer::FactorExpression( void )
{
SkipSpaces();
PNODE pxNewNode = NULL;
PNODE pxRHS = NULL;
PNODE pxLHS = NULL;
while ( m_oEquation[ m_iPosition ] == ' '
&& m_oEquation[ m_iPosition ] != '\0' )
m_iPosition++;
SkipSpaces();
if ( m_oEquation[ m_iPosition ] == '-' )
{
pxNewNode = new NODE;
pxLHS = new NODE;
pxLHS->m_pxRight = NULL;
pxLHS->m_pxLeft = NULL;
pxLHS->m_ucOperation = '@';
pxLHS->m_dValue = -1;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_ucOperation = '*';
m_iPosition++;
pxNewNode->m_pxRight = FactorSumDiff();
if ( NULL == pxNewNode->m_pxRight )
return( NULL );
return( pxNewNode );
}
if ( m_oEquation[ m_iPosition ] == '(' )
{
m_iPosition++;
pxNewNode = FactorSumDiff();
if ( NULL == pxNewNode )
return( NULL );
if ( m_oEquation[ m_iPosition ] != ')' )
{
PruneTree( pxNewNode );
return( NULL );
}
m_iPosition++;
return( pxNewNode );
}
else
if ( m_oEquation[ m_iPosition ] == '|' )
{
m_iPosition++;
pxRHS = FactorSumDiff();
if ( NULL == pxRHS )
return( NULL );
if ( m_oEquation[ m_iPosition ] != '|' )
{
PruneTree( pxNewNode );
return( NULL );
}
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxRHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = '|';
m_iPosition++;
return( pxNewNode );
}
else
return( FactorString() );
return( pxNewNode );
}
VMCalcExpressionAnalyzer::PNODE VMCalcExpressionAnalyzer::FactorString( void )
{
SkipSpaces();
VMCalcValueBase* poValue;
int iPosition;
PNODE pxNewNode = NULL;
PNODE pxLHS = NULL;
PNODE pxReturn = NULL;
bool bNegative = false;
iPosition = m_iPosition;
SkipSpaces();
if ( m_oEquation[ m_iPosition ]== '\0' )
pxReturn = NULL;
if ( m_oEquation[ m_iPosition ]== '-' )
{
bNegative = true;
iPosition++;
m_iPosition++;
}
if ( isdigit( m_oEquation[ m_iPosition ] ) )
{
while ( isdigit( m_oEquation[ m_iPosition ] ) )
m_iPosition++;
if ( m_oEquation[ m_iPosition ] == '.' )
m_iPosition++;
while ( isdigit( m_oEquation[ m_iPosition ] ) )
m_iPosition++;
pxNewNode = new NODE;
pxNewNode->m_pxLeft = NULL;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = '@';
pxNewNode->m_dValue = atof( m_oEquation.substr( iPosition, m_iPosition - iPosition ).c_str() );
if ( bNegative )
{
pxNewNode->m_dValue *= -1;
}
pxReturn = pxNewNode;
}
if ( isalpha( m_oEquation[ m_iPosition ] )
|| m_oEquation[ m_iPosition ] == '_'
|| m_oEquation[ m_iPosition ] == '$' )
{
while ( isalnum( m_oEquation[ m_iPosition ] )
|| m_oEquation[ m_iPosition ] == '_'
|| m_oEquation[ m_iPosition ] == '$' )
m_iPosition++;
std::string oToken = m_oEquation.substr( iPosition,
m_iPosition - iPosition );
std::string oVarName = oToken;
std::string::iterator oStringIter = oToken.begin();
while ( oStringIter != oToken.end() )
{
*(oStringIter) = toupper( *(oStringIter) );
oStringIter++;
}
if ( oToken == "SIN" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 150;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="COS" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 151;
SkipSpaces();
return( pxNewNode );
}
if ( oToken == "EXP" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 152;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="SQRT" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 153;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="LN" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 154;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="TAN" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 155;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="CTG" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 156;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="ASIN" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 157;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="ACOS" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 158;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="ATAN" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 159;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="SINH" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 160;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="COSH" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
pxNewNode->m_pxLeft = pxLHS;
pxNewNode->m_pxRight = NULL;
pxNewNode->m_ucOperation = 161;
SkipSpaces();
return( pxNewNode );
}
if ( oToken =="TANH" )
{
pxLHS = FactorExpression();
pxNewNode = new NODE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -