Can someone help me complete my code? I am having trouble with it and I need some input.

//#include "bigint.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

/*

Create and implement
a bigint data structure that can represent integers whose size is limited
only by the memory capacity of your computer, the range of size_t and
sizeof(void*). There may be no other limits to the size of a bigint.
Your code must implement the following operations for a bigint. For
simplicity, we can assume that each bigint is nonnegative and we only
need to implement addition.

*/

struct bigint
{
    int* data;
    int initialzero;
    int length;
};

typedef struct bigint bigint;

/*

bigint* bi_create(const char*);

Dynamically allocates a new bigint and returns a pointer to it. The
value of the bigint is read from the string given as a parameter, and
which can ce assumed to be a proper integer (which may be 0 and contain
initial zeros). For example, if intialized with the string "12345", the
bigint created would have the value 12345.

*/

bigint* bi_create(const char* charint)
{

    int length = 0;
    const char* temp;
    int flag = 0;



    temp = charint;



    while(*charint != '\0')
    {

        length++;
        charint++;



    }


    charint = temp;


   //  printf("LENGTH = %d \n", length);

    bigint* bi = malloc(sizeof(bigint));

     bi->data = malloc(length * sizeof(int));

    bi->length = length; 

    int counter = 0;
    int* datatemp;

    bi->initialzero = 0;

    datatemp = bi->data;

    while(*charint != '\0')
    {
        if(*charint == '0'  && flag != 1)
        {
            (bi->initialzero)++;

        }else
        {
            flag = 1;
        }

         *bi->data = *charint - '0';

        printf(" %d ", *bi->data);
        charint++;
        bi->data++;
        counter++;

    }
     printf("\n");



    bi->data = datatemp;


   int counter1 = 0;
    while(counter1 < bi->length)
   {
    printf("DATA = %d \n", *bi->data);
       bi->data++;
       counter1++;
   }
   // printf("INITIAL ZERO = %d \n", bi->initialzero);



    return bi;


}



void bi_tostring(const bigint*, char*);

Writes the bigint into a string as text. You can assume that when
this function is called, the second parameter points to somewhere
that has enough space for the representation of the bigint.


void bi_tostring(const bigint* bigI, char* carint)
{
     int *temp;
    int templength = bigI->length;

    temp = bigI->data;

    while(templength>0)
    {
    *carint = *temp + '0';  

        printf(" TOSTRING %c", *carint);

    temp++;
    carint++;


        templength--;
    }

    carint++;
    *carint = '\0';
}

/*

bigint* bi_add(const bigint*, const bigint*);

Adds two bigints and dynamically allocates a new bigint for the
result, returning a pointer to the result. Doesn't modify the original
bigint values.

*/

bigint* bi_add(const bigint* BigI1, const bigint* BigI2)
{

    int *temp1;
    int *temp2;
    int addition = 0;


    int remainder = 0;

    int counter1;
    int counter2;

    char *sum;

    char *tempsum;



    if(BigI1->length > BigI2->length)
    {
        sum = malloc((BigI1->length + 2)*sizeof(char));
        counter1 = BigI1->length;
        counter2 = BigI2->length;

        temp1 = BigI1->data;
        temp2 = BigI2->data;

        temp1 = (temp1 + counter1-1);
         temp2 = (temp2 + counter2-1);

    }
    else if(BigI1->length < BigI2->length)
    {
        sum = malloc((BigI2->length + 2)*sizeof(char));
        counter1 = BigI2->length;
        counter2 = BigI1->length;

        temp1 = BigI2->data;
        temp2 = BigI1->data;

        temp1 = (temp1 + counter1-1);
        temp2 = (temp2 + counter2-1);

    }else
    {
        sum = malloc((BigI2->length + 2)*sizeof(char));
        counter1 = BigI2->length;
        counter2 = BigI1->length;

        temp1 = BigI2->data;
        temp2 = BigI1->data;

        temp1 = (temp1 + counter1-1);
        temp2 = (temp2 + counter2-1);
    }

    tempsum = sum;


     printf("TEMPSUM: %d \n", tempsum);

    sum = (sum + counter1+1);

    *sum = '\0';

    sum--;

    addition = *temp2;

    while(counter1 > 0)
    {

     printf("Counter: %d \n", counter1);


    if((*temp1 + addition + remainder) > 9)
    {
        *sum = ((*temp1 + addition + remainder) - 10) + '0';

        printf("TEMP1: %d \n", *temp1);
        printf("TEMP2: %d \n", addition);
        printf("SUM: %c \n", *sum);


        remainder = 1;



    }else
    {
        *sum = ((*temp1 + addition + remainder)) + '0';
        remainder = 0;
         printf("TEMP1: %d \n", *temp1);
        printf("TEMP2: %d \n", addition);
       printf("SUM: %c \n", *sum);

    }
        temp1--;

        if(counter2 >1)
        {
            printf("Counter: %d \n", counter2);
            temp2--;
            addition = *temp2;    

        }else
        {
            addition = 0;
        }

        counter1--;
        counter2--;
        sum--;


       }


    sum = tempsum;

    if(remainder == 1)
    {
        *sum = 1 + '0';

    }else
    {
        *sum =0 + '0';
    }


    printf("ENDING \n");

    return bi_create(sum);

}

/*

int bi_compare(const bigint*, const bigint*);

Order-compares the two bigints. If first bigint is less than the
second, returns -1. If they are equal, returns 0. If the first bigint
is greater than the second, returns +1.

*/

int bi_compare(const bigint* BigI1, const bigint* BigI2)
{
    int *temp1;
    int *temp2;




}

/*

void bi_release(bigint*);

Releases all memory that has been internally allocated for the given
bigint. After this call, the users are assumed not to use that bigint
object any more ever again.

*/

void bi_release(bigint* BigI)
{
    free(BigI->data);
    free(BigI);

}

Recommended Answers

you forgot to post the troubles your having with completing the code
like what part doesn't work or what are you supppose to do next

Jump to Post

All 2 Replies

you forgot to post the troubles your having with completing the code
like what part doesn't work or what are you supppose to do next

I wanted someone to look over this code to see if it makes sense. I am suppoosed to make a BigInt and each function has a description over it. Currently, the memory allocated is not being released and I am having trouble starting my compare function. How would I do this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.