I found a code that allows you to add two long numbers up to 100 characters long. How to make it so that you can add two long numbers up to 10,000 characters long?

#include<stdio.h> #include<stdlib.h> #include<string.h> #include <conio.h> #define MAX_NUMBER_LEN 126 void input_big_number(char *number); void output_big_number(char *number); void add_big_numbers(char* sum, const char *a, const char* b); int main() { char a[MAX_NUMBER_LEN]; char b[MAX_NUMBER_LEN]; char c[MAX_NUMBER_LEN]; memset(a, 0, MAX_NUMBER_LEN); memset(b, 0, MAX_NUMBER_LEN); memset(c, 0, MAX_NUMBER_LEN); input_big_number(a); input_big_number(b); add_big_numbers(c, a, b); output_big_number(c); printf("\n"); getch(); return 0; } void input_big_number(char *number) { char buffer[MAX_NUMBER_LEN]; char i, j, k; fgets(buffer, MAX_NUMBER_LEN, stdin); if (buffer[strlen(buffer) - 1] != '\n') { exit(EXIT_FAILURE); } buffer[strlen(buffer) - 1] = '\0'; k = strlen(buffer) - 1; for(i = k; i >= 0 ; i--) { number[k - i] = buffer[i] - '0'; } } void output_big_number(char *number) { char i, j, k; for(k = MAX_NUMBER_LEN - 1; number[k] == 0 && k > 0; k--); for(i = k; i >= 0; i--) { printf("%d", number[i]); } } void add_big_numbers(char* sum, const char *a, const char* b) { char carry = 0; char w = 0; char t; char i, j, k; for (t = 0; t <= MAX_NUMBER_LEN; t++) { w = a[t] + b[t] + carry; carry = w / 10; sum[t] = w % 10; } } 
  • one
    to register in #define MAX_NUMBER_LEN 126 correct length? The code is well formatted. - KoVadim
  • @KoVadim, about fine, you are bent. @MahovIV, the main thing in local formatting is (almost like in FORTRAN IV) 4 spaces before each line of code. And it is desirable to replace the tabulation in it with spaces. - avp
  • there is also a case when even 4 spaces do not help at first. This is when before this list. - KoVadim
  • @avp and indent (for example indent -linux) is no longer quoted? - alexlz
  • @alexlz is a great tool, but you still have to add spaces and untabify. - avp

1 answer 1

If possible, don’t reinvent the wheel; apply a ready-made and well-functioning GMP library.


If you still want to do everything yourself (for example, to practice), go to the dynamic size of the numbers - that is, this type of data:

 struct bignunm_t { size_t number_of_digits; unsigned char* digits; }; 

Thus, you will have a variable number of digits.

Operations, of course, will become a bit more complicated:

 bignum_t add(bignum_t l, bignum_t r) { bignum_t result; result.number_of_digits = max(l.number_of_digits, r.number_of_digits) + 1; result.digits = malloc(result.number_of_digits * sizeof(unsigned char)); size_t common = min(l.number_of_digits, r.number_of_digits); for (size_t i = 0; i < common; i++) result.digits[i] = l.digits[i] + r.digits[i]; for (size_t i = common; i < l.number_of_digits; i++) result.digits[i] = l.digits[i]; for (size_t i = common; i < r.number_of_digits; i++) result.digits[i] = r.digits[i]; result.digits[result.number_of_digits - 1] = 0; // fixup for (size_t i = 0; i < result.number_of_digits - 1; i++) { if (result.digits[i] > BASE) { result.digits[i] -= BASE; result.digits[i+1]++; } } }