📄 tintinif.cpp
字号:
break;
}
}
}
if (*pstrArgs != ' ')
{
mathStack[iIndex].SetNext( iIndex + 1 );
iIndex++;
}
pstrArgs++;
}
if (iIndex > 0)
{
mathStack[iIndex].SetNext( 0 );
}
return true;
}
void TinTin::ResetExpression()
{
int iLoop;
for (iLoop = 0; iLoop < MATH_STACK_SIZE; iLoop++)
{
mathStack[iLoop].Reset();
}
}
bool TinTin::DoOneInside( int iBegin, int iEnd )
{
while (true)
{
TTOperator highestOp = opMaximum;
int iSearchPrevIndex = -1;
int iSearchCurrIndex = 0;
int iOpIndex = -1;
int iOpLeftIndex = -1;
int iOpRightIndex;
if (iBegin > -1)
{ /* If we have an explicit start
point, then start searching
for the active operator just
after this point */
iSearchCurrIndex = mathStack[iBegin].GetNext();
}
/* Search for the highest operator
with the highest precedence */
while (iSearchCurrIndex < iEnd)
{
if (mathStack[iSearchCurrIndex].GetOp() < highestOp)
{
/* We found an operator of higher
precedence. Store pointers to
this operator. */
highestOp = mathStack[iSearchCurrIndex].GetOp();
iOpIndex = iSearchCurrIndex;
iOpLeftIndex = iSearchPrevIndex;
}
iSearchPrevIndex = iSearchCurrIndex;
iSearchCurrIndex = mathStack[iSearchCurrIndex].GetNext();
}
if (highestOp == opInt)
{ /* The only thing between iBegin
and iEnd was an integer */
if (iBegin > -1)
{
mathStack[iBegin].SetNext( mathStack[iEnd].GetNext() );
mathStack[iBegin].SetOp( opInt );
mathStack[iBegin].Set( mathStack[iOpIndex].GetInt() );
return true;
}
else
{
mathStack[0].SetNext( mathStack[iEnd].GetNext() );
mathStack[0].SetOp( opInt );
mathStack[0].Set( mathStack[iOpIndex].GetInt() );
return true;
}
}
else if (highestOp == opString)
{ /* The only thing between iBegin
and iEnd was a string */
if (iBegin > -1)
{
mathStack[iBegin].SetNext( mathStack[iEnd].GetNext() );
mathStack[iBegin].SetOp( opString );
mathStack[iBegin].Set( mathStack[iOpIndex].GetString() );
return true;
}
else
{
mathStack[0].SetNext( mathStack[iEnd].GetNext() );
mathStack[0].SetOp( opString );
mathStack[0].Set( mathStack[iOpIndex].GetString() );
return true;
}
}
else if (highestOp == opBooleanNot)
{ // We found a boolean NOT operator
iOpRightIndex = mathStack[iOpIndex].GetNext();
if (!mathStack[iOpRightIndex].IsValue() ||
(mathStack[iOpRightIndex].GetNext() == 0))
{
/* It's an error if nothing follows
the NOT operator, or if it's
not followed by a value */
return false;
}
mathStack[iOpIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
if (mathStack[iOpRightIndex].IsString())
{
string strRight( *(mathStack[iOpRightIndex].GetString()) );
/* This operator makes a string
value into an integer. If the
string is empty or "0", then
the value becomes true.
Otherwise the value becomes
false. */
mathStack[iOpIndex].SetOp( opInt );
if (strRight.IsEmpty() || (strRight == m_strFalse))
{
mathStack[iOpIndex].Set( true );
}
else
{
mathStack[iOpIndex].Set( false );
}
}
else
{
mathStack[iOpIndex].SetOp( opInt );
mathStack[iOpIndex].Set( !mathStack[iOpRightIndex].GetInt() );
}
}
else if (highestOp == opLowercase)
{ /* We found a lowercase operator
for strings */
iOpRightIndex = mathStack[iOpIndex].GetNext();
if (!mathStack[iOpRightIndex].IsValue() ||
(mathStack[iOpRightIndex].GetNext() == 0))
{
/* It's an error if nothing follows
the lowercase operator, or if
it's not followed by a value */
return false;
}
mathStack[iOpIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
if (mathStack[iOpRightIndex].IsString())
{
// Lowercase the affected string
mathStack[iOpIndex].SetOp( opString );
mathStack[iOpIndex].Set( mathStack[iOpRightIndex].GetString() );
mathStack[iOpIndex].GetString()->MakeLower();
}
else
{ // Copy the integer value unchanged
mathStack[iOpIndex].SetOp( opInt );
mathStack[iOpIndex].Set( mathStack[iOpRightIndex].GetInt() );
}
}
else
{
iOpRightIndex = mathStack[iOpIndex].GetNext();
/* Make sure that the left and right
of this operation are values */
if ((iOpLeftIndex == -1) || (mathStack[iOpRightIndex].GetNext() == 0) ||
!mathStack[iOpRightIndex].IsValue())
{
return false;
}
if (!mathStack[iOpLeftIndex].IsValue())
{
return false;
}
// Perform the operation
switch (highestOp)
{
case opMult:
{
if (!mathStack[iOpLeftIndex].IsInt() ||
!mathStack[iOpRightIndex].IsInt())
{
return false;
}
mathStack[iOpLeftIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
mathStack[iOpLeftIndex].GetInt() *=
mathStack[iOpRightIndex].GetInt();
break;
}
case opDiv:
{
if (!mathStack[iOpLeftIndex].IsInt() ||
!mathStack[iOpRightIndex].IsInt())
{
return false;
}
mathStack[iOpLeftIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
mathStack[iOpLeftIndex].GetInt() /=
mathStack[iOpRightIndex].GetInt();
break;
}
case opAdd:
{
mathStack[iOpLeftIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
if (mathStack[iOpLeftIndex].IsString() ||
mathStack[iOpRightIndex].IsString())
{
// String concatenation
string strLeft;
string strRight;
string strTemp;
MakeString( mathStack[iOpLeftIndex], strLeft );
MakeString( mathStack[iOpRightIndex], strRight );
strTemp = strLeft + strRight;
mathStack[iOpLeftIndex].SetOp( opString );
mathStack[iOpLeftIndex].Set( strTemp );
}
else
{
mathStack[iOpLeftIndex].GetInt() +=
mathStack[iOpRightIndex].GetInt();
}
break;
}
case opSubtract:
{
if (!mathStack[iOpLeftIndex].IsInt() ||
!mathStack[iOpRightIndex].IsInt())
{
return false;
}
mathStack[iOpLeftIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
mathStack[iOpLeftIndex].GetInt() -=
mathStack[iOpRightIndex].GetInt();
break;
}
case opCompGreater:
case opCompGreaterEqual:
case opCompLess:
case opCompLessEqual:
case opCompEqual:
case opCompNotEqual:
{
int iResult;
// Compare the left and right
mathStack[iOpLeftIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
iResult = CompareValues( mathStack[iOpLeftIndex],
mathStack[iOpRightIndex],
highestOp );
/* The result of a comparison is
always an int value */
mathStack[iOpLeftIndex].SetOp( opInt );
mathStack[iOpLeftIndex].GetInt() = iResult;
break;
}
case opBoolAnd:
{
if (!mathStack[iOpLeftIndex].IsInt() ||
!mathStack[iOpRightIndex].IsInt())
{
return false;
}
mathStack[iOpLeftIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
mathStack[iOpLeftIndex].GetInt() =
(mathStack[iOpLeftIndex].GetInt() &&
mathStack[iOpRightIndex].GetInt());
break;
}
case opBoolOr:
{
if (!mathStack[iOpLeftIndex].IsInt() ||
!mathStack[iOpRightIndex].IsInt())
{
return false;
}
mathStack[iOpLeftIndex].SetNext( mathStack[iOpRightIndex].GetNext() );
mathStack[iOpLeftIndex].GetInt() =
(mathStack[iOpLeftIndex].GetInt() ||
mathStack[iOpRightIndex].GetInt());
break;
}
default:
{
ErrMessage( "# Programming error! *slap Bill or Coyote*" );
return false;
break;
}
}
}
}
}
bool TinTin::CompareValues( const MathOps& left, const MathOps& right,
TTOperator op )
{
bool boolResult;
if (left.IsString() || right.IsString())
{
string strLeft;
string strRight;
MakeString( left, strLeft );
MakeString( right, strRight );
switch( op )
{
case opCompGreater:
{
boolResult = (strLeft > strRight);
break;
}
case opCompGreaterEqual:
{
boolResult = (strLeft >= strRight);
break;
}
case opCompLess:
{
boolResult = (strLeft < strRight);
break;
}
case opCompLessEqual:
{
boolResult = (strLeft <= strRight);
break;
}
case opCompEqual:
{
boolResult = (strLeft == strRight);
break;
}
case opCompNotEqual:
{
boolResult = (strLeft != strRight);
break;
}
default:
{
ErrMessage( "# Programming error! *slap Bill or Coyote*" );
return false;
}
}
}
else
{
switch( op )
{
case opCompGreater:
{
boolResult = (left.GetInt() > right.GetInt());
break;
}
case opCompGreaterEqual:
{
boolResult = (left.GetInt() >= right.GetInt());
break;
}
case opCompLess:
{
boolResult = (left.GetInt() < right.GetInt());
break;
}
case opCompLessEqual:
{
boolResult = (left.GetInt() <= right.GetInt());
break;
}
case opCompEqual:
{
boolResult = (left.GetInt() == right.GetInt());
break;
}
case opCompNotEqual:
{
boolResult = (left.GetInt() != right.GetInt());
break;
}
default:
{
ErrMessage( "# Programming error! *slap Bill or Coyote*" );
return false;
}
}
}
return boolResult;
}
void TinTin::MakeString( const MathOps& val, string& strVal )
{
if (val.IsString())
{
strVal = *(val.GetString());
}
else
{
strVal.Format( "%d", val.GetInt() );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -