From f34eb76357fdfc314d6451fd11a2e4d6fcfce434 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 15 Apr 2013 15:28:12 -0600 Subject: Initial checkin. This project will most likely just be stuck into libbu++, but I didn't want to deal with building it all in windows. --- src/number.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/number.cpp (limited to 'src/number.cpp') diff --git a/src/number.cpp b/src/number.cpp new file mode 100644 index 0000000..e64be94 --- /dev/null +++ b/src/number.cpp @@ -0,0 +1,54 @@ +#include "number.h" + +#include + +#define iRadix (10) + +Number::Number( int iOrd ) : + iOrd( iOrd ), + aInt( 4 ) +{ +} + +Number::Number( const Bu::String &sData, int iOrd ) : + iOrd( iOrd ), + aInt( 4 ) +{ + for( int j = sData.getSize()-1; j >= 0; j-- ) + aInt.append( sData[j]-'0' ); +} + +Number::~Number() +{ +} + +Number Number::operator+( const Number &rhs ) const +{ + Number ret( iOrd ); + + int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() )+1; + + int iCarry = 0; + for( int j = 0; j < iPlaces; j++ ) + { + int iRes = iCarry + digit( j ) + rhs.digit( j ); + Bu::println(" Place: %1 + %2 + (%3) = %4"). + arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) + .arg( iRes ); + ret.aInt.append( (iRes%iRadix) ); + if( iRes < iRadix ) + iCarry = 0; + else + iCarry = iRes/iRadix; + } + + return ret; +} + +int Number::digit( int iOrder ) const +{ + if( iOrder >= aInt.getSize() ) + return 0; + return aInt[iOrder]; +} + -- cgit v1.2.3