高精度代码为什么报错
#include<iostream>#include<cstring>
using namespace std;
struct BigInt{
int a;
int len;
void save(string x) {
len = x.length();
for(int i=0; i<x.length(); ++i) {
a = x.c_str()-'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 = c.a / 10 + this->a + other.b; c.a %= 10;}
else c.a = this.a + other.a;
}
if(c.a == 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;
}
}
};
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行报错,请问是什么原因{:10_266:} 本帖最后由 jhq999 于 2022-10-2 18:28 编辑
if(i < 199) {c.a = c.a / 10 + this->a + other.b; c.a %= 10;}////////// other.b,成员变量b没有被声明 改了一下,还是有错{:10_266:}#include<iostream>
#include<cstring>
using namespace std;
struct BigInt{
int a;
int len;
void save(string x) {
len = x.length();
for(int i=0; i<x.length(); ++i) {
a = x.c_str()-'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 = c.a / 10 + this->a + other.a; c.a %= 10;}
else c.a = this->a + other.a;
}
if(c.a == 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;
}
}
};
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;
} cout<<a+b;//输出实例吗? jhq999 发表于 2022-10-2 20:57
是的 jhq999 发表于 2022-10-2 20:57
他现在就会跳出来一个叫ios_base.h的文件,然后报错 本帖最后由 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;
int len;
BigInt(){
for(int i=0;i<200;i+=1)a=0;//////////////
}
void save(string x) {
len = x.length();
int i=0;
for(i=0; i<x.length(); ++i) {
a = x.c_str()-'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+=a+other.a;
if(c.a>9)c.a+=1,c.a%=10;//////////////////
}
return c;
}
friend ostream &operator << (ostream Out, const BigInt &p) {
for(int i=199; i>199-p.len; --i) {
Out << p.a;
}
}
};
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;////////////////
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
struct BigInt{
char a;
int len;
BigInt(){
for(int i=0;i<200;i+=1)a=0;
}
void save(string x) {
len = x.length();
int i=0;
for(i=0; i<x.length(); ++i) {
a = x.c_str()-'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+=a+other.a;
if(c.a>9)c.a+=1,c.a%=10;
c.a+='0';
}
if(c.a)c.a+='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;
}
}
};
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;
return 0;
}
页:
[1]