From 25989c6d3911b1d29a5866e668bff52537893afb Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 16 Apr 2013 10:50:07 -0600 Subject: Added operators: -, ==, !=, <, >, <=, >= Still working on division, needed some other operators to make it work. --- src/number.cpp | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 152 insertions(+), 5 deletions(-) (limited to 'src/number.cpp') diff --git a/src/number.cpp b/src/number.cpp index ac29c61..9b6ec55 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -6,15 +6,15 @@ Number::Number( int iOrd ) : iOrd( iOrd ), - aInt( 4 ), - bPositive( true ) + bPositive( true ), + aInt( 4 ) { } Number::Number( const Bu::String &sData, int iOrd ) : iOrd( iOrd ), - aInt( 4 ), - bPositive( true ) + bPositive( true ), + aInt( 4 ) { set( sData ); } @@ -45,7 +45,6 @@ Number Number::operator*( const Number &rhs ) const { Number ret( iOrd ); - int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() ); int iCnt = aInt.getSize()+rhs.aInt.getSize(); int iCarry = 0; @@ -86,6 +85,154 @@ Number Number::operator*( const Number &rhs ) const return ret; } +Number Number::operator/( const Number &rhs ) const +{ + if( rhs.aInt.getSize() > aInt.getSize() ) + return Number( iOrd ); + + Number q( iOrd ); + + int iMinWidth = rhs.aInt.getSize(); + + Bu::println("%1\n-----").arg( aInt.toString() ); + for( int j = aInt.getSize(); j > iMinWidth; j-- ) + { + Number sub( iOrd ); + sub.aInt.set( aInt, j-iMinWidth, iMinWidth ); + Bu::println("%1\n%2\n----").arg( sub.toString() ).arg( rhs.toString() ); + } + + return q; +} + +Number Number::operator-() const +{ + Number neg( *this ); + neg.bPositive = !neg.bPositive; + return neg; +} + +bool Number::operator==( const Number &rhs ) const +{ + if( rhs.bPositive != bPositive || + rhs.iOrd != iOrd || + rhs.aInt.getSize() != aInt.getSize() ) + return false; + + for( int j = 0; j < aInt.getSize(); j++ ) + { + if( aInt[j] != rhs.aInt[j] ) + return false; + } + + return true; +} + +bool Number::operator!=( const Number &rhs ) const +{ + return !(*this == rhs); +} + +bool Number::operator>( const Number &rhs ) const +{ + if( bPositive && !rhs.bPositive ) + return true; + if( !bPositive && rhs.bPositive ) + return false; + + if( aInt.getSize() > rhs.aInt.getSize() ) + return bPositive; + if( aInt.getSize() < rhs.aInt.getSize() ) + return !bPositive; + + for( int j = aInt.getSize()-1; j >= 0; j-- ) + { +// Bu::println(" --> %1 > %2 -> %3").arg( aInt[j] ).arg( rhs.aInt[j] ). +// arg( aInt[j] > rhs.aInt[j] ); + int iDiff = aInt[j] - rhs.aInt[j]; + if( iDiff < 0 ) + return !bPositive; + else if( iDiff > 0 ) + return bPositive; + } + return false; +} + +bool Number::operator<( const Number &rhs ) const +{ + if( bPositive && !rhs.bPositive ) + return false; + if( !bPositive && rhs.bPositive ) + return true; + + if( aInt.getSize() < rhs.aInt.getSize() ) + return bPositive; + if( aInt.getSize() > rhs.aInt.getSize() ) + return !bPositive; + + for( int j = aInt.getSize()-1; j >= 0; j-- ) + { +// Bu::println(" --> %1 > %2 -> %3").arg( aInt[j] ).arg( rhs.aInt[j] ). +// arg( aInt[j] < rhs.aInt[j] ); + int iDiff = aInt[j] - rhs.aInt[j]; + if( iDiff > 0 ) + return !bPositive; + else if( iDiff < 0 ) + return bPositive; + } + return false; +} + +bool Number::operator>=( const Number &rhs ) const +{ + if( bPositive && !rhs.bPositive ) + return true; + if( !bPositive && rhs.bPositive ) + return false; + + if( aInt.getSize() > rhs.aInt.getSize() ) + return bPositive; + if( aInt.getSize() < rhs.aInt.getSize() ) + return !bPositive; + + for( int j = aInt.getSize()-1; j >= 0; j-- ) + { +// Bu::println(" --> %1 > %2 -> %3").arg( aInt[j] ).arg( rhs.aInt[j] ). +// arg( aInt[j] > rhs.aInt[j] ); + int iDiff = aInt[j] - rhs.aInt[j]; + if( iDiff < 0 ) + return !bPositive; + else if( iDiff > 0 ) + return bPositive; + } + return true; +} + +bool Number::operator<=( const Number &rhs ) const +{ + if( bPositive && !rhs.bPositive ) + return false; + if( !bPositive && rhs.bPositive ) + return true; + + if( aInt.getSize() < rhs.aInt.getSize() ) + return bPositive; + if( aInt.getSize() > rhs.aInt.getSize() ) + return !bPositive; + + for( int j = aInt.getSize()-1; j >= 0; j-- ) + { +// Bu::println(" --> %1 > %2 -> %3").arg( aInt[j] ).arg( rhs.aInt[j] ). +// arg( aInt[j] < rhs.aInt[j] ); + int iDiff = aInt[j] - rhs.aInt[j]; + if( iDiff > 0 ) + return !bPositive; + else if( iDiff < 0 ) + return bPositive; + } + return true; +} + void Number::set( const Bu::String &sNum ) { aInt.clear(); -- cgit v1.2.3