Friday, October 12, 2012

Addition an infinite number of digits using dynamic allocation




Using dynamic allocation, without limitation, the following program receives an input of an integer
Addition to the program. Pre-allocate the full size, 2 times the size of the re-
Allocate an array, copy and then continue to receive input.

Receive one character from the keyboard input.
Will be able to easily to modify the file to receive input.

# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
using namespace std;
int inputStr (char ** str, int * n) {
   int i, x;
   char * s, * t;
   / / Calloc function as an argument, number, size, and gives
   / / And makes all initialized to 0.
   / / Size changed to give a pointer to the received
   / / Here we have to change the value of * n is the integer value of the main changes.
   s = (char *) calloc (* n, 1);
   / / Index of wall space characters
   i = 0;
   / / One character getchar () function is used to receive.
   while (x = getchar ()) {
        / / Read characters, numbers, unless the input ends.
        if (! isdigit (x)) break;
        / / S reading the characters in the array binds.
        s [i] = x;
        / / Index helps increase
        i = i +1;
        / / If the allocation of the number of characters read is equal to the size of
        / / Doubles its size gives
        if (i> = * n) {
             / / Gives size doubles.
             t = (char *) calloc (2 * (* n), 1);
             / / Copy the characters read so far, which
             for (i = 0; i <* n; + + i) t [i] = s [i];
             / / The value of n *. Hardsect_size (The integer value of the main changes)
             * N = 2 * (* n);
             / / First allocation s off.
             free (s);
             / Value / s of the newly assigned address. Hardsect_size
             s = t;
        }
   }
   / / 0 at the end of the string to make it binds.
   s [i] = 0;
   / / Can put the address of the string value. The value of the pointer of the main changes.
   * Str = s;
   / / Returns the length of the string.
   return i;
}
int main () {
   char * stra; / / pointer for the first string
   char * strb; / / pointer for the second string
   pointers to an array of integers int * stRes; / / results obtained
   int sizeA = 10, sizeB size 10 / / you want to start at the beginning of 10 starts.
   int nA = 0, nB = 0, nc = 0 / / string length of the wall variables
   int i, j, k, x, y, z, carry; / / integer variable

   / / Reads the first number.
   printf ("enter first number:");
   NA / / the length of the first number on the assignment.
   nA = inputStr (& stra, & sizeA);
   / / Read second number.
   printf ("enter second number:");
   NB / / the second number is the length of the assignment.
   nB = inputStr (& strb, & sizeB);
   / / Herd in addition to the large number of nC gives
   if (nA> = nB) nC = nA +1; else nC = nB +1;
   Allocate memory / / for integer nC by.
   stRes = (int *) calloc (nC, sizeof (int));
   / / First number 1 spot for the index
   i = nA-1;
   / / Index for the digits of the second number is 1
   j = nB-1;
   / / The index of the array to hold the results
   k = 0;
   / / Rounding
   carry = 0;
   / / First number and the second number, if not all add
   while (i> = 0 && j> = 0) {
      / / Character to an integer that will convert.
      x = stra [i] - '0 ';
      / / Character to an integer that will convert.
      y = strb [j] - '0 ';
      Herd under the comment / / and adds
      z = x + y + carry;
      / / 10 digit rises.
      carry = z/10;
      / / Number of digits to the right of the results go into
      z = z% 10;
      stRes [k] = z;
      / / First number and the second number is the index of the reduction. (Comes forward)
      i = i-1;
      j = j-1;
      / / The results of the index increases.
      k = k +1;
   }
   / / If the first number of the remaining
   while (i> = 0) {
      / / Move each digit results in
      / / At this point carry because the value can change.
      x = stra [i] - '0 ';
      z = x + carry;
      carry = z/10;
      z = z% 10;
      stRes [k] = z;
      i = i-1;
      k = k +1;
   }
   / / If the second number is left
   while (j> = 0) {
      / / Move each digit results in
      / / At this point carry because the value can change.
      y = strb [j];
      z = y + carry;
      carry = z/10;
      z = z% 10;
      stRes [k] = z;
      j = j-1;
      k = k +1;
   }
   / / After the number plus the number to be rounded up may
   if (carry) {
      stRes [k] = carry;
      k = k +1;
   }
   / / Output to obtain an index that contains a large number of the most
   k = k-1;
   / / Output each digit which
   while (k> = 0) {
        z = stRes [k];
        cout << z;
        k = k-1;
   }
   return 0;
}


No comments:

Post a Comment