C语言中基于数组的大整数加法实现问题求助
Hey there! Let's work through your C program challenges one by one. First, let's restate your core requirements clearly:
- Store each digit of a positive integer in an array (e.g., 1234 becomes
a[0]=1, a[1]=2, a[2]=3, a[3]=4) - Convert character input digits to integers
- Implement vertical addition for two positive integers (each up to 20 digits)
- Store the result in a 20-element array and output it; if the result exceeds 20 digits, output
"Integer Overflow."
First, Fix Your Existing Code's Small Issues
Your current code has a couple of syntax/logic bugs that need addressing first:
- The
whileloop uses comma operators incorrectly — initialize variables before the loop or use aforloop instead. scanf_s("%c")will read whitespace (spaces/newlines) from input, which will corrupt your digit characters. Usescanf_s(" %c", &number_1[a])(note the leading space to skip whitespace).
Here's the corrected input section to start with:
#include <stdio.h> int main() { int max_1, max_2; char number_1[100], number_2[100]; int digit_arr1[20] = {0}, digit_arr2[20] = {0}; // Arrays for integer digits int result_arr[21] = {0}; // Extra space for possible carry-over int i, carry = 0, result_len; printf("\nEnter the size of the first integer you wish to add:\n"); scanf("%d", &max_1); printf("\nEnter the first integer one number at a time separated by spaces:\n"); for (i = 0; i < max_1; i++) { scanf_s(" %c", &number_1[i]); // Skip whitespace before reading character digit_arr1[i] = number_1[i] - '0'; // Convert char to int here } printf("\nEnter the size of the second integer you wish to add:\n"); scanf("%d", &max_2); printf("\nEnter the second integer one number at a time separated by spaces:\n"); for (i = 0; i < max_2; i++) { scanf_s(" %c", &number_2[i]); digit_arr2[i] = number_2[i] - '0'; // Convert char to int }
Solve Your Key Problems
1. Converting Character Digits to Integers
This is simpler than you think! Digits '0' to '9' have consecutive ASCII values. Subtract the ASCII value of '0' from any digit character to get its integer equivalent:
int int_digit = char_digit - '0';
For example: '5' - '0' equals 5, '9' - '0' equals 9. This is already implemented in the corrected input code above.
2. Implementing Vertical Addition & Storing Results
Vertical addition works from the least significant digit (rightmost) to the most. We need to iterate from the end of both digit arrays to align digits properly:
- Align digits by starting from the end of each array
- Add corresponding digits plus any carry from the previous step
- Store the remainder (sum % 10) in the result array
- Update the carry (sum / 10)
- If carry remains after processing all digits, add it to the front of the result
Here's the code for this logic:
// Find the maximum length of the two input numbers int max_len = (max_1 > max_2) ? max_1 : max_2; // Add digits from right to left (start from the end of each array) for (i = 0; i < max_len; i++) { // Use 0 if we've passed the length of one array int d1 = (i < max_1) ? digit_arr1[max_1 - 1 - i] : 0; int d2 = (i < max_2) ? digit_arr2[max_2 - 1 - i] : 0; int sum = d1 + d2 + carry; result_arr[i] = sum % 10; carry = sum / 10; } // Add remaining carry to the result if needed if (carry == 1) { result_arr[max_len] = 1; result_len = max_len + 1; } else { result_len = max_len; }
3. Checking for Integer Overflow
Simply check if the final result length exceeds 20. If yes, output the overflow message. Otherwise, print the result (note: the result array stores digits from right to left, so we print from the end to the start):
// Check for overflow if (result_len > 20) { printf("Integer Overflow.\n"); } else { printf("\nThe sum is: "); // Print from most significant to least significant digit for (i = result_len - 1; i >= 0; i--) { printf("%d", result_arr[i]); } printf("\n"); } return 0; }
Key Notes for Your Learning
- We used
result_arr[21]to handle possible carry-over, so we don't overflow during calculation before checking the final length - The leading space in
scanf_s(" %c")skips any leftover whitespace (spaces, newlines) in the input buffer - The result array stores digits in reverse order (right to left), so we reverse it when printing to get the correct number
内容的提问来源于stack exchange,提问作者ayetman96

