Cannot get the function to add my numbers..
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[200];
char string2[200];
int i;// loop index
int opr; //Operator 1 or 2 tells us if we add or subtract
struct integer *p;
struct integer *q;
// 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);
printf(string1);//call function to print out information
printf("\n");
printf(string2);
printf("\n");
if(opr == 1)
{
// add
}
else if(opr == 2)
{
// 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\n", 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)));
if(p->size > q->size)
{
addAnsw = (int*)(malloc(sizeof(int)*(p->size)));
}
for(i ; i; i++)
{
addAnsw = p->digits[i] + q->digits[i];
}
return addAnsw;
}