From d4cf48613dd9b1338e174edbf4f47a7736bb2505 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 23 Jan 2009 22:02:15 +0000 Subject: Fixed a peculiar formula residue bug...eeew, formula residue. Also added a clear function to Bu::Stack --- src/formula.h | 21 ++++++++++++++++++--- src/stack.h | 6 ++++++ src/tests/formula.cpp | 27 +++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/formula.h b/src/formula.h index d1e77e3..20ffa5b 100644 --- a/src/formula.h +++ b/src/formula.h @@ -60,6 +60,14 @@ namespace Bu prec run( const Bu::FString &sFormulaSrc ) { + /* + if( !sOper.isEmpty() ) + printf("sOper.isEmpty() == false!\n"); + if( !sValue.isEmpty() ) + printf("sValue.isEmpty() == false!\n"); + if( !sFunc.isEmpty() ) + printf("sFunc.isEmpty() == false!\n"); + */ const char *sFormula = sFormulaSrc.getStr(); for(;;) { @@ -91,8 +99,11 @@ namespace Bu if( tOpr == symEOS ) { reduce(); - return sValue.top(); - break; + prec ret = sValue.top(); + sValue.clear(); + sFunc.clear(); + sOper.clear(); + return ret; } if( !sOper.isEmpty() && getPrec( sOper.top() ) > getPrec( tOpr ) ) { @@ -108,7 +119,11 @@ namespace Bu goto oppart; } } - return sValue.top(); + prec ret = sValue.top(); + sValue.clear(); + sFunc.clear(); + sOper.clear(); + return ret; } varHash hVars; diff --git a/src/stack.h b/src/stack.h index 99ce6e8..bbc12f4 100644 --- a/src/stack.h +++ b/src/stack.h @@ -64,6 +64,12 @@ namespace Bu return ret; } + void clear() + { + while( !isEmpty() ) + pop(); + } + bool isEmpty() { return pTop == NULL; diff --git a/src/tests/formula.cpp b/src/tests/formula.cpp index b3b0d5c..40b10a5 100644 --- a/src/tests/formula.cpp +++ b/src/tests/formula.cpp @@ -18,7 +18,7 @@ int main( int argc, char *argv[] ) Bu::Formula uForm; Bu::Formula dForm; - class CeilFunc : public Bu::Formula::Func + class DblCeilFunc : public Bu::Formula::Func { public: virtual double operator()( double x ) @@ -27,10 +27,29 @@ int main( int argc, char *argv[] ) } }; - dForm.hFunc.insert( "ceil", new CeilFunc() ); + dForm.hFunc.insert( "ceil", new DblCeilFunc() ); + + class IntCeilFunc : public Bu::Formula::Func + { + public: + virtual uint32_t operator()( uint32_t x ) + { + return x; + } + }; -// printf("u: %s = %u\n", argv[1], uForm.run( argv[1] ) ); - printf("d: %s = %f\n", argv[1], dForm.run( argv[1] ) ); + uForm.hFunc.insert( "ceil", new IntCeilFunc() ); + + uForm.hVars.insert("x", 10 ); + dForm.hVars.insert("x", 10.00 ); + uForm.hVars.insert("y", 10 ); + dForm.hVars.insert("y", 10.00 ); + + for( int j = 1; j < argc; j++ ) + { + printf("u: %s = %u\n", argv[j], uForm.run( argv[j] ) ); + printf("d: %s = %f\n", argv[j], dForm.run( argv[j] ) ); + } return 0; } -- cgit v1.2.3