高精度加法、减法、乘法和除法

高精度加法

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string str1,str2;
    int a[250],b[250],len;
    int i;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    cin>>str1>>str2;///读入两个大数
    a[0]=str1.length();///记录第一个的长度
    for(i =1;i<=a[0];i++)
        a[i]=str1[a[0]-i]-'0'; ///按位记录大数的每一位
    b[0]=str2.length();
    for(i=1;i<=b[0];i++)
        b[i]=str2[b[0]-i]-'0';
    len=(a[0]>b[0]? a[0]:b[0]);  ///选择较长的那一个长度
    for(i=1;i<=len;i++)
    {
        a[i]+=b[i];    ///直接相加
        a[i+1]+=a[i]/10;    ///大于10则进位,否则则不
        a[i]%=10;   ///大于10取个位,否则则不
    }
    len++;    ///至多进一位
    while((a[len]==0) && (len>1)) len--;
    for(i=len;i>=1;i--)
        cout<<a[i];
    return 0;
}

高精度减法

#include <bits/stdc++.h>
using namespace std;
int compare(string s1,string s2)
{
    if(s1.length() > s2.length())   return 0;
    if(s1.length() < s2.length())   return 1;
    for(int i=0;i<=s1.length();i++)
    {
        if(s1[i] > s2[i])   return 0;
        if(s1[i] < s2[i])   return 1;
    }
    return 0;
}
int main()
{
    string str1,str2;
    int a[250],b[250],len;
    int i;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    cin>>str1>>str2;
    a[0]=str1.length();
    for(i=1;i<=a[0];i++)
        a[i]=str1[a[0]-i]-'0';
    b[0]=str2.length();
    for(i=1;i<=b[0];i++)
        b[i]=str2[b[0]-i]-'0';
    if(!compare(str1,str2))
    {
        for(i=1;i<=a[0];i++)
        {
            a[i]-=b[i];
            if(a[i]<0)
            {
                a[i+1]--;
                a[i]+=10;
            }
        }
        a[0]++;
        while((a[a[0]] == 0) && (a[0]>1)) a[0]--;
        for(i=a[0];i>=1;i--)
            cout<<a[i];
        cout<<endl;
    }
    else
    {
        cout<<'-';
        for(i=1;i<=b[0];i++)
        {
            b[i]-=a[i];
            if(b[i]<0)
            {
                b[i+1]--;
                b[i]+=10;
            }
        }
        b[0]++;
        while((b[b[0]]==0) && (b[0] > 1))   b[0]--;
        for(i=b[0];i>=1;i--)
            cout<<b[i];
        cout<<endl;
    }
}

高精度乘法

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string str1,str2;
    int a[250],b[250],c[500],len;
    int i,j;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    cin>>str1>>str2;
    a[0]=str1.length();
    for(i=1;i<=a[0];i++)
        a[i]=str1[a[0]-i]-'0';
    b[0]=str2.length();
    for(i=1;i<=b[0];i++)
        b[i]=str2[b[0]-i]-'0';
    memset(c,0,sizeof(c));
    for(i=1;i<=a[0];i++)
        for(j=1;j<=b[0];j++)
    {
        c[i+j-1]+=a[i]*b[j];
        c[i+j]+=c[i+j-1]/10;
        c[i+j-1]%=10;
    }
    len=a[0]+b[0]+1;
    while((c[len]==0) && (len>1))
        len--;
    for(i=len;i>=1;i--)
        cout<<c[i];
    return 0;
}

高精度除法

///高精度除以低精度
#include <bits/stdc++.h>
using namespace std;
int main()
{
    char a1[100],c1[100];
    int a[100],c[100],lena,i,x=0,lenc,b;
    memset(a,0,sizeof(a));
    memset(c,0,sizeof(c));
    gets(a1);
    cin>>b;
    lena=strlen(a1);
    for(i=0;i<=lena-1;i++)
        a[i+1]=a1[i]-48;
    for(i=1;i<=lena;i++)
    {
        c[i]=(x*10+a[i])/b;
        x=(x*10+a[i])%b;
    }
    lenc=1;
    while(c[lenc]==0 && lenc<lena)
        lenc++;
    for(i=lenc;i<=lena;i++)
        cout<<c[i];
    cout<<endl;
    return 0;
}

///高精度除以高精度
#include <bits/stdc++.h>
using namespace std;
int a[100],b[100],c[100];
int compare(int a[],int b[])
{
    int i;
    if(a[0]>b[0])
        return 1;
    if(a[0]<b[0])
        return 0;
    for(i=a[0];i>0;i--)
    {
        if(a[i]>b[i])
            return 1;
        if(a[i]<b[i])
            return 0;
    }
    return 0;
}
void subduction (int a[],int b[])
{
    int flag;
    int i;
    flag=compare(a,b);
    if(flag==0)
    {
        a[0]=0;
        return;
    }
    if(flag==1)
    {
        for(i=1;i<=a[0];i++)
        {
            if(a[i]<b[i])
            {
                a[i+1]--;
                a[i]+=10;
            }
            a[i]-=b[i];
        }
        while(a[0]>0 && a[a[0]]==0)
            a[0]--;
        return;
    }
}
int main()
{
    char str1[100],str2[100];
    int i,j;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    cin>>str1>>str2;
    a[0]=strlen(str1);
    b[0]=strlen(str2);
    for(i=1;i<=a[0];i++)
        a[i]=str1[a[0]-i]-'0';
    for(i=1;i<=b[0];i++)
        b[0]=str1[b[0]-i]-'0';
    int tmp[1000];
    c[0]=a[0]-b[0]+1;
    for(i=c[0];i>0;i--)
    {
        memset(tmp,0,sizeof(tmp));
        for(j=1;j<=b[0];j++)
            tmp[j+i-1]=b[j];
        tmp[0]=b[0]+i-1;
        while(compare(a,tmp)>=0)
        {
            c[i]++;
            subduction(a,tmp);
        }

    }
    while(c[0]>0 && c[c[0]]==0)
        c[0]--;
    ///cout<<"商为:";
    if(c[0]==0)
        cout<<0<<endl;
    else
    {
        for(i=c[0];i>0;i--)
            cout<<c[i];
        cout<<endl;
    }
    ///cout<<"余数为:";
    if(a[0]==0)
        cout<<0<<endl;
    else
    {
        for(i=a[0];i>0;i--)
            cout<<a[i];
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值