#include <iostream>
using namespace std;
#define array_size 50
//enter a big number, and store it as a string into an array ch,
//the size is the numbers of char.
void inputnumbers(char ch[], int& size);
//reverse the elements of the array ch.
void reversearray(char ch[], int size);
//adding two big numbers, and the result will be stored in the array ch3,
//and return the size of the array ch3.
void computeadding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3);
//show the adding result.
void displayresult(char ch[], int size);
int main()
{
char ch1[array_size], ch2[array_size], result[array_size];
int size1 = 0, size2 = 0, resultsize = 0;
cout << "enter the first big number, ending with an enter:" << endl;
inputnumbers(ch1, size1);
cout << "enter the second big number, ending with an enter:" << endl;
inputnumbers(ch2, size2);
reversearray(ch1, size1);
reversearray(ch2, size2);
computeadding(ch1, size1, ch2, size2, result, resultsize);
displayresult(result, resultsize);
system("pause");
return 0;
}
//function inputnumbers
void inputnumbers(char ch[], int& size)
char next;
cin.get(next);
while (next != '\n' && size < array_size)
{
ch[size++] = next;
cin.get(next);
}
}//inputnumbers
//function reversearray
void reversearray(char ch[], int size)
int i = 0, j = size-1;
char temp;
while (i <= j)
temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
i ++;
j --;
}//end reversearray function
//function computeadding's definition
void computeadding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3)
int i,
tmp, //as the temporary sum of two array elements.
carrybit = 0; //the carry-bit is initialized to zero
for (i = 0; i < size1 && i < size2; i++)
tmp = (ch1[i]-'0') + (ch2[i]-'0') + carrybit;
ch3[i] = tmp % 10 + '0';
carrybit = tmp/10;
while ( i<size1 ) { //if the array ch1 has more bits, execute this while loop.
tmp = (ch1[i] - '0') + carrybit;
carrybit = tmp / 10;
i ++;
while ( i < size2 ){
tmp = (ch2[i] - '0') + carrybit;
ch3[i] = tmp % 10 +'0';
if( carrybit)
ch3[i] = carrybit + '0';
i ++;
}
ch3[i] = '\0';
size3 = i;
}//end reversearray
//function displayresult
void displayresult(char ch[], int size)
reversearray(ch, size);//make the number to be normal
cout << "the adding result is:" ;
for (int i = 0; i < size; i++)
cout << ch[i] ;
cout << endl;