forked from FearGod12/monty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_func.c
34 lines (30 loc) · 777 Bytes
/
mod_func.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
#include "monty.h"
/**
* mod_func - compute remainder of the division of second element
* by the top element to store the result in the second stack
* the stack will be one element shorter.
* @stack: pointer to the top of the stack
* @line_number: line number of code from file read
*/
void mod_func(stack_t **stack, unsigned int line_number)
{
stack_t *top, *temp = *stack;
int num = 0;
(void) line_number;
if (!stack || !(*stack) || temp->next == NULL)
{
fprintf(stderr, "L%d: can't mod, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if (temp->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
top = temp;
num = temp->n;
temp = temp->next;
temp->n = temp->n % num;
*stack = temp;
free(top);
}