-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiv_mod.c
65 lines (61 loc) · 1.3 KB
/
div_mod.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
#include "monty.h"
#include <ctype.h>
/**
* divid - divides the second top element of the stack by the top
* element of the stack.
* @ck: double pointer to head
* @count: counter
* Return: void
*/
void divid(stack_t **ck, unsigned int count)
{
stack_t *currt;
int flip;
if (!ck || !(*ck) || !(*ck)->next)
{
fprintf(stderr, "L%u: can't div, stack too short\n", count);
free_stack();
exit(EXIT_FAILURE);
}
currt = *ck;
if (currt->n == 0)
{
fprintf(stderr, "L%u: division by zero", count);
free_stack();
exit(EXIT_FAILURE);
}
flip = currt->next->n / currt->n;
pop(ck, count);
pop(ck, count);
add_node(ck, flip);
}
/**
* mod - computes the rest of the division of the second top element
* of the stack by the top element of the stack
* element of the stack.
* @stack: double pointer
* @count: count unsigned int
* Return: void
*/
void mod(stack_t **stack, unsigned int count)
{
stack_t *currt;
int flip;
if (!(*stack) || !stack || !(*stack)->next)
{
fprintf(stderr, "L%u: can't mod, stack too short\n", count);
free_stack();
exit(EXIT_FAILURE);
}
currt = *stack;
if (currt->n == 0)
{
fprintf(stderr, "L%u: division by zero\n", count);
free_stack();
exit(EXIT_FAILURE);
}
flip = currt->next->n % currt->n;
pop(stack, count);
pop(stack, count);
add_node(stack, flip);
}