标签:cout operator while const swa max 标记 print else
众所周知,高精度一直都是非常不友好滴~,所以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;
}
标签:cout operator while const swa max 标记 print else
原文地址:https://www.cnblogs.com/wljss/p/11529965.html