高精度

高精度乘法

/*
支持高精度与高精度的加减乘除和取余。
支持高精度乘单精度 不过要单精度在右边。
支持所有比较。
支持cin和cout
*/

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<memory>
#include<algorithm>
#include<string>
#include<climits>
using namespace std;

const int THP_MAXLEN=20050;
const int THP_MAXSTRLEN=20050;  //这两个常量都是必须要的,表示高精度长度.

const int L0=10000;  //每一位存4位数
char s[THP_MAXLEN*4]; //int给高精度赋值用

class   thp  //高精度类的名称
{   
    friend istream& operator>>(istream&,thp&);          /*输入运算符友元*/
    friend ostream& operator<<(ostream&,const thp&);    /*输出运算符友元*/
    friend const thp operator-(const thp&,const thp&);
    friend const thp operator+(const thp&,const thp&);
    friend const thp operator*(const thp&,int);        /*高精度乘单精度*/
    friend const thp operator*(const thp&,const thp&);
    friend const thp operator/(const thp&,const thp&);
    friend void thpdiv(const thp&,const thp&,thp&,thp&);
    friend const thp operator%(const thp&,const thp&);
    public:
        const thp& operator=(const thp&);
        const thp& operator=(const char*);
        const thp& operator=(int num);
        bool operator==(const thp&)const;
        bool operator>=(const thp&)const;
        bool operator<=(const thp&)const;
        inline  bool operator!=(const thp&)const;
        bool operator>(const thp&)const;
        bool operator<(const thp&)const;
        inline  void shl(const int);
        const thp& operator ++ ();
        inline void makeempty();
        const char *tostr()const;
        inline  thp& operator-=(const thp&);
        thp();
        thp(const char*);
        const   int thpstd();
    protected:  //几个数据项,_high表示位数。
        int _data[THP_MAXLEN],_high,_sign;  //_data 是每位的存储。
};

const   int thp::thpstd()
{   int i;
    for(i=0;i<=_high;i++)
    {   while(_data[i]<0)
        {   _data[i]+=L0;
            _data[i+1]--;
        }
    }
    return(0);
}

const thp operator % (const thp &a,const thp &b)
{   thp re1,re2;
    thpdiv(a,b,re1,re2);
    return(re2);
}

const thp& thp::operator ++ ()
{   _data[0]++;
    if(_data[0]>=L0)this->thpstd();
    return(*this);      
}

void thp::shl(const int n)
{   int i;
    for(i=_high;i>=0;i--)
    {   _data[i+n]=_data[i];
    }
    _high+=n;
    if(_data[_high]==0)_high=0;
}

thp& thp::operator -= (const thp &o)
{   int tint,i,len;
    for(i=0;i<=_high;i++)
    {   tint=_data[i]-o._data[i];
        if(tint<0)
        {   tint+=L0;
            _data[i+1]--;
        }
        _data[i]=tint;
    }
    for(len=this->_high;len>=0;len--)
    {   if(_data[len]!=0)break;
        if(len==0)break;
    }
    _high=len;
    return(*this);
}

void thpdiv(const thp &a,const thp &b,thp &c,thp &d)
{   int i;
    d.makeempty();
    c.makeempty();
    for(i=a._high;i>=0;i--)
    {   d.shl(1);
        d._data[0]=a._data[i];
        while(b<=d)
        {   d-=b;
            c._data[i]++;
        }
        if(i==0)break;
    }
    c._high=0;
    for(i=a._high;i>0;i--)
    {   if(c._data[i]!=0)
        {   c._high=i;break;
        }
    }
}

inline bool thp::operator != (const thp &o)const
{   return(!(0==memcmp(this,&o,sizeof(thp))));
}

bool thp::operator == (const thp &o)const
{   return(0==memcmp(this,&o,sizeof(thp)));
}

inline  bool thp::operator < (const thp &o)const
{   int i;
    if(*this==o)return(false);
    {   if(this->_high!=o._high)return(this->_high<o._high);
        for(i=this->_high;i>=0;i--)
        {   if(this->_data[i]!=o._data[i])return(this->_data[i]<o._data[i]);
            if(i==0)break;
        }
    }
    return(false);
}

bool thp::operator > (const thp &o)const
{   if(*this==o)return(false);
    return(!(*this<o));
}

bool thp::operator <= (const thp &o)const
{   return(*this<o||*this==o);
}

bool thp::operator >= (const thp &o)const
{   return(!(*this<o));
}

inline int max(int a,int b)
{   return(a>b?a:b);
}

thp::thp()
{   makeempty();
}

thp::thp(const char *s)
{   *this=s;
}

const thp operator-(const thp &a,const thp &b)
{   int i,len,tint;
    thp re;
    len=max(a._high,b._high);
    for(i=0;i<=len;i++)
    {   tint=a._data[i]-b._data[i]+re._data[i];
        if(tint<0)
        {   tint+=L0;
            re._data[i+1]--;
        }
        re._data[i]=tint;
    }
    while(len>0&&re._data[len]<1)len--;
    re._high=len;
    return(re);
}

const thp operator/(const thp &a,const thp &b)
{   thp re1,re2;
    thpdiv(a,b,re1,re2);
    return(re1);
}

const thp operator*(const thp &a,const thp &b)
{   int i,j,tint,len;
    thp re;
    for(i=0;i<=a._high;i++)
        for(j=0;j<=b._high;j++)
        {   tint=a._data[i]*b._data[j]+re._data[i+j];
            re._data[i+j]=tint%L0;
            re._data[i+j+1]+=tint/L0;
        }
    len=i+j;
    if(re._data[len+1]!=0)len++;
    re._high=len;
    return(re);
}
 /*高精度乘以低精度(注意:必须是bign*int顺序不能颠倒,要么会与高精度乘高精度发生冲突*/
 const thp operator*(const thp& a,int b)
 {
  int i,t,c=0;
  thp res;
  for(i=0;i<=a._high;i++)
  {
   t=a._data[i]*b+c;
   res._data[i]=t%L0;
   c=t/L0;
  }
  res._high=a._high;
  while(c!=0)
  {
   res._data[i++]=c%L0;
   c/=L0;
   res._high++;
  }
  return res;
 }
 /*(可选)加法运算符重载*/
const thp operator+(const thp &a,const thp &b)
{   int i,len,tint;
    thp re;
    len=max(a._high,b._high);
    for(i=0;i<=len;i++)
    {   tint=a._data[i]+b._data[i]+re._data[i];
        re._data[i]=tint%L0;
        re._data[i+1]=tint/L0;
    }
    if(re._data[len+1]!=0)len++;
    re._high=len;
    return(re);
}

const char *thp::tostr()const
{   char *re;
    char ts[5];
    int i,j,tint;
    re=(char *)malloc(THP_MAXSTRLEN);
    memset(ts,0,sizeof(ts));
    memset(re,0,THP_MAXSTRLEN);
    for(i=_high;i>=0;i--)
    {   tint=_data[i];
        for(j=0;j<4;j++)
        {   ts[3-j]=tint%10+'0';
            tint/=10;           
        }
        strcat(re,ts);
    }
    while(strlen(re)>1&&*re=='0')re++;
    return(re);
}

inline void thp::makeempty()
{   memset(this,0,sizeof(thp));}

const thp& thp::operator = (const char *s)
{   char ts[5];
    memset(ts,0,sizeof(ts));
    int len,i;
    makeempty();
    len=strlen(s);
    _high=(len-1)/4;
    i=(len-1)%4+1;
    strncpy(ts,s,i);
    s+=i;
    _data[_high]=atoi(ts);
    i=_high;
    while(i>0)
    {   i--;
        memset(ts,0,sizeof(ts));
        strncpy(ts,s,4);
        s+=4;
        _data[i]=atoi(ts);
    }
    return(*this);
}

const thp& thp::operator = (const thp &hp)
{   memcpy(this,&hp,sizeof(thp));
    return(*this);
}

const thp& thp::operator=(const int num)
    {
       memset(s,0,sizeof(s));
       sprintf(s,"%d",num);
       *this=s;
       return *this;
    }
 istream& operator>>(istream &in,thp& x)
 {
  string s;
  in>>s;
  x=s.c_str();
  return in;
 }
 ostream& operator<<(ostream &out,const thp& x)
 {
  out<<x.tostr();
  return out;
 }

int main()
{
    //freopen("P1109.in","r",stdin);

    thp a,b,c;

    cin>>a>>b;
    c=a/b;
    cout<<c<<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值