-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_bigint_add.c
41 lines (38 loc) · 1.46 KB
/
ft_bigint_add.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bigint_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ylagtab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/27 22:06:51 by ylagtab #+# #+# */
/* Updated: 2020/02/27 22:06:53 by ylagtab ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_bigint *ft_bigint_add(t_bigint *a, t_bigint *b)
{
unsigned int i;
unsigned int res_len;
t_bigint *res;
if (a == NULL || b == NULL)
return (NULL);
res_len = ft_max(a->length, b->length) + 1;
if ((res = ft_bigint_new(res_len)) == NULL)
return (NULL);
i = 0;
while (i < res_len)
{
res->digits[i] += i < a->length ? a->digits[i] : 0;
res->digits[i] += i < b->length ? b->digits[i] : 0;
if (res->digits[i] > 9)
{
res->digits[i + 1]++;
res->digits[i] %= 10;
}
i++;
}
i = 0;
res->length -= res->digits[res->length - 1] == 0 ? 1 : 0;
return (res);
}