*sumBigInt implementation 2
[biginteger-lo27.git] / biginteger.c
blob4b0c51c32adbdb4be8f4aadb8e89cf001472aea1
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
19 * THE SOFTWARE.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
28 #include "biginteger.h"
30 // Private Function Impl
32 #define ATOUI(str) ((unsigned int)strtoul(buffer, NULL, 10))
34 BigInteger CreateZeroBigInt()
36 BigInteger temp;
37 temp.Sign = Undefined;
38 temp.AbsValStart = NULL;
39 temp.AbsValEnd = NULL;
40 temp.Count = 0;
42 return temp;
45 BigUnsignedInteger* InitBUint(unsigned int value)
47 BigUnsignedInteger* bigUint = malloc(sizeof(BigUnsignedInteger));
48 bigUint->Value = value;
50 return bigUint;
53 void PrependNode(BigUnsignedInteger* b1, BigUnsignedInteger* b2)
55 b1->Next = b2;
56 b2->Previous = b1;
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;
74 bigInt->Count++;
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;
92 bigInt->Count++;
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);
106 bigInt->Count++;
110 // End Private Function Impl
114 // Public Function Impl
116 BigInteger newBigInteger(char* desc)
118 BigInteger newbigint=CreateZeroBigInt();
119 //iterative variable
120 int i;
121 //array of character where is stored a part of the total list of character
122 char temp[5];
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);
129 temp[5] = '\0';
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
135 if (i < 0) {
136 strncpy(temp, desc, i + 4);
137 temp[i+5] = '\0';
138 tempint = atoi(temp);
139 PushEndNode(&newbigint, tempint);
142 return newbigint;
145 void printBigInteger(BigInteger bint)
150 Boolean isNull(BigInteger bint)
152 return false;
155 int signBigInt(BigInteger bint)
157 return -1;
160 Boolean equalsBigInt(BigInteger b1, BigInteger b2)
162 return false;
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){
174 b2.Sign=Positive;
175 return diffBigInt(b1,b2);
176 }else{
177 //test if b1 is negative
178 if(b1.Sign==Negative && b2.Sign==Positive){
179 b1.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){
192 return Sum;
193 }else{
194 //test if one of the 2 BigInt is null
195 if (b1.Sign == Undefined && b2.Sign != Undefined)
196 Sum = b2;
197 else if (b2.Sign == Undefined && b1.Sign != Undefined)
198 Sum = b1;
202 //test if the 2 BigInt are both positive or both negative
203 int carry=0;
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);
208 //compute the carry
209 if(s >= 10000)
210 //if the sum exceed 10000
211 carry=1;
212 else
213 carry=0;
215 if (p1 == NULL) {//test if p2 is longer than p1
216 p2=p2->Next;
217 }else if (p2==NULL) { //test if p1 is longer than p2
218 p1=p1->Next;
221 if(carry==1){
222 PushEndNode(&Sum,1);
224 //if the two numbers are both negative
225 if(b1.Sign == Negative && b2.Sign == Negative)
226 Sum.Sign=Negative;
228 return Sum;
231 BigInteger diffBigInt(BigInteger b1, BigInteger b2)
237 return CreateZeroBigInt();
240 // End Public Function Impl