-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnum_funct_3.c
137 lines (130 loc) · 2.49 KB
/
num_funct_3.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "holberton.h"
/**
* div_by_10_exp - Divides a number by a power of 10
* @num: The number to divide
* @n: The power of 10
* @can_free: Specifies whether the given number can be freed
*
* Return: The quotient of the number and the power of 10, otherwise NULL
*/
char *div_by_10_exp(char *num, unsigned short n, char can_free)
{
char *result;
int i, j, dec_pos, new_pos, size, len;
if (n == 0)
return (num);
len = str_len(num);
dec_pos = index_of(num, '.');
if (dec_pos < 0)
size = len - n > 0 ? len + 1 : (len - n == 0 ? len + 2 : n - len + len + 2);
else if (dec_pos > 0)
size = dec_pos - n > 0 ? len
: (dec_pos - n == 0 ? len + 1 : len + (n - dec_pos + 1));
else
size = len + n + 1;
new_pos = dec_pos < 0 ? size - n - 1
: (dec_pos > 0 ? size - (len - dec_pos + n) : 1);
result = malloc(sizeof(char) * (size + 1));
if (result)
{
mem_set(result, size, '0');
j = len - 1;
for (i = size - 1; i >= 0; i--)
{
if (i == new_pos)
*(result + i) = '.';
i -= (i == new_pos ? 1 : 0);
if (*(num + j) == '.')
j--;
if (*(num + j) != '.' && j >= 0)
*(result + i) = *(num + j);
j--;
}
*(result + size) = '\0';
if (can_free)
free(num);
}
return (result);
}
/**
* two_exp - Computes 2 raised to the given power
* @n: The power
*
* Return: The value of 2 index n, otherwise NULL
*/
char *two_exp(short n)
{
char *result, *pow_b5;
short i;
result = malloc(sizeof(char) * (2));
if (result)
{
*(result + 0) = '1';
*(result + 1) = '\0';
if (n > 0)
{
for (i = 0; i < n; i++)
{
pow_b5 = malloc(sizeof(char) * (2));
if (pow_b5)
{
*(pow_b5 + 0) = '2';
*(pow_b5 + 1) = '\0';
result = mul_int(pow_b5, result, TRUE);
}
}
}
else if (n < 0)
{
pow_b5 = five_exp(0 - n);
result = div_by_10_exp(pow_b5, 0 - n, TRUE);
}
}
return (result);
}
/**
* five_exp - Computes 5 raised to the given positive power
* @n: The power
*
* Return: The value of 5 index n, otherwise NULL
*/
char *five_exp(unsigned short n)
{
char *result, *base;
unsigned short i;
result = malloc(sizeof(char) * (2));
if (result)
{
result[0] = '1';
result[1] = '\0';
for (i = 0; i < n; i++)
{
base = malloc(sizeof(char) * (2));
if (base)
{
base[0] = '5';
base[1] = '\0';
result = mul_int(base, result, TRUE);
}
}
}
return (result);
}
/**
* two_pexp - Computes 2 raised to the given positive power
* @n: The positive index
*
* Return: The value of 2 exponent n
*/
unsigned int two_pexp(unsigned int n)
{
unsigned int result = 1;
unsigned int i;
for (i = 0; i < n; i++)
result *= 2;
return (result);
}