summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main.cpp79
-rw-r--r--src/number.cpp157
-rw-r--r--src/number.h10
-rw-r--r--src/packedintarray.cpp13
-rw-r--r--src/packedintarray.h3
5 files changed, 253 insertions, 9 deletions
diff --git a/src/main.cpp b/src/main.cpp
index d78bfc3..3678278 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -149,14 +149,89 @@ void numbertest1()
149 arg( a.toString() ). 149 arg( a.toString() ).
150 arg( b.toString() ). 150 arg( b.toString() ).
151 arg( (a * b).toString() ); 151 arg( (a * b).toString() );
152
153 a = "123";
154 b = "45";
155
156 println("%1 / %2 = %3").
157 arg( a.toString() ).
158 arg( b.toString() ).
159 arg( (a / b).toString() );
160}
161
162#define compcheck( anum, op, bnum ) \
163 a = #anum; b = #bnum; \
164 println("%4: %1 " #op " %2 = %3").arg( a.toString() ).arg( b.toString() ). \
165 arg( a op b ).arg( ((a op b) == (anum op bnum)) ? "pass" : "fail" )
166
167void numbertestcomp()
168{
169 Number a, b;
170
171 println("-==- Greater Than -==-");
172 compcheck( 5, >, 10 );
173 compcheck( 10, >, 5 );
174 compcheck( 5, >, 5 );
175 compcheck( 7, >, 5 );
176 compcheck( 5, >, 7 );
177 compcheck( 123, >, 122 );
178 compcheck( 123, >, 123 );
179 compcheck( 123, >, 124 );
180 compcheck( -123, >, 122 );
181 compcheck( -123, >, -122 );
182 compcheck( -122, >, -123 );
183 compcheck( 123, >, -122 );
184
185 println("-==- Less Than -==-");
186 compcheck( 5, <, 10 );
187 compcheck( 10, <, 5 );
188 compcheck( 5, <, 5 );
189 compcheck( 7, <, 5 );
190 compcheck( 5, <, 7 );
191 compcheck( 123, <, 122 );
192 compcheck( 123, <, 123 );
193 compcheck( 123, <, 124 );
194 compcheck( -123, <, 122 );
195 compcheck( -123, <, -122 );
196 compcheck( -122, <, -123 );
197 compcheck( 123, <, -122 );
198
199 println("-==- Greater Than or Equal To -==-");
200 compcheck( 5, >=, 10 );
201 compcheck( 10, >=, 5 );
202 compcheck( 5, >=, 5 );
203 compcheck( 7, >=, 5 );
204 compcheck( 5, >=, 7 );
205 compcheck( 123, >=, 122 );
206 compcheck( 123, >=, 123 );
207 compcheck( 123, >=, 124 );
208 compcheck( -123, >=, 122 );
209 compcheck( -123, >=, -122 );
210 compcheck( -122, >=, -123 );
211 compcheck( 123, >=, -122 );
212
213 println("-==- Less Than or Equal To -==-");
214 compcheck( 5, <=, 10 );
215 compcheck( 10, <=, 5 );
216 compcheck( 5, <=, 5 );
217 compcheck( 7, <=, 5 );
218 compcheck( 5, <=, 7 );
219 compcheck( 123, <=, 122 );
220 compcheck( 123, <=, 123 );
221 compcheck( 123, <=, 124 );
222 compcheck( -123, <=, 122 );
223 compcheck( -123, <=, -122 );
224 compcheck( -122, <=, -123 );
225 compcheck( 123, <=, -122 );
152} 226}
153 227
154int main( int argc, char *argv[] ) 228int main( int , char *[] )
155{ 229{
156 println("CliC"); 230 println("CliC");
157 231
158// packedtest1(); 232// packedtest1();
159 numbertest1(); 233// numbertest1();
234 numbertestcomp();
160 235
161 return 0; 236 return 0;
162} 237}
diff --git a/src/number.cpp b/src/number.cpp
index ac29c61..9b6ec55 100644
--- a/src/number.cpp
+++ b/src/number.cpp
@@ -6,15 +6,15 @@
6 6
7Number::Number( int iOrd ) : 7Number::Number( int iOrd ) :
8 iOrd( iOrd ), 8 iOrd( iOrd ),
9 aInt( 4 ), 9 bPositive( true ),
10 bPositive( true ) 10 aInt( 4 )
11{ 11{
12} 12}
13 13
14Number::Number( const Bu::String &sData, int iOrd ) : 14Number::Number( const Bu::String &sData, int iOrd ) :
15 iOrd( iOrd ), 15 iOrd( iOrd ),
16 aInt( 4 ), 16 bPositive( true ),
17 bPositive( true ) 17 aInt( 4 )
18{ 18{
19 set( sData ); 19 set( sData );
20} 20}
@@ -45,7 +45,6 @@ Number Number::operator*( const Number &rhs ) const
45{ 45{
46 Number ret( iOrd ); 46 Number ret( iOrd );
47 47
48 int iPlaces = Bu::buMax(rhs.aInt.getSize(), aInt.getSize() );
49 int iCnt = aInt.getSize()+rhs.aInt.getSize(); 48 int iCnt = aInt.getSize()+rhs.aInt.getSize();
50 49
51 int iCarry = 0; 50 int iCarry = 0;
@@ -86,6 +85,154 @@ Number Number::operator*( const Number &rhs ) const
86 return ret; 85 return ret;
87} 86}
88 87
88Number Number::operator/( const Number &rhs ) const
89{
90 if( rhs.aInt.getSize() > aInt.getSize() )
91 return Number( iOrd );
92
93 Number q( iOrd );
94
95 int iMinWidth = rhs.aInt.getSize();
96
97 Bu::println("%1\n-----").arg( aInt.toString() );
98 for( int j = aInt.getSize(); j > iMinWidth; j-- )
99 {
100 Number sub( iOrd );
101 sub.aInt.set( aInt, j-iMinWidth, iMinWidth );
102 Bu::println("%1\n%2\n----").arg( sub.toString() ).arg( rhs.toString() );
103 }
104
105 return q;
106}
107
108Number Number::operator-() const
109{
110 Number neg( *this );
111 neg.bPositive = !neg.bPositive;
112 return neg;
113}
114
115bool Number::operator==( const Number &rhs ) const
116{
117 if( rhs.bPositive != bPositive ||
118 rhs.iOrd != iOrd ||
119 rhs.aInt.getSize() != aInt.getSize() )
120 return false;
121
122 for( int j = 0; j < aInt.getSize(); j++ )
123 {
124 if( aInt[j] != rhs.aInt[j] )
125 return false;
126 }
127
128 return true;
129}
130
131bool Number::operator!=( const Number &rhs ) const
132{
133 return !(*this == rhs);
134}
135
136bool Number::operator>( const Number &rhs ) const
137{
138 if( bPositive && !rhs.bPositive )
139 return true;
140 if( !bPositive && rhs.bPositive )
141 return false;
142
143 if( aInt.getSize() > rhs.aInt.getSize() )
144 return bPositive;
145 if( aInt.getSize() < rhs.aInt.getSize() )
146 return !bPositive;
147
148 for( int j = aInt.getSize()-1; j >= 0; j-- )
149 {
150// Bu::println(" --> %1 > %2 -> %3").arg( aInt[j] ).arg( rhs.aInt[j] ).
151// arg( aInt[j] > rhs.aInt[j] );
152 int iDiff = aInt[j] - rhs.aInt[j];
153 if( iDiff < 0 )
154 return !bPositive;
155 else if( iDiff > 0 )
156 return bPositive;
157 }
158 return false;
159}
160
161bool Number::operator<( const Number &rhs ) const
162{
163 if( bPositive && !rhs.bPositive )
164 return false;
165 if( !bPositive && rhs.bPositive )
166 return true;
167
168 if( aInt.getSize() < rhs.aInt.getSize() )
169 return bPositive;
170 if( aInt.getSize() > rhs.aInt.getSize() )
171 return !bPositive;
172
173 for( int j = aInt.getSize()-1; j >= 0; j-- )
174 {
175// Bu::println(" --> %1 > %2 -> %3").arg( aInt[j] ).arg( rhs.aInt[j] ).
176// arg( aInt[j] < rhs.aInt[j] );
177 int iDiff = aInt[j] - rhs.aInt[j];
178 if( iDiff > 0 )
179 return !bPositive;
180 else if( iDiff < 0 )
181 return bPositive;
182 }
183 return false;
184}
185
186bool Number::operator>=( const Number &rhs ) const
187{
188 if( bPositive && !rhs.bPositive )
189 return true;
190 if( !bPositive && rhs.bPositive )
191 return false;
192
193 if( aInt.getSize() > rhs.aInt.getSize() )
194 return bPositive;
195 if( aInt.getSize() < rhs.aInt.getSize() )
196 return !bPositive;
197
198 for( int j = aInt.getSize()-1; j >= 0; j-- )
199 {
200// Bu::println(" --> %1 > %2 -> %3").arg( aInt[j] ).arg( rhs.aInt[j] ).
201// arg( aInt[j] > rhs.aInt[j] );
202 int iDiff = aInt[j] - rhs.aInt[j];
203 if( iDiff < 0 )
204 return !bPositive;
205 else if( iDiff > 0 )
206 return bPositive;
207 }
208 return true;
209}
210
211bool Number::operator<=( const Number &rhs ) const
212{
213 if( bPositive && !rhs.bPositive )
214 return false;
215 if( !bPositive && rhs.bPositive )
216 return true;
217
218 if( aInt.getSize() < rhs.aInt.getSize() )
219 return bPositive;
220 if( aInt.getSize() > rhs.aInt.getSize() )
221 return !bPositive;
222
223 for( int j = aInt.getSize()-1; j >= 0; j-- )
224 {
225// Bu::println(" --> %1 > %2 -> %3").arg( aInt[j] ).arg( rhs.aInt[j] ).
226// arg( aInt[j] < rhs.aInt[j] );
227 int iDiff = aInt[j] - rhs.aInt[j];
228 if( iDiff > 0 )
229 return !bPositive;
230 else if( iDiff < 0 )
231 return bPositive;
232 }
233 return true;
234}
235
89void Number::set( const Bu::String &sNum ) 236void Number::set( const Bu::String &sNum )
90{ 237{
91 aInt.clear(); 238 aInt.clear();
diff --git a/src/number.h b/src/number.h
index 1489623..7d65096 100644
--- a/src/number.h
+++ b/src/number.h
@@ -12,9 +12,19 @@ public:
12 virtual ~Number(); 12 virtual ~Number();
13 13
14 Number &operator=( const Bu::String &sNum ); 14 Number &operator=( const Bu::String &sNum );
15
15 Number operator+( const Number &rhs ) const; 16 Number operator+( const Number &rhs ) const;
16 Number operator-( const Number &rhs ) const; 17 Number operator-( const Number &rhs ) const;
17 Number operator*( const Number &rhs ) const; 18 Number operator*( const Number &rhs ) const;
19 Number operator/( const Number &rhs ) const;
20 Number operator-() const;
21
22 bool operator==( const Number &rhs ) const;
23 bool operator!=( const Number &rhs ) const;
24 bool operator>( const Number &rhs ) const;
25 bool operator<( const Number &rhs ) const;
26 bool operator>=( const Number &rhs ) const;
27 bool operator<=( const Number &rhs ) const;
18 28
19 void set( const Bu::String &sNum ); 29 void set( const Bu::String &sNum );
20 30
diff --git a/src/packedintarray.cpp b/src/packedintarray.cpp
index 7654356..7c54063 100644
--- a/src/packedintarray.cpp
+++ b/src/packedintarray.cpp
@@ -96,7 +96,7 @@ PackedIntArray::Unit PackedIntArray::get( int idx ) const
96 return ret; 96 return ret;
97} 97}
98 98
99PackedIntArray::Unit PackedIntArray::set( int idx, PackedIntArray::Unit i ) 99void PackedIntArray::set( int idx, PackedIntArray::Unit i )
100{ 100{
101 int iStore = idx*iBitWidth/StoreBits; 101 int iStore = idx*iBitWidth/StoreBits;
102 int iBit = (idx*iBitWidth)%StoreBits; 102 int iBit = (idx*iBitWidth)%StoreBits;
@@ -116,6 +116,17 @@ PackedIntArray::Unit PackedIntArray::set( int idx, PackedIntArray::Unit i )
116 } 116 }
117} 117}
118 118
119void PackedIntArray::set( const PackedIntArray &rSrc, int iStart, int iSize )
120{
121 iCount = iSize;
122 checkCapacity();
123
124 for( int j = 0; j < iSize; j++ )
125 {
126 set( j, rSrc.get( iStart+j ) );
127 }
128}
129
119Bu::String PackedIntArray::toBitString() const 130Bu::String PackedIntArray::toBitString() const
120{ 131{
121 Bu::String sRet; 132 Bu::String sRet;
diff --git a/src/packedintarray.h b/src/packedintarray.h
index 8fcf794..a865df5 100644
--- a/src/packedintarray.h
+++ b/src/packedintarray.h
@@ -17,8 +17,9 @@ public:
17 void remove(); 17 void remove();
18 Unit operator[]( int idx ) const { return get( idx ); } 18 Unit operator[]( int idx ) const { return get( idx ); }
19 Unit get( int idx ) const; 19 Unit get( int idx ) const;
20 Unit set( int idx, Unit i ); 20 void set( int idx, Unit i );
21 int getSize() const { return iCount; } 21 int getSize() const { return iCount; }
22 void set( const PackedIntArray &rSrc, int iStart, int iSize );
22 23
23 Bu::String toBitString() const; 24 Bu::String toBitString() const;
24 Bu::String toString() const; 25 Bu::String toString() const;