鱼C论坛

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

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

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

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

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

x
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. struct BigInt{
  5.         int a[200];
  6.         int len;
  7.         void save(string x) {
  8.                 len = x.length();
  9.                 for(int i=0; i<x.length(); ++i) {
  10.                         a[200-x.length()+i] = x.c_str()[i]-'0';
  11.                 }
  12.         }
  13.         BigInt& operator+(BigInt& other) {
  14.                 BigInt c;
  15.                 c.len = max(this->len, other.len) + 1;
  16.                 for(int i=199; i>199-max(this->len, other.len); --i) {
  17.                         if(i < 199) {c.a[i] = c.a[i-1] / 10 + this->a[i] + other.b[i]; c.a[i-1] %= 10;}
  18.                         else c.a[i] = this.a[i] + other.a[i];
  19.                 }
  20.                 if(c.a[200-c.len] == 0) c.len--;
  21.                 return c;
  22.         }
  23.         friend ostream &operator << (ostream Out, const BigInt &p) {
  24.                 for(int i=199; i>199-p.len; --i) {
  25.                         Out << p.a[i];
  26.                 }
  27.         }
  28. };
  29. BigInt input() {
  30.         string temp1; cin>>temp1;
  31.         BigInt temp2; temp2.save(temp1);
  32.         return temp2;
  33. }
  34. int main()
  35. {
  36.         BigInt a = input(), b = input();
  37.         cout<<a+b;
  38.         return 0;
  39. }
复制代码

如题,这段代码总是在第17行报错,请问是什么原因
最佳答案
2022-10-3 13:21:31
本帖最后由 jhq999 于 2022-10-3 13:43 编辑
tommyyu 发表于 2022-10-2 21:26
他现在就会跳出来一个叫ios_base.h的文件,然后报错

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. struct BigInt{
  5.         int a[200];
  6.         int len;
  7.         BigInt(){
  8.             for(int i=0;i<200;i+=1)a[i]=0;//////////////
  9.         }
  10.         void save(string x) {
  11.                 len = x.length();
  12.                 int i=0;
  13.                 for(i=0; i<x.length(); ++i) {
  14.                         a[200-x.length()+i] = x.c_str()[i]-'0';
  15.                 }

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

  23.                 }
  24.                 return c;
  25.         }
  26.         friend ostream &operator << (ostream Out, const BigInt &p) {
  27.                 for(int i=199; i>199-p.len; --i) {
  28.                         Out << p.a[i];
  29.                 }
  30.         }
  31. };
  32. BigInt input() {
  33.         string temp1; cin>>temp1;
  34.         BigInt temp2; temp2.save(temp1);
  35.         return temp2;
  36. }
  37. int main()
  38. {
  39.         BigInt a = input(), b = input();
  40.         BigInt c=a+b;
  41.         for(int i=200-c.len;i<200;i+=1)cout<<c.a[i];////////////////
  42.         return 0;
  43. }
复制代码
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. struct BigInt{
  5.         char a[200];
  6.         int len;
  7.         BigInt(){
  8.             for(int i=0;i<200;i+=1)a[i]=0;
  9.         }
  10.         void save(string x) {
  11.                 len = x.length();
  12.                 int i=0;
  13.                 for(i=0; i<x.length(); ++i) {
  14.                         a[199-x.length()+i] = x.c_str()[i]-'0';
  15.                 }

  16.         }
  17.         BigInt operator+(BigInt& other) {
  18.                 BigInt c;
  19.                 c.len = max(this->len, other.len);
  20.                 int i;
  21.                 for(i=198; i>=199-c.len; --i) {
  22.                         c.a[i]+=a[i]+other.a[i];
  23.                         if(c.a[i]>9)c.a[i-1]+=1,c.a[i]%=10;
  24.                         c.a[i]+='0';

  25.                 }
  26.                 if(c.a[i])c.a[i]+='0',c.len+=1;
  27.                 return c;
  28.         }
  29.         friend ostream &operator << (ostream Out, const BigInt &p) {
  30.                 for(int i=199; i>199-p.len; --i) {
  31.                         Out << p.a[i];
  32.                 }
  33.         }
  34. };
  35. BigInt input() {
  36.         string temp1; cin>>temp1;
  37.         BigInt temp2; temp2.save(temp1);
  38.         return temp2;
  39. }
  40. int main()
  41. {
  42.         BigInt a = input(), b = input();
  43.         BigInt c=a+b;
  44.         cout<<c.a+(199-c.len);
  45.         //for(int i=200-c.len;i<200;i+=1)cout<<c.a[i];
  46.         return 0;
  47. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 18:27:02 | 显示全部楼层
本帖最后由 jhq999 于 2022-10-2 18:28 编辑
  1. 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 | 显示全部楼层
改了一下,还是有错
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. struct BigInt{
  5.         int a[200];
  6.         int len;
  7.         void save(string x) {
  8.                 len = x.length();
  9.                 for(int i=0; i<x.length(); ++i) {
  10.                         a[200-x.length()+i] = x.c_str()[i]-'0';
  11.                 }
  12.         }
  13.         BigInt& operator+(BigInt& other) {
  14.                 BigInt c;
  15.                 c.len = max(this->len, other.len) + 1;
  16.                 for(int i=199; i>199-max(this->len, other.len); --i) {
  17.                         if(i < 199) {c.a[i] = c.a[i-1] / 10 + this->a[i] + other.a[i]; c.a[i-1] %= 10;}
  18.                         else c.a[i] = this->a[i] + other.a[i];
  19.                 }
  20.                 if(c.a[200-c.len] == 0) c.len--;
  21.                 return c;
  22.         }
  23.         friend ostream &operator << (ostream Out, const BigInt &p) {
  24.                 for(int i=199; i>199-p.len; --i) {
  25.                         Out << p.a[i];
  26.                 }
  27.         }
  28. };
  29. BigInt input() {
  30.         string temp1; cin>>temp1;
  31.         BigInt temp2; temp2.save(temp1);
  32.         return temp2;
  33. }
  34. int main()
  35. {
  36.         BigInt a = input(), b = input();
  37.         cout<<a+b;
  38.         return 0;
  39. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-2 20:57:01 | 显示全部楼层
  1. 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的文件,然后报错

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. struct BigInt{
  5.         int a[200];
  6.         int len;
  7.         BigInt(){
  8.             for(int i=0;i<200;i+=1)a[i]=0;//////////////
  9.         }
  10.         void save(string x) {
  11.                 len = x.length();
  12.                 int i=0;
  13.                 for(i=0; i<x.length(); ++i) {
  14.                         a[200-x.length()+i] = x.c_str()[i]-'0';
  15.                 }

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

  23.                 }
  24.                 return c;
  25.         }
  26.         friend ostream &operator << (ostream Out, const BigInt &p) {
  27.                 for(int i=199; i>199-p.len; --i) {
  28.                         Out << p.a[i];
  29.                 }
  30.         }
  31. };
  32. BigInt input() {
  33.         string temp1; cin>>temp1;
  34.         BigInt temp2; temp2.save(temp1);
  35.         return temp2;
  36. }
  37. int main()
  38. {
  39.         BigInt a = input(), b = input();
  40.         BigInt c=a+b;
  41.         for(int i=200-c.len;i<200;i+=1)cout<<c.a[i];////////////////
  42.         return 0;
  43. }
复制代码
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. struct BigInt{
  5.         char a[200];
  6.         int len;
  7.         BigInt(){
  8.             for(int i=0;i<200;i+=1)a[i]=0;
  9.         }
  10.         void save(string x) {
  11.                 len = x.length();
  12.                 int i=0;
  13.                 for(i=0; i<x.length(); ++i) {
  14.                         a[199-x.length()+i] = x.c_str()[i]-'0';
  15.                 }

  16.         }
  17.         BigInt operator+(BigInt& other) {
  18.                 BigInt c;
  19.                 c.len = max(this->len, other.len);
  20.                 int i;
  21.                 for(i=198; i>=199-c.len; --i) {
  22.                         c.a[i]+=a[i]+other.a[i];
  23.                         if(c.a[i]>9)c.a[i-1]+=1,c.a[i]%=10;
  24.                         c.a[i]+='0';

  25.                 }
  26.                 if(c.a[i])c.a[i]+='0',c.len+=1;
  27.                 return c;
  28.         }
  29.         friend ostream &operator << (ostream Out, const BigInt &p) {
  30.                 for(int i=199; i>199-p.len; --i) {
  31.                         Out << p.a[i];
  32.                 }
  33.         }
  34. };
  35. BigInt input() {
  36.         string temp1; cin>>temp1;
  37.         BigInt temp2; temp2.save(temp1);
  38.         return temp2;
  39. }
  40. int main()
  41. {
  42.         BigInt a = input(), b = input();
  43.         BigInt c=a+b;
  44.         cout<<c.a+(199-c.len);
  45.         //for(int i=200-c.len;i<200;i+=1)cout<<c.a[i];
  46.         return 0;
  47. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 18:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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