From 9f7cbe624a91008a4cbe47c7b369c5e3f96a4eff Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 28 Nov 2014 13:45:37 -0700 Subject: Division fixes, better config, more tests. Other minor fixes and options such as --version being added. --- src/config.h | 8 +++++++- src/main.cpp | 3 ++- src/number.cpp | 2 ++ src/options.cpp | 12 +++++++++++- src/options.h | 1 + src/parser.cpp | 20 +++++++++++--------- src/unitnumber.cpp | 2 ++ 7 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/config.h b/src/config.h index 00fb067..a3f8b9e 100644 --- a/src/config.h +++ b/src/config.h @@ -1,8 +1,14 @@ #ifndef CONFIG_H #define CONFIG_H +#define CLIC_VERSION "0.11" +#define CLIC_VERSION_STR "Clic v" CLIC_VERSION + #define DEBUG_DIVIDE false -#define DBS( s, x ) if( DEBUG_ ##s ) { x; } (void)0 +#define DEBUG_PARSE false +#define DBS( s, x ) if( DEBUG_ ##s ) { x; } (void)0 +#define DBS_START( s ) if( DEBUG_ ##s ) { (void)0 +#define DBS_END() } (void)0 #endif diff --git a/src/main.cpp b/src/main.cpp index a0faa6a..a1d7672 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "config.h" #include "number.h" #include "packedintarray.h" #include "lexer.h" @@ -17,7 +18,7 @@ int main( int argc, char *argv[] ) { Options opt( argc, argv ); - println("CliC - 0.10"); + println(CLIC_VERSION_STR); println("Try \\exit, \\help, \\scale, and \\radix commands."); println(""); diff --git a/src/number.cpp b/src/number.cpp index 8fc1d7f..05e310c 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -434,6 +434,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const DBS( DIVIDE, Bu::println(" -> '%1'").arg( rhs.digit( j ) ) ); nDiv.aInt.append( rhs.digit( j ) ); } + nDiv.aInt.trim(); DBS( DIVIDE, Bu::println("New divisor: %1").arg( nDiv ) ); // Anchor is the position in the output the new digit will appear. @@ -463,6 +464,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const DBS( DIVIDE, Bu::println(" ->[%1] '%2'").arg( j ).arg( digit(j) ) ); nNum.aInt.append( digit( j ) ); } + nNum.aInt.trim(); DBS( DIVIDE, Bu::println("New numerator: %1").arg( nNum ) ); while( iAnchor >= -iScale && (!nNum.isZero() || iAnchor > 0)) // division loop { diff --git a/src/options.cpp b/src/options.cpp index 4d64b22..d9d34fa 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -1,5 +1,7 @@ #include "options.h" +#include "config.h" + #include "unitnumber.h" #include "unitparser.h" #include "number.h" @@ -34,6 +36,8 @@ Options::Options( int argc, char *argv[] ) : "use all remaining parameters as the equation (you may wish to quote " "your equation to prevent special characters from being interpreted by " "your shell)."); + addOption( Bu::slot(this, &Options::version), 'v', "version", + "Show the version string ('" CLIC_VERSION_STR "')"); addHelpOption('h', "help", "This help"); parse( argc, argv ); @@ -189,9 +193,15 @@ int Options::execute( Bu::StringArray aArgs ) lex.setRadix( iRadix ); Parser parser( lex, mbOut ); parser.parse(); - Bu::println( mbOut.getString() ); + Bu::print( mbOut.getString() ); exit( 0 ); return aArgs.getSize(); } +int Options::version( Bu::StringArray aArgs ) +{ + Bu::println( CLIC_VERSION_STR ); + exit( 0 ); + return 0; +} diff --git a/src/options.h b/src/options.h index cd0fd2d..9afa0ac 100644 --- a/src/options.h +++ b/src/options.h @@ -18,6 +18,7 @@ private: int isPrime( Bu::StringArray aArgs ); int convert( Bu::StringArray aArgs ); int execute( Bu::StringArray aArgs ); + int version( Bu::StringArray aArgs ); int iScale; int iRadix; diff --git a/src/parser.cpp b/src/parser.cpp index 72f7bb3..bd49c38 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1,3 +1,4 @@ +#include "config.h" #include "parser.h" #include "lexer.h" #include "number.h" @@ -6,7 +7,6 @@ #include #include -//#define DEBUG Parser::Parser( Lexer &lex, Bu::Stream &rOut ) : lex( lex ), @@ -140,16 +140,17 @@ void Parser::parse() getPriority( tsNonTerminal.peek().eType ) < getPriority( t.eType ) ) { -#ifdef DEBUG - Bu::println("Pushing non-terminal: %1").arg( t.eType ); -#endif + DBS( PARSE, + Bu::println("Pushing non-terminal: %1").arg( t.eType ) + ); tsNonTerminal.push( t ); } else { -#ifdef DEBUG - Bu::println("Unwinding stack before pushing: %1").arg( t.eType ); -#endif + DBS( PARSE, + Bu::println("Unwinding stack before pushing: %1") + .arg( t.eType ) + ); unwind(); tsNonTerminal.push( t ); } @@ -172,7 +173,7 @@ void Parser::unwind() { for(;;) { -#ifdef DEBUG + DBS_START( PARSE ); for( TokenStack::iterator i = tsTerminal.begin(); i; i++ ) { if( (*i).eType == Token::tNumber ) @@ -184,7 +185,8 @@ void Parser::unwind() for( TokenStack::iterator i = tsNonTerminal.begin(); i; i++ ) Bu::print(" <%1>").arg( (*i).eType ); Bu::println(""); -#endif + DBS_END(); + if( tsNonTerminal.isEmpty() ) return; diff --git a/src/unitnumber.cpp b/src/unitnumber.cpp index c7b85b9..985f0d9 100644 --- a/src/unitnumber.cpp +++ b/src/unitnumber.cpp @@ -75,6 +75,8 @@ void UnitNumber::divide1() unitTest(Number("4") / Number("10") == "0"); unitTest(Number("4") % Number("10") == "4"); unitTest(Number("200") / Number("2") == "100"); + unitTest(Number("10", 5) / Number("0.01", 5) == "1000"); + unitTest(Number("552", 5) / Number("0.051", 5) == "10823.52941"); } #define mathTest( anum, op, bnum, answ ) \ -- cgit v1.2.3