summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-04-15 23:45:48 -0600
committerMike Buland <mike@xagasoft.com>2013-04-15 23:45:48 -0600
commit44542adf023315d60a8ffc4863f2b161b3c1eb90 (patch)
tree3500c1d6cfa3e5670bde199e24125c4b8e0c33db /src
parentf34eb76357fdfc314d6451fd11a2e4d6fcfce434 (diff)
downloadclic-44542adf023315d60a8ffc4863f2b161b3c1eb90.tar.gz
clic-44542adf023315d60a8ffc4863f2b161b3c1eb90.tar.bz2
clic-44542adf023315d60a8ffc4863f2b161b3c1eb90.tar.xz
clic-44542adf023315d60a8ffc4863f2b161b3c1eb90.zip
Addition, subtraction, and multiplication work now
Division isn't working yet, there are too many options, I'll figure out something eventually :-P
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp117
-rw-r--r--src/number.cpp166
-rw-r--r--src/number.h11
-rw-r--r--src/packedintarray.cpp29
-rw-r--r--src/packedintarray.h3
5 files changed, 304 insertions, 22 deletions
diff --git a/src/main.cpp b/src/main.cpp
index be49a1d..d78bfc3 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -33,21 +33,130 @@ void numbertest1()
33{ 33{
34 println("-==- Number test -==-"); 34 println("-==- Number test -==-");
35 35
36 Number a("523"); 36 Number a("1000902491523000321");
37 Number b("498"); 37 Number b("3004392012498000700");
38 38
39 println("%1 + %2 = %3"). 39 println("%1 + %2 = %3").
40 arg( a.toString() ). 40 arg( a.toString() ).
41 arg( b.toString() ). 41 arg( b.toString() ).
42 arg( (a + b).toString() ); 42 arg( (a + b).toString() );
43
44 println("%1 * %2 = %3").
45 arg( a.toString() ).
46 arg( b.toString() ).
47 arg( (a * b).toString() );
48
49 a = "-872";
50 b = "123";
51
52 println("%1 + %2 = %3").
53 arg( a.toString() ).
54 arg( b.toString() ).
55 arg( (a + b).toString() );
56
57 a = "728";
58 b = "-51";
59
60 println("%1 + %2 = %3").
61 arg( a.toString() ).
62 arg( b.toString() ).
63 arg( (a + b).toString() );
64
65 a = "44";
66 b = "-55";
67
68 println("%1 + %2 = %3").
69 arg( a.toString() ).
70 arg( b.toString() ).
71 arg( (a + b).toString() );
72
73 a = "44";
74 b = "-66";
75
76 println("%1 + %2 = %3").
77 arg( a.toString() ).
78 arg( b.toString() ).
79 arg( (a + b).toString() );
80
81 a = "44";
82 b = "-66";
83
84 println("%1 - %2 = %3").
85 arg( a.toString() ).
86 arg( b.toString() ).
87 arg( (a - b).toString() );
88
89 a = "44";
90 b = "66";
91
92 println("%1 - %2 = %3").
93 arg( a.toString() ).
94 arg( b.toString() ).
95 arg( (a - b).toString() );
96
97 a = "7814";
98 b = "24";
99
100 println("%1 * %2 = %3").
101 arg( a.toString() ).
102 arg( b.toString() ).
103 arg( (a * b).toString() );
104
105 a = "12345";
106 b = "678";
107
108 println("%1 * %2 = %3").
109 arg( a.toString() ).
110 arg( b.toString() ).
111 arg( (a * b).toString() );
112
113 a = "3592846";
114 b = "944634757";
115
116 println("%1 * %2 = %3").
117 arg( a.toString() ).
118 arg( b.toString() ).
119 arg( (a * b).toString() );
120
121 a = "3592846";
122 b = "";
123
124 println("%1 * %2 = %3").
125 arg( a.toString() ).
126 arg( b.toString() ).
127 arg( (a * b).toString() );
128
129 a = "123";
130 b = "-45";
131
132 println("%1 * %2 = %3").
133 arg( a.toString() ).
134 arg( b.toString() ).
135 arg( (a * b).toString() );
136
137 a = "-123";
138 b = "45";
139
140 println("%1 * %2 = %3").
141 arg( a.toString() ).
142 arg( b.toString() ).
143 arg( (a * b).toString() );
144
145 a = "-123";
146 b = "-45";
147
148 println("%1 * %2 = %3").
149 arg( a.toString() ).
150 arg( b.toString() ).
151 arg( (a * b).toString() );
43} 152}
44 153
45int main( int argc, char *argv[] ) 154int main( int argc, char *argv[] )
46{ 155{
47 println("CliC"); 156 println("CliC");
48 157
49 packedtest1(); 158// packedtest1();
50// numbertest1(); 159 numbertest1();
51 160
52 return 0; 161 return 0;
53} 162}
diff --git a/src/number.cpp b/src/number.cpp
index e64be94..ac29c61 100644
--- a/src/number.cpp
+++ b/src/number.cpp
@@ -6,45 +6,110 @@
6 6
7Number::Number( int iOrd ) : 7Number::Number( int iOrd ) :
8 iOrd( iOrd ), 8 iOrd( iOrd ),
9 aInt( 4 ) 9 aInt( 4 ),
10 bPositive( true )
10{ 11{
11} 12}
12 13
13Number::Number( const Bu::String &sData, int iOrd ) : 14Number::Number( const Bu::String &sData, int iOrd ) :
14 iOrd( iOrd ), 15 iOrd( iOrd ),
15 aInt( 4 ) 16 aInt( 4 ),
17 bPositive( true )
16{ 18{
17 for( int j = sData.getSize()-1; j >= 0; j-- ) 19 set( sData );
18 aInt.append( sData[j]-'0' );
19} 20}
20 21
21Number::~Number() 22Number::~Number()
22{ 23{
23} 24}
24 25
26Number &Number::operator=( const Bu::String &sNum )
27{
28 set( sNum );
29 return *this;
30}
31
32template<typename t> t tabs( t x ) { return (x<(t)0)?(-x):x; }
33
25Number Number::operator+( const Number &rhs ) const 34Number Number::operator+( const Number &rhs ) const
26{ 35{
36 return add( rhs, false );
37}
38
39Number Number::operator-( const Number &rhs ) const
40{
41 return add( rhs, true );
42}
43
44Number Number::operator*( const Number &rhs ) const
45{
27 Number ret( iOrd ); 46 Number ret( iOrd );
28 47
29 int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() )+1; 48 int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() );
49 int iCnt = aInt.getSize()+rhs.aInt.getSize();
30 50
31 int iCarry = 0; 51 int iCarry = 0;
32 for( int j = 0; j < iPlaces; j++ ) 52 int iZeros = 0;
53 for( int j = 0; j < iCnt || iCarry > 0; j++ )
33 { 54 {
34 int iRes = iCarry + digit( j ) + rhs.digit( j ); 55// Bu::println("Pos %1").arg(j);
35 Bu::println(" Place: %1 + %2 + (%3) = %4"). 56 int iPos = iCarry;
36 arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) 57 iCarry = 0;
37 .arg( iRes ); 58 for( int k = 0; k < rhs.aInt.getSize(); k++ )
38 ret.aInt.append( (iRes%iRadix) ); 59 {
39 if( iRes < iRadix ) 60 if( j-k < 0 )
40 iCarry = 0; 61 break;
62
63 int iRes = digit(j-k)*rhs.digit(k);
64 iPos += iRes;
65
66// Bu::println(" [%1] %2 * [%3] %4 = %5 -> %6").
67// arg( j-k ).arg( digit(j-k) ).arg( k ).arg( rhs.digit(k) ).
68// arg( iRes ).arg( iPos ).arg( iCarry );
69 }
70 if( (iPos%iRadix) == 0 )
71 iZeros++;
41 else 72 else
42 iCarry = iRes/iRadix; 73 {
74 for(; iZeros > 0; iZeros-- )
75 ret.aInt.append( 0 );
76 ret.aInt.append( iPos%iRadix );
77 }
78 iCarry += iPos/iRadix;
43 } 79 }
44 80
81 if( bPositive == rhs.bPositive )
82 ret.bPositive = true;
83 else
84 ret.bPositive = false;
85
45 return ret; 86 return ret;
46} 87}
47 88
89void Number::set( const Bu::String &sNum )
90{
91 aInt.clear();
92 bPositive = true;
93 for( int j = sNum.getSize()-1; j >= 0; j-- )
94 {
95 if( sNum[j] == '+' )
96 break;
97 if( sNum[j] == '-' )
98 {
99 bPositive = false;
100 break;
101 }
102 aInt.append( sNum[j]-'0' );
103 }
104}
105
106Bu::String Number::toString() const
107{
108 if( aInt.getSize() == 0 )
109 return "0";
110 return (bPositive?"":"-") + aInt.toString();
111}
112
48int Number::digit( int iOrder ) const 113int Number::digit( int iOrder ) const
49{ 114{
50 if( iOrder >= aInt.getSize() ) 115 if( iOrder >= aInt.getSize() )
@@ -52,3 +117,76 @@ int Number::digit( int iOrder ) const
52 return aInt[iOrder]; 117 return aInt[iOrder];
53} 118}
54 119
120Number Number::add( const Number &rhs, bool bSub ) const
121{
122 Number ret( iOrd );
123
124 int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() );
125
126 int iZeros = 0;
127 int iCarry = 0;
128
129 if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive))
130 {
131 ret.bPositive = bPositive;
132 for( int j = 0; j < iPlaces || iCarry > 0; j++ )
133 {
134 int iRes = iCarry + digit( j ) + rhs.digit( j );
135// Bu::println(" Place: %1 + %2 + (%3) = %4").
136// arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry )
137// .arg( iRes );
138 if( iRes == 0 )
139 {
140 iZeros++;
141 continue;
142 }
143 for(; iZeros > 0; iZeros-- )
144 ret.aInt.append( 0 );
145
146 ret.aInt.append( (iRes%iRadix) );
147 if( iRes < iRadix )
148 iCarry = 0;
149 else
150 iCarry = iRes/iRadix;
151 }
152 }
153 else
154 {
155 iCarry = 1;
156// Bu::println("--method b--");
157 ret.bPositive = bPositive;
158 int iRes;
159 for( int j = 0; j < iPlaces; j++ )
160 {
161 iRes = digit( j ) + (9-rhs.digit( j )) + iCarry;
162// Bu::println(" Place: %1 + %2 + (%3) = %4").
163// arg( digit(j) ).arg( 9-rhs.digit( j ) ).arg( iCarry )
164// .arg( iRes );
165 if( iRes == 0 )
166 {
167 iZeros++;
168 continue;
169 }
170 for(; iZeros > 0; iZeros-- )
171 ret.aInt.append( 0 );
172
173 ret.aInt.append( (iRes%iRadix) );
174 if( iRes < iRadix )
175 iCarry = 0;
176 else
177 iCarry = iRes/iRadix;
178 }
179 if( iCarry == 0 )
180 {
181 ret.bPositive = false;
182 ret.aInt.set( 0, 9-ret.aInt.get( 0 )+1);
183 for( int j = 1; j < ret.aInt.getSize(); j++ )
184 {
185 ret.aInt.set( j, 9-ret.aInt.get( j ));
186 }
187 }
188 }
189
190 return ret;
191}
192
diff --git a/src/number.h b/src/number.h
index 4ada829..1489623 100644
--- a/src/number.h
+++ b/src/number.h
@@ -11,19 +11,28 @@ public:
11 Number( const Bu::String &sData, int iOrd=0 ); 11 Number( const Bu::String &sData, int iOrd=0 );
12 virtual ~Number(); 12 virtual ~Number();
13 13
14 Number &operator=( const Bu::String &sNum );
14 Number operator+( const Number &rhs ) const; 15 Number operator+( const Number &rhs ) const;
16 Number operator-( const Number &rhs ) const;
17 Number operator*( const Number &rhs ) const;
18
19 void set( const Bu::String &sNum );
15 20
16 operator Bu::String() const 21 operator Bu::String() const
17 { 22 {
18 return aInt.toString(); 23 return aInt.toString();
19 } 24 }
20 25
21 Bu::String toString() const { return aInt.toString(); } 26 Bu::String toString() const;
22 27
23 int digit( int iOrder ) const; 28 int digit( int iOrder ) const;
24 29
25private: 30private:
31 Number add( const Number &rhs, bool bSub ) const;
32
33private:
26 int iOrd; 34 int iOrd;
35 bool bPositive;
27 PackedIntArray aInt; 36 PackedIntArray aInt;
28}; 37};
29 38
diff --git a/src/packedintarray.cpp b/src/packedintarray.cpp
index 817a7ab..7654356 100644
--- a/src/packedintarray.cpp
+++ b/src/packedintarray.cpp
@@ -41,11 +41,28 @@ PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth, int iCount ):
41 uMask |= (1<<j); 41 uMask |= (1<<j);
42} 42}
43 43
44PackedIntArray::PackedIntArray( const PackedIntArray &rSrc ) :
45 iBitWidth( rSrc.iBitWidth ),
46 aData( NULL ),
47 iCapacity( rSrc.iCapacity ),
48 iCount( rSrc.iCount ),
49 uMask( rSrc.uMask )
50{
51 int iSize = StoreCount(iCapacity);
52 aData = new Store[iSize];
53 memcpy( aData, rSrc.aData, iSize*sizeof(Store) );
54}
55
44PackedIntArray::~PackedIntArray() 56PackedIntArray::~PackedIntArray()
45{ 57{
46 delete[] aData; 58 delete[] aData;
47} 59}
48 60
61void PackedIntArray::clear()
62{
63 iCount = 0;
64}
65
49void PackedIntArray::append( PackedIntArray::Unit i ) 66void PackedIntArray::append( PackedIntArray::Unit i )
50{ 67{
51 iCount++; 68 iCount++;
@@ -53,6 +70,12 @@ void PackedIntArray::append( PackedIntArray::Unit i )
53 set( iCount-1, i ); 70 set( iCount-1, i );
54} 71}
55 72
73void PackedIntArray::remove()
74{
75 if( iCount > 0 )
76 iCount--;
77}
78
56PackedIntArray::Unit PackedIntArray::get( int idx ) const 79PackedIntArray::Unit PackedIntArray::get( int idx ) const
57{ 80{
58 int iStore = idx*iBitWidth/StoreBits; 81 int iStore = idx*iBitWidth/StoreBits;
@@ -121,12 +144,12 @@ void PackedIntArray::checkCapacity()
121{ 144{
122 if( iCount > iCapacity ) 145 if( iCount > iCapacity )
123 { 146 {
124 Bu::println("!!! Resizing !!!"); 147// Bu::println("!!! Resizing !!!");
125 Store *aOldData = aData; 148 Store *aOldData = aData;
126 int iNewSize = StoreCount(iCapacity*2); 149 int iNewSize = StoreCount(iCapacity*2);
127 int iSize = StoreCount(iCapacity); 150 int iSize = StoreCount(iCapacity);
128 Bu::println(" %1 => %2 (%3 bit words)").arg( iSize ).arg( iNewSize ) 151// Bu::println(" %1 => %2 (%3 bit words)").arg( iSize ).arg( iNewSize )
129 .arg( StoreBits ); 152// .arg( StoreBits );
130 aData = new Store[iNewSize]; 153 aData = new Store[iNewSize];
131 memset( aData, 0, iNewSize*sizeof(Store) ); 154 memset( aData, 0, iNewSize*sizeof(Store) );
132 memcpy( aData, aOldData, iSize*sizeof(Store) ); 155 memcpy( aData, aOldData, iSize*sizeof(Store) );
diff --git a/src/packedintarray.h b/src/packedintarray.h
index 953492b..8fcf794 100644
--- a/src/packedintarray.h
+++ b/src/packedintarray.h
@@ -9,9 +9,12 @@ public:
9 typedef uint_fast8_t Unit; 9 typedef uint_fast8_t Unit;
10 PackedIntArray( Unit iBitWidth ); 10 PackedIntArray( Unit iBitWidth );
11 PackedIntArray( Unit iBitWidth, int iCapacity ); 11 PackedIntArray( Unit iBitWidth, int iCapacity );
12 PackedIntArray( const PackedIntArray &rSrc );
12 virtual ~PackedIntArray(); 13 virtual ~PackedIntArray();
13 14
15 void clear();
14 void append( Unit i ); 16 void append( Unit i );
17 void remove();
15 Unit operator[]( int idx ) const { return get( idx ); } 18 Unit operator[]( int idx ) const { return get( idx ); }
16 Unit get( int idx ) const; 19 Unit get( int idx ) const;
17 Unit set( int idx, Unit i ); 20 Unit set( int idx, Unit i );