-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.c
35 lines (32 loc) · 759 Bytes
/
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
#include "monty.h"
/**
* _mod - computes the rest of the division of the
* second top element of the stack by the top element
* @stack: double pointer to head node
* @line_number: line number of file where instruction was parsed
*/
void _mod(stack_t **stack, unsigned int line_number)
{
int n1, n2;
stack_t *tmp;
if (*stack && (*stack)->next)
{
if ((*stack)->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
tmp = *stack;
n1 = tmp->n;
n2 = tmp->next->n;
tmp->next->n = n2 % n1;
tmp->next->prev = NULL;
*stack = tmp->next;
free(tmp);
return;
}
fprintf(stderr, "L%d: can't mod, stack too short\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}