Precision Calculation-big number multiplied by large number, precision calculation by large number

Source: Internet
Author: User

Precision Calculation-big number multiplied by large number, precision calculation by large number
The algorithm is used to calculate a large number (a number that cannot be expressed by an existing data type) multiplied by a large number (a number that cannot be expressed by an existing data type ). The algorithm is to put every bit of a large number as a character into a character array, and then multiply each bit of the multiplier and each bit of the multiplier from the highest bit, store the result in a two-dimensional array res. For example, 12*12, res [0] [0] = 1, res [0] [1] = 2, res [1] [0] = 2, res [1] [1] = 4, then the final result array s, s [0] = res [0] [0] = 1, s [1] = res [0] [1] + res [1] [0] = 4, s [2] = res [1] [1] = 4, however, if the output is 441, we need to use the for loop to reverse the result. The Calculation of more digits will be more troublesome in the process, but it is the same as the calculation of more digits.

#include<stdio.h>#include<string.h>void mult(char a[],char b[],char s[]);main(){char a[65] = "12";char b[65] = "12";char s[130] = "";mult(a,b,s);printf("%s",s);}void mult(char a[],char b[],char s[]){int i,j,k=0,alen,blen,sum=0,res[65][65]={0},flag=0;char result[65];alen=strlen(a);blen=strlen(b); for (i=0;i<alen;i++){for (j=0;j<blen;j++){res[i][j]=(a[i]-'0')*(b[j]-'0');}}for (i=alen-1;i>=0;i--){for (j=blen-1;j>=0;j--){sum=sum+res[i+blen-j-1][j];}result[k]=sum%10;k=k+1;sum=sum/10;}for (i=blen-2;i>=0;i--){for (j=0;j<=i;j++){sum=sum+res[i-j][j];}result[k]=sum%10;k=k+1;sum=sum/10;}if (sum!=0) {result[k]=sum;k=k+1;}for (i=0;i<k;i++) {result[i]+='0';}for (i=k-1;i>=0;i--) {s[i]=result[k-1-i];}s[k]='\0';while(1){if (strlen(s)!=strlen(a)&&s[0]=='0') strcpy(s,s+1);elsebreak;}}

Below is the implementation process of my C language.

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.