A_zjzj 2020-04-05 13:31 采纳率: 0%
浏览 87

为什么我重载了运算符之后就会RE,然后如果把函数中的内容直接放在主函数里面就可以的?

开始我写的高精度重载运算符其他的都可以就是+不行,总是RE。

把函数中的内容直接放在主函数里面就可以了,为甚?

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
struct bign{
    int num[100001];
    int len;
    bool sign;
    bign();
    bign(const int &x);
    bign(const char *x);
    bign(const string &x);
    bign operator = (const int &x);
    bign operator = (const char *x);
    bign operator = (const string &x);
    friend istream& operator >> (istream& in,bign &x);
    friend ostream& operator << (ostream& out,const bign &x);
    bool operator < (const bign &x)const;
    bool operator < (const int &x)const;
    bool operator > (const bign &x)const;
    bool operator > (const int &x)const;
    bool operator <= (const bign &x)const;
    bool operator <= (const int &x)const;
    bool operator >= (const bign &x)const;
    bool operator >= (const int &x)const;
    bool operator == (const bign &x)const;
    bool operator == (const int &x)const;
    bool operator != (const bign &x)const;
    bool operator != (const int &x)const;
    bign operator + (const bign &x)const;
    bign operator + (const int &x)const;
    bign operator ++ ();
    bign operator += (const bign &x);
    bign operator += (const int &x);
    bign operator - (const bign &x)const;
    bign operator - (const int &x)const;
    bign operator -- ();
    bign operator -= (const bign &x);
    bign operator -= (const int &x);
    bign operator * (const bign &x)const;
    bign operator * (const int &x)const;
    bign operator *= (const bign &x);
    bign operator *= (const int &x);
    bign operator / (const bign &x)const;
    bign operator / (const int &x)const;
    bign operator /= (const bign &x);
    bign operator /= (const int &x);
    bign operator % (const bign &x)const;
    bign operator % (const int &x)const;
    bign operator %= (const bign &x);
    bign operator %= (const int &x);
    bign operator ! ()const;
};
bign abs(const bign &x);
bign pow(const bign &x,const bign &y);
bign pow(const bign &x,const int &y);
bign pow(const int &x,const bign &y);
bign pow(const int &x,const int &y);
bign qpow(const bign &x,const bign &y);
bign qpow(const bign &x,const int &y);
bign qpow(const int &x,const bign &y);
bign qpow(const int &x,const int &y);
bign max(const bign &x,const bign &y);
bign max(const bign &x,const int &y);
bign max(const int &x,const bign &y);
bign min(const bign &x,const bign &y);
bign min(const bign &x,const int &y);
bign min(const int &x,const bign &y);
bign::bign(){
    memset(num,0,sizeof(num));
    len=1;
    sign=1;
}
bign::bign(const int &x){
    memset(num,0,sizeof(num));
    int k=x;
    if(k<0)sign=0,k=-k;
    if(!k)len=1;
    else{
        for(len=1;k;len++)num[len]=k%10000,k/=10000;
        len--;
    }
}
bign::bign(const char *x){
    memset(num,0,sizeof(num));
    if(x[0]=='-'){
        sign=0;
        int xx=0,t=0,k=1;
        len=0;
        for(int i=strlen(x);i>=1;i--){
            xx+=(x[i]-'0')*k;
            k*=10;
            t++;
            if(t==4){
                num[++len]=xx;
                xx=t=0;
                k=1;
            }
        }
        if(xx)num[++len]=xx;
    }
    else{
        sign=1;
        int xx=0,t=0,k=1;
        len=0;
        for(int i=strlen(x);i>=0;i--){
            xx+=(x[i]-'0')*k;
            k*=10;
            t++;
            if(t==4){
                num[++len]=xx;
                xx=t=0;
                k=1;
            }
        }
        if(xx)num[++len]=xx;
    }
}
bign::bign(const string &x){
    memset(num,0,sizeof(num));
    if(x[0]=='-'){
        sign=0;
        int xx=0,t=0,k=1;
        len=0;
        for(int i=x.length()-1;i>=1;i--){
            xx+=(x[i]-'0')*k;
            k*=10;
            t++;
            if(t==4){
                num[++len]=xx;
                xx=t=0;
                k=1;
            }
        }
        if(xx)num[++len]=xx;
    }
    else{
        sign=1;
        int xx=0,t=0,k=1;
        len=0;
        for(int i=x.length()-1;i>=0;i--){
            xx+=(x[i]-'0')*k;
            k*=10;
            t++;
            if(t==4){
                num[++len]=xx;
                xx=t=0;
                k=1;
            }
        }
        if(xx)num[++len]=xx;
    }
}
bign bign::operator = (const int &x){
    *this=bign(x);
    return *this;
}
bign bign::operator = (const char *x){
    *this=bign(x);
    return *this;
}
bign bign::operator = (const string &x){
    *this=bign(x);
    return *this;
}
istream& operator >> (istream &in,bign &x){
    x=bign();
    string s;
    in>>s;
    x=s;
    return in;
}
ostream& operator << (ostream &out,const bign &x){
    if(x.sign==0)out<<"-";
    if(x.len==1&&x.num[1]==0)out<<0;
    else{
        if(x.num[x.len]!=0)out<<x.num[x.len];
        for(int i=x.len-1;i>=1;i--)printf("%04d",x.num[i]);
    }
    return out;
}
bool bign::operator < (const bign &x)const{
    if(sign!=x.sign)return sign==0;
    if(len!=x.len)return (len<x.len)^sign^1;
    for(int i=len;i>=1;i--){
        if(num[i]!=x.num[i]){
            return (num[i]<x.num[i])^sign^1;
        }
    }
    return 0;
}
bool bign::operator < (const int &x)const{
    return *this<bign(x);
}
bool bign::operator > (const bign &x)const{
    return x<*this;
}
bool bign::operator > (const int &x)const{
    return *this>bign(x);
}
bool bign::operator <= (const bign &x)const{
    return !(*this>x);
}
bool bign::operator <= (const int &x)const{
    return *this<=bign(x);
}
bool bign::operator >= (const bign &x)const{
    return x<=*this;
}
bool bign::operator >= (const int &x)const{
    return *this>=bign(x);
}
bool bign::operator == (const bign &x)const{
    return !((*this<x)||(*this>x));
}
bool bign::operator == (const int &x)const{
    return *this==bign(x);
}
bool bign::operator != (const bign &x)const{
    return !(*this==x);
}
bool bign::operator != (const int &x)const{
    return *this!=bign(x);
}
bign abs(const bign &x){
    bign k=x;
    k.sign=1;
    return k;
}
bign bign::operator + (const bign &x)const{
    bign ans;
    ans=0;
    if(sign==x.sign){
        ans.sign=sign;
        ans.len=(len>x.len?len:x.len);
        for(int i=1;i<=ans.len;i++){
            ans.num[i]+=num[i]+x.num[i];
            ans.num[i+1]+=ans.num[i]/10000;
            ans.num[i]%=10000;
        }
        if(ans.num[ans.len+1])ans.len++;
        return ans;
    }
    else{
        bign a=*this,b=x;
        if(a.sign==0){
            bign k=a;
            a=b;
            b=k;
        }
        ans.sign=(a>=abs(b)?1:0);
        if(ans.sign){
            ans.len=a.len;
            for(int i=1;i<=ans.len;i++){
                ans.num[i]+=a.num[i]-b.num[i];
                if(ans.num<0){
                    ans.num[i]+=10000;
                    ans.num[i-1]-=1;
                }
            }
            if(ans.num[ans.len]==0)ans.len--;
            return ans;
        }
        else{
            ans.len=b.len;
            for(int i=1;i<=ans.len;i++){
                ans.num[i]+=b.num[i]-a.num[i];
                if(ans.num<0){
                    ans.num[i]+=10000;
                    ans.num[i-1]-=1;
                }
            }
            if(ans.num[ans.len]==0)ans.len--;
            return ans;
        }
    }
}
bign bign::operator + (const int &x)const{
    return *this+bign(x);
}
bign bign::operator ++ (){
    return *this=*this+1;
}
bign bign::operator += (const bign &x){
    return *this=*this+x;
}
bign bign::operator += (const int &x){
    return *this=*this+x;
}
bign bign::operator - (const bign &k)const{
    bign x;
    x=k;
    x.sign^=1;
    return *this+x;
}
bign bign::operator - (const int &x)const{
    return *this-bign(x);
}
int main(){
    bign a,b;
    cin>>a>>b;
    cout<<a+b;//到这一句RE
    return 0;
}

各位帮帮我啊!

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 17:58
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。