鱼C论坛

 找回密码
 立即注册
查看: 2022|回复: 6

[已解决]高精度代码为什么报错

[复制链接]
发表于 2022-10-2 08:41:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include<cstring>
using namespace std;
struct BigInt{
        int a[200];
        int len;
        void save(string x) {
                len = x.length();
                for(int i=0; i<x.length(); ++i) {
                        a[200-x.length()+i] = x.c_str()[i]-'0';
                }
        }
        BigInt& operator+(BigInt& other) {
                BigInt c;
                c.len = max(this->len, other.len) + 1;
                for(int i=199; i>199-max(this->len, other.len); --i) {
                        if(i < 199) {c.a[i] = c.a[i-1] / 10 + this->a[i] + other.b[i]; c.a[i-1] %= 10;}
                        else c.a[i] = this.a[i] + other.a[i];
                }
                if(c.a[200-c.len] == 0) c.len--;
                return c;
        }
        friend ostream &operator << (ostream Out, const BigInt &p) {
                for(int i=199; i>199-p.len; --i) {
                        Out << p.a[i];
                }
        }
};
BigInt input() {
        string temp1; cin>>temp1;
        BigInt temp2; temp2.save(temp1);
        return temp2;
}
int main()
{
        BigInt a = input(), b = input();
        cout<<a+b;
        return 0;
}
如题,这段代码总是在第17行报错,请问是什么原因
最佳答案
2022-10-3 13:21:31
本帖最后由 jhq999 于 2022-10-3 13:43 编辑
tommyyu 发表于 2022-10-2 21:26
他现在就会跳出来一个叫ios_base.h的文件,然后报错

#include<iostream>
#include<cstring>
using namespace std;
struct BigInt{
        int a[200];
        int len;
        BigInt(){
            for(int i=0;i<200;i+=1)a[i]=0;//////////////
        }
        void save(string x) {
                len = x.length();
                int i=0;
                for(i=0; i<x.length(); ++i) {
                        a[200-x.length()+i] = x.c_str()[i]-'0';
                }

        }
        BigInt operator+(BigInt& other) {///////////////////
                BigInt c;
                c.len = max(this->len, other.len) + 1;
                for(int i=199; i>c.len; --i) {
                        c.a[i]+=a[i]+other.a[i];
                        if(c.a[i]>9)c.a[i-1]+=1,c.a[i]%=10;//////////////////

                }
                return c;
        }
        friend ostream &operator << (ostream Out, const BigInt &p) {
                for(int i=199; i>199-p.len; --i) {
                        Out << p.a[i];
                }
        }
};
BigInt input() {
        string temp1; cin>>temp1;
        BigInt temp2; temp2.save(temp1);
        return temp2;
}
int main()
{
        BigInt a = input(), b = input();
        BigInt c=a+b;
        for(int i=200-c.len;i<200;i+=1)cout<<c.a[i];////////////////
        return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
struct BigInt{
        char a[200];
        int len;
        BigInt(){
            for(int i=0;i<200;i+=1)a[i]=0;
        }
        void save(string x) {
                len = x.length();
                int i=0;
                for(i=0; i<x.length(); ++i) {
                        a[199-x.length()+i] = x.c_str()[i]-'0';
                }

        }
        BigInt operator+(BigInt& other) {
                BigInt c;
                c.len = max(this->len, other.len);
                int i;
                for(i=198; i>=199-c.len; --i) {
                        c.a[i]+=a[i]+other.a[i];
                        if(c.a[i]>9)c.a[i-1]+=1,c.a[i]%=10;
                        c.a[i]+='0';

                }
                if(c.a[i])c.a[i]+='0',c.len+=1;
                return c;
        }
        friend ostream &operator << (ostream Out, const BigInt &p) {
                for(int i=199; i>199-p.len; --i) {
                        Out << p.a[i];
                }
        }
};
BigInt input() {
        string temp1; cin>>temp1;
        BigInt temp2; temp2.save(temp1);
        return temp2;
}
int main()
{
        BigInt a = input(), b = input();
        BigInt c=a+b;
        cout<<c.a+(199-c.len);
        //for(int i=200-c.len;i<200;i+=1)cout<<c.a[i];
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 18:27:02 | 显示全部楼层
本帖最后由 jhq999 于 2022-10-2 18:28 编辑
if(i < 199) {c.a[i] = c.a[i-1] / 10 + this->a[i] + other.b[i]; c.a[i-1] %= 10;}////////// other.b[i],成员变量b没有被声明

评分

参与人数 1鱼币 +3 收起 理由
tommyyu + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-2 18:55:44 | 显示全部楼层
改了一下,还是有错
#include<iostream>
#include<cstring>
using namespace std;
struct BigInt{
        int a[200];
        int len;
        void save(string x) {
                len = x.length();
                for(int i=0; i<x.length(); ++i) {
                        a[200-x.length()+i] = x.c_str()[i]-'0';
                }
        }
        BigInt& operator+(BigInt& other) {
                BigInt c;
                c.len = max(this->len, other.len) + 1;
                for(int i=199; i>199-max(this->len, other.len); --i) {
                        if(i < 199) {c.a[i] = c.a[i-1] / 10 + this->a[i] + other.a[i]; c.a[i-1] %= 10;}
                        else c.a[i] = this->a[i] + other.a[i];
                }
                if(c.a[200-c.len] == 0) c.len--;
                return c;
        }
        friend ostream &operator << (ostream Out, const BigInt &p) {
                for(int i=199; i>199-p.len; --i) {
                        Out << p.a[i];
                }
        }
};
BigInt input() {
        string temp1; cin>>temp1;
        BigInt temp2; temp2.save(temp1);
        return temp2;
}
int main()
{
        BigInt a = input(), b = input();
        cout<<a+b;
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-2 20:57:01 | 显示全部楼层
cout<<a+b;//输出实例吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-2 21:25:00 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-2 21:26:32 | 显示全部楼层


他现在就会跳出来一个叫ios_base.h的文件,然后报错
屏幕截图 2022-10-02 212530.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-3 13:21:31 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-10-3 13:43 编辑
tommyyu 发表于 2022-10-2 21:26
他现在就会跳出来一个叫ios_base.h的文件,然后报错

#include<iostream>
#include<cstring>
using namespace std;
struct BigInt{
        int a[200];
        int len;
        BigInt(){
            for(int i=0;i<200;i+=1)a[i]=0;//////////////
        }
        void save(string x) {
                len = x.length();
                int i=0;
                for(i=0; i<x.length(); ++i) {
                        a[200-x.length()+i] = x.c_str()[i]-'0';
                }

        }
        BigInt operator+(BigInt& other) {///////////////////
                BigInt c;
                c.len = max(this->len, other.len) + 1;
                for(int i=199; i>c.len; --i) {
                        c.a[i]+=a[i]+other.a[i];
                        if(c.a[i]>9)c.a[i-1]+=1,c.a[i]%=10;//////////////////

                }
                return c;
        }
        friend ostream &operator << (ostream Out, const BigInt &p) {
                for(int i=199; i>199-p.len; --i) {
                        Out << p.a[i];
                }
        }
};
BigInt input() {
        string temp1; cin>>temp1;
        BigInt temp2; temp2.save(temp1);
        return temp2;
}
int main()
{
        BigInt a = input(), b = input();
        BigInt c=a+b;
        for(int i=200-c.len;i<200;i+=1)cout<<c.a[i];////////////////
        return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
struct BigInt{
        char a[200];
        int len;
        BigInt(){
            for(int i=0;i<200;i+=1)a[i]=0;
        }
        void save(string x) {
                len = x.length();
                int i=0;
                for(i=0; i<x.length(); ++i) {
                        a[199-x.length()+i] = x.c_str()[i]-'0';
                }

        }
        BigInt operator+(BigInt& other) {
                BigInt c;
                c.len = max(this->len, other.len);
                int i;
                for(i=198; i>=199-c.len; --i) {
                        c.a[i]+=a[i]+other.a[i];
                        if(c.a[i]>9)c.a[i-1]+=1,c.a[i]%=10;
                        c.a[i]+='0';

                }
                if(c.a[i])c.a[i]+='0',c.len+=1;
                return c;
        }
        friend ostream &operator << (ostream Out, const BigInt &p) {
                for(int i=199; i>199-p.len; --i) {
                        Out << p.a[i];
                }
        }
};
BigInt input() {
        string temp1; cin>>temp1;
        BigInt temp2; temp2.save(temp1);
        return temp2;
}
int main()
{
        BigInt a = input(), b = input();
        BigInt c=a+b;
        cout<<c.a+(199-c.len);
        //for(int i=200-c.len;i<200;i+=1)cout<<c.a[i];
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-20 11:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表