From 58d3d29557c6ac02c9da175f4f96812d7dc49a6a Mon Sep 17 00:00:00 2001 From: nihonium Date: Thu, 17 Feb 2022 23:24:00 +0300 Subject: [PATCH] big_num_sum --- numbers/bignum_bin_sum.c | 81 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 numbers/bignum_bin_sum.c diff --git a/numbers/bignum_bin_sum.c b/numbers/bignum_bin_sum.c new file mode 100644 index 0000000..2ca8bd7 --- /dev/null +++ b/numbers/bignum_bin_sum.c @@ -0,0 +1,81 @@ +#include +#include + +int main() { + char a[1001] = ""; + char b[1001] = ""; + char res[1002] = "" + char null = 0; + int len, len_; + int i, j; + + char cf = 0; + + scanf("%s", a); + scanf("%s", b); + + j = 0; + if (a[0] == '0') + null = 1; + if (null) { + for (i = 0; (i <= (int)strlen(a)) && (a[i] != '\0'); ++i) { + + if (!null || a[i] != '0') { + res[j] = a[i]; + ++j; + null = 0; + } + } + strcpy(a, res); + } + if (null) { + a[0] = '0'; + a[1] = '\0'; + } + + null = 0; + j = 0; + if (b[0] == '0') + null = 1; + if (null) { + for (i = 0; (i <= (int)strlen(b)) && (b[i] != '\0'); ++i) { + if (!null || b[i] != '0') { + res[j] = b[i]; + ++j; + null = 0; + } + } + strcpy(b, res); + } + if (null) { + b[0] = '0'; + b[1] = '\0'; + } + if (strlen(a) < strlen(b)) { + strcpy(res, a); + strcpy(a, b); + strcpy(b, res); + } + len = strlen(a); + len_ = strlen(b); + strcpy(res, b); + for (i = 0; i < len-len_; ++i) { + b[i] = '0'; + } + for (i = 0; i < len_+1; ++i) { + b[len-i] = res[len_-i]; + } + + for (i = len-1; i >= 0; --i) { + res[i] = '0' + (a[i] + b[i] - 2 * '0' + cf) % 2; + cf = (int)((a[i] + b[i] - 2 * '0' + cf) / 2); + } + + if (cf) { + putchar('1'); + printf("%s", res); + return 0; + } + printf("%s", res); + return 0; +}