"Algorithm" high-precision arithmetic

Source: Internet
Author: User

High-precision Addition

    #include<iostream>    #include<cstring>    #include<cstdio>    using namespace std;    char a[200],b[200];    int a1[200],b1[200],c1[200];    int l1,l2,l3;    int main()    {        scanf("%s",a);        scanf("%s",b);        l1=strlen(a);        l2=strlen(b);        for(int i=0;i<=strlen(a)-1;i++)        a1[l1-i]=a[i]-48;        for(int i=0;i<=strlen(b)-1;i++)        b1[l2-i]=b[i]-48;        int k=0;        l3=1;        while(l3<=l1||l3<=l2)        {            c1[l3]=a1[l3]+b1[l3]+k;            k=c1[l3]/10;            c1[l3]%=10;            l3++;        }        c1[l3]=k;        if(c1[l3]==0)        l3--;        for(int i=l3;i>=1;i--)        cout<<c1[i];    }

High Precision subtraction (only two positive integers supported)

 #include <iostream> #include <cstring> #include <cstdio> using namespace std; Char a[200],b[200],n[200]; int a1[200],b1[200],c1[200]; int l1,l2,l3; int main () {scanf ("%s", a); scanf ("%s", b); if (strlen (a) <strlen (b) | | (Strlen (a) ==strlen (b) &&strcmp (a) <0)) {strcpy (n,a); strcpy (A, b); strcpy (B,n); cout<< "-"; } L1=strlen (a); L2=strlen (b); for (int I=0;i<=strlen (a) -1;i++) a1[l1-i]=a[i]-48; for (int i=0;i<=strlen (b) -1;i++) b1[l2-i]=b[i]-48; L3=1; while (l3<=l1| | L3<=L2) {if (A1[l3]<b1[l3]) {a1[l3]+=10; a1[l3+1]--; } C1[L3]=A1[L3]-B1[L3]; l3++; } while ((c1[l3]==0) &&l3>1) l3--; for (int i=l3;i>=1;i--) cout<<c1[i]; }

High-precision multiplication

    #include<iostream>    #include<cstring>    #include<cstdio>    using namespace std;    char a[2002],b[2002];    int a1[2002],b1[2002],c1[4040];    int l1,l2,l3;    int k;    int main()    {        scanf("%s",a);        scanf("%s",b);        l1=strlen(a);        l2=strlen(b);        for(int i=0;i<=l1-1;i++)        a1[l1-i]=a[i]-48;        for(int i=0;i<=l2-1;i++)        b1[l2-i]=b[i]-48;        for(int i=1;i<=l1;i++)        {            k=0;            for(int j=1;j<=l2;j++)            {                c1[i+j-1]=c1[i+j-1]+k+a1[i]*b1[j];                k=c1[i+j-1]/10;                c1[i+j-1]%=10;            }            c1[l2+i]=k;        }        l3=l1+l2;        while(l3>1&&c1[l3]==0)        l3--;        for(int i=l3;i>=1;i--)        cout<<c1[i];    }

High Precision Division (High precision divided by high precision)

    #include <iostream> #include <cstdio> #include <cstring> using namespace std;    int a[200],b[200],c[200],tem[200];        void Intt (int a[]) {string S;        cin>>s;        A[0]=s.length ();    for (int i=1;i<=a[0];i++) a[i]=s[a[0]-i]-48;            } void print (int a[]) {if (a[0]==0) {cout<<0<<endl;        Return        } for (int i=a[0];i>=1;i--) cout<<a[i];    cout<<endl;        } int Compare (int a[],int b[]) {if (a[0]>b[0]) return 1;        if (a[0]<b[0]) return-1;            for (int i=a[0];i>=1;i--) {if (A[i]>b[i]) return 1;        if (A[i]<b[i]) return-1;    } return 0;        } int Charu (int b[],int tem[],int k) {for (int i=1;i<=b[0];i++) tem[i+k-1]=b[i];    Tem[0]=b[0]+k-1;        } void Jian (int a[],int b[]) {int flag; Flag=compare (A, b);            if (flag==0) {a[0]=0;        Return                } if (Flag==1) {for (int i=1;i<=a[0];i++) {if (A[i]<b[i])                    {a[i]+=10;                A[i+1]-=1;            } A[i]=a[i]-b[i];        } while (a[0]>0&&a[a[0]]==0) a[0]--;        }} void Chufa (int a[],int b[],int c[]) {c[0]=a[0]-b[0]+1;            for (int i=c[0];i>=1;i--) {memset (tem,0,sizeof (TEM));            Charu (B,tem,i);                while (compare (A,tem) >=0) {c[i]++;            Jian (a,tem);    }} while (c[0]>0&&c[c[0]]==0) c[0]--;        } int main () {Intt (a);        Intt (b);        Chufa (A,B,C);        Print (a);    print (c); }

High Precision Division (High fine divided by low precision)

    #include<iostream>    #include<cstdio>    #include<cstring>    using namespace std;    int a1[20001],b1[20001];    char a[20001];    int l1,l2,x=0,b;    int main()    {        scanf("%s",a);        l1=strlen(a);        cin>>b;        for(int i=0;i<=l1-1;i++)        a1[i+1]=a[i]-48;        l2=1;        for(int i=1;i<=l1;i++)        {            b1[i]=(10*x+a1[i])/b;            x=(x*10+a1[i])%b;        }        while(b1[l2]==0&&l2<l1)        l2++;        for(int i=l2;i<=l1;i++)        cout<<b1[i];}

"Algorithm" high-precision arithmetic

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.