ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

高精度模板

2019-09-16 21:00:28  阅读:237  来源: 互联网

标签:zheng return 高精度 int len && gj 模板


众所周知,高精度一直都是非常不友好滴~,所以wljss在这里为大家提供一下重载后的结构体高精度,还是非常实用滴^_^.

本模板不定期更新,目前重载的符号有:

1.*:高精度乘法 2.+:高精度加法 3.-:高精度减法 4./:高精除低精 5.>>:高精度cin读入 6.<<:高精度cout输出。

  #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
int m,l,r;
long long b;
char a[5100];
struct gj
{
    int len,zheng;//len:长度 zheng:正负标记0为负1为正 
    int v[10000];
    gj(){len=0;memset(v,0,sizeof(v));zheng=1;}
    gj(int x)
    {
        if(x>=0)zheng=1;
        else x=-x,zheng=0;
        len=0;memset(v,0,sizeof(v));
        while(x)
        {
            v[++len]=x%10;
            x/=10;
        }
    }
    friend bool operator <(const gj &a,const gj &b)
    {
        if(a.len<b.len)return 1;
        if(a.len>b.len)return 0;
        for(int i=a.len;i>=1;--i)
        {
            if(a.v[i]<b.v[i])return 1;
            if(a.v[i]>b.v[i])return 0;
        }
        return 0;
    }
}n;
/*------以下为重载部分------*/ 
ostream& operator << (ostream &out,const gj &a);
istream& operator >> (istream &in,gj &a);
gj operator -(gj a,gj b);
gj operator +(gj a,gj b);
gj operator *(gj a,gj b);

gj operator +(gj a,gj b)
{
    if(!a.zheng&&!b.zheng)
    {
        a.zheng=b.zheng=1;
        gj c=a+b;
        c.zheng=0;
        return c;
    }
    if(!a.zheng&&b.zheng)
    {
        a.zheng=b.zheng=1;
        return b-a;
    }
    if(a.zheng&&!b.zheng)
    {
        a.zheng=b.zheng=1;
        return a-b;
    }
    int len=a.len+b.len;
    gj c;
    c.len=len;
    for(int i=1;i<=len;++i)c.v[i]=a.v[i]+b.v[i];
    for(int i=1;i<=len;++i)
    {
        if(c.v[i]>=10)
        {
            ++c.v[i+1];
            c.v[i]-=10;
        }
    }
    while(c.len&&!c.v[c.len])c.len--;
    return c;
}
gj operator -(gj a,gj b)
{
    if(!a.zheng&&!b.zheng)
    {
        a.zheng=b.zheng=1;
        return b-a;
    }
    if(!a.zheng&&b.zheng)
    {
        a.zheng=1;
        gj c=a+b;
        c.zheng=0;
        return c;
    }
    if(a.zheng&&!b.zheng)
    {
        b.zheng=1;
        gj c=a+b;
        return c;
    }
    if(a.zheng&&b.zheng&&a<b)
    {
        gj c=b-a;
        c.zheng=0;
        return c;
    }
    int len=max(a.len,b.len);
    gj c;
    for(int i=1;i<=len;++i)c.v[i]=a.v[i]-b.v[i];
    c.len=len;
    for(int i=1;i<=c.len;++i)
    {
        if(c.v[i]<0)
        {
            c.v[i+1]--;
            c.v[i]+=10;
        }
    }
    while(c.len&&!c.v[c.len])c.len--;
    return c;
}
gj operator *(gj a,gj b)
{
    gj c;
    for(int i=1;i<=a.len;++i)
    for(int j=1;j<=b.len;++j)
    c.v[i+j-1]+=a.v[i]*b.v[j];
    c.len=a.len+b.len;
    for(int i=1;i<=c.len-1;++i)
    {
        if(c.v[i]>=10)
        {
            c.v[i+1]+=c.v[i]/10;
            c.v[i]%=10;
        }
    }
    while(c.v[c.len]==0&&c.len>1)--c.len;
    if(a.zheng!=b.zheng)c.zheng=0;
    else c.zheng=1;
    return c;
}
gj operator /(gj a,long long b)
{
    gj c;int d=0;
    for(int i=a.len;i>=1;--i)
    c.v[++c.len]=((d*10+a.v[i])/b),d=(d*10+a.v[i])%b;
    for(int i=1;i<=c.len/2;++i)swap(c.v[i],c.v[c.len-i+1]);
    if(!a.len||!b||(a.zheng&&b>0)||(!a.zheng&&b<0))c.zheng=1;
    else c.zheng=0;
    while(c.v[c.len]==0&&c.len>1)--c.len;
    return c;
}
istream& operator >> (istream &in,gj &a)//方便使用cin
{
    char lin[5010];int len;
    scanf("%s",lin+1);
    len=a.len=strlen(lin+1);
    if(lin[1]=='-')a.zheng=0,a.len--;
    else a.zheng=1;
    for(int i=1;i<=a.len;++i)a.v[i]=lin[len-i+1]-'0';
    return in;
}
ostream& operator << (ostream &out,const gj &a)//方便使用cout 
{
    if(!a.len)//一定要注意答案是0得情况 
    {
        cout<<"0";
        return out;
    }
    if(!a.zheng)cout<<"-";
    for(int i=a.len;i>=1;i--)printf("%d",a.v[i]);
    return out;
}
/*------以上为重载部分------*/ 
int main()
{
    cin>>n>>b;
    cout<<n/b;
    return 0;
}

标签:zheng,return,高精度,int,len,&&,gj,模板
来源: https://www.cnblogs.com/wljss/p/11529965.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有