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/main.cpp | 79 ++++++++++++++++++++++++- src/number.cpp | 157 +++++++++++++++++++++++++++++++++++++++++++++++-- src/number.h | 10 ++++ src/packedintarray.cpp | 13 +++- src/packedintarray.h | 3 +- 5 files changed, 253 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d78bfc3..3678278 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -149,14 +149,89 @@ void numbertest1() arg( a.toString() ). arg( b.toString() ). arg( (a * b).toString() ); + + a = "123"; + b = "45"; + + println("%1 / %2 = %3"). + arg( a.toString() ). + arg( b.toString() ). + arg( (a / b).toString() ); +} + +#define compcheck( anum, op, bnum ) \ + a = #anum; b = #bnum; \ + println("%4: %1 " #op " %2 = %3").arg( a.toString() ).arg( b.toString() ). \ + arg( a op b ).arg( ((a op b) == (anum op bnum)) ? "pass" : "fail" ) + +void numbertestcomp() +{ + Number a, b; + + println("-==- Greater Than -==-"); + compcheck( 5, >, 10 ); + compcheck( 10, >, 5 ); + compcheck( 5, >, 5 ); + compcheck( 7, >, 5 ); + compcheck( 5, >, 7 ); + compcheck( 123, >, 122 ); + compcheck( 123, >, 123 ); + compcheck( 123, >, 124 ); + compcheck( -123, >, 122 ); + compcheck( -123, >, -122 ); + compcheck( -122, >, -123 ); + compcheck( 123, >, -122 ); + + println("-==- Less Than -==-"); + compcheck( 5, <, 10 ); + compcheck( 10, <, 5 ); + compcheck( 5, <, 5 ); + compcheck( 7, <, 5 ); + compcheck( 5, <, 7 ); + compcheck( 123, <, 122 ); + compcheck( 123, <, 123 ); + compcheck( 123, <, 124 ); + compcheck( -123, <, 122 ); + compcheck( -123, <, -122 ); + compcheck( -122, <, -123 ); + compcheck( 123, <, -122 ); + + println("-==- Greater Than or Equal To -==-"); + compcheck( 5, >=, 10 ); + compcheck( 10, >=, 5 ); + compcheck( 5, >=, 5 ); + compcheck( 7, >=, 5 ); + compcheck( 5, >=, 7 ); + compcheck( 123, >=, 122 ); + compcheck( 123, >=, 123 ); + compcheck( 123, >=, 124 ); + compcheck( -123, >=, 122 ); + compcheck( -123, >=, -122 ); + compcheck( -122, >=, -123 ); + compcheck( 123, >=, -122 ); + + println("-==- Less Than or Equal To -==-"); + compcheck( 5, <=, 10 ); + compcheck( 10, <=, 5 ); + compcheck( 5, <=, 5 ); + compcheck( 7, <=, 5 ); + compcheck( 5, <=, 7 ); + compcheck( 123, <=, 122 ); + compcheck( 123, <=, 123 ); + compcheck( 123, <=, 124 ); + compcheck( -123, <=, 122 ); + compcheck( -123, <=, -122 ); + compcheck( -122, <=, -123 ); + compcheck( 123, <=, -122 ); } -int main( int argc, char *argv[] ) +int main( int , char *[] ) { println("CliC"); // packedtest1(); - numbertest1(); +// numbertest1(); + numbertestcomp(); return 0; } 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(); diff --git a/src/number.h b/src/number.h index 1489623..7d65096 100644 --- a/src/number.h +++ b/src/number.h @@ -12,9 +12,19 @@ public: virtual ~Number(); Number &operator=( const Bu::String &sNum ); + Number operator+( const Number &rhs ) const; Number operator-( const Number &rhs ) const; Number operator*( const Number &rhs ) const; + Number operator/( const Number &rhs ) const; + Number operator-() const; + + bool operator==( const Number &rhs ) const; + bool operator!=( const Number &rhs ) const; + bool operator>( const Number &rhs ) const; + bool operator<( const Number &rhs ) const; + bool operator>=( const Number &rhs ) const; + bool operator<=( const Number &rhs ) const; void set( const Bu::String &sNum ); diff --git a/src/packedintarray.cpp b/src/packedintarray.cpp index 7654356..7c54063 100644 --- a/src/packedintarray.cpp +++ b/src/packedintarray.cpp @@ -96,7 +96,7 @@ PackedIntArray::Unit PackedIntArray::get( int idx ) const return ret; } -PackedIntArray::Unit PackedIntArray::set( int idx, PackedIntArray::Unit i ) +void PackedIntArray::set( int idx, PackedIntArray::Unit i ) { int iStore = idx*iBitWidth/StoreBits; int iBit = (idx*iBitWidth)%StoreBits; @@ -116,6 +116,17 @@ PackedIntArray::Unit PackedIntArray::set( int idx, PackedIntArray::Unit i ) } } +void PackedIntArray::set( const PackedIntArray &rSrc, int iStart, int iSize ) +{ + iCount = iSize; + checkCapacity(); + + for( int j = 0; j < iSize; j++ ) + { + set( j, rSrc.get( iStart+j ) ); + } +} + Bu::String PackedIntArray::toBitString() const { Bu::String sRet; diff --git a/src/packedintarray.h b/src/packedintarray.h index 8fcf794..a865df5 100644 --- a/src/packedintarray.h +++ b/src/packedintarray.h @@ -17,8 +17,9 @@ public: void remove(); Unit operator[]( int idx ) const { return get( idx ); } Unit get( int idx ) const; - Unit set( int idx, Unit i ); + void set( int idx, Unit i ); int getSize() const { return iCount; } + void set( const PackedIntArray &rSrc, int iStart, int iSize ); Bu::String toBitString() const; Bu::String toString() const; -- cgit v1.2.3