From 3472194b082acb961480a535bdbbb21f6d478fe0 Mon Sep 17 00:00:00 2001 From: nihonium Date: Sat, 19 Feb 2022 16:25:38 +0300 Subject: [PATCH] elong --- numbers/bignum_bin_sum.c | 2 +- numbers/elong_add.c | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 numbers/elong_add.c diff --git a/numbers/bignum_bin_sum.c b/numbers/bignum_bin_sum.c index 2ca8bd7..fab49e4 100644 --- a/numbers/bignum_bin_sum.c +++ b/numbers/bignum_bin_sum.c @@ -4,7 +4,7 @@ int main() { char a[1001] = ""; char b[1001] = ""; - char res[1002] = "" + char res[1002] = ""; char null = 0; int len, len_; int i, j; diff --git a/numbers/elong_add.c b/numbers/elong_add.c new file mode 100644 index 0000000..6ec34b5 --- /dev/null +++ b/numbers/elong_add.c @@ -0,0 +1,57 @@ +#include +#include +#define N 100 + +struct _Decimal { + char a[N]; // number is a[0]*10^0 + a[1]*10^1 + ..+ a[n]*10^n + unsigned int n; +}; +typedef struct _Decimal Decimal; + +Decimal zero = {{0}, 0}; + +Decimal add(Decimal a, Decimal b) { + Decimal c; + char cf = 0; + unsigned int i; + + if (a.n < b.n) { + c = a; + a = b; + b = c; + } + c.n = a.n; + + for (i = 0; i <= b.n; ++i) { + c.a[i] = (a.a[i] + b.a[i] + cf) % 10; + cf = (int)((a.a[i] + b.a[i] + cf) / 10); + } + + for (; i <= a.n; ++i) { + c.a[i] = (a.a[i] + cf) % 10; + cf = (int)((a.a[i] + cf) / 10); + } + if (cf) { + ++c.n; + c.a[i] = cf; + } + return c; + +} + +void print(Decimal x) { + for (int i = x.n; i >= 0; --i) + printf("%d", x.a[i]); + printf("\n"); +} + +int main(){ + Decimal b = {{9, 9, 9}, 2}; + Decimal a = {{2}, 0}; + + Decimal res; + + res = add(a, b); + print(res); + return 0; +}