Beispiel #1
0
bigInteger restBigInt(bigInteger a, bigInteger b)
{
    /* Declarations */
    bigInteger result=NULL;
    bigInteger quo=NULL;
    bigInteger mul=NULL;
    result=malloc(sizeof(nb));
    quo=malloc(sizeof(nb));
    mul=malloc(sizeof(nb));

    /* a / b = q * b + r so r = a - (b * q) */
    quo=quotientBigInt(a,b);
    mul=mulBigInt(b,quo);
    result=diffBigInt(a,mul);


    return result;
}
Beispiel #2
0
int main(void)
{
    int choice = 0, lastOpe = 0;
    unsigned long fact = 0;
    char *str = NULL;
    BigInteger biOne = NULL, biTwo = NULL, res = NULL, temp = NULL;

    do
    {
        choice = display_menu();

        switch(choice)
        {
            case 1:
                clean_stdin();
                str = new_str_of_big_int(1);
                biOne = newBigInteger(str);
                free(str);
                str = NULL;
                break;
            case 2:
                clean_stdin();
                str = new_str_of_big_int(2);
                biTwo = newBigInteger(str);
                free(str);
                str = NULL;
                break;
            case 3:
                temp = fromBigIntegerToBigInteger(biOne);
                biOne = fromBigIntegerToBigInteger(biTwo);
                biTwo = fromBigIntegerToBigInteger(temp);
                break;
            case 4:
                lastOpe = '+';
                res = sumBigInt(biOne, biTwo);
                break;
            case 5:
                lastOpe = '-';
                res = diffBigInt(biOne, biTwo);
                break;
            case 6:
                lastOpe = '*';
                res = mulBigInt(biOne, biTwo);
                break;
            case 7:
                printf("Enter the number :\t");
                scanf("%lu", &fact);
                res = factorial(fact);
                printf("(%ld)! = ", fact);
                printBigInteger(res);
                break;
            case 8:
                printf("BigInteger 1 :\t");
                printBigInteger(biOne);
                printf("BigInteger 2:\t");
                printBigInteger(biTwo);
                if(lastOpe != 0)
                {
                    printf("\n(1) %c (2) = ", lastOpe);
                    printBigInteger(res);
                }
                break;
        }
        printf("\n");
    }while (choice != 9);

    delete_big_int(biOne);
    delete_big_int(biTwo);
    delete_big_int(res);
    delete_big_int(temp);
    return 0;
}
Beispiel #3
0
bigInteger sumBigInt(bigInteger a, bigInteger b)
{
    /* Declarations */
    int ta,tb,r=0,sum;
    bigInteger result=NULL;
    Element* l1=malloc(sizeof(Element));
    Element* l2=malloc(sizeof(Element));
    result=malloc(sizeof(nb));
    /* If one or the two is/are null */
    if(a==NULL || b==NULL)
    {
        if(a==b)
        {
            l1=insertTail(l1,0);
            removeHead(l1);
            result->absvalue=l1;
            return result;
        }
        else if(a==NULL)
        {
            return b;
        }
        else if(b==NULL)
        {
            return a;
        }
    }
    /* Easier to work with DlinkedList instead of bigInteger */
    l1=a->absvalue;
    l2=b->absvalue;
    /* Same sign ? */
    if(a->sign==b->sign)
    {
        /* While the two aren't null */
        while(l1!=NULL || l2!=NULL)
        {
            sum=0;
            /* Make sure to work with a valid value for l1 */
            if(l1==NULL)
            {
                ta=0;
            }
            else
            {
                ta=l1->value;
                l1=l1->next;
            }
            /* Make sure to work with a valid value for l2 */
            if(l2==NULL)
            {
                tb=0;
            }
            else
            {
                tb=l2->value;
                l2=l2->next;
            }
            /* Simple sum */
            sum=ta+tb+r;
            /* Rest or not ? */
            if(sum>9999)
            {
                r=1;
                sum-=10000;
            }
            else
            {
                r=0;
            }
            /* Insert the package in a DlinkedList, as an element */
            result->absvalue=insertTail(result->absvalue,sum);
        }
        /* Don't forget the final rest ! */
        if(r>0)
        {
            result->absvalue=insertTail(result->absvalue,1);
        }
        /* Put the right sign to the result */
        result->sign=a->sign;
    }
    else
    {
        /* Contrary signs, so that's a soustraction, let's call diffBigInt with some order and signs changes */
        if(a->sign==TRUE)
        {
            a->sign=FALSE;
            result=diffBigInt(b,a);
            a->sign=TRUE;
        }
        else
        {
            b->sign=FALSE;
            result=diffBigInt(a,b);
            b->sign=TRUE;
        }
    }

    return result;
}
Beispiel #4
0
bigInteger quotientBigInt(bigInteger a, bigInteger b)
{
    /* Declarations */
    Dlist l=malloc(sizeof(Element));
    Dlist m=malloc(sizeof(Element));
    Dlist tmp=malloc(sizeof(Element));
    bigInteger plop=malloc(sizeof(nb));
    bigInteger temp=malloc(sizeof(nb));
    bigInteger result=malloc(sizeof(nb));
    int i=0,j=0, depart=0,k=0,compare,X=-1,n;
    int bsign=FALSE;
    int asign=FALSE;
    char* string=NULL;
    char* qr=NULL;
    /* No null bigInteger ? */
    if(a!=NULL && b!= NULL)
    {
        l=a->absvalue;
        m=b->absvalue;
    }
    else
    {
        result=sumBigInt(result,0);
        return result;
    }
    /* No null bigInteger ? (bis) */
    if(m->value == 0 && m->next==NULL)
    {
        tmp=insertTail(tmp,0);
        removeHead(tmp);
        result->absvalue=tmp;
        result->sign=FALSE;
        return result;
    }
    /* Store the signs of a and b, and remove them to the bigInteger to avoid issues with the other fonctions used into quotientBigInt */
    asign=a->sign;
    a->sign=FALSE;
    bsign=b->sign;
    b->sign=FALSE;

    /* If b > a, result will be < 1, not a bigInteger in fact, so 0 */
    if(compareBigInt(a,b) == -1 || isNull(b))
    {
        a->sign=asign;
        if(!isNull(b))
        {
            b->sign=bsign;
        }
        tmp=insertTail(tmp,0);
        removeHead(tmp);
        result->absvalue=tmp;
        result->sign=FALSE;
        return result;
    }


    /* Basic division ? */
    if(l->value<=9999 && l->next==NULL)
    {
        if(asign==bsign)
        {
            result->sign=FALSE;
        }
        else
        {
            result->sign=TRUE;
        }
        a->sign=asign;
        b->sign=bsign;
        tmp=insertTail(tmp,l->value/m->value);
        tmp=removeHead(tmp);
        result->absvalue=tmp;
        return result;
    }

    /* Count the number of characters in a and b and store it into i and j */
    while(m->next!=NULL)
    {
        i+=4;
        m=m->next;
    }
    if(m->value>999)
    {
        i+=4;
    }
    else if(m->value>99)
    {
        i+=3;
    }
    else if(m->value>9)
    {
        i+=2;
    }
    else
    {
        i+=1;
    }
    /* Dynamic allocation for the first number divided by b, we don't need more than the number of characters +1 in b */
    string=malloc((i+1)*sizeof(char));


    while(l->next!=NULL)
    {
        j+=4;
        l=l->next;
    }
    if(l->value>999)
    {
        j+=4;
    }
    else if(l->value>99)
    {
        j+=3;
    }
    else if(l->value>9)
    {
        j+=2;
    }
    else
    {
        j+=1;
    }
    /* Dynamic allocation for the quotient, can't be larger than a */
    qr=malloc(j*sizeof(char));

    /* i is the number of characters of b
    * We take the i first characters of a
    * Then we make an bigInteger with that */
    l=a->absvalue;
    while(l->next!=NULL)
    {
        l=l->next;
    }
    /* First 4 figures's package */
    depart=j%4;
    if(depart==3)
    {
        k+=3;
        string[2]=l->value%10+'0';
        string[1]=(l->value%100-l->value%10)/10+'0';
        string[0]=(l->value%1000-l->value%100)/100+'0';
        l=l->prev;
    }
    else if(depart==2)
    {
        k+=2;
        string[1]=l->value%10+'0';
        string[0]=(l->value%100-l->value%10)/10+'0';
        l=l->prev;
    }
    else if(depart==1)
    {
        k+=1;
        string[0]=l->value%10+'0';
        l=l->prev;
    }
    depart=i;

    /* All the others 4 figures' packages, except the last one */
    while(depart-k>=0)
    {
        string[k+3]=l->value%10+'0';
        string[k+2]=(l->value%100-l->value%10)/10+'0';
        string[k+1]=(l->value%1000-l->value%100)/100+'0';
        string[k]=(l->value-l->value%1000)/1000+'0';
        k+=4;
        if(l->prev!=NULL)
        {
            l=l->prev;
        }
    }

    /* The last one */
    if(k-i==3)
    {
        string[k]=l->value%10+'0';
    }
    else if(k-i==2)
    {
        string[k+1]=l->value%10+'0';
        string[k]=(l->value%100-l->value%10)/10+'0';
    }
    else if(k-i==1)
    {
        string[k+2]=l->value%10+'0';
        string[k+1]=(l->value%100-l->value%10)/10+'0';
        string[k]=(l->value%1000-l->value%100)/100+'0';
    }

    /* Don't forget to close the string */
    string[i]='\0';
    plop=newBigInteger(string);
    /* Our bigInteger is ready */

    /* If our new biginteger is < b, we need to take one more figure from a */
    if(compareBigInt(plop,b)==-1)
    {
        tmp=NULL;
        tmp=insertTail(tmp,10);
        temp->absvalue=tmp;
        plop=mulBigInt(plop,temp);

        X=getNumberN(a,j-i);
        tmp=NULL;
        tmp=insertTail(tmp,X);
        temp->absvalue=tmp;
        plop=sumBigInt(plop,temp);
        i++;
    }

    i--;
    n=0;
    /* Compute the soustraction, like in primary school */
    while(j-i>0)
    {
        i++;
        k=0;
        compare=0;
        /* How many times can I put the quotient ? */
        while(compare>=0 && k<10)
        {
            k++;
            tmp=NULL;
            tmp=insertTail(tmp,k);
            temp->absvalue=tmp;
            temp=mulBigInt(b,temp);

            compare=compareBigInt(plop,temp);
        }
        k--;

        /* Store the figures of the quotient into an array of char */
        tmp=NULL;
        tmp=insertTail(tmp,k);
        temp->absvalue=tmp;
        temp=mulBigInt(b,temp);

        qr[n]=k+'0';
        n++;
        plop=diffBigInt(plop,temp);

        tmp=NULL;
        tmp=insertTail(tmp,10);
        temp->absvalue=tmp;
        plop=mulBigInt(plop,temp);

        /* Go down the next number */
        if(j-i>0)
        {
            X=getNumberN(a,j-i);
        }
        tmp=NULL;
        tmp=insertTail(tmp,X);
        temp->absvalue=tmp;
        plop=sumBigInt(plop,temp);
    }
    /* Close the string */
    qr[n]='\0';
    /* Build the result bigInteger */
    result=newBigInteger(qr);

    a->sign=asign;
    b->sign=bsign;

    /* Put the right sign */
    if(a->sign==b->sign)
    {
        result->sign=FALSE;
    }
    else
    {
        result->sign=TRUE;
    }

    return result;

}
Beispiel #5
0
bigInteger diffBigInt(bigInteger a, bigInteger b)
{
    /* Declarations */
    int ta,tb,r=0,diff;
    bigInteger result=NULL;
    Element* temp=malloc(sizeof(Element));
    Element* l1=malloc(sizeof(Element));
    Element* l2=malloc(sizeof(Element));
    result=malloc(sizeof(nb));
    /* No null bigInteger ? */
    if(a!=NULL && b!= NULL)
    {
        /* Easier to work with DlinkedList instead of bigInteger */
        l1=a->absvalue;
        l2=b->absvalue;
    }
    else
    {
        if(a==b)
        {
            l1=insertTail(l1,0);
            removeHead(l1);
            result->absvalue=l1;
            return result;
        }
        else if(a==NULL)
        {
            return b;
        }
        else
        {
            return a;
        }
    }
    /* Is it really a soustraction ? Let's check that */
    if(a->sign==TRUE&&b->sign==FALSE)
    {
        a->sign=FALSE;
        result=sumBigInt(a,b);
        result->sign=TRUE;
        a->sign=TRUE;
    }
    else if(a->sign==TRUE&&b->sign==TRUE)
    {
        a->sign=FALSE;
        b->sign=FALSE;
        result=diffBigInt(b,a);
        a->sign=TRUE;
        b->sign=TRUE;
    }
    else if(a->sign==FALSE&&b->sign==TRUE)
    {
        b->sign=FALSE;
        result=sumBigInt(a,b);
        b->sign=TRUE;
    }
    else
    {
        /* If b>a, do b-a and put a '-' before the result */
        if(compareBigInt(a,b)==-1)
        {
            temp=l1;
            l1=l2;
            l2=temp;
            result->sign=TRUE;
        } /* Equal ? so no need to compute anything */
        else if(compareBigInt(a,b)==0)
        {
            result->absvalue=insertTail(result->absvalue,0);
            result->sign=FALSE;
            return result;
        }
        /* While the two aren't null */
        while(l1!=NULL || l2!=NULL)
        {
            /* Make sure to work with a valid value for l1 */
            if(l1==NULL)
            {
                ta=0;
            }
            else
            {
                ta=l1->value-r;
                l1=l1->next;
            }
            /* Make sure to work with a valid value for l2 */
            if(l2==NULL)
            {
                tb=0;
            }
            else
            {
                tb=l2->value;
                l2=l2->next;
            }
            /* Do the soustraction 4 by 4 */
            if(ta>tb)
            {
                diff=ta-tb;
                r=0;
            }
            else if(tb>ta)
            {
                diff=(10000+ta)-tb;
                r=1;
            }
            else
            {
                diff=0;
                r=0;
            }
            /* Insert the 4 figures' package in a DlinkedList, as an element */
            result->absvalue=insertTail(result->absvalue,diff);
        }
        temp=result->absvalue;
        /* Remove useless 0 */
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        while(temp->value==0 && temp->prev!=NULL)
        {
            temp=temp->prev;
            removeTail(temp);
        }

    }
    return result;
}