You can use two strings str1 and str2 to add two large numbers. The reference program is as follows:
# Include <iostream>
# Include <string>
# Include <stack>
Using namespace std;
Int main (void)
{
String str1, str2;
Stack <int> stack_ex;
Int str1_length, str2_length;
Int carry = 0; // carry Control
Int tmp;
While (cin> str1> str2)
{
Str1_length = str1.size ();
Str2_length = str2.size ();
Int I = str1_length-1;
Int j = str2_length-1;
For (; I> = 0 & j> = 0; I --, j --)
{
Tmp = str1 [I]-0 + str2 [j]-0 + carry;
If (tmp <10)
{
Carry = 0;
Stack_ex.push (tmp );
}
Else
{
Carry = 1;
Stack_ex.push (tmp % 10 );
}
}
If (str1_length> = str2_length)
{
For (; I> = 0; I --)
{
Tmp = str1 [I]-0 + carry;
If (tmp <10)
{
Carry = 0;
Stack_ex.push (tmp );
}
Else
{
Carry = 1;
Stack_ex.push (tmp % 10 );
}
}
}
Else
{
For (; j> = 0; j --)
{
Tmp = str2 [j]-0 + carry;
If (tmp <10)
{
Carry = 0;
Stack_ex.push (tmp );
}
Else
{
Carry = 1;
Stack_ex.push (tmp % 10 );
}
}
}
While (! Stack_ex.empty ())
{
Cout <stack_ex.top ();
Stack_ex.pop ();
}
Cout <endl;
}
Return 0;
}