-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiv.c
35 lines (33 loc) · 807 Bytes
/
div.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"
#include <stdlib.h>
#include <stdio.h>
/**
* _div - divides the first two numbers
* on the stack
* @stack: stack to print
* @line_number: current line number
* Return: void
*/
void _div(stack_t **stack, unsigned int line_number)
{
stack_t *temp = NULL;
if (*stack == NULL)
{
fprintf(stderr, "L%d: can't div, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*stack)->next == NULL)
{
fprintf(stderr, "L%d: can't div, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*stack)->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
(*stack)->next->n = (*stack)->next->n / (*stack)->n;
temp = *stack;
*stack = (*stack)->next;
free(temp);
}