-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils1.c
85 lines (76 loc) · 2.06 KB
/
utils1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oscarlo <oscarlo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/01 21:52:52 by oscarlo #+# #+# */
/* Updated: 2021/10/02 17:35:48 by oscarlo ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractal.h"
int is_numeric(char c)
{
if (c >= '0' && c <= '9')
return (1);
else
return (0);
}
void assign_and_sign(int *aux, double *sign, int *i, char *src)
{
while (src[*i] == ' ' || (src[*i] >= 9 && src[*i] <= 13))
*i = *i + 1;
*aux = 0;
*sign = 1;
if (src[*i] == '-' || src[*i] == '+')
{
if (src[*i] == '-')
*sign = -1;
*i = *i + 1;
}
}
double ft_atof(char *src)
{
int i;
int aux;
double res;
double sign;
if (!src || !src[0])
return (exit_program("[ -- Incorrect numeric value -- ]\n"));
i = 0;
res = 0;
assign_and_sign(&aux, &sign, &i, src);
while ((src[i] == '.' && !aux) || is_numeric(src[i]))
{
if (src[i] == '.' || aux || !src[i + 1])
aux++;
if (src[i] != '.')
res = res * 10 + (src[i] - '0');
i++;
}
if (src[i])
return (exit_program("[ -- Incorrect numeric value -- ]\n"));
res = (double) res * pow(10, -(--aux));
return (res * sign);
}
t_cmplx init_cmplx(double re, double im)
{
t_cmplx complex;
complex.re = re;
complex.im = im;
return (complex);
}
void *ft_alloc(int size)
{
void *res;
unsigned char *it;
res = malloc(size);
if (res)
{
it = (unsigned char *)res;
while (size--)
*it++ = '\0';
}
return (res);
}