|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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行报错,请问是什么原因
本帖最后由 jhq999 于 2022-10-3 13:43 编辑
- #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;
- }
复制代码
|
|