/* =========================================== filename: big_integer_addition.c revision: January 20, 2006 C Program to read two big integers from a file mydata.txt and write the sum to the console. Number of digits cannot must be less than MAX_DIGITS which is set to be 1,000,000 Input file has restricted format: First integer begins on first line. If it carries over to the next line, this is indicated with a backslash character. The last line of the first integer does not have a trailing backslash. Exactly one line is skipped between the two integers. Then the second integer begins. Same rules apply for formatting the second integer. ========================================== */ #include #include #include #define MAX_LINE 100 #define MAX_DIGITS 1000000 int main () { int i,j,m,n; int empty_line; int current_integer = 1; int length_one = 0; int length_two = 0; int length_three = 0; int carry; int offset; char mychar; char last_char; char p; char mystring[150]; int *bigint1; int *bigint2; int *bigint3; FILE *input_file; bigint1 = (int *)malloc((MAX_DIGITS+1)*sizeof(int)); bigint2 = (int *)malloc((MAX_DIGITS+1)*sizeof(int)); bigint3 = (int *)malloc((MAX_DIGITS+1)*sizeof(int)); if (bigint1 == NULL || bigint2 == NULL || bigint3 == NULL) { printf("Problem allocating memory for big integers.\n"); exit(1); } else { printf("Memory for big integers successfully allocated.\n"); } // Open file for reading if ((input_file = fopen("mydata.txt","r")) == NULL) { printf("Unable to read input file.\n"); exit(1); } else { printf("Input file successfully opened.\n"); } i = 0; current_integer = 1; last_char = '\n'; while ((mychar=getc(input_file)) != EOF) { if(mychar != '\\' && mychar != '\n') { if (current_integer == 1) { bigint1[i] = mychar-'0'; length_one++; i++; } if (current_integer == 2) { bigint2[i] = mychar-'0'; length_two++; i++; } } if (mychar == '\n' && last_char == '\n' && current_integer == 1) { current_integer = 2; bigint1[i]='\0'; i = 0; } last_char = mychar; } bigint2[i]='\0'; m = fclose(input_file); if (m != 0) { printf("Problem closing input file.\n"); exit(1); } else { printf("Input file successfully closed.\n"); } printf("Length_one = %d\n", length_one); printf("Length_two = %d\n", length_two); printf("\n"); printf("The first integer is:\n"); i = 0; while (length_one - i > 60) { for (j = 0; j < 60; j++) { printf("%d", bigint1[i]); i++; } printf("\\\n"); } while (i < length_one) { printf("%d", bigint1[i]); i++; } printf("\n"); printf("\n"); printf("The second integer is:\n"); i = 0; while (length_two - i > 60) { for (j = 0; j < 60; j++) { printf("%d", bigint2[i]); i++; } printf("\\\n"); } while (i < length_two) { printf("%d", bigint2[i]); i++; } printf("\n"); printf("\n"); for (i = 0, j = length_one - 1; i < j; i++, j--) { p = bigint1[i]; bigint1[i] = bigint1[j]; bigint1[j] = p; } for (i = 0, j = length_two - 1; i < j; i++, j--) { p = bigint2[i]; bigint2[i] = bigint2[j]; bigint2[j] = p; } i=0; carry = 0; while (i < length_one && i < length_two) { bigint3[i] = (bigint1[i] + bigint2[i]+carry)%10; carry = (bigint1[i] + bigint2[i]+carry)/10; i++; } if (length_one == length_two) { length_three = length_one; if (carry > 0) { bigint3[length_one] = carry; length_three++; } } if (length_one > length_two) { length_three = length_one; while (i < length_one) { bigint3[i] = (bigint1[i] + carry)%10; carry = (bigint1[i] + carry)/10; i++; } if (carry > 0) { bigint3[length_one] = carry; length_three++; } } if (length_two > length_one) { length_three = length_two; while (i < length_two) { bigint3[i] = (bigint2[i] + carry)%10; carry = (bigint2[i] + carry)/10; i++; } if (carry > 0) { bigint3[length_two] = carry; length_three++; } } for (i = 0, j = length_three - 1; i < j; i++, j--) { p = bigint3[i]; bigint3[i] = bigint3[j]; bigint3[j] = p; } printf("The sum of these two integers is:\n"); i = 0; while (length_three - i > 60) { for (j = 0; j < 60; j++) { printf("%d", bigint3[i]); i++; } printf("\\\n"); } while (i < length_three) { printf("%d", bigint3[i]); i++; } printf("\n"); return(0); }