Addition and multiplication of large numbers, addition and multiplication of large numbers

Source: Internet
Author: User

Addition and multiplication of large numbers, addition and multiplication of large numbers

When calculating the sum of two small integer numbers, it is easy to solve the problem by using the plus operator, but it will overflow for two large numbers. You cannot use the + operator. Of course, you can use the + operator.

In fact, for the addition of two big integers, we use the method of addition in elementary school ------- vertical addition, one-bit addition, and the sum of the sum is greater than 9 as long as one digit is OK.

The Code is as follows:

# Include <string. h> # include <stdio. h> const int N = 100; char s1 [N], s2 [N]; // save the two big integers int bign1 [N], bign2 [N]; // convert a large integer to an integer. // convert a character to an integer in reverse order. void char_num (char * s, int bin []) {int len = strlen (s); for (int I = len-1, k = 0; I> = 0; I --) bin [k ++] = s [I]-'0';} // calculate the two big integers and void _ fun (int bin1 [], int bin2 []) {int I, j; for (I = 0, j = 0; I <N; I ++, j ++) {bin1 [I] + = bin2 [j]; if (bin1 [I]> 9) {bin1 [I] % = 10; bin1 [I + 1] ++ ;}} void pri Nt (int bin []) {bool flag = false; for (int I = N; I> = 0; I --) {if (flag) printf ("% d ", bin [I]); else if (bin [I]) {// outputs the first non-zero number. Printf ("% d", bin [I]); flag = true ;}} putchar ('\ n ');} /// only two positive integers can be calculated and added to int main () {while (scanf ("% s", s1, s2 )! = EOF) {memset (bign1, 0, sizeof (bign1); memset (bign2, 0, sizeof (bign2); char_num (s1, bign1); char_num (s2, bign2); _ fun (bign1, bign2); print (bign1);} return 0 ;}

Now, the basic idea of multiplication is the same.

Now we use usigned ans1 [200] And usigned ans2 [200] to save the two multiplier, and reault [400] to store the product of the two multiplier. It should be noted that the number of two 200 BITs is multiplied, and its product is the maximum number of 400 bits. The calculation process is basically the same as the column-vertical multiplication in elementary school, but it is easy for programming to put the carry steps in the same way.

Take 835x49 as an example:

1 step: 835x9. 5x9 get 45 1, 3x9 get 27 10, 8x9 get 72 100. Because do not process carry, the result is:

Number of digits 3 2 1 0

----- 0 0 0 72 27 45

Then calculate 4x5 and get 20 10. Therefore, the above result is changed:

Number of digits 3 2 1 0

----- 0 0 0 72 47 45

Calculate 4x3 and get 12 100. Therefore, the above result is:

Number of digits 3 2 1 0

----- 0 0 0 84 47 45

Finally, 4x8 is calculated, and 32 1000 are obtained. Therefore, the above result is:

Number of digits 3 2 1 0

----- 0 0 32 84 47 45

Since then, multiplication has been completed. Next we will handle the carry issue.

Leave realut [0] 5, add 4 to reault [1], get 51, leave 1, and add 5 to reault [2]. 89 .........

Finally, we get:

Number of digits 4 3 2 1 0

---- 0 4 0 9 1 5

To sum up a rule, the I-th digit of a number is multiplied by the j-th digit of another number, and must be accumulated to the I + j-th digit of the result. (I, j are from 0 and from right to left)

The Code is as follows:

# Include <stdio. h> # include <string. h> const int N = 200; int len1, len2; void char_num (char * s, unsigned bin []) {int len = strlen (s ); for (int I = len-1, k = 0; I> = 0; I --) bin [k ++] = s [I]-'0 ';} void _ fun (unsigned bin1 [], unsigned bin2 [], unsigned ans []) {for (int I = 0; I <len1; I ++) {for (int j = 0; j <len2; j ++) ans [I + j] + = bin1 [I] * bin2 [j]; // The I j-bits of the two numbers are multiplied to the I + j-bits of the result.} Void _ carry (unsigned bin []) {int len = 2 * N; for (int I = 0; I <len; I ++) {if (bin [I]> 9) {bin [I + 1] + = bin [I]/10; bin [I] % = 10 ;}}} void print (unsigned bin []) {bool flag = false; for (int I = N * 2; I> = 0; I --) {if (flag) printf ("% d", bin [I]); else if (bin [I]) {printf ("% d", bin [I]); flag = true ;}} if (! Flag) putchar ('0'); // conditions where the value is multiplied by 0. Putchar ('\ n');} int main () {unsigned ans1 [n], ans2 [N], reault [2 * N + 10]; char s1 [N], s2 [N]; while (scanf ("% s", s1, s2 )! = EOF) {memset (ans1, 0, sizeof (ans1); memset (ans2, 0, sizeof (ans2); memset (reault, 0, sizeof (reault )); len1 = strlen (s1), len2 = strlen (s2); char_num (s1, ans1); char_num (s2, ans2); _ fun (ans1, ans2, reault ); _ carry (reault); print (reault);} return 0 ;}

Related Article

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.