summaryrefslogtreecommitdiff
path: root/src/number.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-04-15 15:28:12 -0600
committerMike Buland <mike@xagasoft.com>2013-04-15 15:28:12 -0600
commitf34eb76357fdfc314d6451fd11a2e4d6fcfce434 (patch)
treed4769ae51a1c703b0e55af0ef38a31991f9f7c3a /src/number.cpp
downloadclic-f34eb76357fdfc314d6451fd11a2e4d6fcfce434.tar.gz
clic-f34eb76357fdfc314d6451fd11a2e4d6fcfce434.tar.bz2
clic-f34eb76357fdfc314d6451fd11a2e4d6fcfce434.tar.xz
clic-f34eb76357fdfc314d6451fd11a2e4d6fcfce434.zip
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.
Diffstat (limited to 'src/number.cpp')
-rw-r--r--src/number.cpp54
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
7Number::Number( int iOrd ) :
8 iOrd( iOrd ),
9 aInt( 4 )
10{
11}
12
13Number::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
21Number::~Number()
22{
23}
24
25Number 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
48int Number::digit( int iOrder ) const
49{
50 if( iOrder >= aInt.getSize() )
51 return 0;
52 return aInt[iOrder];
53}
54