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 void PushEndNode(BigInteger
* bigInt
, unsigned int value
)
79 BigUnsignedInteger
* buint
= InitBUint(value
);
81 // Do the magic swapping
82 if (bigInt
->AbsValEnd
!= NULL
)
83 PrependNode(bigInt
->AbsValEnd
, buint
);
84 // Update BigInt's head field
85 bigInt
->AbsValEnd
= buint
;
87 // If it's the first node added, correctly set bigInt->AbsValStart
88 if (bigInt
->AbsValStart
== NULL
)
89 bigInt
->AbsValStart
= buint
;
94 // Used to insert a node in the *middle* of the BigInt.
95 // It Do *not* check if we reproduce Push(Front|End)Node behavior
96 // Use Push(Front|End)Node if you intend to do what these functions are meant for
97 void InsertNode(BigInteger
* bigInt
, BigUnsignedInteger
* next
, unsigned int value
)
99 BigUnsignedInteger
* buint
= InitBUint(value
);
100 BigUnsignedInteger
* temp
= next
->Previous
;
102 PrependNode(buint
, next
);
103 PrependNode(temp
, buint
);
108 // End Private Function Impl
112 // Public Function Impl
114 BigInteger
newBigInteger(char* desc
)
119 void printBigInteger(BigInteger bint
)
124 Boolean
isNull(BigInteger bint
)
129 int signBigInt(BigInteger bint
)
134 Boolean
equalsBigInt(BigInteger b1
, BigInteger b2
)
139 int compareBigInt(BigInteger b1
, BigInteger b2
)
144 BigInteger
sumBigInt(BigInteger b1
, BigInteger b2
)
149 BigInteger
diffBigInt(BigInteger b1
, BigInteger b2
)
154 // End Public Function Impl