Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 200
struct integer* read_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);
struct integer{
int *digits;
int size;
};
int main(void){
int numlines; //Number of lines stored in the file
char string1[MAXLEN];
char string2[MAXLEN];
int i;// loop index
int opr; //Operator 1 or 2 tells us if we add or subtract
struct integer *p;
struct integer *q;
struct integer *answer;
// Open the file and read from bigint.txt
FILE *fin;
fin = fopen("bigint.txt","r");
fscanf(fin, "%d", &numlines); //Read in how many lines there are in the file
// read a line (\n)
// read the operator(1 or 2) - forget about this one. 3 fscanf
//scanf first sting; call read int (1st) function
//same with the second string
//if 1 then call add, if 2 then call subtract
// end of loop
for(i = 0; i < numlines; i++)
{
fscanf(fin, "%d", &opr); // find out whether its add or subtract
fscanf(fin, "%s", string1); // scan in 2nd number on the first line of input file
p = read_integer(string1);
fscanf(fin, "%s", string2);// scan in 3rd number on the first line of input file
q = read_integer(string2);
print(p);//call function to print out information
printf(" ");
print(q);
printf("\n");
if(opr == 1)
{
//add
answer = add(p, q);
print(answer = add(p, q));
}
else if(opr == 2)
{
printf("subtract here\n");
// subtract func
}
}
fclose(fin);
system("PAUSE");
return 0;
}
//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* read_integer(char* stringInt)
{
int len;
struct integer *myString;//create a new struct
int i;//loop index
//allocate memory for the struct
myString = (struct integer*)(malloc(sizeof(struct integer)));
len = strlen(stringInt); // length of the strinInt in digits
myString->size = len; // length of string assigned to size in struct myString
//allocate memory for digits
myString->digits = (int*)(malloc(sizeof(int)* len));
// assign number to digits, convert to integers
for(i = 0; i < len; i++)
{
myString->digits[i] = stringInt [len-1-i] - '0';
}
return myString;
}
//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)
{
//given pointer, want to print out digits in reverse order...
int i;
// no reversing in add or subtract, but want to reverse here
for(i = p->size-1; i >= 0; i--)
{
printf("%d", p->digits[i]);
}
}
//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)
{
int i;
struct integer *addAnsw; //create new struct for answer
//Allocate memory for the new struct
addAnsw = (struct integer*)(malloc(sizeof(struct integer)));
//Allocate memory for the digits in the new struct addAnsw
addAnsw->digits = (int*)(malloc(sizeof(int)* (p->size)));
for(i = 1;i <2 i++)
{
printf("%d + %d = %d\n", p->digits[i], q->digits[i], addAnsw->digits[i]);
addAnsw->digits[i] = p->digits[i] + q->digits[i];
}
return addAnsw;
}
//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)
{
struct integer *subtractAnsw;
//Allocate memory for the new struct
subtractAnsw = (struct integer*)(malloc(sizeof(struct integer)));
if(p->size < q->size)
{
subtractAnsw->digits = (int*)(malloc(sizeof(int)*(p->size)));
}
}
//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.