/******************************************** * Rollin92 * * http://www.warpdworld.com * * The Program accepts two integers * * and adds them together. It can * * handle up to 300 digits. * ********************************************/ #include #include int main() { //Variable declaration char buffer[300]; int* num1; int* num2; int* answer; int num1_length = 0; int num2_length = 0; int longest = 0; int i = 0; int j = 0; int k = 0; int carry = 0; int sum = 0; //Prompt user for first number cout << "Enter the first integer, up to 300 digits: "; cin.getline(buffer,sizeof(buffer)); //Dynamically allocate space for number num1 = new int[strlen(buffer)]; num1_length = strlen(buffer); //Copy number into integer array for(i = 0; i < strlen(buffer); i++) { //I used - 48 to change the number from its ascii value to the actual number //Probably could use atoi instead, but this works num1[i] = (buffer[i] - 48); } //Get second number from user cout << endl << endl << "Enter the second integer, up to 300 digits: "; cin.getline(buffer,sizeof(buffer)); //Dynamically allocate space for number num2 = new int[strlen(buffer)]; num2_length = strlen(buffer); //Copy number into integer array for(i = 0; i < strlen(buffer); i++) { num2[i] = (buffer[i] - 48); } //if statements used to see which number is longest //The answer can't be longer than longest number + 1 //So I used this to figure out how long to make the answer array if((num1_length) > (num2_length)) { answer = new int[num1_length + 1]; //k doesn't get a plus 1 since arrays start at 0 k = num1_length; longest = num1_length + 1; } else { answer = new int[num2_length + 1]; //k doesn't get a plus 1 since arrays start at 0 k = num2_length; longest = num2_length + 1; } //Set i and j to the length of numbers 1 and 2 //Use - 1 since array starts at 0 i = (num1_length - 1); j = (num2_length - 1); //While loop for the addition of the numbers //Add them one at a time while((i >= 0) && (j >= 0)) { //Add the numbers and any carry together and //hold in sum sum = num1[i] + num2[j] + carry; //If no carry if(sum < 10) { answer[k] = sum; carry = 0; } //If there is a carry else { answer[k] = (sum - 10); carry = 1; } //Decrement variables i--; j--; k--; } //This runs if number2 is longer than number 1 //It dumps the rest of number2 into the answer //array. It does take care of the carry bit also if((i <= 0) && (j >= 0)) { while(j >= 0) { sum = num2[j] + carry; if(sum < 10) { answer[k] = sum; carry = 0; } else { answer[k] = (sum - 10); carry = 1; } j--; k--; } } //This runs if number1 is longer than number 2 //It dumps the rest of number1 into the answer //array. It handles the carry bit if there is one. if((i >= 0) && (j <= 0)) { while(i >= 0) { sum = num1[i] + carry; if(sum < 10) { answer[k] = sum; carry = 0; } else { answer[k] = (sum - 10); carry = 1; } i--; k--; } } //Part of display cout << endl << endl << "Answer:"; //This runs if there is a carry bit left over //but both numbers have already been added //It puts the carry into the front of the //answer array. if((i <= 0) && (j <= 0) && (carry == 1)) { answer[k] = carry; //Displays the answer for(i = 0; i < longest; i++) { cout << answer[i]; } } //If there isn't a carry then this runs else { //Displays the answer //It skips the first element of the //answer array since that element is //reserved for the carry that could be //left over at the end of the addition for(i = 1; i < longest; i++) { cout << answer[i]; } } cout << endl << endl; return 0; }//End of main /* Sample Run: Enter the first integer, up to 300 digits: 999999999999999999999999999999 Enter the second integer, up to 300 digits: 999999999999999999999999999999 Answer:1999999999999999999999999999998 Press any key to continue */