summaryrefslogtreecommitdiff
path: root/src/number.cpp
blob: e64be9422d3e5b53ae7e2a3fb18f2cc16280549c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "number.h"

#include <bu/sio.h>

#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];
}