Home > Back-end >  Addition of large Numbers vs run normally produce correct results but in devc collapse to solve
Addition of large Numbers vs run normally produce correct results but in devc collapse to solve

Time:09-19


#include
#include
#include
#include
using namespace std;
Const int MAX=110;
The class CHugeInt {
Public:
Char * cs;
Int cn;

CHugeInt (char _s [])
{
If (cs! Cs=NULL) delete [];
Cs=new char [strlen (_s) + 1);
Strcpy (cs, _s);

}
CHugeInt (int _n) : cn (_n) {}
Friend char * operator + (CHugeInt & amp; A1, CHugeInt & amp; B1)
{
Int a2 [210];
Int b2 [210];
Char b2_str [20].
Int sum [210];
Int Lena=strlen (a1. Cs);
for (int i=0; i{
A2 [I]=a1. Cs [I] - '0';
}
_itoa (b1. Cn, b2_str, 10);
Int lenb=strlen (b2_str);
For (int I=Lena - 1; I & gt;=0; I -)
{
If (lenb & lt;=0)
{
Lenb=0;
B2 [I]=0;
}
The else
B2 [I]=b2_str [-- lenb] - '0';
}
Int jinwei=0;
For (int I=Lena - 1; I & gt;=0; I -)
{
The sum [I]=(a2 + b2 [I] + [I] jinwei) % 10;
Jinwei=(a2 + b2 [I] + [I] jinwei)/10;
}
If (jinwei)
{
For (int I=Lena; I> 0; I -)
The sum [I]=sum (I - 1),
The sum of [0]=1;
};
Char * STR=new char [210];
Char STR [210].
for (int i=0; i{
STR [I]=sum [I] + '0';
}
STR [Lena + 1]='\ 0';
return str;
}
~ CHugeInt ()
{
The delete [] cs;
}
//added your code here
};
Int main ()
{
Char s [210];
int n;

While (cin & gt;> S & gt;> N) {
CHugeInt a (s);
CHugeInt b (n);

Cout & lt; //cout & lt; //cout & lt; //b +=n;
//cout & lt; <+ + b & lt; //cout & lt; //cout & lt; }
return 0;
}

CodePudding user response:

devc++ interrupt mode, the Internet has examined the mean is recycled to the STR space visited unallocated space so don't know whether this reason but vs no problem, if it is for this reason is there any way to solve, ask for advice

CodePudding user response:

Remove the this char STR [210]; The for loop above that

CodePudding user response:

The remove char STR [210]; The for loop above that not do or not

CodePudding user response:

Char * STR=new char [210];
Char STR [210].

Define two STR variable, can compile?

CodePudding user response:

Redefine, use the following will be recycled, with the above was temporarily no problem, but must pay attention to the leak,
 
Char * STR=new char [210];
Char STR [210].

CodePudding user response:

And redefine it doesn't matter, I have a still not check it online STR memory be recycled visited unallocated space what is the solution

CodePudding user response:

refer to 6th floor at the moment I'm at home to feed pigs response:
and redefine it doesn't matter, I have a still not check it online STR memory are recycled, visited the unallocated space is there a way to solve it

In the first will not be recycled
  • Related