-
Notifications
You must be signed in to change notification settings - Fork 2
/
operations.c
82 lines (71 loc) · 2.12 KB
/
operations.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
** EPITECH PROJECT, 2020
** all operations
** File description:
** operations
*/
#include "./include/my.h"
#include <stdlib.h>
char *addition_initialization(char **av);
char *multiplication(char *small_nbr, char *big_nbr, int base);
char *division_initialization(char **av, int base);
char *modulo_initialization(char **av, int base);
char *my_add(char *nbr_1, char *nbr_2, int base)
{
char **nbrs = malloc(sizeof(char *) * 3);
nbrs[0] = nbr_1;
nbrs[1] = nbr_2;
nbrs[2] = base;
return (addition_initialization(nbrs));
}
char *my_sub(char *nbr_1, char *nbr_2, int base)
{
char **nbrs = malloc(sizeof(char *) * 3);
nbrs[0] = nbr_1;
nbrs[1] = my_add_char(nbr_2, 1, '-');
nbrs[2] = base;
return (addition_initialization(nbrs));
}
char *my_mul(char *nbr_1, char *nbr_2, int base)
{
int negative = ((nbr_1[0] == '-') + (nbr_2[0] == '-'));
char *result;
nbr_1 += (nbr_1[0] == '-');
nbr_2 += (nbr_2[0] == '-');
if ((my_nbrcmp(nbr_1, nbr_2)) == 1)
my_strswap(&nbr_1, &nbr_2);
result = multiplication(nbr_1, nbr_2, base);
return (my_add_char(result, (negative == 1), '-'));
}
char *my_div(char *nbr_1, char *nbr_2, int base)
{
char **nbrs = malloc(sizeof(char *) * 2);
int negative = ((nbr_1[0] == '-') + (nbr_2[0] == '-'));
char *result;
if (nbr_2[0] == '0' && nbr_2[1] == '\0') {
my_putstr_error(SYNTAX_ERROR_MSG);
exit(EXIT_USAGE);
}
nbrs[0] = nbr_1;
nbrs[1] = nbr_2;
nbrs[0] += (nbrs[0][0] == '-');
nbrs[1] += (nbrs[1][0] == '-');
result = division_initialization(nbrs, base);
return (my_add_char(result, (negative == 1), '-'));
}
char *my_mod(char *nbr_1, char *nbr_2, int base)
{
char **nbrs = malloc(sizeof(char *) * 2);
int negative = ((nbr_1[0] == '-'));
char *result;
if (nbr_2[0] == '0' && nbr_2[1] == '\0') {
my_putstr_error(SYNTAX_ERROR_MSG);
exit(EXIT_USAGE);
}
nbrs[0] = nbr_1;
nbrs[1] = nbr_2;
nbrs[0] += (nbrs[0][0] == '-');
nbrs[1] += (nbrs[1][0] == '-');
result = modulo_initialization(nbrs, base);
return (my_add_char(result, (negative == 1), '-'));
}