diff options
Diffstat (limited to 'src/number.cpp')
| -rw-r--r-- | src/number.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
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 @@ | |||
| 1 | #include "number.h" | ||
| 2 | |||
| 3 | #include <bu/sio.h> | ||
| 4 | |||
| 5 | #define iRadix (10) | ||
| 6 | |||
| 7 | Number::Number( int iOrd ) : | ||
| 8 | iOrd( iOrd ), | ||
| 9 | aInt( 4 ) | ||
| 10 | { | ||
| 11 | } | ||
| 12 | |||
| 13 | Number::Number( const Bu::String &sData, int iOrd ) : | ||
| 14 | iOrd( iOrd ), | ||
| 15 | aInt( 4 ) | ||
| 16 | { | ||
| 17 | for( int j = sData.getSize()-1; j >= 0; j-- ) | ||
| 18 | aInt.append( sData[j]-'0' ); | ||
| 19 | } | ||
| 20 | |||
| 21 | Number::~Number() | ||
| 22 | { | ||
| 23 | } | ||
| 24 | |||
| 25 | Number Number::operator+( const Number &rhs ) const | ||
| 26 | { | ||
| 27 | Number ret( iOrd ); | ||
| 28 | |||
| 29 | int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() )+1; | ||
| 30 | |||
| 31 | int iCarry = 0; | ||
| 32 | for( int j = 0; j < iPlaces; j++ ) | ||
| 33 | { | ||
| 34 | int iRes = iCarry + digit( j ) + rhs.digit( j ); | ||
| 35 | Bu::println(" Place: %1 + %2 + (%3) = %4"). | ||
| 36 | arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) | ||
| 37 | .arg( iRes ); | ||
| 38 | ret.aInt.append( (iRes%iRadix) ); | ||
| 39 | if( iRes < iRadix ) | ||
| 40 | iCarry = 0; | ||
| 41 | else | ||
| 42 | iCarry = iRes/iRadix; | ||
| 43 | } | ||
| 44 | |||
| 45 | return ret; | ||
| 46 | } | ||
| 47 | |||
| 48 | int Number::digit( int iOrder ) const | ||
| 49 | { | ||
| 50 | if( iOrder >= aInt.getSize() ) | ||
| 51 | return 0; | ||
| 52 | return aInt[iOrder]; | ||
| 53 | } | ||
| 54 | |||
