Big Integer Program

Posted on by on February 12th, 2011 | 0 Comments »

Downloads:

Assignment :

Source Code :

Compiled Executables :

Documentation :

Sample Test Files :

Assignment:

GDE Error: Unable to load profile settings

Source Code:

////////////////////////////////////////////////////////////////////////////////
///
///  \file bigint.c
///
///  <br>Author(s): Nicholas Guthrie
///  <br>Created: 19 January 2011
///  <br>Email: nickguthrie@knights.ucf.edu
///  <br>Web: http://nickguthrie.com
///  <br>Title: Big Integer
///  <br>Course: COP3502C  Computer Science I
///  <br> Class #: 16173
///  <br>Section #: 0001
///
///  THIS SOFTWARE IS PROVIDED BY THE NICHOLAS GUTHRIE''AS IS'' AND ANY EXPRESS
///  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
///  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
///  NO EVENT SHALL UCF BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
///  EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
///  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
///  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
///  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
///  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
///  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///  
////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h> //necessary for malloc
#include<string.h>

struct integer 
{
  int *digits;
  int size;
};

struct integer* convert_integer(char* stringInt);
void print(FILE *ntw, 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(){
  FILE *ntw;
  FILE *output;
  int i, j, operation, diff, repeat;
  struct integer * data[3];
  output = fopen("out.txt", "w");

  int vs, big, small, reverse;
  char op;
  
  char itemp[201]; //number collection
  
  //read in file
  ntw = fopen ("bigint.txt", "r");
  if(ntw == NULL)
    {
      printf("File not Found, Exiting Program Now\n\n");
      return 0;
    }

  fscanf(ntw, "%d", &repeat); //determine number of equations to solve
  for(i = 0; i<repeat; i++)
    {
      fscanf(ntw, "%d", &operation); //collect operation (+/-)
      op = '+';
      if(operation == 2)
	op = '-';
      

      for(j = 0; j<2; j++)
	{
	  fscanf(ntw, "%s", itemp); //collect big numer as string
	  data[j] = convert_integer(itemp);
	}

      //find which file is bigger, assume first is bigger
      big = 0;
      small = 1;
      reverse = 0;
      //check if first is smaller
      if(compare(data[0],data[1]) < 0)
	{
	  big = 1;
	  small = 0;
	  reverse = 1;
	}

      //addition
      if(operation == 1)
	{
	  data[2] = add(data[big],data[small]);
	  //prepare for printing
	  if(reverse == 1)
	    {
	      big = 0;
	      small = 1;
	    }
	}
      
      //subtraction
      else if(operation == 2)
	data[2] = subtract(data[big],data[small]);
      
      print(output, data[big]);
      fprintf(output, " %c ", op);
      print(output, data[small]);
      fprintf(output, " = ");
      print(output, data[2]);
      fprintf(output, "\n");
      
    }
  fclose(ntw);
  fclose(output);
  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* convert_integer(char* stringInt)
{
  //pointer to struct integer construction & memory allocation
  struct integer* data;
  data = (struct integer*)malloc(sizeof(struct integer));
  data->digits = (int*)malloc(strlen(stringInt) * sizeof(int));

  //conversion variables
  int length =   data->size = strlen(stringInt);
  char ctemp = 'x';
  char *cpoint = &ctemp;
  
  //navigation variables
  int i;
  

  //read the digits of the large integer, character by character
  for(i = 0; i<length; i++)
    {
      ctemp = stringInt[i];
      data->digits[length - i-1] = atoi(cpoint);
    }
  return data;
}


//Preconditions:  p is a pointer to a big integer.
//Postconditions: The big integer pointed to by p is printed out.
void print(FILE *ntw, struct integer *p)
{
  int i;
  for(i = p->size-1; i>=0; i--)
    fprintf(ntw, "%d", p->digits[i]);
}


//Preconditions:  p and q are pointers to struct integers.
//Postconditions: A new struct integer is created that stores the sum
//                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)
{
  //pointer to struct integer construction & memory allocation
  struct integer* data;
  data = (struct integer*)malloc(sizeof(struct integer));
  data->digits = (int*)malloc((p->size+1) * sizeof(int));
  data->size = p->size;
  
  int i;
  int result, remainder;
  remainder = 0;
  
  //do for all
  for(i = 0; i < p->size; i++)
    {
      if(i < q->size)
	{
	  result = p->digits[i] + q->digits[i] + remainder;
	  data->digits[i] = result%10;
	}
      //case where no more digits left in smaller q
      else
	{
	  result = p->digits[i]+remainder;
	  data->digits[i] = result%10;
	}

      //handle remainders
      //if the result is > 9 carry the one
      if(result > 9)
	remainder = 1;
      else
	remainder = 0;
    }
  //case where there is a remainder and a digit remaining
  if(remainder == 1)
    {
      data->digits[p->size] = 1;
      data->size++;
    }    
  return data;
}



//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)
{
  //pointer to struct integer construction & memory allocation
  struct integer* data;
  data = (struct integer*)malloc(sizeof(struct integer));
  data->digits = (int*)malloc(p->size * sizeof(int));
  data->size = p->size;
  
  //navigation variables
  int i,j;
  int remainder = 0;
  int big, small;
  
  for(i = 0; i < p->size; i++)
    {
      big = p->digits[i];


      //handle remainder & big
      //big > 1, use 0
      if(remainder == 1)
	{
	  if(big - remainder >=0)
	    {
	      big--;
	      remainder = 0;
	    }
	  //big = 0, borrow from next digit
	  else if(big==0)
	    big=9;
	}
	  

      //if the current position is greater than the size of the smaller number
      if(i < q->size)
	{
	  small = q->digits[i];
	  //calculate values
	  if(big-small>=0)
	    data->digits[i] = big-small;
	  else
	    {
	      data->digits[i] = big-small+10;
	      remainder = 1;
	    }
	}
      //no more q digits to subtract
      else
	{
	  if(big>=0)
	    data->digits[i] = big;
	  else
	    {
	      data->digits[i] = big+10;
	      remainder = 1;
	    }
	}
    }

  int pos = data->size-1;

  //remove leading 0s (in back of this array
  while(data->digits[pos] == 0 && pos !=0)
    {
      data->size--;
      pos--;
    }
  return data;
}


//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)
{
  int i;
  if(p->size > q->size) //first number is bigger
    return 1;
  else if(p->size < q->size) //first number is smaller
    return -1;
  
  for(i = p->size-1; i>=0; i--)
    if(p->digits[i] > q->digits[i])
      return 1;
    else if(p->digits[i] < q->digits[i])
      return -1;
    else if(i == 0)
      return 0;
}
//////////////////////////////////////////////////////////////////////////////// /// ///  \file bigint.c /// ///  <br>Author(s): Nicholas Guthrie ///  <br>Created: 19 January 2011 ///  <br>Email: nickguthrie@knights.ucf.edu ///  <br>Web: http://nickguthrie.com ///  <br>Title: Big Integer ///  <br>Course: COP3502C  Computer Science I ///  <br> Class #: 16173 ///  <br>Section #: 0001 /// ///  THIS SOFTWARE IS PROVIDED BY THE NICHOLAS GUTHRIE''AS IS'' AND ANY EXPRESS ///  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ///  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN ///  NO EVENT SHALL UCF BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ///  EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ///  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ///  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ///  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR ///  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ///  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /// //////////////////////////////////////////////////////////////////////////////// #include<stdio.h> #include<stdlib.h> //necessary for malloc #include<string.h> struct integer { int *digits; int size; }; struct integer* convert_integer(char* stringInt); void print(FILE *ntw, 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(){ FILE *ntw; FILE *output; int i, j, operation, diff, repeat; struct integer * data[3]; output = fopen("out.txt", "w"); int vs, big, small, reverse; char op; char itemp[201]; //number collection //read in file ntw = fopen ("bigint.txt", "r"); if(ntw == NULL) { printf("File not Found, Exiting Program Now\n\n"); return 0; } fscanf(ntw, "%d", &repeat); //determine number of equations to solve for(i = 0; i<repeat; i++) { fscanf(ntw, "%d", &operation); //collect operation (+/-) op = '+'; if(operation == 2) op = '-'; for(j = 0; j<2; j++) { fscanf(ntw, "%s", itemp); //collect big numer as string data[j] = convert_integer(itemp); } //find which file is bigger, assume first is bigger big = 0; small = 1; reverse = 0; //check if first is smaller if(compare(data[0],data[1]) < 0) { big = 1; small = 0; reverse = 1; } //addition if(operation == 1) { data[2] = add(data[big],data[small]); //prepare for printing if(reverse == 1) { big = 0; small = 1; } } //subtraction else if(operation == 2) data[2] = subtract(data[big],data[small]); print(output, data[big]); fprintf(output, " %c ", op); print(output, data[small]); fprintf(output, " = "); print(output, data[2]); fprintf(output, "\n"); } fclose(ntw); fclose(output); 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* convert_integer(char* stringInt) { //pointer to struct integer construction & memory allocation struct integer* data; data = (struct integer*)malloc(sizeof(struct integer)); data->digits = (int*)malloc(strlen(stringInt) * sizeof(int)); //conversion variables int length =   data->size = strlen(stringInt); char ctemp = 'x'; char *cpoint = &ctemp; //navigation variables int i; //read the digits of the large integer, character by character for(i = 0; i<length; i++) { ctemp = stringInt[i]; data->digits[length - i-1] = atoi(cpoint); } return data; } //Preconditions:  p is a pointer to a big integer. //Postconditions: The big integer pointed to by p is printed out. void print(FILE *ntw, struct integer *p) { int i; for(i = p->size-1; i>=0; i--) fprintf(ntw, "%d", p->digits[i]); } //Preconditions:  p and q are pointers to struct integers. //Postconditions: A new struct integer is created that stores the sum //                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) { //pointer to struct integer construction & memory allocation struct integer* data; data = (struct integer*)malloc(sizeof(struct integer)); data->digits = (int*)malloc((p->size+1) * sizeof(int)); data->size = p->size; int i; int result, remainder; remainder = 0; //do for all for(i = 0; i < p->size; i++) { if(i < q->size) { result = p->digits[i] + q->digits[i] + remainder; data->digits[i] = result%10; } //case where no more digits left in smaller q else { result = p->digits[i]+remainder; data->digits[i] = result%10; } //handle remainders //if the result is > 9 carry the one if(result > 9) remainder = 1; else remainder = 0; } //case where there is a remainder and a digit remaining if(remainder == 1) { data->digits[p->size] = 1; data->size++; } return data; } //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) { //pointer to struct integer construction & memory allocation struct integer* data; data = (struct integer*)malloc(sizeof(struct integer)); data->digits = (int*)malloc(p->size * sizeof(int)); data->size = p->size; //navigation variables int i,j; int remainder = 0; int big, small; for(i = 0; i < p->size; i++) { big = p->digits[i]; //handle remainder & big //big > 1, use 0 if(remainder == 1) { if(big - remainder >=0) { big--; remainder = 0; } //big = 0, borrow from next digit else if(big==0) big=9; } //if the current position is greater than the size of the smaller number if(i < q->size) { small = q->digits[i]; //calculate values if(big-small>=0) data->digits[i] = big-small; else { data->digits[i] = big-small+10; remainder = 1; } } //no more q digits to subtract else { if(big>=0) data->digits[i] = big; else { data->digits[i] = big+10; remainder = 1; } } } int pos = data->size-1; //remove leading 0s (in back of this array while(data->digits[pos] == 0 && pos !=0) { data->size--; pos--; } return data; } //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) { int i; if(p->size > q->size) //first number is bigger return 1; else if(p->size < q->size) //first number is smaller return -1; for(i = p->size-1; i>=0; i--) if(p->digits[i] > q->digits[i]) return 1; else if(p->digits[i] < q->digits[i]) return -1; else if(i == 0) return 0; }
Watch the NASA Mars Rover Built Live
My .emacs

Categorized Under

ProgrammingProgramsSchoolwork

About Nick Guthrie

» has written 38 posts

Leave a Reply

You must be logged in to post a comment.

History