Code:
#include <stdio.h>
#include <string.h>
struct integer
{
int digit;
struct integer *next;
};
FILE* fin;
FILE* fout;
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);
int main()
{
char string1[200], string2[200];
char temp1, temp2;
int i, j, k;
int length1, length2;
int num_operations;
int operation;
struct integer* list1 = NULL;
struct integer* list2 = NULL;
// struct integer* p;
// struct integer* q;
//Open the input file
fin = fopen("bigint.txt", "r");
if (fin == NULL)
printf("File failed to open for reading.\n");
//Open the output file
fout = fopen("out.txt", "w");
if (fout == NULL)
printf("File failed to open for writing.\n");
//The first line will contain a single positive integer representing the
//number of operations to carry out
fscanf(fin, "%d ", &num_operations);
//Run the following loop for each line of numbers in the input file,
//indicated by the num_operations variable
for (i = 0; i < num_operations; i++)
{
//First number on the line tells which operation to perform
fscanf(fin, "%d ", &operation);
//Read in the number for the first string in the list
fscanf(fin, "%s ", &string1);
printf("This is string1: %s\n", string1); //Debug
//Read in the number for the second string in the list
fscanf(fin, "%s", &string2);
printf("This is string2: %s\n", string2); //Debug
length1 = strlen(string1) - 1; //Length of the first string
length2 = strlen(string2) - 1; //Length of the second string
//First big int in reverse
for(j = 0; j < strlen(string1)/2; j++)
{
temp1 = string1[j];
string1[j] = string1[length1];
string1[length1--] = temp1;
}
printf("\nThis is string1 in reverse: %s\n", string1); //Debug
//Second big in in reverse
for(k = 0; k < strlen(string2)/2; k++)
{
temp2 = string2[k];
string2[k] = string2[length2];
string2[length2--] = temp2;
}
printf("This is string2 in reverse: %s\n\n", string2); //Debug
//Send the reversed strings to the read function. This will put them
//in a linked list
list1 = read_integer(string1);
list2 = read_integer(string2);
if(operation == 1) //add
{
printf("\nWe are adding these\n"); //Debug
add(list1, list2);
}
else if (operation == 2) //subtract
{
printf("\nWe are subtracting these\n"); //Debug
subtract(list1, list2);
}
}
fclose(fin);
fclose(fout);
system("PAUSE");
return 0;
}
// Preconditions: the first parameter is a string that 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 place them in
// nodes of a linked list. The pointer to the head of the list is returned.
struct integer* read_integer(char* stringInt)
{
int i = 0;
int length;
length = strlen(stringInt);
struct integer *bigint;
struct integer* localString = (struct integer *) malloc(length*sizeof(struct integer));
struct integer* help_ptr = NULL;
for(i = 0; i < length; i++)
{
bigint[i].digit = stringInt[i] - '0';
bigint->next = help_ptr;
help_ptr = (struct integer*)&bigint[i].digit;
printf("%d", bigint[i].digit); //Debug
}
return bigint;
}
//Preconditions: p is a pointer to a big integer, stored in reverse order,
//least to most significant digit, with no leading zeros.
//Postconditions: The big integer pointed to by p is printed out.
void print(struct integer *p)
{
}
//Preconditions: p and q are pointers to big integers, stored in reverse order,
//least to most significant digit, with no leading zeros.
//Postconditions: A new big 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)
{
}
//Preconditions: p and q are pointers to big integers, stored in reverse order,
// least to most significant digit, with no leading zeros.
//Postconditions: A new big 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)
{
}
//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)
{
}