From 20115d9183f6fb7a15cb106a3d698070eb54c3ea Mon Sep 17 00:00:00 2001 From: Jourdain Marine Date: Sun, 7 Dec 2008 15:22:24 +0100 Subject: [PATCH] *sumBigInt implementation 2 --- biginteger.c | 119 +++++++++++++++++++++++++++++++++++++++++------------------ biginteger.h | 2 +- 2 files changed, 85 insertions(+), 36 deletions(-) diff --git a/biginteger.c b/biginteger.c index cc898ea..4b0c51c 100644 --- a/biginteger.c +++ b/biginteger.c @@ -115,23 +115,30 @@ void InsertNode(BigInteger* bigInt, BigUnsignedInteger* next, unsigned int value BigInteger newBigInteger(char* desc) { - BigInteger newbigint=CreateZeroBigInt(); - int i;//iterative variable - char temp[5];//array of character where islu - unsigned int tempint;//temporary variable where is stored the integer incoming from the transformation 'character to interger' - - for(i=strlen(desc)-5;i>=0;i=i-4){//loop to build the double linked list until we reach the last group of 4 characters - strncpy(temp,desc + i,4); - temp[5]='\0'; - tempint=atoi(temp); - PushEndNode(&newbigint,tempint); + BigInteger newbigint=CreateZeroBigInt(); + //iterative variable + int i; + //array of character where is stored a part of the total list of character + char temp[5]; + //temporary variable where is stored the integer incoming from the transformation 'character to interger' + unsigned int tempint; + + //loop to build the double linked list until we reach the last group of 4 characters + for(i = strlen(desc)-5; i >= 0; i = i - 4) { + strncpy(temp, desc + i,4); + temp[5] = '\0'; + tempint = atoi(temp); + PushEndNode(&newbigint,tempint); } - if(i<0){//test if the number of digit of the number is divisible by 4. if it's not the case, it completes with the rest - strncpy(temp,desc,i+4); - temp[i+5]='\0'; - tempint=atoi(temp); - PushEndNode(&newbigint,tempint); + + //test if the number of digit of the number is divisible by 4. if it's not the case, it completes with the rest + if (i < 0) { + strncpy(temp, desc, i + 4); + temp[i+5] = '\0'; + tempint = atoi(temp); + PushEndNode(&newbigint, tempint); } + return newbigint; } @@ -142,50 +149,92 @@ void printBigInteger(BigInteger bint) Boolean isNull(BigInteger bint) { - + return false; } int signBigInt(BigInteger bint) { - + return -1; } Boolean equalsBigInt(BigInteger b1, BigInteger b2) { - + return false; } int compareBigInt(BigInteger b1, BigInteger b2) -{ - +{ + } BigInteger sumBigInt(BigInteger b1, BigInteger b2) { - BigInterger Sum=CreateZeroBigInt();//variable which will be the result of the sum - BigInteger p1=b1;//temporary pointer over b1 - BigInterger p2=b2;//temporary pointer over b2 - int carry=0;//the carry can be equal to 0 or 1 - while(p1!=NULL && p2!=NULL){//do the addition until we reach the last node of one of the two numbers, or both - PushFrontNode(&Sum, (p1->value + p2->value + carry)%10000); - if(p1->value + p2->value>=10000)//compute the carry + //test if b2 is negative + if(b1.Sign==Positive && b2.Sign==Negative){ + b2.Sign=Positive; + return diffBigInt(b1,b2); + }else{ + //test if b1 is negative + if(b1.Sign==Negative && b2.Sign==Positive){ + b1.Sign=Positive; + return diffBigInt(b2,b1); + } + } + //variable which will be the result of the sum + BigInteger Sum = CreateZeroBigInt(); + //temporary pointer over b1 + BigUnsignedInteger* p1 = b1.AbsValStart; + //temporary pointer over b2 + BigUnsignedInteger* p2 = b2.AbsValStart; + //test if the two BigInteger are null + + if (b1.Sign == Undefined && b2.Sign == Undefined){ + return Sum; + }else{ + //test if one of the 2 BigInt is null + if (b1.Sign == Undefined && b2.Sign != Undefined) + Sum = b2; + else if (b2.Sign == Undefined && b1.Sign != Undefined) + Sum = b1; + + } + + //test if the 2 BigInt are both positive or both negative + int carry=0; + //do the addition until we reach the last node of one of the two numbers, or both + while(p1 != NULL || p2 != NULL){ + unsigned int s=(p1==NULL ? 0 : p1->Value) + (p2==NULL ? 0 : p2->Value) + carry; + PushEndNode(&Sum, s%10000); + //compute the carry + if(s >= 10000) + //if the sum exceed 10000 carry=1; else carry=0; - p1=p1->next; - p2=p2->next; - } - if(p1==NULL && p2!=NULL) - PushFrontNode(&Sum,p2->value); - else{ - if(p2==NULL && p1!=NULL) - PushFrontNode(&Sum,p1->value); + + if (p1 == NULL) {//test if p2 is longer than p1 + p2=p2->Next; + }else if (p2==NULL) { //test if p1 is longer than p2 + p1=p1->Next; + } } + if(carry==1){ + PushEndNode(&Sum,1); + } + //if the two numbers are both negative + if(b1.Sign == Negative && b2.Sign == Negative) + Sum.Sign=Negative; + + return Sum; } BigInteger diffBigInt(BigInteger b1, BigInteger b2) { + + + + return CreateZeroBigInt(); } // End Public Function Impl diff --git a/biginteger.h b/biginteger.h index e729429..b053bd8 100644 --- a/biginteger.h +++ b/biginteger.h @@ -51,7 +51,7 @@ typedef struct { // The absolute value which is multiplied by sign to get the real integer value BigUnsignedInteger* AbsValStart; // Head BigUnsignedInteger* AbsValEnd; // Tail - + int Count; } BigInteger; -- 2.11.4.GIT