-
Notifications
You must be signed in to change notification settings - Fork 0
/
infin_div.c
113 lines (103 loc) · 2.89 KB
/
infin_div.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
** EPITECH PROJECT, 2021
** delivery
** File description:
** infindiv
*/
#include <unistd.h>
#include <stdlib.h>
#include "include/my.h"
#include "include/bistromatic.h"
#include <stdio.h>
char *infinadd(char const *s1, char const *s2);
int compare_char(char const *s1, char const *s2);
char *divfaster(char *f, char *temp, char *add, int j);
char *check_div(char *final, char *diviseur)
{
if (diviseur[0] == '-' && diviseur[1] == '0' && diviseur[2] == '\0') {
write(2, "syntax error", 13);
exit(EXIT_USAGE);
}
if (diviseur[0] == '-' && diviseur[1] == '1' && diviseur[2] == '\0')
return final;
if (my_strlen(final) < my_strlen(&diviseur[1]))
return "0";
if (my_strlen(final) == my_strlen(&diviseur[1])) {
if (compare_char(final, &diviseur[1]) == 2)
return 0;
}
return NULL;
}
char *division(char *final, char *diviseur)
{
char *temp = malloc(sizeof(char) * my_strlen(final) + 1);
char *add = malloc(sizeof(char) * my_strlen(final) + 1);
int j;
int a = (my_strlen(final) - (my_strlen(diviseur) - 1) - 1);
if (check_div(final, diviseur) != NULL)
return check_div(final, diviseur);
for (j = 0; j != my_strlen(diviseur); j++)
temp[j] = diviseur[j];
for (; j <= (((my_strlen(diviseur)) + a)); j++) {
temp[j] = '0';
temp[j + 1] = '\0';
add[(j - my_strlen(diviseur) + 1)] = '0';
add[(j - my_strlen(diviseur) + 2)] = '\0';
}
add[0] = '1';
return divfaster(final, temp, add, j);
}
char *infindiv(char const *s1, char const *s2)
{
char *final = malloc(sizeof(char) * (my_strlen(s1) + 2));
char *diviseur = malloc(sizeof(char) * (my_strlen(s2) + 3));
int i = 0;
int j = 0;
for (int i = 0; i != my_strlen(s2) + 2; i++)
diviseur[i] = '\0';
for (j = 0; s1[j] != '\0'; j++) {
final[j] = s1[j];
final[j + 1] = '\0';
}
diviseur[i] = '-';
for (i = 0; s2[i] != '\0'; i++)
diviseur[i + 1] = s2[i];
diviseur[i + 2] = '\0';
return (division(final, diviseur));
}
int check_sign_div(char const *s1, char const *s2)
{
int a;
if (s1[0] != '-' && s2[0] != '-')
a = 0;
if (s1[0] == '-' && s2[0] != '-')
a = 1;
if (s2[0] == '-' && s1[0] != '-')
a = 2;
if (s1[0] == '-' && s2[0] == '-')
a = 3;
return a;
}
char *sign_div(char const *s1, char const *s2)
{
char *temp = malloc(sizeof(char) * (my_strlen(s1) + 1));
char *result = malloc(sizeof(char) * (my_strlen(s1) + 1));
int i = 0;
int a = 0;
int b = 0;
if (check_sign_div(s1, s2) == 1 || check_sign_div(s1, s2) == 2) {
result[i] = '-';
i++;
}
if (s1[0] == '-')
a++;
if (s2[0] == '-')
b++;
temp = infindiv(&s1[a], &s2[b]);
for (int j = 0; temp[j] != '\0'; j++) {
result[i] = temp[j];
i++;
}
result[i] = '\0';
return result;
}