forked from FearGod12/monty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiv_func.c
35 lines (30 loc) · 772 Bytes
/
div_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
35
#include "monty.h"
/**
* div_func - divides the second element of the stack
* by the top element to store the result in the second stack
* element so the stack is one element shorter.
* @stack: pointer to the top of the stack
* @line_number: line number of code from file read
*/
void div_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 div, 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);
}