Jump to content

Recommended Posts

Posted

Dupa cum am spus si in titlu, am nevoie de include-ul "Numlib" deoarece nu se mai gaseste pe net. Cine il posteaza primul, primeste +1.

Pentru orice calcul foloseste:

 

 

#define OVERFLOW        (-1)        // Calculated number is too big for the result array

#define SUCCESS            (1)            // Addition successful

#define MAX_PART_NUM    (999999999)

#define PART_VALUE        (1000000000)

 

// Basically this just loops through all number blocks in the

// arrays, adds them, eventually adds the carry of the last step,

// and stores the result in another array at the right position

stock addBigNumber(n1[], n2[], result[], l1 = sizeof(n1), l2 = sizeof(n2), lr = sizeof(result)) {

    // Check validity of partial numbers (optional)

    for (new i = 0; i < l1; i++) {

        if (n1 > MAX_PART_NUM) return 0;

    }

    for (new i = 0; i < l2; i++) {

        if (n2 > MAX_PART_NUM) return 0;

    }

    

    if (l1 > l2) {

        // l1 got more partial numbers than l2

        new carry = 0;

        for (new i = 0; i < l1; i++) {

            if (i + 1 > lr) return OVERFLOW;

            if (i >= l2) {

                // if n2 got no more numbers, just add the carry

                result = n1 + carry;

            } else {

             // else add both number blocks and the carry of the last step

                result = n1 + n2 + carry;

            }

            

            carry = 0;

            if (result > MAX_PART_NUM) {

                // Calculate carry, it will always be 1 for sums of course

// but this calculation is more general for other operations

                carry = (result - PART_VALUE) / PART_VALUE + 1;

                // limit the result to 9 digits

                result = result - PART_VALUE;

            }

 

            if (carry > 0) {

                if (l1 + 1 > lr) return OVERFLOW;

                result[l1] = carry;

            }

        }

    } else if (l2 > l1) {

        // l2 got more partial numbers than l1

        new carry = 0;

        for (new i = 0; i < l2; i++) {

            if (i + 1 > lr) return OVERFLOW;

            if (i >= l1) {

                result = n2 + carry;

            } else {

                result = n1 + n2 + carry;

            }

            

            carry = 0;

            if (result > MAX_PART_NUM) {

                // Calculate carry

                carry = (result - PART_VALUE) / PART_VALUE + 1;

                result = result - PART_VALUE;

            }

 

            if (carry > 0) {

                if (l1 + 1 > lr) return OVERFLOW;

                result[l1] = carry;

            }            

        }

    } else {

        // l1 and l2 got the same amount of partial numbers

        

        // Start with the last partial number for easier carry calculation

        new carry = 0;

        for (new i = 0; i < l1; i++) {

            if (i + 1 > lr) return OVERFLOW;

            result = n1 + n2 + carry;

            carry = 0;

            if (result > MAX_PART_NUM) {

                // Calculate carry

                carry = (result - PART_VALUE) / PART_VALUE + 1;

                result = result - PART_VALUE;

            }            

        }

        if (carry > 0) {

            if (l1 + 1 > lr) return OVERFLOW;

            result[l1] = carry;

        }

    }

    return SUCCESS;

}

 

// Creates a human-readable string of an array-number

stock formatBigNumber(n1[], res[], l1 = sizeof(n1), lr = sizeof(res)) {

// First partial number without trailing 0

    new firstnotnull;

    // Find first number != null

    for (new i = l1 - 1; i >= 0; i--) {

        if (n1 != 0) {

            firstnotnull = i;

            break;

        }

    }

    format(res, lr, "%d", n1[firstnotnull]);

    for (new i = firstnotnull - 1; i >= 0; i--) {

        format(res, lr, "%s%09d", res, n1);

    }

}

 

 

De exemplu: 

 

 

// These big numbers are aligned in 9-digit blocks right-to-left

new n1[] = {356363461, 346452359, 993673499, 921945436, 934776456, 231543675, 135798642, 111111111};

new n2[] = {946783486, 124869357, 934569257, 123456789, 987654321, 165776555, 999999999, 999999999};

new result[10];        

new w1[96], w2[96], res[96];        

 

addBigNumber(n1, n2, result);

        

formatBigNumber(n1, w1);

formatBigNumber(n2, w2);

formatBigNumber(result, res);

printf("%s + %s = %s", w1, w2, res);

 

/* Output:

111111111135798642231543675934776456921945436993673499346452359356363461

+ 999999999999999999165776555987654321123456789934569257124869357946783486

= 1111111111135798641397320231922430778045402226928242756471321717303146947

*/

Iar acolo unde este printf("%s + %s = %s", w1, w2, res); poti folosi : * / - ^ < > ...

 

 

  • Upvote 1
Posted (edited)

Pentru orice calcul foloseste:

 

 

#define OVERFLOW        (-1)        // Calculated number is too big for the result array

#define SUCCESS            (1)            // Addition successful

#define MAX_PART_NUM    (999999999)

#define PART_VALUE        (1000000000)

 

// Basically this just loops through all number blocks in the

// arrays, adds them, eventually adds the carry of the last step,

// and stores the result in another array at the right position

stock addBigNumber(n1[], n2[], result[], l1 = sizeof(n1), l2 = sizeof(n2), lr = sizeof(result)) {

    // Check validity of partial numbers (optional)

    for (new i = 0; i < l1; i++) {

        if (n1 > MAX_PART_NUM) return 0;

    }

    for (new i = 0; i < l2; i++) {

        if (n2 > MAX_PART_NUM) return 0;

    }

    

    if (l1 > l2) {

        // l1 got more partial numbers than l2

        new carry = 0;

        for (new i = 0; i < l1; i++) {

            if (i + 1 > lr) return OVERFLOW;

            if (i >= l2) {

                // if n2 got no more numbers, just add the carry

                result = n1 + carry;

            } else {

             // else add both number blocks and the carry of the last step

                result = n1 + n2 + carry;

            }

            

            carry = 0;

            if (result > MAX_PART_NUM) {

                // Calculate carry, it will always be 1 for sums of course

// but this calculation is more general for other operations

                carry = (result - PART_VALUE) / PART_VALUE + 1;

                // limit the result to 9 digits

                result = result - PART_VALUE;

            }

 

            if (carry > 0) {

                if (l1 + 1 > lr) return OVERFLOW;

                result[l1] = carry;

            }

        }

    } else if (l2 > l1) {

        // l2 got more partial numbers than l1

        new carry = 0;

        for (new i = 0; i < l2; i++) {

            if (i + 1 > lr) return OVERFLOW;

            if (i >= l1) {

                result = n2 + carry;

            } else {

                result = n1 + n2 + carry;

            }

            

            carry = 0;

            if (result > MAX_PART_NUM) {

                // Calculate carry

                carry = (result - PART_VALUE) / PART_VALUE + 1;

                result = result - PART_VALUE;

            }

 

            if (carry > 0) {

                if (l1 + 1 > lr) return OVERFLOW;

                result[l1] = carry;

            }            

        }

    } else {

        // l1 and l2 got the same amount of partial numbers

        

        // Start with the last partial number for easier carry calculation

        new carry = 0;

        for (new i = 0; i < l1; i++) {

            if (i + 1 > lr) return OVERFLOW;

            result = n1 + n2 + carry;

            carry = 0;

            if (result > MAX_PART_NUM) {

                // Calculate carry

                carry = (result - PART_VALUE) / PART_VALUE + 1;

                result = result - PART_VALUE;

            }            

        }

        if (carry > 0) {

            if (l1 + 1 > lr) return OVERFLOW;

            result[l1] = carry;

        }

    }

    return SUCCESS;

}

 

// Creates a human-readable string of an array-number

stock formatBigNumber(n1[], res[], l1 = sizeof(n1), lr = sizeof(res)) {

// First partial number without trailing 0

    new firstnotnull;

    // Find first number != null

    for (new i = l1 - 1; i >= 0; i--) {

        if (n1 != 0) {

            firstnotnull = i;

            break;

        }

    }

    format(res, lr, "%d", n1[firstnotnull]);

    for (new i = firstnotnull - 1; i >= 0; i--) {

        format(res, lr, "%s%09d", res, n1);

    }

}

 

 

De exemplu: 

 

 

// These big numbers are aligned in 9-digit blocks right-to-left

new n1[] = {356363461, 346452359, 993673499, 921945436, 934776456, 231543675, 135798642, 111111111};

new n2[] = {946783486, 124869357, 934569257, 123456789, 987654321, 165776555, 999999999, 999999999};

new result[10];        

new w1[96], w2[96], res[96];        

 

addBigNumber(n1, n2, result);

        

formatBigNumber(n1, w1);

formatBigNumber(n2, w2);

formatBigNumber(result, res);

printf("%s + %s = %s", w1, w2, res);

 

/* Output:

111111111135798642231543675934776456921945436993673499346452359356363461

+ 999999999999999999165776555987654321123456789934569257124869357946783486

= 1111111111135798641397320231922430778045402226928242756471321717303146947

*/

Iar acolo unde este printf("%s + %s = %s", w1, w2, res); poti folosi : * / - ^ < > ...

 

 

Sa traiesti nepoate. T/C

Edited by Kannon
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

moduri samp