Four arithmetic operations for large numbers

Source: Internet
Author: User

When processing the operation of large numbers, we generally use Arrays for simulation. The following describes the implementation methods of the addition, subtraction, multiplication, and division of large numbers.

1. Addition.

For example:

Input: 123456789123456789123456789

1

Output: 123456789123456789123456790

The input is saved in a character array, and the input is saved in an integer array. Then, the input can be added by bit. Pay attention to carry processing.

#include<stdio.h>#include<string.h>int max(int x,int y){    if(x>y)        return x;    else        return y;}int main(void){        char str1[510],str2[510];    while(scanf("%s %s",str1,str2)==2)    {                int a[510]={0},b[510]={0},c[510]={0},i;        int m,n,max1=0;        m=strlen(str1);        n=strlen(str2);        max1=max(m,n);        for(i=0;i<max1;i++)        {            a[m-i-1]=str1[i]-48;            b[n-i-1]=str2[i]-48;        }        for(i=0;i<max1;i++)            c[i]=a[i]+b[i];        for(i=0;i<max1;i++)        {             c[i+1]=c[i]/10+c[i+1];            c[i]=c[i]%10;        }        if(c[max1]!=0)        {            for(i=max1;i>=0;i--)                printf("%d",c[i]);        }        else        {            for(i=max1-1;i>=0;i--)                printf("%d",c[i]);        }        printf("\n");    }    return0;}

2. Subtraction

Input: 123456789123456789

1

Output: 123456789123456788

The principle is the same as that of addition (Here we assume that the first number is greater than the second number)

#include<stdio.h>#include<string.h>int main(void){    char s1[505],s2[505];    while(scanf("%s%s",s1,s2)==2)    {        int i,j,len1,len2;        int a[105]={0},b[105]={0};        len1=strlen(s1);        len2=strlen(s2);        for(i=len1-1,j=0;i>=0;i--,j++)        {            a[j]=s1[i]-48;        }        for(i=len2-1,j=0;i>=0;i--,j++)        {            b[j]=s2[i]-48;        }        for(i=0;i<len1;i++)        {            a[i]=a[i]-b[i];            if(a[i]<0)            {                a[i]+=10;                a[i+1]--;            }        }        i=len1-1;        while(a[i]==0)        {            i--;        }        for(;i>=0;i--)        {            printf("%d",a[i]);        }        printf("\n");    }    return0;}

3. Multiplication

Array simulation is also used in principle.

A [I] 12345

B [J] 23

Use C [k] to save each operation result, K = I + J;

C [I + J] = C [I + J] + A [I] * B [J];

Here we simulate a multiplication process:

123

* 12

--------------

246

+ 123

--------------

1476

#include<iostream>#include<string.h>usingnamespace std;int main(void){    char s1[510],s2[510],temp[510];    int a[510],b[510],c[1010];    while(scanf("%s%s",s1,s2)==2)    {        int i,j,h;        int len1,len2;        if(strlen(s1)<strlen(s2))        {            strcpy(temp,s1);            strcpy(s1,s2);            strcpy(s2,temp);        }        len1=strlen(s1);        len2=strlen(s2);        memset(c,0,sizeof(c));        for(i=len1-1,j=0;i>=0;i--,j++)        {            a[j]=s1[i]-48;        }        for(i=len2-1,j=0;i>=0;i--,j++)        {            b[j]=s2[i]-48;        }        for(i=0;i<len2;i++)        {            for(j=0;j<len1;j++)            {                c[i+j]=a[j]*b[i]+c[i+j];            }        }        for(i=0;i<2*len1;i++)        {            if(c[i]>=10)            {                c[i+1]=c[i+1]+c[i]/10;                c[i]=c[i]%10;            }        }        i=2*len1;        while(c[i]==0)        {            i--;        }        if(i<0)        {            printf("0\n");        }        else        {            for(;i>=0;i--)                printf("%d",c[i]);            printf("\n");        }        }    return0;}

4. Division

Division also uses array simulation. However, division is not directly calculated based on division. Instead, division is converted to subtraction to obtain the result.

# Include <stdio. h> # include <string. h> int len1, len2; char S1 [905]; char S2 [905]; int re [905]; void sub () {int I = 0; Int J; while (1) {If (S1 [I] = '0') I ++; else {J = I; break ;}} for (; I <len2; I ++) {S1 [I] = S1 [I]-S2 [I] + '0';} for (I = len2-1; I> J; I --) // low start detection is less than 0 {If (S1 [I] <'0') {S1 [I] + = 10; S1 [I-1] --;} int main (void) {int I, P; while (scanf ("% S % s", S1, S2) = 2) {len1 = strlen (S1 ); len2 = strlen (S2); If (len1 <len2 | (len1 = len2 & strncmp (S1, S2, len2) <0) // If a <B, directly output 0 {printf ("0 \ n"); continue;} p = 0; while (1) {re [p] = 0; while (strncmp (S1, S2, len2)> = 0) // subtract until it cannot be subtracted {sub (); Re [p] ++;} p ++; If (len1 = len2) break; for (I = len2-1; I> = 0; I --) // Add 0 in front of S2, to perform the subtraction operation {S2 [I + 1] = S2 [I];} S2 [0] = '0'; len2 ++; s2 [len2] = '\ 0';} I = 0; while (1) {If (Re [I] = 0) I ++; else break ;} for (; I <p; I ++) printf ("% d", re [I]); printf ("\ n") ;} return0 ;}
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.