Simple addition of large numbers

Source: Internet
Author: User

Ideas:

Use a string to represent a large number, and then use a column-like vertical method to sum two numeric strings by bit.

123 +

24

_____

147

Specific implementation:

Char * BigNumberAddition (const char * number1, const char * number2) {if (! Number1 |! Number2) {return NULL;} int len1 = strlen (number1); int len2 = strlen (number2 ); // the result of adding two numbers is the number of digits of a large number or the number of digits of a large number + 1 int result_len = max (len1, len2) + 1; char * result = (char *) malloc (result_len + 1); memset (result, 0, result_len + 1); int index = result_len; -- index; -- len1; -- len2; while (len1> = 0 & len2> = 0) {// add result by bit [index] + = (number1 [len1]-'0 ') + (number2 [len2]-'0'); if (result [index]> = 10) {// carry result [index-1] + = result [index]/10; result [index] = result [index] % 10;} -- index; -- len1; -- len2;} while (len1> = 0) {result [index] + = (number1 [len1]-'0 '); if (result [index]> = 10) {// carry result [index-1] + = result [index]/10; result [index] = result [index] % 10;} -- len1; -- index;} while (len2> = 0) {result [index] + = (number2 [len2]-'0'); if (result [index]> = 10) {// carry r Esult [index-1] + = result [index]/10; result [index] = result [index] % 10;} -- len2; -- index ;} // remove the first 0 int gap = result [0] = 0? 1: 0; for (int I = 0; I <result_len; I ++) {result [I] = result [I + gap] + '0 ';} result [result_len-gap] = '\ 0'; return result ;}

Note:

1. Carry should be taken into account when adding one by one

2. Generally, two numbers are added together. For example, if the number of digits in number 1 is a and the number of digits in number 2 is B, the number of digits in the addition result may be max (a, B) or max (a, B) + 1. therefore, note that the first part of the add result may be 0.

3. When a string is used to represent a number, it is used to represent the 0 --> '0' of the number. Therefore, the character must be converted to the corresponding number during addition calculation.

 

Int _ tmain (int argc, _ TCHAR * argv [])
{
Char X number1 = "9999999 ";
Char X number2 = "9999999 ";
Char * result = BigNumberAddition (number1, number2 );
Cout <result;
Free (result );

Return 0;

}

 

Running result:

 

Simple addition of large numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.