summaryrefslogtreecommitdiff
path: root/src/number.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-04-16 10:50:07 -0600
committerMike Buland <mike@xagasoft.com>2013-04-16 10:50:07 -0600
commit25989c6d3911b1d29a5866e668bff52537893afb (patch)
treedd07375016a8a3836605cf4e2d9eca829193693f /src/number.cpp
parent44542adf023315d60a8ffc4863f2b161b3c1eb90 (diff)
downloadclic-25989c6d3911b1d29a5866e668bff52537893afb.tar.gz
clic-25989c6d3911b1d29a5866e668bff52537893afb.tar.bz2
clic-25989c6d3911b1d29a5866e668bff52537893afb.tar.xz
clic-25989c6d3911b1d29a5866e668bff52537893afb.zip
Added operators: -, ==, !=, <, >, <=, >=
Still working on division, needed some other operators to make it work.
Diffstat (limited to 'src/number.cpp')
-rw-r--r--src/number.cpp157
1 files changed, 152 insertions, 5 deletions
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();