Spec:
Create a program that reads from a file and converts strings to integers (using a struct). Then add and subtract them.
Question/Problem:
I'm working on the adding function. I noticed that for numbers that add up and exceed the struct integer array (ie. sum->digits is an array in a struct). For example 55 + 55 = 110 (3 digits in a 2 digit array). So, what I'm trying to do is use Realloc() operator/function to extend the array in a struct by one more digit to store the carry over from the addition. The program fails when it runs the Realloc().
Code:
/*
* Peter Nguyen
* COP 3502C - 0001
* Program 1 - BigIntegers
* 01/27/10
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct integer {
int* digits;
int size;
};
struct integer* convert_integer(char* stringInt);
void print(struct integer *p);
struct integer* add(struct integer *p, struct integer *q);
struct integer* subtract(struct integer *p, struct integer *q);
int compare(struct integer *p, struct integer *q);
int main (void)
{
int numOflines; // Number of lines in "bigint.txt" file
int i = 0,
j = 0;
int operator = 0;
char string[201]; // Store string
struct integer *num1,
*num2,
*answer;
// Open "bigint.txt" file
FILE * myFile;
myFile = fopen("bigint.txt", "r");
// Reads number of operations
fscanf(myFile, "%d", &numOflines);
// Run loop for the number of operations
for(i = 0; i < numOflines; i++)
{
// Read operator (1 = addition and 2 = subtraction)
fscanf(myFile, "%d", &operator);
// Read line and store the two strings
fscanf(myFile, "%s", &string);
num1 = convert_integer(string);
fscanf(myFile, "%s", &string);
num2 = convert_integer(string);
print(num1);
print(num2);
if(operator == 1)
{
answer = add(num1, num2);
print(answer);
}
// subtract function not written yet
/* else if (operator == 2)
answer = subtract(num1, num2);
else
// If operator is not + or -
printf("The wrong operator!"\n);
*/
free(num1->digits);
free(num1);
free(num2->digits);
free(num2);
free(answer->digits);
free(answer);
}
return 0;
system("pause");
}
/////////////////////////////////////////////////////////////////////
//Preconditions: the first parameter is string that stores //
// only contains digits, doesn't start with //
// 0, and is 200 or fewer characters long. //
//Postconditions: The function will read the digits of the //
// large integer character by character, //
// convert them into integers and return a //
// pointer to the appropriate struct integer. //
/////////////////////////////////////////////////////////////////////
struct integer* convert_integer(char* stringInt)
{
int i,
j = 0;
struct integer *p = (struct integer*)(malloc(sizeof(struct integer)));
// Allocate memory
p->size = strlen(stringInt);
p->digits = (int *)(malloc(sizeof(int)*p->size));
// Reverse the order
for(i = p->size - 1; i >= 0; i--, j++)
p->digits[i] = (int)(stringInt[j] - '0');
return p;
}
/////////////////////////////////////////////////////////////////////
//Preconditions: p is a pointer to a big integer. //
//Postconditions: The big integer pointed to by p is //
// printed out. //
/////////////////////////////////////////////////////////////////////
void print(struct integer *p)
{
int i;
for(i = p->size - 1; i >= 0; i--)
printf("%d", p->digits[i]);
printf("\n");
}
/////////////////////////////////////////////////////////////////////
//Preconditions: p and q are pointers to struct integers. //
//Postconditions: A new struct integer is created that //
// stores the sum of the integers pointed to //
// by p and q and a pointer to it is //
// returned. //
/////////////////////////////////////////////////////////////////////
struct integer* add(struct integer *p, struct integer *q)
{
struct integer *sum = (struct integer*)(malloc(sizeof(struct integer)));
int i,
num1,
num2,
carry = 0;
sum->digits = (int*)(malloc(sizeof(int)*sum->size));
if(p->size > q->size)
sum->size = p->size;
else
sum->size = q->size;
for(i = 0; i <= sum->size; i++)
{
if(p->digits[i] == '\0')
num1 = 0;
else
{
num1 = p->digits[i];
if(q->digits[i] == '\0')
num2 = 0;
else
num2 = p->digits[i];
if(i == sum->size && carry == 1)
{
sum->digits = (int*)realloc(sum->digits, (sum->size*sizeof(int)));
num1 = 0;
num2 = 0;
}
sum->digits[i] = num1 + num2 + carry;
if(sum->digits[i] >= 10)
{
sum->digits[i] -= 10;
carry = 1;
}
else
carry = 0;
}
return sum;
}
/////////////////////////////////////////////////////////////////////
//Preconditions: p and q are pointers to struct integers. //
//Postconditions: A new struct integer is created that //
// stores the absolute value of the //
// difference between the two and a pointer //
// to this is returned. //
/////////////////////////////////////////////////////////////////////
struct integer* subtract(struct integer *p, struct integer *q)
{
return 0;
}
/////////////////////////////////////////////////////////////////////
//Preconditions: Both parameters of the function are //
// pointers to struct integer. //
//Postconditions: The function compares the digits of two //
// numbers and returns: //
// -1 if the first number is smaller than the second, //
// 0 if the first number is equal to the second number, //
// 1 if the first number is greater than the second. //
/////////////////////////////////////////////////////////////////////
int compare(struct integer *p, struct integer *q)
{
return 0;
}