1 /* Copyright (c) 2008 Jérémie Laval, Marine Jourdain
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "biginteger.h"
30 // Private Function Impl
32 #define ATOUI(str) ((unsigned int)strtoul(buffer, NULL, 10))
34 BigInteger
CreateZeroBigInt()
37 temp
.Sign
= Undefined
;
38 temp
.AbsValStart
= NULL
;
39 temp
.AbsValEnd
= NULL
;
45 BigUnsignedInteger
* InitBUint(unsigned int value
)
47 BigUnsignedInteger
* bigUint
= malloc(sizeof(BigUnsignedInteger
));
48 bigUint
->Value
= value
;
53 void PrependNode(BigUnsignedInteger
* b1
, BigUnsignedInteger
* b2
)
59 // Used to push_front a value
60 void PushFrontNode(BigInteger
* bigInt
, unsigned int value
)
62 BigUnsignedInteger
* bigUint
= InitBUint(value
);
64 // Do the magic swapping
65 if (bigInt
->AbsValStart
!= NULL
)
66 PrependNode(bigUint
, bigInt
->AbsValStart
);
67 // Update BigInt's head field
68 bigInt
->AbsValStart
= bigUint
;
70 // If it's the first node added, correctly set bigInt->AbsValEnd
71 if (bigInt
->AbsValEnd
== NULL
)
72 bigInt
->AbsValEnd
= bigUint
;
77 //Used to push_end a value
78 void PushEndNode(BigInteger
* bigInt
, unsigned int value
)
80 BigUnsignedInteger
* buint
= InitBUint(value
);
82 // Do the magic swapping
83 if (bigInt
->AbsValEnd
!= NULL
)
84 PrependNode(bigInt
->AbsValEnd
, buint
);
85 // Update BigInt's tail field
86 bigInt
->AbsValEnd
= buint
;
88 // If it's the first node added, correctly set bigInt->AbsValStart
89 if (bigInt
->AbsValStart
== NULL
)
90 bigInt
->AbsValStart
= buint
;
95 // Used to insert a node in the *middle* of the BigInt.
96 // It Do *not* check if we reproduce Push(Front|End)Node behavior
97 // Use Push(Front|End)Node if you intend to do what these functions are meant for
98 void InsertNode(BigInteger
* bigInt
, BigUnsignedInteger
* next
, unsigned int value
)
100 BigUnsignedInteger
* buint
= InitBUint(value
);
101 BigUnsignedInteger
* temp
= next
->Previous
;
103 PrependNode(buint
, next
);
104 PrependNode(temp
, buint
);
110 // End Private Function Impl
114 // Public Function Impl
116 BigInteger
newBigInteger(char* desc
)
118 BigInteger newbigint
=CreateZeroBigInt();
121 //array of character where is stored a part of the total list of character
123 //temporary variable where is stored the integer incoming from the transformation 'character to interger'
124 unsigned int tempint
;
126 //loop to build the double linked list until we reach the last group of 4 characters
127 for(i
= strlen(desc
)-5; i
>= 0; i
= i
- 4) {
128 strncpy(temp
, desc
+ i
,4);
130 tempint
= atoi(temp
);
131 PushEndNode(&newbigint
,tempint
);
134 //test if the number of digit of the number is divisible by 4. if it's not the case, it completes with the rest
136 strncpy(temp
, desc
, i
+ 4);
138 tempint
= atoi(temp
);
139 PushEndNode(&newbigint
, tempint
);
145 void printBigInteger(BigInteger bint
)
150 Boolean
isNull(BigInteger bint
)
155 int signBigInt(BigInteger bint
)
160 Boolean
equalsBigInt(BigInteger b1
, BigInteger b2
)
165 int compareBigInt(BigInteger b1
, BigInteger b2
)
170 BigInteger
sumBigInt(BigInteger b1
, BigInteger b2
)
172 //test if b2 is negative
173 if(b1
.Sign
==Positive
&& b2
.Sign
==Negative
){
175 return diffBigInt(b1
,b2
);
177 //test if b1 is negative
178 if(b1
.Sign
==Negative
&& b2
.Sign
==Positive
){
180 return diffBigInt(b2
,b1
);
183 //variable which will be the result of the sum
184 BigInteger Sum
= CreateZeroBigInt();
185 //temporary pointer over b1
186 BigUnsignedInteger
* p1
= b1
.AbsValStart
;
187 //temporary pointer over b2
188 BigUnsignedInteger
* p2
= b2
.AbsValStart
;
189 //test if the two BigInteger are null
191 if (b1
.Sign
== Undefined
&& b2
.Sign
== Undefined
){
194 //test if one of the 2 BigInt is null
195 if (b1
.Sign
== Undefined
&& b2
.Sign
!= Undefined
)
197 else if (b2
.Sign
== Undefined
&& b1
.Sign
!= Undefined
)
202 //test if the 2 BigInt are both positive or both negative
204 //do the addition until we reach the last node of one of the two numbers, or both
205 while(p1
!= NULL
|| p2
!= NULL
){
206 unsigned int s
=(p1
==NULL
? 0 : p1
->Value
) + (p2
==NULL
? 0 : p2
->Value
) + carry
;
207 PushEndNode(&Sum
, s
%10000);
210 //if the sum exceed 10000
215 if (p1
== NULL
) {//test if p2 is longer than p1
217 }else if (p2
==NULL
) { //test if p1 is longer than p2
224 //if the two numbers are both negative
225 if(b1
.Sign
== Negative
&& b2
.Sign
== Negative
)
231 BigInteger
diffBigInt(BigInteger b1
, BigInteger b2
)
237 return CreateZeroBigInt();
240 // End Public Function Impl